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.
// Conditions to enter short positions
ignored, 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 THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
// Conditions to exit short positions
ignored, indicator4 = CALL "PowerX 2"
c6 = (indicator4 < 3)
indicator5 = Average[50](close)
c7 = (close > indicator5)
IF c6 OR c7 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 12
SET TARGET pPROFIT 15
I appreciate every help.
Thank you.
Sascha
Points 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.
// Conditions to enter short positions
ignored, 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 THEN
SELLSHORT 1 SHARES AT low-1*pointsize stop
barorder=barindex
atr=averagetruerange[14]
// Stops and targets
SET STOP LOSS 2*atr
SET TARGET PROFIT 2.5*atr
ENDIF
// Conditions to exit short positions
ignored, 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) THEN
EXITSHORT AT MARKET
barorder=0
ENDIF
Hello 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
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”.
Hi 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
how 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.
Hi 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
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 200000
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryBeforeTime = 060000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 235900
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// CONDITIONS TO ENTER SHORT POSITIONS
ignored, 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 < 40
IF (c1 AND c2 AND c3 AND c4 AND c5 AND c6) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 1 SHARES AT low-1*pointsize stop
barorder=barindex
ATR = Averagetruerange[14]
// Stops and targets
SET STOP LOSS 2*atr
SET TARGET PROFIT 2.5*atr
ENDIF
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
//check if the current order has made 2*ATR of signal day
if shortonmarket and tradeprice-close >=2*ATR then
breakeven=1
endif
//put stoploss at open price - 3 pip
if breakeven=1 then
buy at tradeprice-3*pointsize stop
endif
// Conditions to exit short positions
ignored, indicator3 = CALL "PowerX 2"
c6 = (indicator3 > -3)
c7 = (close > SMA50)
IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) THEN
EXITSHORT AT MARKET
barorder=0
ENDIF
// CONDITIONS TO ENTER LONG POSITIONS
indicator4, 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 < 40
IF (d1 AND d2 AND d3 AND d4 AND d5 AND d6) AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 1 SHARES AT high+1*pointsize stop
barorder=barindex
ATR = Averagetruerange[14]
// Stops and targets
SET STOP LOSS 2*atr
SET TARGET PROFIT 2.5*atr
ENDIF
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
//check if the current order has made 2*ATR of signal day
if longonmarket and close-tradeprice >=2*ATR then
breakeven=1
endif
//put stoploss at open price + 3 pip
if breakeven=1 then
sell at tradeprice+3*pointsize stop
endif
// Conditions to exit long positions
indicator8, ignored = CALL "PowerX 2"
d6 = (indicator8 < 3)
d7 = (close < SMA50)
IF d6 OR d7 and (barindex-barorder>=1 and barorder>0) THEN
SELL AT MARKET
barorder=0
ENDIF
Hi 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?
// Conditions to exit long positions
indicator8, 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) THEN
SELL AT MARKET
barorder=0
ENDIF
Hi 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?
With just
GRAPH barindex-barorder
you’ll save a lot of time debugging a simple condition 🙂
Hi 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.
I’ll try to understand your comment. Maybe I’ll find another article that helps me understand it.
Nicolas is referring to this instruction:
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:
https://www.prorealcode.com/prorealtime-documentation/
Thanks 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?
// Conditions to exit short positions
ignored, indicator3 = CALL "PowerX 2"
c6 = (indicator3 > -3)
c7 = (close > SMA50)
IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) THEN
EXITSHORT AT MARKET
barorder=0
ENDIF
I’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.