Okay I have modified my code using the XAO as the filter code.
Code:MaxPositions = 4; SetOption("MaxOpenPositions", MaxPositions ); SetOption("WorstRankHeld", MaxPositions + 2 ); SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); // trade on next day open SetTradeDelays( 1, 1, 1, 1 ); BuyPrice = Open; SetBacktestMode( backtestRotational ); // offsetting by large positive number // makes sure that our score is always positive and we don't enter short trades SetForeign("XAO"); Filter = Cross( EMA(C,180), EMA(C,50)); RestorePriceArrays(); PositionScore = Iif( Filter, 10000 - ROC( C, 252 ), 0 );
The results are not as I have expected.
It has made a correct entry point [when condition TRUE on Filter = Cross( EMA(C,180), EMA(C,50));]
but closes the position unexpectedly [no negative crossover FALSE on Filter = Cross( EMA(C,180), EMA(C,50));] a few days later,
then makes a new entry a month or so later - at a time when no positive cross [TRUE] has occurred and then closes again without a negative crossover.
Hopefully what I have explained is somewhat clear.
Your Filter is wrong
Code:
MaxPositions = 4;
SetOption("MaxOpenPositions", MaxPositions );
SetOption("WorstRankHeld", MaxPositions + 2 );
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity );
// trade on next day open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;
SetBacktestMode( backtestRotational );
SetForeign( "XAO" );
Filter = EMA( C, 50 ) > EMA( C, 180 );
RestorePriceArrays();
// offsetting by large positive number
// makes sure that our score is always positive and we don't enter short trades
PositionScore = Iif( Filter, 10000 - ROC( C, 252 ), 0 );
StaticVarSet( Name() + "Score", 10000 - ROC( C, 252 ) );
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.backtest( 1 );
// closed trades
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{ // two additional columns
trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) );
trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) );
}
// open trades
for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )
{ // two additional columns
trade.addcustomMetric( "Score@entry", Lookup( StaticVarget( trade.symbol + "Score" ), trade.EntryDateTime ) );
trade.addcustomMetric( "Score@exit", Lookup( StaticVarget( trade.symbol + "Score" ), trade.ExitDateTime ) );
}
bo.listTrades();
}