开拓者期货期权程序化系统交易论坛

标题: 多头不止损,而空头第二根bar就平仓,是什么原因? [打印本页]

作者: kyler    时间: 2012-5-18 09:55:20     标题: 多头不止损,而空头第二根bar就平仓,是什么原因?

一个简单的股指程序,高于upline开空,低于downline开多,开仓比较顺利,但止损问题很大,多头不止损,而空头第二根bar就平仓,是什么原因?请高手指点
Params
    Numeric offset(10);
    Numeric stoplossSet(5);
Vars
        Numeric EntryPrice;
        NumericSeries upline;
        NumericSeries downline;
        Bool Condition1;
        Bool Condition2;
        BoolSeries bShortStoped ;
        BoolSeries bLongStoped ;
Begin

   
       
        if(CurrentBar == 0 || Date != Date[1])
        {
           bShortStoped = False ;
           bLongStoped = False ;
        } Else
        {
           upline = Close[1] + offset ;
           downline = Close[1] - offset;
       
           Condition1 =( MarketPosition == 0 && High> upline && bShortStoped == False && Time < 0.151000);
           Condition2 = (MarketPosition == 0 && Low< downline && bLongStoped == False && Time < 0.151000);
          
           Commentary("Condition1="+IIFString(Condition1,"True","False"));
           Commentary("Condition2="+IIFString(Condition2,"True","False"));

           PlotNumeric ("upline",upline) ;
           PlotNumeric ("dowmline",downline) ;
        }
       
        if (Condition1)
        {
                EntryPrice=Min(open,upline-0.2);
                SellShort(1,EntryPrice);
        }
       
        if (Condition2)
        {
                EntryPrice = Max(open,downline+0.2);
                Buy(1,EntryPrice);
        }
       
       
        If(MarketPosition==1 /*&& Low <= EntryPrice - StopLossSet */&& BarsSinceEntry>0)
        {
                Sell(1,Min(Open,EntryPrice - StopLossSet));
                bLongStoped = True;
        }
        if(MarketPosition==-1 /*&& High >= EntryPrice + StopLossSet */&& BarsSinceEntry>0)
        {
                BuyToCover(1,Max(EntryPrice + StopLossSet,open));
                bShortStoped = True;
        }
       
        if(Time >= 0.1513)
        {
                BuyToCover(1,Open);
                Sell(1,Open);
        }
       
End
作者: nopain    时间: 2012-5-18 14:15:35

为什么你的EntryPrice要在不同条件下分别计算?直接用AvgEntryPrice代替试试?
作者: kyler    时间: 2012-5-18 14:34:24

nopain 发表于 2012-5-18 14:15
为什么你的EntryPrice要在不同条件下分别计算?直接用AvgEntryPrice代替试试?

谢谢。可试了还是不行啊。。。纠结。开仓部分没什么问题,止损部分问题大
作者: nopain    时间: 2012-5-18 15:55:03

你的upline,dnline计算有问题。
完整代码如下:
  1. Params
  2.     Numeric offset(10);
  3.     Numeric stoplossSet(5);
  4. Vars
  5.     Numeric MyEntryPrice;
  6.     NumericSeries upline;
  7.     NumericSeries downline;
  8.     Bool Condition1;
  9.     Bool Condition2;
  10.     BoolSeries bShortStoped;
  11.     BoolSeries bLongStoped;
  12. Begin
  13.         if(CurrentBar == 0 || Date != Date[1])
  14.         {
  15.                 bShortStoped = False ;
  16.                 bLongStoped = False ;
  17.                 upline = Close[1] + offset;
  18.                 downline = Close[1] - offset;          
  19.         } Else
  20.         {      
  21.                 Condition1 =( MarketPosition == 0 && High> upline && bShortStoped == False && Time < 0.151000);
  22.                 Condition2 = (MarketPosition == 0 && Low< downline && bLongStoped == False && Time < 0.151000);
  23.          
  24.                 Commentary("Condition1="+IIFString(Condition1,"True","False"));
  25.         Commentary("Condition2="+IIFString(Condition2,"True","False"));

  26.         PlotNumeric ("upline",upline) ;
  27.         PlotNumeric ("dowmline",downline) ;
  28.         }
  29.       
  30.         if (Condition1)
  31.         {
  32.                 MyEntryPrice=Min(open,upline-0.2);
  33.                 SellShort(1,MyEntryPrice);
  34.         }
  35.    
  36.         if (Condition2)
  37.         {
  38.                 MyEntryPrice = Max(open,downline+0.2);
  39.                 Buy(1,MyEntryPrice);
  40.         }
  41.       
  42.       
  43.         If(MarketPosition==1 && Low <= AvgEntryPrice - StopLossSet && BarsSinceEntry>0)
  44.         {
  45.                 Sell(1,Min(Open,AvgEntryPrice - StopLossSet));
  46.                 bLongStoped = True;
  47.         }
  48.         if(MarketPosition==-1 && High >= AvgEntryPrice + StopLossSet && BarsSinceEntry>0)
  49.         {
  50.                 BuyToCover(1,Max(AvgEntryPrice + StopLossSet,open));
  51.                 bShortStoped = True;
  52.         }
  53.    
  54.         if(Time >= 0.1513)
  55.         {
  56.                 BuyToCover(1,Open);
  57.                 Sell(1,Open);
  58.         }       
  59. End
复制代码

作者: kyler    时间: 2012-5-18 17:05:02

nopain 发表于 2012-5-18 15:55
你的upline,dnline计算有问题。
完整代码如下:

多谢多谢,可以正常止损了。值得好好总结




欢迎光临 开拓者期货期权程序化系统交易论坛 (http://bbs.tb18.net/) Powered by Discuz! X2