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);
So here are 2 recent explorations. This one was for entries to take on the day of 29/11:
and then this one for 30/11:
Now notice however the backtester on the 29/11 takes the correct positions, but on the 30/11 its missing FLNT:
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 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
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 backtestboth 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?
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.
For example backtester has feature "Add artificial future bar"
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.
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.
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.
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!
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.
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. Really appreciated!
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).