系统交易论坛 - 开拓者期货自动交易平台's Archiver

thjyqr 发表于 2009-5-10 17:07

请教nopain,写了5小时,实在是很为难啊。

求教了,这个该怎样编写啊。
如果获利达到2R了那么后面行情无论涨跌我都采取一种立场方式。但是到3R后我又采取另外一种方式立场。比如KDJ指标。
因为收盘价达到2R后,后面有可能下一个BAR 有没有2R了,但是只要前面达到一次我就必须采用一种策略出场。

nopain 发表于 2009-5-11 09:54

其实很简单,就是记住开仓后的最高价或最低价,然后判断相应的处理。类似于多级跟踪止损。

先建两个序列变量:
        NumericSeries HigherAfterEntry;
        NumericSeries LowerAfterEntry;

在程序开始的地方初始化:

If(BarsSinceEntry == 1)
        {
                HigherAfterEntry = AvgEntryPrice;
                LowerAfterEntry = HigherAfterEntry;
        }Else If(BarsSinceEntry > 1)
        {
                HigherAfterEntry = max(HigherAfterEntry[1],High[1]);
                LowerAfterEntry = min(LowerAfterEntry[1],Low[1]);
        }

最后进行处理:
If(MarketPosition==1)
{
        MaxProfit = HigherAfterEntry-AvgEntryPrice;
        Commentary("MaxProfit="+Text(MaxProfit));
        If(MaxProfit > 60*MinPoint)
        {
                StopLine = HigherAfterEntry-MaxProfit*0.4;
                Commentary("启动二级跟踪");
        }Else If(MaxProfit > 30*MinPoint)
        {
                StopLine = HigherAfterEntry-MaxProfit*0.5;
                Commentary("启动一级跟踪");
        }Else
        {
                StopLine = AvgEntryPrice - StopLoss*MinPoint;
                Commentary("启动原始止损");
        }
       
        If(Low <= StopLine)
        {
                MyPrice = StopLine;
                If(Open < MyPrice ) MyPrice = Open;
                Sell(1,MyPrice);
                Commentary("触发止损");
        }
}else if(MarketPosition==-1)
{
        MaxProfit = AvgEntryPrice-LowerAfterEntry;
        Commentary("MaxProfit="+Text(MaxProfit));
        If(MaxProfit > 60*MinPoint)
        {       
                StopLine = LowerAfterEntry+MaxProfit*0.4;
                Commentary("启动二级跟踪");
        }Else If(MaxProfit > 30*MinPoint)
        {
                StopLine = LowerAfterEntry+MaxProfit*0.5;
                Commentary("启动一级跟踪");
        }Else
        {
                StopLine = AvgEntryPrice + StopLoss*MinPoint;
                Commentary("启动原始止损");
        }
       
        If(High >= StopLine)
        {
                MyPrice = StopLine;
                If(Open > MyPrice ) MyPrice = Open;
                BuyToCover(1,MyPrice);
                Commentary("触发止损");
        }
}

高架桥 发表于 2009-5-11 15:28

楼主的R是指什么???

thjyqr 发表于 2009-5-12 12:17

R是风险值。

可以通过设置全局变量来实现这一部分吗?

If(MarketPosition==1)
{
        MaxProfit = HigherAfterEntry-AvgEntryPrice;
        Commentary("MaxProfit="+Text(MaxProfit));
        If(MaxProfit > 60*MinPoint)
        {
                StopLine = HigherAfterEntry-MaxProfit*0.4;
                Commentary("启动二级跟踪");
        }Else If(MaxProfit > 30*MinPoint)
        {
                StopLine = HigherAfterEntry-MaxProfit*0.5;
                Commentary("启动一级跟踪");
        }Else
        {
                StopLine = AvgEntryPrice - StopLoss*MinPoint;
                Commentary("启动原始止损");
        }
        
        If(Low <= StopLine)
        {
                MyPrice = StopLine;
                If(Open < MyPrice ) MyPrice = Open;
                Sell(1,MyPrice);
                Commentary("触发止损");
        }

thjyqr 发表于 2009-5-12 12:21

只要前面达到一次我就必须采用一种策略出场。至于是不是一定出场那要看策略来说。这里的逻辑关系有点模糊

nopain 发表于 2009-5-12 12:45

回复 #4 thjyqr 的帖子

这里是用序列变量啊。没有全局变量

thjyqr 发表于 2009-5-13 08:29

谢谢NOPAIN的细心解答。但是我想这有必要设置两个变量吗?用
SetGlobalVar(HIGH,BarsSinceEntry),SetGlobalVar(LOW,BarsSinceEntry就搞定了这样有什么不妥吗?我是一手一手交易的。交易频繁。

nopain 发表于 2009-5-13 09:28

回复 #7 thjyqr 的帖子

你这写的什么,看不懂?

thjyqr 发表于 2009-5-13 15:38

我写错了,是这样写的。
SetGlobalVar(0,highest(HIGH,BarsSinceEntry)),SetGlobalVar(1,lowest(LOW,BarsSinceEntry))

nopain 发表于 2009-5-13 15:52

回复 #9 thjyqr 的帖子

1、这样运算慢
2、这样最后一个Bar的High/Low 也算进去了。

chengjun1201 发表于 2010-3-19 15:56

多级跟踪止损  good!!!!!!!!!

nabaobao80 发表于 2010-3-19 16:12

尽量不要在不是非常必要的时候用全局变量。
一般的用序列完全能解决问题了。
全局会弄的很乱。逻辑不清楚。

h882010790 发表于 2010-3-19 23:21

如何在历史测试中在一分钟线天的最高价啊

如何在历史测试中在一分钟线上调用任何前面任何一天的最高价啊

yangtse010 发表于 2010-3-20 06:50

highD()   只要1分钟K线数据足够多

笑啸霄 发表于 2010-4-6 15:27

If(BarsSinceEntry == 1)
        {
                HigherAfterEntry = AvgEntryPrice;
                LowerAfterEntry = HigherAfterEntry;
        }Else If(BarsSinceEntry > 1)
        {
                HigherAfterEntry = max(HigherAfterEntry[1],High[1]);
                LowerAfterEntry = min(LowerAfterEntry[1],Low[1]);
        }
如果是日内交易的话,这里的High[1]是指开仓BAR以后的high[1]还是从开盘以后的第一根BAR算起至当前BAR当中的最高?

lh948 发表于 2010-4-6 15:30

High[1]是指图表上前一个bar的最高价

sx810 发表于 2010-4-26 22:22

回复 3# 高架桥 的帖子

If(MarketPosition==1)
{
        MaxProfit = HigherAfterEntry-AvgEntryPrice;
        Commentary("MaxProfit="+Text(MaxProfit));
        If(MaxProfit > 60*MinPoint)问题1::这里是指盈利有60个点吗?
        {
                StopLine = HigherAfterEntry-MaxProfit*0.4;问题2:这里是最高价回撤40%吗?需要计算各个品种吗?
                Commentary("启动二级跟踪");
        }        }Else
        {
                StopLine = AvgEntryPrice - StopLoss*MinPoint;问题3:StopLoss这里指什么意思?
                Commentary("启动原始止损");
        }
        
     请回答,谢谢!

lh948 发表于 2010-4-27 13:20

1.是的
2.是的,不是各个计算的
3.设置的止损的点数

深蓝魔力 发表于 2010-5-6 18:16

这个跟踪系统牛!
先收集,等有空好好琢磨下。

choir2001 发表于 2010-6-26 01:25

一级跟踪,二级跟踪,人间大炮发射!

页: [1] 2

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.