PositionScore in Backtest Differs To PositionScore for Exploration

I am having a problem where a Backtester is taking different positions than the Exploration with the same PositionScore Ranking output.
So the process I would like to do is running is run an exploration in the morning on the data from the day before, and enter any positions from the exploration at the open of that morning.
To replicate that logic, as you will see, I have set the Buy Delay to 1 in my settings and the BuyPrice to Open.
However I am finding that the Backtester is taking different positions to what the Exploration suggested, with the exact same formula though for both!

So my code is as follows:

SetOption("AllowSameBarExit", True );    
EquityBal = 100000; 
SetOption( "initialequity",EquityBal);
MaxPos = 3; 													 
SetOption( "maxopenpositions", MaxPos );  
SetPositionSize((100/MaxPos),spsPercentOfEquity);  

SetTradeDelays(1,0,0,0);  
BuyPrice = Open;  
SellPrice = Close; //sell today at close  
    
length_MA = 200;

Ranking = (C - MA(C,50))/C;
PositionScore = IIf(Ranking<0,0,Ranking);   
  
BuySignal = (C > MA(C,length_MA)) AND C < MA(C,5);
Buy = BuySignal  
BuyPrice = Open;
  
Sell = C>MA(C,5);

intrade = False; 
for( i = 0; i < BarCount; i++ ) 
{ 
	if( NOT intrade ) 
	{ 
		if( Buy[ i ] )  
		{ 
			intrade = True; 
		 
			// same bar sell 
			if( Sell[ i ] ) intrade = False; 
		} 
	} 
	else 
	{ 
		if( Sell[ i ] )  
		{ 
			intrade = False; 
			Buy[ i ] = False; // remove buy if exited this bar 
		} 
	} 
}  
 
 
  
Filter = BuySignal;  
  
AddTextColumn( FullName(), "FullName");  
AddColumn(C,"Close Price");    
AddColumn(Ranking,"Position Score",1.3);  
SetSortColumns(-5);  

The loop in the code is to only allow 1 open trade in the same company at any one time as per Using OR in a Sell Condition allows Mutliple entries on same Symbol

So here are 2 recent explorations. This one was for entries to take on the day of 29/11:
image
and then this one for 30/11:
image
Now notice however the backtester on the 29/11 takes the correct positions, but on the 30/11 its missing FLNT:
image
There are many more examples of this on different days and I cant figure out why its doing it.
Any explanations will really be greatly appreciated!

I forgot to mention that I have also tried setting the Buy Delay to 0 and changing the Buy to Buy = Ref(BuySignal,-1);
But the issue remains.

You have a buy entry delay so you have to add delay to exploration also.
This includes adding delay to PositionScore.

BTW, since loop code is not by yourself you should add reference to creator.

SetOption("AllowSameBarExit", True );    
EquityBal = 100000; 
SetOption( "initialequity",EquityBal);
MaxPos = 3; 													 
SetOption( "maxopenpositions", MaxPos );  
SetPositionSize((100/MaxPos),spsPercentOfEquity);  

SetTradeDelays(1,0,0,0);  
BuyPrice = Open;  
SellPrice = Close; //sell today at close  
    
length_MA = 200;

Ranking = (C - MA(C,50))/C;
PositionScore = IIf(Ranking<0,0,Ranking);   
  
BuySignal = (C > MA(C,length_MA)) AND C < MA(C,5);
Buy = BuySignal;  
BuyPrice = Open;
  
Sell = C>MA(C,5);

/// code from here
/// @link https://forum.amibroker.com/t/using-or-in-a-sell-condition-allows-mutliple-entries-on-same-symbol/6888/10
intrade = False; 
for( i = 0; i < BarCount; i++ ) 
{ 
	if( NOT intrade ) 
	{ 
		if( Buy[ i ] )  
		{ 
			intrade = True; 
		 
			// same bar sell 
			if( Sell[ i ] ) intrade = False; 
		} 
	} 
	else 
	{ 
		if( Sell[ i ] )  
		{ 
			intrade = False; 
			Buy[ i ] = False; // remove buy if exited this bar 
		} 
	} 
}  
 
 
  
Filter = Ref(Buy,-1);  
  
