My original script was a MA crossover entry and exit with Pyramiding for each 
incremental increase of 5% from the entry.

The initial script was working as intended with the sigScaleIn.  I removed the 
crossover exit and added a sigScaleOut exit which was only barely altered from 
the last example provided here -> http://www.amibroker.com/guide/h_pyramid.html

However, when I run a backtest I get zero trades.  I suspect something might be 
wrong at the SetPositionSIze() near the bottom.  Maybe mixing Buy signals, 
sigScaleOut and sigScaleOut for position sizing does not work?  Here is that 
snipet from the bottom:

SetPositionSize( IIf( Buy == sigScaleIn, 1, IIf(Buy = sigScaleOut, 50, 5 )), 
IIf( Buy == sigScaleOut, spsPercentOfPosition, spsPercentOfEquity));

Any ideas?




Here is my code.

FastMALength = 100;                                             //Set Fast 
Moving Average
SlowMALength = 300;                                             //Set Slow 
Moving Average

FastMA = DEMA(Close, FastMALength);                             //Fast Moving 
Average
SlowMA = DEMA(Close, SlowMALength);                             //Slow Moving 
Average


bi = BarIndex();

Cond1 = MA(Volume, 100) > 100000;
Cond2 = OI > 5;
Cond3 = Cross(FastMA, SlowMA);
Cond4 = bi != LastValue(bi);

SetBarsRequired(SlowMALength, 0);
SetOption("MaxOpenPositions", 20);
PositionScore = MA(Volume, 100);

Buy =   Cond3;                                                  //Buy Rule #1 - 
Fast MA Crosses Above Slow MA
Sell =  0;

BuyInc = 5;                                                     //Incremental 
Buy Amount (%)
NextBuyARRAY = Null;                                            //Next 
Incremental Buy Value
priceatbuy = 0;                                         //Initial Buy Value
CounterARRAY = Null;                                            //Counts Number 
of Buys
NextSellARRAY = Null;
NextSell2ARRAY = Null;
exit = 0;

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

        if(priceatbuy == 0 AND Buy[ i ] )
        {
                priceatbuy = BuyPrice[ i ];  //Set inital entry Price
        }
        
                if(priceatbuy > 0 )
                {
                
                //Set next BuyPrice
                
                counterARRAY[ i ] = Max( CounterARRAY[ i - 1], 1 + int( ( 
(Close[ i - 1 ] - priceatbuy) / priceatbuy) / (BuyInc/100) ) );
                NextBuyARRAY[ i ] = Max( NextBuyARRAY[ i ], priceatbuy * (1 + 
((BuyInc / 100) * counterARRAY[ i ])));
                NextSellARRAY[ i ] = ( (NextBuyARRAY[ i ] - ( ( (BuyInc / 100) 
* priceatbuy ) * 5 ) ) ) /** ( (Min(10, CounterARRAY [ i ]) ) / 10) ) */;
                NextSell2ARRAY[ i ] = ( (NextBuyARRAY[ i ] - ( ( (BuyInc / 100) 
* priceatbuy ) * 10 ) ) );
                        if(Close[ i ]  > NextBuyARRAY[ i ])
                        {
                        Buy[ i ] = sigScaleIn;
                        BuyPrice[ i ] = Close[ i ];
                        }
        

                if( exit == 0 AND
          Close[ i ] < NextSellARRAY[ i ] )
       {
         // first profit target hit - scale-out
         exit = 1;
         Buy[ i ] = sigScaleOut;
       }

      if( exit == 1 AND
          Close[ i ] < NextSell2ARRAY[ i ] )
       {
         // second profit target hit - exit
         exit = 2;
         SellPrice[ i ] = Close[ i ];
       }

      if( SlowMA[ i ] < FastMA[ i ] )
       {
         // trailing stop hit - exit
         exit = 3;   
         SellPrice[ i ] = Close[ i ];
       }

      if( exit >= 2 )
       {
         Buy[ i ] = 0;
         Sell[ i ] = exit + 1; // mark appropriate exit code
         exit = 0;
         priceatbuy = 0; // reset price
       } 
                
                }
        

}

Plot(Close, "Price", colorBlack, styleCandle);
Plot(NextBuyARRAY, "NextBuy", colorRed);
PlotOHLC(NextBuyARRAY, NextBuyARRAY, NextSellARRAY, NextSellARRAY, "", 
colorYellow, styleCloud);
PlotOHLC(NextSellARRAY, NextSellARRAY, NextSellARRAY, NextSellARRAY, "", 
colorYellow, styleCloud);
SetPositionSize( IIf( Buy == sigScaleIn, 1, IIf(Buy = sigScaleOut, 50, 5 )), 
IIf( Buy == sigScaleOut, spsPercentOfPosition, spsPercentOfEquity));

_SECTION_BEGIN("DEMA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), 
ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("DEMA2");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( DEMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), 
ParamStyle("Style") ); 
_SECTION_END();


Reply via email to