Let's consider this example rules:
Buy = RSI(14) < 30 ;
Short = RSI(14) > 70 ;
Exit condition is just a time based condition: after one month we exit the trade:
ApplyStop(stopTypeNBar, stopModeBars, 22, 1);
Now comes the most difficult part
At day10, for example, i enter a long trade on symb1. we know that we will be in the trade for one month. In the meanwhile, if my position go down of -5% i want to scalein and i want to scalein too if my position go down at -7% and -9%.
This for every symbols and until the sell\cover rules will be met.
Concerning position size, it's very simple:
Qty = 4;
SetPositionSize(100/Qty,spsPercentOfEquity);
SetOption("MaxOpenPositions", Qty);
Position size for scalein is 5% for every trade. It follows that max possible leverage should be 160%
So i've used SetOption("MarginRequirement",40);
Below is my attempt to code this logic.
I think that something is wrong because sometime scalein doesn't arrive even if i see a MAE large enough.
I can't understand why.
Qty = 4;
SetOption("InitialEquity",50 * 1000);
SetPositionSize(100/Qty,spsPercentOfEquity);
SetBacktestMode( backtestRegular );
SetOption("MaxOpenPositions", Qty );
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.1);
SetOption("MaxOpenPositions", Qty);
SetOption("InterestRate",0);
SetOption("MarginRequirement",10);
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("AllowPositionShrinking",True);
SetOption("AllowSameBarExit" , False);
SetTradeDelays(0,0,0,0);
RoundLotSize = 0;
BuySignal = RSI(14) < 30 ;
ShortSignal = RSI(14) > 70 ;
Buy = Buysignal ;
Short= 0;
Sell = ShortSignal;
Cover = 0; ;
ApplyStop(stopTypeNBar, stopModeBars, 22, 1);
BuyPrice = C;
ShortPrice = C;
PositionScore=(1000-RSI(14));
PositionScore = IIf( Buy, PositionScore, 1/PositionScore );
SetCustomBacktestProc("");
SetOption("UseCustomBacktestProc", True );
EachPosPercent = 100/Qty;
dt = DateTime() ;
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );
if ( Status("stocknum") == 0 )
{
StaticVarRemove( "value*" );
for ( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
{
StaticVarSet ( "value1" + symbol, -5 );
// _TRACE( " Symbol bla: " + Symbol );
}
}
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess(); // Initialize backtester
for(bar=0; bar < BarCount; bar++)
{
//bo.ProcessTradeSignals( bar );
CurEquity = bo.Equity;
for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
{
posprofit = pos.GetPercentProfit();
// _TRACE("posprofit " + posprofit);
price = pos.GetPrice( bar +1 , "O" );
valuein = (EachPosPercent/100 * 0.20 * CurEquity) ;
//_TRACE(DateTimeToStr( dt[bar]) + " valuein " + valuein + " posprofit " + posprofit + " CurEquity " + curequity + " EachPosPercent " + EachPosPercent );
// _TRACE(DateTimeToStr( dt[bar]) + " Symbol: " + pos.Symbol );
// _TRACE(DateTimeToStr( dt[bar]) + " Static Var Get: " + StaticVarGet("value1" + pos.Symbol) );
// _TRACE(DateTimeToStr( dt[bar]) + " PosProfit: " + posprofit );
if( posprofit < StaticVarGet("value1" + pos.Symbol) AND posprofit > -10 )
{
bo.ScaleTrade( bar, pos.Symbol, True, price, valuein );
StaticVarSet("value1" + pos.Symbol, StaticVarGet("value1" + pos.Symbol) - 2);
// _TRACE(DateTimeToStr( dt[bar]) + " Static Var Get: " + StaticVarGet("value1" + pos.Symbol) );
}
}
bo.ProcessTradeSignals(bar);
}
bo.PostProcess(); // Finalize backtester
}