I just joined the forum and was looking for a solution to a scale-in/scale-out problem similar as described in this thread. Like this:
1) I have a strategy that enters with 10 contracts All-in, sets 3 targets with different size. And this works fine.
2) I want to instead investigate how I can enter at multiple bars, 1 contract at a time. I thought this would be easy, but even though the contract accumulation is working correctly, all contracts are dumped on the first target even though I have monitored the size variable and it is set correctly.
Posting the code below. I am sure that this is a piece of cake many of you more experienced guys. Any help is really appreciated. Hope that I can help you guys with other things down the road.
Code for accumulating contracts. This works as expected.
Code for calculating size and targets. I have monitored the 3 different sizes, and the calculations are correct.
However all 10 contracts are sold on "EXIT Long Target1".
you can look into the reserved word "total". This is the explanation from the manual:
Used in strategy exit statements, following a numerical expression and the words Shares or Contracts, to indicate that only the number of contracts or shares specified by the numerical expression is to be sold or covered in total, regardless of the number of open entries. The contracts or shares will be sold or covered in the same order they were bought or shorted: First In, First Out. If the word Total is not used, the number of contracts or shares specified by the numerical expression will be sold or covered for each one of the open entries.
If this doesn't give you the desired result you will have to tie the exit orders to specific entry orders. For example you can dynamically change the entry name for each order, so each entry is unique. You can for example add the number of contracts at the time of the entry (converted to a string).
This will also ensure that an exit is not re-used.
The following user says Thank You to ABCTG for this post:
That is really excellent advice. I just added "total" and it works as expected now.
Your second suggestion is also a good one and I'll keep that in mind for future work.
Many thanks once again, Vielen Dank nochmal, I really appreciate it.