Hello,
I have a pretty simple pyramiding script which buys and sells at MA crossovers,
and then scales in and out when the price goes up or down. I am looking to add
the ability to additionally scale out of all positions in the portfolio equity
goes down by a certain amount over a specific period. I am assuming that I
would have to use a custom backtest to do this. Is this true?
I have never used the Amibroker Custom Backtest. Any tips as to how I could
get started figuring this out?
Below is my current code so far. Thank you in advance for any help or advice.
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
Chng = ( (Close - TimeFrameGetPrice("C", inDaily, -1 ) ) / Close );
bi = BarIndex();
Cond1 = MA(Volume, 100) > 100000;
Cond2 = OI >= 5;
Cond3 = Cross(FastMA, SlowMA);
Cond4 = bi != LastValue(bi);
SetBarsRequired(SlowMALength, 0);
PosQty = 25;
SetOption("MaxOpenPositions", PosQty );
//PositionScore = MA(Volume, 100);
Buy = Cond1 AND Cond2 AND Cond3;
//Buy Rule #1 - Fast MA Crosses Above Slow MA
Sell = Cross(SlowMA, FastMA) OR bi == LastValue(bi) OR Chng < -0.2;
//Sell Rule #1 - Fast MA Crosses Below Slow MA
PosMul = 100 / PosQty;
InEquity = PosMul * 0.5;
AddEquity = PosMul * 0.2;
OutEquity = 10;
BuyInc = 5;
//Incremental Buy Amount (%)
SellInc = 5;
NextBuyARRAY = Null;
//Next Incremental Buy Value
priceatbuy = 0;
//Initial Buy Value
CounterARRAY = Null;
//Counts Number of Buys
sellCounterARRAY = Null;
NextSellARRAY = Null;
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 ])));
sellCounterARRAY[ i ] = Max(sellCounterARRAY[ i - 1], 1 + int(
( ( NextBuyARRAY[ i ] - Close[ i - 1] ) / NextBuyARRAY[ i ] ) / (SellInc/100) )
);
NextSellARRAY[ i ] = Max( NextSellARRAY[ i ], NextBuyARRAY[ i ]
* (1 - ((Sellinc / 100) * SellCounterARRAY[ i ] )));
if(Close[ i ] > NextBuyARRAY[ i ])
{
Buy[ i ] = sigScaleIn;
BuyPrice[ i ] = Close[ i ];
}
if(Close[ i ] < NextSellARRAY[ i ])
{
Buy[ i ] = sigScaleOut;
BuyPrice[ i ] = Close[ i ];
}
}
if(Sell[ i ])
{
priceatbuy = 0;
}
}
Plot(Close, "Price", colorBlack, styleCandle);
Plot(NextBuyARRAY, "NextBuy", colorRed);
SetPositionSize( IIf( Buy == sigScaleIn, InEquity, AddEquity),
spsPercentOfEquity );
SetPositionSize( OutEquity, IIf( Buy == sigScaleOut, spsPercentOfPosition,
spsNoChange));