Different results on exploration and Trade

Hi, I am facing a very peculiar problem. I am testing a new system and while backtesting I see that my sell Signal is generated on a particular date but the actual sell signal is traded on the week after even though my trade delay is set to 0 and scenario is Trade current bar on close, stops exit on close. Please see below snapshot of the discrepancy. Any suggestions of what am I doing wrong here?

chart results

As you can see, on the chart my sell signal is active on 21sep2018 but on the result page it gives me a sell signal on 28Sep2018

Thanks in advance for your help.

@gautamlaungani,

I think it is on line 47, column 18.... Sorry for the irreverent humor, but how could we guess what the issue might be without seeing your code?

Please post code and don't forget to use the code blocks (proper formatting and makes it easy for us to copy your code and test it out).

4 Likes

fair enough, i take your humor in good faith. will just clean up code removing all extra lines etc and paste it here in a bit. cheers.

Hi Snoopy, thanks for your help in advance. below is the full code for your ref.

Apart from the above-mentioned problems i see two other issues. When i run exploration the monthly values i get for RSI and RelativeStrenght + EMA's of RelativeStrength are not matching with what i see on the chart.

Finally, if i move my cursor on the chart the stockup and stockdown line changes (just on scrolling back/fwd on chart)

I would appreciate any help here. Thanks

_SECTION_BEGIN("Multi Timeframe Exploration");

Tradedelay = 0;            
SetTradeDelays( Tradedelay,Tradedelay,Tradedelay,Tradedelay); // 0 = Same day/week, 1 = buyprice or closeprice of next day/week and so on //              
BuyPrice = SellPrice = ShortPrice = CoverPrice = close; // determines what price to use for backtesting. in this eg. CLOSE with TRADEDELAY of 0 means closing price of the day/week //   


SetChartOptions(0,chartShowArrows|chartShowDates);
TF2 = inWeekly;
TF3 = inMonthly;

Inidcatorlength= 14;
    

/* switch to weekly & monthly time frame */

TimeFrameSet( TF3 );   ///monthly
monthly_RSI = RSI(Inidcatorlength);
monthly_rel = RelStrength("nifty");
monthly_rel_ema_short = EMA(monthly_rel, 5);
monthly_rel_ema_long = EMA(monthly_rel, 13);
TimeFrameRestore();

//MONTHLY
m_RSI = TimeFrameExpand( monthly_RSI, TF3 );
m_rel = TimeFrameExpand( monthly_rel, TF3 );
m_rel_ema_short = TimeFrameExpand( monthly_rel_ema_short, TF3 );
m_rel_ema_long = TimeFrameExpand( monthly_rel_ema_long, TF3 );


TimeFrameSet( TF2 );             //weekly
weekly_vol = Volume;
weekly_RSI = RSI(Inidcatorlength);
weekly_rel = RelStrength("nifty");
weekly_rel_ema_short = EMA(weekly_rel, 5);
weekly_rel_ema_long = EMA(weekly_rel, 13);
TimeFrameRestore();

//WEEKLY
w_vol = TimeFrameExpand( weekly_vol, TF2 );
w_rsi = TimeFrameExpand(weekly_rsi, TF2);
w_rel = TimeFrameExpand( weekly_rel, TF2 );
w_rel_ema_short = TimeFrameExpand( weekly_rel_ema_short, TF2 );
w_rel_ema_long = TimeFrameExpand( weekly_rel_ema_long, TF2 );


// BULLISH ENTRY //

m_bullish_brekout_level = 60;   
m_bullish_breakdown_level = 40;  

m_bull  = IIf(  m_RSI > m_bullish_brekout_level, 1, 0);     
m_bear = IIf(  m_RSI < m_bullish_breakdown_level, 1, 0);     

m_bullishentry = Flip(m_bull,m_bear);          
m_bullishexit = Flip(m_bear,m_bull); 


W_bullish_brekout_level = 60;
W_bullish_breakdown_level = 40;

w_bull  = IIf(  w_RSI > W_bullish_brekout_level, 1, 0);     
w_bear = IIf(  w_RSI < W_bullish_breakdown_level, 1, 0);     

w_bullishentry = Flip(w_bull,w_bear);          
w_bullishexit = Flip(w_bear,w_bull); 


stockup =  m_rel > m_rel_ema_short AND m_rel_ema_short > m_rel_ema_long AND weekly_rel > w_rel_ema_short AND w_rel_ema_short > w_rel_ema_long;

