ProRealCode - Trading & Coding with ProRealTime™
I just spent over 2 hours filing a ticket about something being totally wrong today, unless I am blind and don’t see the cause. So if anyone seems to notice similar as this …
Side note : this is for V11 and V12 and in two accounts of IB. I did not check IG.
Backtest results are not the same, I think after the platform reset 06:30 (Amsterdam) this morning. But, this depends very much on what the code does. A small quote of what I wrote to support :
If we look back in ##PRTzz304c, we can see suspects like StrategyProfit, Floor, Min. I use these throughout the program code as well, including Round and Max.
What I am saying is : it is not necessarily the data because it can be the commands as well. I could add that these commands are almost (or fully ?) exclusively used in the MM code, which is much larger than the small part I showed. I have no reasons to use these commands elsewhere, except for StrategyProfit which I use to determine the exits of trades. Because that all keeps working (as far as I can tell) it should not be that.
The kind of fun is that I can compare the backtests with Live from the past 3 weeks, and nothing is the same any more. This counts for 3 running Systems I checked.
When it stopped working I did not restart PRT somewhere – the backtest results still on screen from earlier this morning, just don’t work the same any more for certain situations.
If someone else also has issues suddenly, it would be good to know about it ?
This appears way more complex than I could initially see. It is possibly (probably ?) related to the last line of code you see in this snippet pulled from the program of concern :
If MM and DownRevFactor = 1 then // 14-01-2024,PS.
//*** ********************************************************
//*** ********************************************************
//TestFactor = 2 // TEST !!!
//If PositionAmountRealB / StrategyProfitNet < TestFactor then // Trial.
If StrategyProfit <> StrategyProfit[1] then // Why does > not work ?!?
Obviously I am making my observations from backtesting. In this case I find that where > should work (it is what I want because I want to do things when making profit in the last trade), that doesn’t do a thing and I notice that <> does do something with effect. As if losses are now incorporated too. Special environmental to this “problem” : the backtests do not show losses and so making it <> as a temporary solution is for that moment fine with me. It only means that when there is this one loss out of 100 trades (or whatever I exactly expect), the contract amount will be increased after a loss as well. This is insignificant.
In practice different things occur than what the eye can see; in practice, with backtesting in IB (!), there are TWO occasions when one trade unwinds/exits and when the code is called in two subsequent events (bars) this happens – the first change of StrategyProfit is when the trade indeed exits and the second change of StrategyProfit is when the backtesting mechanism adds the broker fees. Careful, this is how it works for IB. For IG this does not happen because IG works with spread for income (in this situation which is about Futures, Indexes for IG).
This implies a kind of cancellation of the reported profit to the code, because in the first call (at the Exit) there will be profit (can also be a loss), while the second call implies a loss (broker fee is a deduction).
This is how backtesting works.
In Live no such thing happens … In Live there is just less gain reported, as the broker fee is right away deducted from the gain of the trade.
We now arrive at the more complex situation that the contracts-adding code is called twice – which out of all I already noticed and what made me halve the added contracts, so net it would be fine (Peter, this is a wrong thing to do, man). But code like this is everywhere in the program, so many more things go wrong.
Try to envision that I had to adjust the MM code which originally was triggered by losses only, and which I could not get to work in a situation with “profits only” (MM did not do a thing now). So now I changed the MM by means of letting it act on profits, but because profits also imply losses each time, each trade reports a double virtual profit (envision what that code snippet does on the condition you see) and the MM is down the drain.
The net effect I can not reason yet, but the outcome of it is that Live performs way way better than backtest, while backtest actually was “fit” to what I see happening. Thus, what I see happening should increase the number of contracts gradually and if that goes too fast for what the portfolio can bear, I slow it down in the code. And that slowing down does not work in Live because of the StrategyProfit acting differently (no “no-profit” because of broker fee).
And so all ‘n all backtesting is fine. Live is also fine, but very different. Net for the situation, Live is not so fine at all because after each profit 1 contract is added. Totally crazy stuff you see below, and which makes me sit there and wait till I deem it too much and kill the system (which I did twice so far from of Jan 4).
… And all I thought was an “oh well, *that* is working nicely !”, meanwhile knowing that I must be doing something wrong because I am not really into blowing an account by this strange act;
In the other topic (about Money Management) I see myself writing about how I approach that, actually not being able to justify this craziness, where you can see things go out of whack. But hey, I derived that from Live and not from Backtest. Backtest works as how I made it (for net result) and there nothing crazily grows. The 2nd attachment shows how it turned out in Live and where I could not deem that wrong. This coincidentally relates to the profit per trade which turned out way better for the latest market situation than I envisioned (better : saw happening in backtest) when designing this, and so I thought that was the reason and let it go for 3 weeks. … Until I wanted to showcase this per backtest in that MM topic and simply could not mimic the Live situation. And then I created the topic you are reading right now …
Almost the worst is that despite you see something totally illegal in the 2nd attachment, it would work on the longer term just the same. Thus, it would not be so that a loss would make the system go under water. The loss(es) would be huge, but the profits have been more huge to begin with. However, the growth just makes no sense and eventually it would blow up the system itself because of a lack of funds in the account. This is what we talk about in that MM thread … and that it is harmless in itself. But hey …
Money Managment ideas
More specifically this post this post and beyond.
On my latest backtest, I am not reproducing last week’s results.
See above for similar / same Issue// 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).
first of all I don’t find any code for a possible trailing stop, I don’t mean function Ptrailing, just anyone. Do you use something in particular for IB?Alessio, I see that in the other topic you’re asking for a self-coded solution for the IB environment, but I wouldn’t know why such a provided solution won’t work with PRT-IB. Unless of course, comparisons with StrategyProfit are in order. 😉 Anyway, No, indeed no standard solutions for Trailing are available in PRT-IB. And if you need PRT-IB specific solutions, then probably nobody provided that, because people like Roberto work with IG. And Yes, the both (IG and IB) may not be compatible in all cases because IG would mostly be too simple, with Contract Values always at 1 (a constant which just lacks in PRT, as in “Qty x 1000” for Forex with PRT-IB and PointSize and PointValue which in PRT-IB always matter and have to be incorporated (if you use real Futures with IG you already have those incorporated, but the provided trailing examples will not, as far as I know by heart). Regards, Peter
Backtesting kaput ? (IB)
This topic contains 6 replies,
has 3 voices, and was last updated by PeterSt
2 years, 3 months ago.
| Forum: | Platform Support: Charts, Data & Broker Setup |
| Language: | English |
| Started: | 01/26/2024 |
| Status: | Active |
| Attachments: | 2 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.