这个程式码到底是哪里错了?恳请指导一下

Discussion in 'AmiBroker' started by Joker-Kid, Nov 24, 2013.

  1. 海洋的前辈,小弟最近写了一个程式码,出现了几个问题,想了几天还是没想出来,希望前辈帮忙看看哪里出错了,恳请指导。

    我想实现的功能是:
    (1)达到做多条件则开仓,开仓价位标记为PriceAtBuy。达到加仓条件(新高大于开仓价位+20),则加仓,加仓价位设定是PriceAtBuy+20,即上次买入价位+20,后续加仓条件一样。
    (2)达到做空条件则开仓,开仓价位标记为PriceAtShort。达到加仓条件(新低少于开仓价位-20),则加仓,加仓价位设定是PriceAtShort-20,即上次卖空价位-20,后续加仓条件一样。
    (3)做多&做空的仓位最多是4仓。

    现在遇到的问题:
    (1)做多时仓位是少于等于4仓,但是做空时仓位却会一直加,如何限制空头仓位依然是最多4仓?
    (2)做空时,加仓的实际成交价格不是上一次加仓价格-20,而是上一条K线最低价-20,程式码是不是哪里写错了,需要怎么修改?

    程式码如下:
    1. ///////////// Enter & Exit Rule

      H20=Ref(HHV(H,20),-1);
      H10=Ref(HHV(H,10),-1);
      L20=Ref(LLV(L,20),-1);
      L10=Ref(LLV(L,10),-1);

      SellPrice=Ref(LLV(L,10),-1);
      CoverPrice=Ref(HHV(H,10),-1);

      Buy=Sell=Short=Cover=0;
      for(i=0;i<BarCount;i++)
      {
      if(i>19)
      {
      if(H>H20)
      {Buy=1;}

      if(L<L10)
      {Sell=1;}

      if(L<L20)
      {Short=1;}

      if(H>H10)
      {Cover=1;}
      }
      }


      Buy=ExRem(Buy,Sell);
      Sell=ExRem(Sell,Buy);
      ////////////////////////PositionSize setting

      TrailStopAmount = 2 * ATR( 20 );
      Capital = 100000;
      PointValue=10;
      Risk = 0.01*Capital;
      PositionSize = (Risk/Ref(ATR(20),-1)/PointValue)*BuyPrice;

      /////////////////////////////ScaleIn Setting

      PriceAtBuy=0;
      PriceAtShort=0;
      RefHValue=Ref(HHV(H,20),-1);
      RefLValue=Ref(LLV(L,20),-1);
      LongSignal=0;
      ShortSignal=0;

      for(i=0;i<BarCount;i++)
      {


      if(Buy==1)
      {
      BuyPrice=RefHValue;
      PriceAtBuy=BuyPrice;
      LongSignal=1;
      }

      if( High>=(PriceAtBuy+20) AND LongSignal>=1 AND LongSignal<4)
      {
      BuyPrice=PriceAtBuy+20;
      Buy=sigScaleIn;
      PriceAtBuy=BuyPrice[i];
      LongSignal=LongSignal+1;
      }


      if(Short[i]==1)
      {
      ShortPrice[i]=RefLValue[i];
      PriceAtShort=ShortPrice[i];
      ShortSignal=1;
      }

      if( Low[i]<=(PriceAtShort-50) AND ShortSignal>=1 AND ShortSignal<4)
      {
      ShortPrice[i]=PriceAtShort-50;
      Short[i]=sigScaleIn;
      PriceAtShort=ShortPrice[i];
      ShortSignal=ShortSignal+1;
      }


      }

      [/i][/i][/i][/i][/i][/i][/i][/i][/i]
    [i][i][i][i][i][i][i][i][/i][/i][/i][/i][/i][/i][/i][/i]
     
  2. 上面程序最后部分修正一下:

    if( Low<=(PriceAtShort-20) AND ShortSignal>=1 AND ShortSignal<4)
    {
    ShortPrice=PriceAtShort-20;
    Short=sigScaleIn;
    PriceAtShort=ShortPrice;
    ShortSignal=ShortSignal+1;
    }


    }

    Reply With Quote
     
  3. 刚找到第一个问题的原因
    (1)做多时仓位是少于等于4仓,但是做空时仓位却会一直加,如何限制空头仓位依然是最多4仓?

    原因是一些做空大波段里,short的条件一直符合,所以系统一直帮忙加仓。只要添加下面的程式码就OK。

    Short=ExRem(Short,Cover);
     
  4. 厄...加了上面的程式码后,第二个问题也被KO了... —____—