Entry, Stop Loss and Profit Target coding question
Forums › ProRealTime English forum › ProOrder support › Entry, Stop Loss and Profit Target coding question
- This topic has 13 replies, 3 voices, and was last updated 6 years ago by
Vonasi.
-
-
04/02/2019 at 9:59 PM #95358
Hello,
I tried to code a trading system and started with the Simplified Creation but this doesn’t allow me to do all the things I want.
I need some help with a few coding questions.
- My first issue is, once all my conditions are met, I would like to sell short 1 pip below of the signal candle.
- For my exit condition (c6), I want it to trigger only after the close of the current bar?
- I would like to create the following Stop Loss, Profit Target and Trailing Stop Loss:
Stop Loss: Entry + 2 x AverageTrueRange (14)
Profit Target: Entry – 2.5 x AverageTrueRange (14)
Trailing Stop Loss: When Entry – 2x AverageTrueRange(14) is reached, move Stop Loss to Entry – 1pip.
How do I code these conditions?
Below you can find the code I have created so far with Simplified Creation.
Momentum Strategy123456789101112131415161718192021222324252627282930// Conditions to enter short positionsignored, ignored, indicator1 = CALL "Momentum_F"c1 = (indicator1 = 1)c2 = (indicator1[1] = 1)ignored, indicator2 = CALL "PowerX 2"c3 = (indicator2 = 3)c4 = (indicator2[1] < 3)indicator3 = Average[50](close)c5 = (close < indicator3)IF (c1 AND c2 AND c3 AND c4 AND c5) AND timeEnterAfter AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT MARKETENDIF// Conditions to exit short positionsignored, indicator4 = CALL "PowerX 2"c6 = (indicator4 < 3)indicator5 = Average[50](close)c7 = (close > indicator5)IF c6 OR c7 THENEXITSHORT AT MARKETENDIF// Stops and targetsSET STOP pLOSS 12SET TARGET pPROFIT 15I appreciate every help.
Thank you.
Sascha
04/03/2019 at 9:15 AM #95371Points 1,2 and 3 included in the below version. Not tested since I don’t have any of the indicators you are calling in your program.
1234567891011121314151617181920212223242526272829303132// Conditions to enter short positionsignored, ignored, indicator1 = CALL "Momentum_F"c1 = (indicator1 = 1)c2 = (indicator1[1] = 1)ignored, indicator2 = CALL "PowerX 2"c3 = (indicator2 = 3)c4 = (indicator2[1] < 3)indicator3 = Average[50](close)c5 = (close < indicator3)IF (c1 AND c2 AND c3 AND c4 AND c5) AND timeEnterAfter AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT low-1*pointsize stopbarorder=barindexatr=averagetruerange[14]// Stops and targetsSET STOP LOSS 2*atrSET TARGET PROFIT 2.5*atrENDIF// Conditions to exit short positionsignored, indicator4 = CALL "PowerX 2"c6 = (indicator4 < 3)indicator5 = Average[50](close)c7 = (close > indicator5)IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) THENEXITSHORT AT MARKETbarorder=0ENDIF04/03/2019 at 9:58 AM #95378Hello Nicolas,
Thank you very much for the super fast reply. That’s amazing.
How do I code the Trailing Stop Loss in point 3.)
Trailing Stop Loss: When Entry – 2x AverageTrueRange(14) is reached, move Stop Loss to Entry – 1pip. (meaning 1 pip in profit)
Also, I always try to understand the code so I can learn something. As I don’t fully understand the conditions you created with barindex and barorder,
I tried to find the meaning of barindex and found in an older post the following definition: Bar Index is the number of bars the System has been running
So does the condition barindex–barorder>=1 make the system wait until the end of the bar?
Why do we add barorder>0 (line 29) and then barorder=0 (in line 31)?
Thanks so much.
Sascha
04/03/2019 at 10:26 AM #9538104/03/2019 at 1:47 PM #95406Hi Nicolas,
Thanks for your comment.
Meanwhile I’ve been reading in various posts about Stop Losses and it’s surprising how difficult it seems to implement in PRT code what seems like a simple one step stop loss adjustment.
So far I haven’t found any solution for how to code my Stop loss change to breakeven + 1 pip profit.
Stop Loss: When Entry – 2x AverageTrueRange(14) is reached, move Stop Loss to Entry – 1pip. (meaning 1 pip in profit)
The question is how do you capture or code the moment of entry and then tell PRT to move the stop loss in the desired way?
Any feedback is highly appreciated.
Thank you.
Sascha
04/08/2019 at 8:54 AM #95677how difficult it seems to implement in PRT code what seems like a simple one step stop loss adjustment.
Because there are billions of variants and way to code it. What you ask if a never seen before feature.
Simple breakeven code here: https://www.prorealcode.com/topic/move-sl-during-current-candle/#post-79130
Should be adapted to move the stoploss to breakeven when the 2x AverageTrueRange(14) is reached though, so you have to save that value when you launch an order and use it to put the order to BE. Let me know if you are in trouble.
04/10/2019 at 10:15 AM #95924Hi Nicolas,
Thank you for your reply and sending me the link to the code.
I looked through it and tried to implement it.
Attached I send you how it looks like in my code. I am not sure if I adjusted it correctly in terms of saving the value when I launch an order, as you said.
I would appreciate if you could take a quick look at it.
Is there a difference between Averagetruerange[14] and Averagetrurange[14](close)?
By the way, I created the code for the long side as well. I assume I can just put it beneath the code for the short trade?
Thanks again.
Sascha
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Cancel all pending orders and close all positions at the "FLATAFTER" timeDEFPARAM FLATAFTER = 200000// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryBeforeTime = 060000timeEnterBefore = time >= noEntryBeforeTime// Prevents the system from placing new orders to enter the market or increase position size after the specified timenoEntryAfterTime = 235900timeEnterAfter = time < noEntryAfterTime// Prevents the system from placing new orders on specified days of the weekdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// CONDITIONS TO ENTER SHORT POSITIONSignored, ignored, indicator1 = CALL "Momentum_F"c1 = (indicator1 = 1)c2 = (indicator1[1] = 1)ignored, indicator2 = CALL "PowerX 2"c3 = (indicator2 = -3)c4 = (indicator2[1] > -3)SMA50 = Average[50](close)c5 = (close < SMA50)c6 = ADX < 40IF (c1 AND c2 AND c3 AND c4 AND c5 AND c6) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THENSELLSHORT 1 SHARES AT low-1*pointsize stopbarorder=barindexATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 2*atrSET TARGET PROFIT 2.5*atrENDIF//if not order on market, reset the breakeven statusif not onmarket thenbreakeven=0endif//check if the current order has made 2*ATR of signal dayif shortonmarket and tradeprice-close >=2*ATR thenbreakeven=1endif//put stoploss at open price - 3 pipif breakeven=1 thenbuy at tradeprice-3*pointsize stopendif// Conditions to exit short positionsignored, indicator3 = CALL "PowerX 2"c6 = (indicator3 > -3)c7 = (close > SMA50)IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) THENEXITSHORT AT MARKETbarorder=0ENDIF// CONDITIONS TO ENTER LONG POSITIONSindicator4, ignored, ignored = CALL "Momentum_F"d1 = (indicator4 = 1)d2 = (indicator4[1] = 1)indicator5, ignored = CALL "PowerX 2"d3 = (indicator5 = 3)d4 = (indicator5[1] < 3)d5 = (close > SMA50)d6 = ADX < 40IF (d1 AND d2 AND d3 AND d4 AND d5 AND d6) AND timeEnterAfter AND not daysForbiddenEntry THENBUY 1 SHARES AT high+1*pointsize stopbarorder=barindexATR = Averagetruerange[14]// Stops and targetsSET STOP LOSS 2*atrSET TARGET PROFIT 2.5*atrENDIF//if not order on market, reset the breakeven statusif not onmarket thenbreakeven=0endif//check if the current order has made 2*ATR of signal dayif longonmarket and close-tradeprice >=2*ATR thenbreakeven=1endif//put stoploss at open price + 3 pipif breakeven=1 thensell at tradeprice+3*pointsize stopendif// Conditions to exit long positionsindicator8, ignored = CALL "PowerX 2"d6 = (indicator8 < 3)d7 = (close < SMA50)IF d6 OR d7 and (barindex-barorder>=1 and barorder>0) THENSELL AT MARKETbarorder=0ENDIF04/18/2019 at 3:39 PM #96761Hi Nicolas,
I have a question regarding barindex and barorder that you created for me two weeks ago.
I added another exit condition which says if there is a Key Reversal bar (e3) then exit.
However, in one trade example the condition was met right the first candle after entry and the trade was not exited.
When I deleted the condition: (barindex-barorder>=1 and barorder>0 the trade was exited. So I assume something would need to be adjusted with this condition.
Does this condition not allow other conditions to work the bar after entry?
12345678910// Conditions to exit long positionsindicator8, ignored = CALL "PowerX 2"e1 = (indicator8 < 3)e2 = (close < SMA50)e3 = (high>high[1]) and (low<low[1]) and (close<open[1])IF e1 OR e2 OR e3 and (barindex-barorder>=1 and barorder>0) THENSELL AT MARKETbarorder=0ENDIF04/18/2019 at 3:46 PM #96763Hi Nicolas,
A quick update:
I just changed (barindex–barorder>=1 and barorder>0) to (barindex–barorder>=1 and barorder>=0).
All I changed was barorder>=0.
Now the trade was exited after the first bar, but I am not sure if that changes the original purpose of this condition?
04/18/2019 at 4:21 PM #9676504/18/2019 at 9:17 PM #96788Hi Nicolas,
I’ll try to understand your comment. Maybe I’ll find another article that helps me understand it. First I need to fully understand the condition.
In a recent comment regarding (barindex–barorder>=1 and barorder>0) that you added to my code, you said:
” Because we want to make sure at least 1 bar is elapsed since the opening of the order and that we are trying to close the current running order and not an “old one”. ”
So, barindex–barorder>=1 makes sure at least 1 bar is elapsed?
And barorder>0 would close the current running order and not an “old one”? I guess I am struggling to understand this part. What if I set it to barorder>=0, would it still close the current running order? In my example mentioned, changing it to barorder>=0 closed the trade after the first candle in the trade, and leaving it at barorder>0 the trade was not exited. But I don’t understand why because I don’t understand the condition.
In the code your wrote barorder = barindex, so can barorder ever be 0?
I would say no as barindex is the current bar loaded buy a trading system, so I would think at the time of a trading signal the barindex is not 0.
I guess you can see that I am confused. I’m sorry.
04/18/2019 at 9:51 PM #96789I’ll try to understand your comment. Maybe I’ll find another article that helps me understand it.
Nicolas is referring to this instruction:
https://www.prorealcode.com/documentation/graph/
It and information and examples of all instructions can be found by hovering over help and clicking on ‘ProBuilder language documentation’ which will take you to here:
04/19/2019 at 12:30 AM #96795Thanks Vonasi!
In the meantime I had searched for the meaning of the GRAPH function and I was able to draw the variable.
I think my problem is that Nicolas gave me a little piece of code and I don’t fully understand it.
When I use the GRAPH function to draw Barorder, then I see that this variable only jumps from 0 to the value of Barindex in the moment when my conditions are met and with the next candle it drops back to 0 and stays there all the time until next signal.
This means that the piece of code from Nicolas Barorder>0 can never be true for my exits as it is always zero accept on the signal day. Consequently my exit conditions should never work.
However, they do work. That’s the confusing part. When I look at trade examples, trades are exited with my conditions (for example when condition d6 was met). Therefore I don’t understand it.
How can that be?
12345678910// Conditions to exit short positionsignored, indicator3 = CALL "PowerX 2"c6 = (indicator3 > -3)c7 = (close > SMA50)IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) THENEXITSHORT AT MARKETbarorder=0ENDIF04/19/2019 at 7:47 AM #96805I’ve not looked in detail at your code (not enough coffee!) but my first thought was that you might want to try adding NOT ONMARKET to your entry IF THEN conditions and ONMARKET to your IF THEN exit conditions because without this even though you do not enter market because you are already on market and CUMULATEORDERS = FALSE any variable values will still be changed if your conditions are met.
-
AuthorPosts
Find exclusive trading pro-tools on