Hello Alessio,
Yes, I solved that problem, but by means of a backdoor solution. In this case it cannot be applied “generally”, hence I can’t tell about any solution for the by me unknown code.
Please notice that I am not even sure that you are bothered by the issue I ran into, which – summarized- is only related to wanting to know whether the last trade made profit or not. Yes, that simple, and that does not work *at all*, mainly because there is no PRT command for it. I complained about *that* a dozen times (also in this forum), but that never helped. This very issue, which I had in a combination in the code that made it untraceable, could be solved because I recalled the base issue from longer ago :
The Broker Fee which is charged in backtesting after the last exit, thus in the next bar. And because Broker Fee is always a cost, it is always a loss once observed in that next bar.
You could say that out of the blue a loss occurs, which only gets detected by you (as the coder) if you ask for the difference in StrategyProfit.
Need I say more ? it requires hoopla x 10 to solve such an issue, because of all the combinations which may occur. One example of this : if you go out on an Exit in bar #10314, but in-bar – thus in that same bar #10314 you Enter again (which is a possible combination with a Limit order) – and the Exit lead to a profit, then the Broker Fee as a loss would not even be detected (assumed the Broker Fee is smaller than the profit).
If you understand the elements in order here, you will be able to mimic it with backtesting and graph and such. In normal situations you will always see an extra trade after the formal trade exited, and it is a loss at the height of the Broker Fee.
Matters as these will never be solved for PRT because the developers are 100% hidden from us, the user, and while I could easily have it solved together with a developer, it now just won’t get through because the person you talk to can not code a single bit. It just will not get through.
… Just as this post, unless you can mimic it with graph as just told. And then you will need to solve it yourself for your specific situation.
If not clear yet : this situation causes that no backtest is ever the same as Live, because in Live this extra trade is not implied. I could also say : avoid comparisons with StrategyProfit[1] because it will never work as should. Also, with IG this will never happen because no Broker Fee is charged. And in Live it is fine, because the Broker Fee is incorporated in the cost f the trade, thus in the StrategyProfit reported right after the trade. So, the problem is that you can’t backtest in PRT-IB.
// 28-01-2024,PS, Determine a version of StrategyProfit which eliminates the observed change when Backtesting charges the BrokerFee.
// Notice that this is IG compatible, never mind it was made for the IB situation and the variable StrategyProfitIB is used.
// Although it is not tested, it should also work for internally accumulate / decumulate position.
StrategyGainChanged = 0
//StrategyProfitIB = StrategyProfit // 28-01-2024,PS. 05-03-2024,PS, Not this. See below.
Once StrategyProfitIB = StrategyProfitBaseB // 25-02-2024,PS.
//StrategyProfitIB = StrategyProfit // 28-01-2024,PS. 25-02-2024,PS, Not here.
StrategyProfitIBHlp = StrategyProfit // 25-02-2024,PS, Helper, for the same strange reason StrategyProfitIB was required here in the firts place (see code right under here).
////graph StrategyProfitIBHlp coloured ("yellow") // 20-04-2024,PS.
// WATCH OUT : You see below that we use StrategyProfitIB instead of StrategyProfit itself. I have really no clue as of why, but
// when StrategyProfit is used in the below couple if lines, it does not work. Then all the entries show as losses.
If Not OnMarket[1] and OnMarket then
StrageyGainChanged = 0 // This is when IB will have added BrokerFee - at the Entry - which we don't want to know about. It already at 0 here, so the commented-out line is only for clearness. 21-04-2024,PS, Activated for clearness. Add to MGC.
ElsIf StrategyProfitIBHlp > StrategyProfitIBHlp[1] then // 25-02-2024,PS, In this piece of code the Hlp version.
StrategyGainChanged = 1
ElsIf StrategyProfitIBHlp < StrategyProfitIBHlp[1] then // 25-02-2024,PS, In this piece of code the Hlp version.
StrategyGainChanged = -1
endif
This is the base of the solution. Further down the line you can now use StrategyProfitChanged (it won’t cover for the in-bar two trades). But interpret this part correctly :
Once StrategyProfitIB = StrategyProfitBaseB // 25-02-2024,PS.
//StrategyProfitIB = StrategyProfit // 28-01-2024,PS. 25-02-2024,PS, Not here.
The first line is unrelated because it solves the throwing out issues and restarting the System (and preserve the gain so far). If not in order it can be initialized at 0.
The second line is for yourself to determine
somewhere, because it is related to the accumulation of the gain over trades. Example from my own code a few 100 lines further down the line :
//StrategyProfitIB = StrategyProfitBaseB + StrategyProfit // 25-02-2024,PS, (In this way) For StartThrough.
StrategyProfitIB = StrategyProfitBaseB + StrategyProfitIBHlp // 20-04-2024,PS, Like this (otherwise the normal version contains an old value ! Add to MGC.
And Yes, you can see that I am actually still struggling with it. Or at least 10 days ago I was. Of course this is all with the added dimension of Money Management, but hey …
Now, if you have the feeling that this can help you, please go ahead. If you (anyone) have the feeling that this indeed bothers you, please create a ticket for the PRT engineers so they can provide a solution which is transparent to us, the users.
In any event have fun (not).