stockdown = (m_rel < m_rel_ema_long) OR  (m_rel_ema_short < m_rel_ema_long) OR (weekly_rel_ema_short < w_rel_ema_long) OR (w_rel < w_rel_ema_long);

RiskPerShare = C * ((w_RSI/W_bullish_breakdown_level)-1);

Buysignal = w_bullishentry and M_bullishentry AND stockup;

Sellsignal = w_bullishexit OR m_bullishexit OR stockdown ;


//Initial Capital & Risk//           

PositionRisk = 1; 							// 1;  
PctSize =   PositionRisk  * BuyPrice / RiskPerShare; ; 										// position size calculation - no of shares      
SetOption("InitialEquity",1000000.00);      												// Initial Equity Starting Value      
SetOption( "UsePrevBarEquityForPosSizing", True); 											// set the use of last bars          
SetOption("MinPosValue", 2000); 															// min value of shares to purchase            
SetOption( "AllowPositionShrinking", True ); 												// Shrink Position on Loss           
TickSize = 0.05;    																		// Tick Size      
SetOption("RefreshWhenCompleted", True); 													// Refresh when completed            
SetOption("CommissionMode", 1);																// Commission in Percent Mode            
SetOption("CommissionAmount", 0.15); 														// 0.15% of Trade Value            
Totalpositions = 25; 																		// Max no of positions allowed (just change here)            
SetOption("MaxOpenPositions", Totalpositions ); 											// formula for max open positions        
SetBacktestMode( backtestRegular);      													// Backtest mode - Regular
SetPositionSize(PctSize, spsPercentOfEquity );  											// Position size in Pct



// Scaling In //     
 
ft = SumSince(sellsignal, buysignal);    
 
xsigs = ft <=2;    
firsttrade = ft <=1;    
   
XsigsX =  xsigs;    
   
Buy = IIf( BuySignal  AND xsigsX, sigScaleIn, 0); 

exitLastBar = datetime() >= GetFnData("DelistingDate"); 
 
Sell =   Sellsignal OR exitLastBar  ; // Exit delisted cos  
 

Plot(stockup, "Buysignal",colorGreen, styleLine);
Plot(stockdown, "Sellsignal",colorred, styleLine);

Filter = 1;
AddColumn(w_rel, "weekly Relative Value", format = 1.2);
AddColumn(w_rel_ema_short, "w_rel_ema_short", format = 1.2);
AddColumn(w_rel_ema_long, "w_rel_ema_long", format = 1.2);

AddColumn(m_rel, "monthly Relative Value", format = 1.2);
AddColumn(m_rel_ema_short, "m_rel_ema_short", format = 1.2);
AddColumn(m_rel_ema_long, "m_rel_ema_long", format = 1.2);

_SECTION_END();

See why-analysis-results-and-chart-output-may-differ and Multi Time-frames (especially about 'expandLast' & 'expandFirst')

1 Like

See also many existing threads explaining that Exploration is NOT Backtest:

https://forum.amibroker.com/search?q=exploration%20is%20not%20backtest

1 Like

You are using RelStrength incorrectly in both weekly and monthly TF functions.
It should not be used with TimeFrameSet as it internally restores price array similar to Foreign() function being used together with TimeFrameSet.

Instead you should use SetForeign and create RelStrengh yourself. It is just division.


//https://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
SetForeign("Nifty");
	TimeFrameSet( TF3 );   ///monthly
		monthly_C1 = C;
RestorePriceArrays();

TimeFrameSet( TF3 );   ///monthly
	monthly_C2 = C;
	// replicating RelStrength result
	monthly_rel = monthly_C2 / monthly_C1*1e3;
	//
	monthly_rel_ema_short = EMA(monthly_rel, 5);
	monthly_rel_ema_long = EMA(monthly_rel, 13);
RestorePriceArrays();	

m_rel = TimeFrameExpand( monthly_rel, TF3 );
m_rel_ema_short = TimeFrameExpand( monthly_rel_ema_short, TF3 );
m_rel_ema_long = TimeFrameExpand( monthly_rel_ema_long, TF3 );

Besides as been mentioned here and in other threads already... TimeFrameExpand default mode is expandLast. There are three modes. Read about what each mode stands for.


This should be because of QuickAFL. Has been discussed in forum to lengths already.

1 Like

I haven't tried this yet, but I am assuming going back to basic and creating the indicator as you suggested from scratch makes sense. will work on it now. thanks for pointing me in the right direction. cheers