Set position size depending on profitability of the strategy query
Forums › ProRealTime English forum › ProOrder support › Set position size depending on profitability of the strategy query
- This topic has 12 replies, 3 voices, and was last updated 1 year ago by JS.
-
-
12/14/2023 at 6:31 PM #225252
Hi All
I have been going through the Advanced programming courses, and I am unsure about the code that is used in the “Set position size depending on profitability of the strategy” lesson.
The way I understand it, the code starts with a trade size of 5 contracts. Each time there is a loss, the code reduces the contract size by 1, and after a win, the size moves back to 5 contracts.
I have completed a backtest and gone through the first 20 trades. Because the system uses a loop of i = 1 TO 10, I am assuming that is why the first ten trades remain at 5 contracts.
When trade 10 is a loss, trade 11 is set to 4 contracts. Trade 11 is also a loss, so trade 12 should be set using 3 contracts. Yet, trade 12 uses 5 contracts in the backtest. Is someone able to explain to me why this happens, please? It is very possible that I am misunderstanding the code logic.
These are the results from the first 20 trades in my backtest:
The first number is the trade number: 1 to 20
L = Loss W = Win
The last number is the contract size: 1, 2, 3, 4 or 51. L 5
2. W 5
3. W 5
4. W 5
5. L 5
6. W 5
7. L 5
8. L 5
9. W 5
10. L 5
11. L 4
12. W 5
13. L 5
14. L 5
15. L 4
16. L 5
17. W 3
18. L 4
19. L 5
20. L 3Thanks, Brad
12/14/2023 at 6:44 PM #22525512/14/2023 at 8:33 PM #22525812/15/2023 at 6:46 AM #225265I think it is allowed to post it as it will be out of context of the course. And if not, a moderator may remove it again, say tomorrow. But post your code (the .itf) The mere reason would be that at this moment, to me the code seems to do wrong things. And in any event just helping you. 🙂
I think it is also necessary to copy the given description, as mis-interpretation could be in order. For example :
and gone through the first 20 trades. Because the system uses a loop of i = 1 TO 10, I am assuming that is why the first ten trades remain at 5 contracts.
That is too much “off” from any normal reality. Thus, would this assumption be correct, then it has to be in the description. Also, it easily can be in “the small print” or the context of the assignment, which may tell that it is not allowed to go under water. Read : first build up some cash before “money management” jumps in, which would include that the system must be profitable to begin with. You could go all directions with it; actually I think it will be fun, also for others to read the workout (with a couple of judgments of other community members too).
Regards,
Peter1 user thanked author for this post.
12/15/2023 at 3:43 PM #225287The code used in the1234567891011121314151617181920212223242526272829MyMACD = MACDline[12,26,9](close)Increase = MyMACD > MyMACD[1]Test = Highest[5](Increase) = 0IF Test AND Test[1] = 0 THENSignalBar = BarIndexENDIFCount = 0Size = 5FOR i = 1 TO 10 DOIF TRADEINDEX(i) > SignalBar THENCount = Count + 1ENDIFIF POSITIONPERF(i) < 0 THENSize = Size - 1ELSESize = 5ENDIFNEXTIF NOT ONMARKET AND Close > Average[200] AND Test AND Count < 3 THENSELLSHORT MAX(1,Size) CONTRACTS AT MARKETENDIFSET TARGET PPROFIT 50SET STOP PLOSS 1512/15/2023 at 7:17 PM #225300Hi Brad,
I looked very very briefly before opening a couple of beers over here, but this :
1Test = Highest[5](Increase) = 0is as far as I know not proper ProRealCode syntax. The Editor / Interpreter does not reject it, and to my belief (without really checking) the result of this will be 0 (false). You could compare it to
12345Test = 1 = 0// orIf Test = 1 = 0 thenwhich never will be true.
I can’t guess what the intentions are here, so maybe you can try to figure out the (most probably) two commands on separate lines you (or the book) intend(s).
And possibly – when you solved that – it already runs as expected ?Otherwise it would be good if you tell on what instrument you test it, and in what TimeFrame of the chart (1 minute, 5 minute, 1 hour, etc.). Then we would all be able to communicate over it.
1 user thanked author for this post.
12/16/2023 at 11:47 AM #225320I used Dax [Germany 40 Cash] on IG for the backtesting.
As you will see in the code below, I added a daily timeframe condition, but the code is implemented on the 1-hour timeframe. (Not the TP & SL have also been changed).
Following the Win/Loss sequence for the first 20 trades, the first trade was placed on 16 February 2023.
Regarding:
1Test = Highest[5](Increase) = 0The following explanation was given in a previous video:
// Boolean test: Test is true (equal to one) when the highest value of increase over the last 5 periods is zero. This means the MACD has not increased at all over the last 5 consecutive periods.
1-Hour code with Daily Condition12345678910111213141516171819202122232425262728293031323334353637TIMEFRAME(Daily)IF TimeSeriesAverage[45](close) <= TimeSeriesAverage[45](close)[1] THENTradeValid = 1ELSETradeValid = 0ENDIFTIMEFRAME(1Hour)MyMACD = MACDline[12,26,9](close)Increase = MyMACD > MyMACD[1]Test = Highest[5](Increase) = 0IF Test AND Test[1] = 0 THENSignalBar = BarIndexENDIFCount = 0Size = 5FOR i = 1 TO 10 DOIF TRADEINDEX(i) > SignalBar THENCount = Count + 1ENDIFIF POSITIONPERF(i) < 0 THENSize = Size - 1ELSESize = 5ENDIFNEXTIF NOT ONMARKET AND TradeValid AND Close > Average[200] AND Test AND Count < 3 THENSELLSHORT MAX(1,Size) CONTRACTS AT MARKETENDIFSET TARGET PPROFIT 70SET STOP PLOSS 5012/16/2023 at 12:10 PM #225321OK, I will follow a course myself. Haha.
But why did I ask to post the .itf ? (which emerges on disk when you Export the strategy) … because then all would be clear how you set things. So in this case this can be about the Tick by Tick mode, and the Spread which I don’t know (can look it up, but you must apply this correctly just the same).
And also : it is always good to add a screenshot from the result you get. This allows us to compare and see whether our bases match.Below you see what I get with a Spread of 4. First trade is indeed at Feb 16 with 5K bars of 1 hour data.
1 user thanked author for this post.
12/16/2023 at 3:03 PM #22533212/16/2023 at 3:49 PM #225338Brad, I think you did a great job.
Look below what I made of it, which is a 10 minute attempt. IOW, there is potential.Notice that you need to use the wrench you see in the 3rd attachment to proceed (and load the .itf attached first). The SL Parameter in there is not used by me. I only added it so it can be used by you if you want.
Further, all I changed really was this line :1SET STOP PLOSS TP * SLRatio //50which is totally unrelated to the program logic. N.b.: That “ratio” naming is a bit besides the truth, but you can name it as you want.
If you like, you could have a next assignment from me :
- You don’t know which Parameter combination I used, so you must find one yourself which will be able to compete with the future (you should soon see what I mean).
- The combination you finally chose, should be accompanied by your description why it is the best for you. Hint : you don’t want to lose your money.
- Of course you are going to show the results and post the .itf again. 🙂
Mind the Graph command I added and which you can do yourself just the same for any variable you like. GraphOnPrice also exists. And Print in V12.
Again, very well done.
PeterPS: Is that Spread of 2 something you dug up from IG web pages ? And does it count for the whole day ?
1 user thanked author for this post.
12/16/2023 at 4:14 PM #225342But I have also another small assignment for you (if you like) :
As you can see below, Size can become Negative and this is not intentional for this Short-only strategy (SellShort -1 will not work). So you should fine the culprit in this, or else at least prevent it (backdoor solution). The former is the way to go of course (solve it in the base).
1 user thanked author for this post.
12/17/2023 at 7:07 PM #225367Hi Peter
Thanks for your feedback and assistance.
- It looks like you used a TP of 180 and a ‘ratio’ setting of 0.3. This creates a 1:3.33 RR Ratio.
- I don’t want a ratio of zero; a SL won’t be set. Trades will continue until the TP is reached or my account goes bust. (The drawdown is bigger than the set account size).
- When looking at the results, setting a TP of 180 with a SL ratio of 0.3 or 0.4 gives the best return with the lowest drawdown. Ideally, the win rate needs work.
- Thanks for the graph command. That is going to help going forward.
- Regarding the spread, I didn’t put much thought into that. I’m more concerned about understanding the coding. I will focus on the finer details once I am more comfortable with the coding logic.
Response to your next post:
-
1SELLSHORT MAX(1,Size) CONTRACTS AT MARKET
The “MAX(1, Size)” part of the code is supposed to prevent negative contract size. The video also explained this: When selling short, the maximum size will be used (Size = 5), but not below 1.
I’m keen to hear your comments on the coding video once you have viewed them. The code that determines the contract size vs. the backtest results still baffles me.
Regards
Brad
12/18/2023 at 12:40 PM #225382Hi @Brad
I wouldn’t spend any more time on it because the code of the example is very illogical and full of errors…
For example, when I look at the “SignalBar” it becomes the same as the “BarIndex” when the conditions are true for the first time, but… this “SignalBar” is never reset…
I think the example is more something to highlight certain parts, but as a whole there is no logic to it…
-
AuthorPosts
Find exclusive trading pro-tools on