How to code target and stop ?
Forums › ProRealTime English forum › ProOrder support › How to code target and stop ?
- This topic has 4 replies, 2 voices, and was last updated 8 years ago by jonjon.
-
-
11/07/2016 at 1:03 PM #16076
A request that was addressed to ProRealTime:
Hi. I’m new to PRT and to coding and I’m creating my first trading system. I have just started a Premium subscription. The entry for my system seems to be working however I don’t know where to start with coding the targets and stops. I’ve read the trading system manual and have looked through the 22 pages of trading support conversations but can’t find what I’m after. The closest thing I found is here but this doesn’t give me exactly what I want: http://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/
Example here is where I have a long position in the Bund but same would apply (but opposite) if I was short. The three steps are set out. Idea is to have a position with 2 target prices and then a trailing stop for the remaining position (in this instance, Targets 1 and 2 to cover 2/3s of the position and then running remaining 1/3).
Price
e.g. long 3 lots Bund @ 162.401 Stop (3 lots) -4 ticks 162.36
Target 1: 1 lot at +4 ticks 162.44
Target 2: 1 lot at +8 ticks 162.482 Price moves up to Target 1 162.44
Stop (now 2 lots) moves to break even 162.40
Target 2 unchanged 162.483 Price moves up to Target 2 162.48
1 lot remaining which I want to run
Stop (for 1 lot) becomes a trailing stop (-4 ticks) 162.44Suggestion for an anwser:
123456789101112131415161718192021// Conditions pour ouvrir une position acheteuseIF NOT LongOnMarket THENbuy 3 shares at marketsell 1 shares at close+4*pointsize limitsell 1 share at close+8*pointsize limitset stop ploss 4ENDIF// Conditions pour fermer une position acheteuseIf LongOnMarket THENif countoflongshares=3 thensell 1 shares at tradeprice+4*pointsize limitsell 1 share at tradeprice+8*pointsize limitelsif countoflongshares=2 thensell 1 share at tradeprice+8*pointsize limitsell at tradeprice(2) stopset stop ploss 0elsif countoflongshares=1 thenset stop ptrailing 4ENDIFendif1 user thanked author for this post.
11/07/2016 at 9:25 PM #16124Henry. Many many thanks for the help. Really appreciated. I’m having a problem with it but I suspect it could be how I’ve incorporated it into the system. I’ve looked at a short position today (so swapped your code around). When I back tested it it seemed to hit the stop 4 away (as it should), flatten the position (again as it should), but then it took a 1 lot long position. Could it be that the covers
12buy <span class="token number">1</span> <span class="token keyword">shares</span> <span class="token keyword">at</span> <span class="token keyword">close-</span><span class="token number">4</span><span class="token operator">*</span><span class="token keyword">pointsize</span> <span class="token keyword">limit</span><span class="token keyword">buy</span> <span class="token number">1</span> <span class="token keyword">share</span> <span class="token keyword">at</span> <span class="token keyword">close-</span><span class="token number">8</span><span class="token operator">*</span><span class="token keyword">pointsize</span> <span class="token keyword">limit</span>are being left in the market after the stop is hit? Reason I think this is the system went short the Bund at 162.13, stop got hit in the next bar at 162.17 (4 units away, as it should) but it then went long at 162.09 (which would equate with inprice of 162.13 – (4*0.01)).? The market then reversed so didn’t get chance to see if an order was left at 162.13 – (8*0.01)).
Here is the code that I used (which incorporated your suggestion) although I excluded the “If NOT LongOnMarket THEN” (or” If NOT ShortOnMarket THEN” in my case as I went short)
12345678910111213141516171819IF c1 AND c2 THENSELLSHORT 3 SHARES AT MARKETbuy 1 shares at tradeprice-(4*0.01) limitbuy 1 share at tradeprice-(8*0.01) limitset stop ploss 4ENDIFIf ShortOnMarket THENif countofshortshares=3 thenbuy 1 shares at tradeprice-(4*0.01) limitbuy 1 share at tradeprice-(8*0.01) limitelsif countofshortshares=2 thenbuy 1 share at tradeprice-(8*0.01) limitbuy at tradeprice(2) stopset stop ploss 0elsif countofshortshares=1 thenset stop ptrailing 4ENDIFENDIFMany thanks again for your help.
11/14/2016 at 12:45 PM #16492Hi JonJon,
You can find below the code modified.
12345678910111213141516171819IF c1 AND c2 THENSELLSHORT 3 SHARES AT MARKETexitshort 1 share at close-(4*pointsize) limitexitshort 1 share at close-(8*pointsize) limitset stop ploss 4ENDIFIf ShortOnMarket THENif countofshortshares=3 thenexitshort 1 share at tradeprice-(4*pointsize) limitexitshort 1 share at tradeprice-(8*pointsize) limitelsif countofshortshares=2 thenexitshort 1 share at tradeprice(2)-(8*pointsize) limitexitshort at tradeprice(2) stopset stop ploss 0elsif countofshortshares=1 thenset stop ptrailing 4ENDIFENDIF1 user thanked author for this post.
11/15/2016 at 12:36 PM #1656611/18/2016 at 4:32 PM #16782Hi Henry
It’s me again (unfortunately). I’ve been testing the code all week. Works really well however one question and one observation:
1) Can PRT update the orders mid bar? The strategy looks at 15min bars (I know, a lot can happen in 15min). For example,
Triggers and sells 3 @ 160.94
1st cover: buys 1 @ 160.90 (as it should) (-4 ticks away)
2nd cover: 1 buys @ 160.86 (as it should) (-8 ticks away).
net position: -1…all happening in the first 15 min. At the moment I think the code switches to:
12<span class="token keyword">elsif</span> <span class="token keyword">countofshortshares</span><span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">then</span><span class="token keyword">set stop</span> ptrailing <span class="token number">4</span>at the beginning of the second bar instead of partway through the first bar as ideally it should. I think the answer is “no” as I understand that PRT only recalculates at the end of each bar? Just checking.
2) I’ve spotted an “nuance” which repeats itself every now and again. Happens where we get triggered and only 1 cover in first 15min as follows:
15min #1
Triggers and sells 3 @ 161.20
1st cover: buys 1 @ 160.16 (as it should) (-4 ticks away)
net position: -2
15min #2
Market continues to fall so stop does not get triggered (stop now at inprice of 161.20 which is as it should and is great).
Bar open at 161.14
System puts in 2 covers:
Buys 1 @ 161.10 (-4 from bar open)
Buys 1 @ 161.06 (-8 from bar open)
net position = 0
a) It is almost as if it doesn’t recognise that the countofshortshares is 2 at the start of this bar and places the two covers which flattens the position when they are hit. Unfortunately this means that I am not able to run the last 1 lot with a trailing stop which is the intention.
b) these covers are placed based on the opening price of the second 15min bar (in this example 161.14, and -4 and -8 ticks below this) instead of the inprice of 161.20 which was the intention. This is actually in my favour when the market is falling however just pointing it out….
I’ve tried playing with the code for days however same results. Any help you can give will be very appreciated.
Thanks for all your help thus far.
-
AuthorPosts
Find exclusive trading pro-tools on