help with break-even code as a second position and second exit
Forums › ProRealTime English forum › ProOrder support › help with break-even code as a second position and second exit
- This topic has 38 replies, 3 voices, and was last updated 5 years ago by
robertogozzi.
Tagged: estimation, prc_qqe, qqe, qualitative, quantitative
-
-
12/02/2020 at 6:21 PM #152432
I mean the Red Arrows sorry not the blue ones forget the blue ones
12/03/2020 at 2:15 AM #152450There was an issue with AVERAGETRUERANGE, since it returns a very small value (such as 0.00963), it is always rounded to 0.
There’s no need to ROUND() the value returned by ATR; TP & SL can retain decimal values.
I also had to add PIPSIZE when setting STOP LOSS.
There you go:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103// Conditions to enter long positionsdefparam cumulateorders = falsePointsToKeep = 2 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)// Conditions to enter long positionsindicator2, indicator1, ignored = CALL "PRC_QQE indicator"//[11, 20, 4.236]A1 = (indicator1 CROSSES OVER indicator2)indicator3 = Williams[8](close)A2 = (indicator3 >= -20)IF A1 AND A2 AND Not OnMarket THEN// Stops and targets :St = AverageTrueRange[14](CLOSE)*2Tp = AverageTrueRange[14](CLOSE)*1.5startBreakeven = TP //how much pips/points in gain to activate the breakeven function?BUY 2 PERPOINT AT MARKETSET STOP PLOSS St * pipsize //first stoplossendif//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIFIF LONGONMARKET AND Close >= (TradePrice + Tp) AND abs(CountOfPosition) = 2 THENSELL 1 PERPOINT AT MARKETENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIF// Conditions to exit long positionsindicator5, indicator4, ignored = CALL "PRC_QQE indicator"//[11, 20, 4.236]B1 = (indicator4 CROSSES UNDER indicator5)IF B1 THENSELL AT MARKETENDIF// Conditions to enter short positions// Conditions to enter short positionsindicator7, indicator6, ignored = CALL "PRC_QQE indicator"//[11, 20, 4.236]C1 = (indicator6 CROSSES UNDER indicator7)indicator8 = Williams[8](close)C2 = (indicator8 <= -80)IF C1 AND C2 AND Not OnMarket THEN// Stops and targets :St = AverageTrueRange[14](CLOSE)*2Tp = AverageTrueRange[14](CLOSE)*1.5startBreakeven = TP //how much pips/points in gain to activate the breakeven function?SELLSHORT 2 PERPOINT AT MARKETSET STOP PLOSS St * pipsize //first stoplossendif//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// --- Sell SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF SHORTONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIFIF SHORTONMARKET AND Close <= (TradePrice - Tp) AND abs(CountOfPosition) = 2 THENEXITSHORT 1 PERPOINT AT MARKETENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENEXITSHORT AT breakevenLevel STOPENDIF// Conditions to exit short positionsindicator10, indicator9, ignored = CALL "PRC_QQE indicator"//[11, 20, 4.236]D1 = (indicator9 CROSSES OVER indicator10)IF D1 THENEXITSHORT AT MARKETENDIFGraph B1graph TP coloured(0,0,255,255)Graph abs(CountOfPosition)GraphOnPrice (TradePrice + Tp) coloured(0,255,0,255) AS "Long EXIT 1"GraphOnPrice (TradePrice - Tp) coloured(0,0,255,255) AS "Short EXIT 1"graph AverageTrueRange[14](CLOSE)I left some GRAPH and GRAPHONPRICE instructions at the end to help you debug your code.
1 user thanked author for this post.
12/08/2020 at 9:01 PM #153005Hello Roberto the break-even doing what it’s meant to do I added count of position to line 29.
The major problem I am having is the data from the average true range at the beginning of the trade. it takes the right data point from the buy trade price(tradeprice) but the problem comes from the scaling out because that becomes a new (tradeprice) line 34, so that’s a new data point from the ATR which then changes my stop loss my take profit and (St)
I just want to focus on how to keep the data point the same throughout the trade, is there a way of numbering the trade prices first trade price second trade price would that be the same as tradeindex.I’ve added the code is just the long side of it just to keep it simple and I’ve also added a picture that should explain this even clearer thank you very much
257123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// Conditions to enter long positionsdefparam cumulateorders = falsePointsToKeep = 2 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)// Conditions to enter long positions// Conditions to enter long positionsindicator2, indicator1, ignored = CALL "PRC_QQE indicator"[11, 20, 4.236]A1 = (indicator1 CROSSES OVER indicator2)indicator3 = Williams[8](close)A2 = (indicator3 >= -20)IF A1 AND A2 AND Not OnMarket THEN// Stops and targets :St = AverageTrueRange[14](CLOSE)*2Tp = AverageTrueRange[14](CLOSE)*1.5startBreakeven = TP //how much pips/points in gain to activate the breakeven function?BUY 2 PERPOINT AT MARKETSET STOP PLOSS St * pipsize //first stoplossendif//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND abs(CountOfPosition) = 2 and close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIFIF LONGONMARKET AND Close >= (TradePrice + Tp) AND abs(CountOfPosition) = 2 THENSELL 1 PERPOINT AT MARKETENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIFGraphOnPrice (breakevenlevel) coloured(0,100,0,150) AS "breakeven1"Graph abs(CountOfPosition)GraphOnPrice (TradePrice + Tp) coloured(0,0,255,255) AS "Long EXIT 1"GraphOnPrice (TradePrice + St) coloured(0,255,0,255) AS "Long EXIT 2"GraphOnPrice (TradePrice - St) coloured(220,20,60) AS "Short EXIT 1"12/09/2020 at 1:28 AM #153019Hello Roberto I’ve made a mistake in the statement above
the data point from the ATR is not actually changing it’s doing what it’s meant to do, and what I want it to do but where it’s anchored from the trade price that’s changing I need to somehow anchor it to the first trade price the buying of the two contracts when I scale-out half that becomes the new trade price the new image below I put rulers on it to take measurements on the left side of the trade when there are two contracts and on the right side when there’s one contract,
And you can see the measurements are the same but they’ve moved because the Anchor point is the trade price and the scaling out add new trade prices12/09/2020 at 2:28 AM #153028Replace any occurrence of TRADEPRICE(1) with:
1TRADEPRICE(N)Add this at line 3:
1ONCE N = 1add this line between lines 19 and 20:
1N = 1and add this line between lines 35 and 36:
1N = 2All these changes will allow your code to refer TRADEPRICE(1) when 2 positions are bought, then TRADEPRICE(2) after half is sold, because at that time the EXIT price becomes the new TRADEPRICE(1) so you’ll have to refer to the previous one which has become TRADEPRICE(2).
1 user thanked author for this post.
12/11/2020 at 8:23 PM #153433Hello Roberto
I’ve added a trailing stop to the code it seems to be working alright 90% I’ve added some pictures down below to help explain but the TS and TP there’s some spikes on the chart that worry me a little you might be able to iron them out not sure. The trailing stop is nearly close to perfect but you might be able to make it actually perfect, again I’m not sure so I’ve added 2 pictures and if you take a look it should explain it, The trailing stop is just missing its target or missing a candle altogether
2591234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// Conditions to enter long positionsdefparam cumulateorders = falseONCE N = 1PointsToKeep = 20 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)// Conditions to enter long positions// Conditions to enter long positionsindicator2, indicator1, ignored = CALL "PRC_QQE indicator"[11, 20, 4.236]A1 = (indicator1 CROSSES OVER indicator2)indicator3 = Williams[8](close)A2 = (indicator3 >= -20)IF A1 AND A2 AND Not OnMarket THEN// Stops and targets :St = AverageTrueRange[14](CLOSE)*2Tp = AverageTrueRange[14](CLOSE)*1.5startBreakeven = TP //how much pips/points in gain to activate the breakeven function?trailingstart = St //trailing will start @trailinstart points profittrailingstep = Tp* pipsize //trailing step to move the "stoploss"BUY 2 PERPOINT AT MARKETSET STOP PLOSS St *pipsize //first stoplossN = 1endif//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND abs(CountOfPosition) = 2 and close-TRADEPRICE(N)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = TRADEPRICE(N)+PointsToKeep*pipsizeENDIFIF LONGONMARKET AND Close >= (TRADEPRICE(N) + Tp) AND abs(CountOfPosition) = 2 THENSELL 1 PERPOINT AT MARKETN = 2ENDIFIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-TRADEPRICE(N)>=trailingstart*pipsize THENnewSL = high-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = high-trailingstep*pipsizeENDIFENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIFIF newSL>0 THENSELL AT newSL STOPENDIFGraphOnPrice (newSL) coloured(255,165,0) AS "Trallingstop"GraphOnPrice (breakevenlevel) coloured(0,100,0,150) AS "breakeven1"Graph abs(CountOfPosition)GraphOnPrice (TradePrice(N) + Tp) coloured(0,0,255,255) AS "Long EXIT 1"GraphOnPrice (TradePrice(N) + St) coloured(0,255,0,255) AS "Long EXIT 2"GraphOnPrice (TradePrice(N) - St) coloured(220,20,60) AS "Short EXIT 1"//GraphOnPrice (TradePrice(N) - tp) coloured(220,20,60) AS "Short EXIT 2"Thank you
12/11/2020 at 11:00 PM #153443good evening i just arrived on this topic..cannot find: PRC_QQE indicator
12/12/2020 at 12:41 AM #153453There you go https://www.prorealcode.com/prorealtime-indicators/qqe-quantitative-qualitative-estimation/.
1 user thanked author for this post.
12/12/2020 at 12:27 PM #153491Your trailing stop code follows HIGH, what’s wrong with it?
As to spikes, you can mitigate ATR by applying an average to it (I used 20 periods):
1average[20,0](AverageTrueRange[14](CLOSE)) -
AuthorPosts
Find exclusive trading pro-tools on