AddTextColumn( FullName(), "FullName");  
AddColumn(C,"Close Price");    
AddColumn(Ref(Ranking,-1),"Position Score",1.3);  
//SetSortColumns(-5);  

1 Like

I just realised that on the 30th its entering a position on the same day as the exit (which cant happen as I exit on close so the capital wont be available). Possibly this has something to do with it? How can we prevent an entry on the same day? ( I assumed the loop helped but thats only preventing the SAME company to enter again... not a different one.) Love some guidance here!

Thanks @fxshrat, however seeing im running the exploration for the next day, and entering the next day, the exploration needs to give the the signals from the current bar, not the day before.
Ie. When i run the exploration before the open on the 4th today, i want it ranked by the data on the 3rd, which it is currently doing without the REF

Just a small query, if there is no data today, then for you sun may have risen and its a new day but not for AB :slight_smile:
Are you considering this ?

1 Like

@fxshrat, NOW I FINALLY understand what you meant in the previous posts: Using OR in a Sell Condition allows Mutliple entries on same Symbol
I think this has the same issue - entering on the same bar as a closed position

You are funny. In the other thread I already told you that you would need to use CBT to prevent additional positions at same bar for same symbol and different symbol. But apparently you have not listened.

Once again....
I only told you that in order to get same positionscore as in backtest at same day you have to add Ref() delay to both filter and positionscore in exploration because in backtest both signal and position score are delayed.

Have you actually run the edited code I posted? You should get same results at same day now.
Do you see this?

511

659

22


BTW, exploration is not backtest. Exploration does not support portfolio backtest. That's why it is called backtest button because backtester has several features other analysis environments (such as scan, exploration) do not support.

1 Like

For example backtester has feature "Add artificial future bar"
838

If that's what you are looking for.


BTW, in previous posts I was mainly replying to your thread subject which right now is
"PositionScore in Backtest Differs To PositionScore for Exploration"
As proven by me above it does not differ.

1 Like

According to @Tomasz in the previous thread, if I used 100% position sizing and SetOption("SettlementDelay", 1); it should work without overlaps. Any ideas why adding a settlementDelay doesnt work in this instance?

@fxshrat Your suggestion of the delay on the position score and filter does not work in the exploration - I still get different entries using your suggestion.
Besides, why would i want to run the exploration after the bar is formed, but acces the bar before the last bar? (ie after bar for the 3rd of december is formed, i want to run exploration and get the highest ranked stock of THAT bar to enter the following morning of the 4th) The logic you are suggesting doesnt make sense to me, so my initial logic seems more correct.

Sorry but apparently you are not able to read properly.

  1. I responded to thread subject where it is said that
    "PositionScore in Backtest Differs To PositionScore for Exploration"
    That was proven to be wrong here. Scores do not differ. If a symbol is in score list of BT then you can output the same score for same day in exploration. Those scores simply do not differ.
  2. Next I said that exploration is not equal to what backtester does.
    And FYI, your Exploration just outputs (raw) scores on raw signals. And if you would add signal output to exploration then it would be just raw signals too. But no trades picking as backtester does.
    Needless to say (again) also.... exploration does not have portfolio feature as backtester does have it. It does not do portfolio trade output. Do you understand that eventually?
    You are comparing apples to oranges. Backtester is different animal than exploration!
  3. And if you want to have tomorrow trade picks you should check "Add artificial futures bar" posted here. That feature is backtester feature but not exploration feature.

BTW, you were asking why FLNT wasn't part of entry signals(score) list on 30/11 in detailed log of BT. FLNT was entered on 29/11 already and it was in open positions list on 30/11. So further entry signals of FLNT were ignored.
Also see backtest modes.

5 Likes

Hi @fxshrat, i just realised that i never thanked you for your time and patience with me on this topic. Thank you very much. It took me a long time to grasp what was happening. Im quite slow mentally, so it takes many re-reads of articles to digest! But thanks again. :slight_smile: Really appreciated!

3 Likes

Hi, is is possible to setup this option in the script programatically?

No, "Add artificial future bar" can not be set programmatically via AFL.
But analysis settings can be saved to analysis project file (which in addition includes AFL to be used for analysis).

1 Like