Break even coding
Forums › ProRealTime English forum › ProOrder support › Break even coding
- This topic has 21 replies, 4 voices, and was last updated 6 years ago by rmhandel.
Tagged: Break Even, BreakEven
-
-
05/29/2018 at 9:07 PM #71638
I want to include code that enables a trade to go to break even after n pips. I was given this code but does not seem to work on auto back test
123456789101112if not onmarket thenbreakeven=0endifif longonmarket thenif close-tradeprice(1)>=400 thenbreakeven = 1endifif breakeven thensell at tradeprice(1) stoendifendifThank you
Chris
05/29/2018 at 9:34 PM #71644Please try to post in the correct forum to get the best chance of assistance. I have moved your request from the ProBuilder forum which is for indicators to the ProOrder forum which is for auto-trading strategies.
Also it is preferred if you use the ‘Insert PRT Code’ button to make your posts more readable when they include code. I have tidied up your post for you 🙂
05/29/2018 at 9:44 PM #71646I’ll assume that it is not just because there is no ‘p’ in STOP!
Try this:
1234567891011if not longonmarket thenbreakeven = 0endifif longonmarket and close-tradeprice>=400 thenbreakeven = 1endifif longonmarket and breakeven thensell at tradeprice stopendifAre you testing in tick by tick mode?
05/29/2018 at 9:58 PM #7164805/29/2018 at 11:04 PM #7165105/29/2018 at 11:10 PM #7165205/29/2018 at 11:12 PM #71653Your code can be abridged like that
123if longonmarket and close-tradeprice>=400 thensell at tradeprice stopendiffor SHORT trades
123if shortonmarket and tradeprice-close>=400 thenexitshort at tradeprice stopendifThis code will work for DAX. S&P500 and other instruments, but not for FX pairs, since 400 will never be reached by any currency!!!! You should use 400*pipsize to make your code portable to all instruments.
05/29/2018 at 11:40 PM #71658123456789101112131415161718192021222324/ Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Conditions to enter long positionsindicator1 = ExponentialAverage[19](close)indicator2 = ExponentialAverage[63](close)c1 = (indicator1 CROSSES OVER indicator2)IF c1 and Not OnMarket THENBUY 19 CONTRACT AT MARKETENDIF// Conditions to enter short positionsindicator3 = ExponentialAverage[5](close)indicator4 = ExponentialAverage[68](close)c2 = (indicator3 CROSSES UNDER indicator4)IF c2 and Not OnMarket THENSELLSHORT 19 CONTRACT AT MARKETENDIF// Stops and targetsSET STOP pLOSS 20SET TARGET pPROFIT 60This is my basic code, and now I want it to trail to break even after 40 pips in profit.
05/30/2018 at 12:10 AM #71660Test this one, I added a few lines between 2 and 4
123456789101112131415161718192021222324252627282930313233343536373839404142// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivatedIF Not OnMarket THENBreakEven = 0ELSIF LongOnMarket THENIF (close - TradePrice) >= (40 * pipsize) THENBreakEven = 1ENDIFIF BreakEven THENSELL AT TradePrice STOPENDIFELSIF ShortOnMarket THENIF (TradePrice - close) >= (40 * pipsize) THENBreakEven = 1ENDIFIF BreakEven THENEXITSHORT AT TradePrice STOPENDIFENDIF// Conditions to enter long positionsindicator1 = ExponentialAverage[19](close)indicator2 = ExponentialAverage[63](close)c1 = (indicator1 CROSSES OVER indicator2)IF c1 and Not OnMarket THENBUY 19 CONTRACT AT MARKETENDIF// Conditions to enter short positionsindicator3 = ExponentialAverage[5](close)indicator4 = ExponentialAverage[68](close)c2 = (indicator3 CROSSES UNDER indicator4)IF c2 and Not OnMarket THENSELLSHORT 19 CONTRACT AT MARKETENDIF// Stops and targetsSET STOP pLOSS 20SET TARGET pPROFIT 6005/30/2018 at 3:14 AM #71667My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.
For some reason the theory doesnt seem to apply with this or any other code I have found.
Am I missing something?
05/30/2018 at 6:37 AM #71669Your code can be abridged like that
123if longonmarket and close–tradeprice>=400 thensell at tradeprice stopendiffor SHORT trades
123if shortonmarket and tradeprice–close>=400 thenexitshort at tradeprice stopendifWas your intention to do away with the BreakEven condition? Used on their own these will only close the trade if profit is above 400 and then falls in the same candle all the way back to zero. If profit is a above 400 and on the next candle drops to 300 then the sell order is not placed so there is no break even stop even though the trigger level was previously reached.
05/30/2018 at 10:06 AM #71674Vonasi, that was a simple suggestion on existing code, I did not even think about breakeven. There was no strategy, so I could only correct what was unnecessary and the way pips should be referenced.
I later posted the whole strategy revised with Breakeven.
05/30/2018 at 10:13 AM #71676My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.
For some reason the theory doesnt seem to apply with this or any other code I have found.
Am I missing something?
That code is, as requested, for trailing to Breakeven, which is done once 40 pips profit have been reached. If they go ahead and reach 60 they’ll cash 60 pips, if the go backwards they’ll exit at the entry price. If you want to trail the whole think, it’s not just a question of breakeven, it’s a question of a complete trailing stop system.
There’s one here that has been used for some years by many users and works quite well (MTF apart which will allow to improve the system): https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.
and official documentation https://www.prorealcode.com/blog/learning/kinds-trailing-stop-proorder/.
By searching you should also find a combination of the trailing stop code with breakeven code.
05/30/2018 at 10:23 AM #71677Vonasi, that was a simple suggestion on existing code, I did not even think about breakeven. There was no strategy, so I could only correct what was unnecessary and the way pips should be referenced.
I later posted the whole strategy revised with Breakeven.
No problem. I was just a bit concerned that someone reading it would think that that was a working break even code. 🙂
05/30/2018 at 10:28 AM #71678My analysis is that if I have 20 trades reach 40 pip profit and 16 trades reach 60 pip profit you would assume that with a 40 pip trail to break even that 4 trades would be break even if I was using a 60 pip take profit limit – which is the difference of the trades that reached 40 minus 60 pip trades.
For some reason the theory doesnt seem to apply with this or any other code I have found.
Am I missing something?
I would suggest graphing the close minus tradeprice and the high minus tradeprice so that you can more closely inspect what is happening when a long trade is opened (and the opposite for a short) which will give you more clues as to why it is not behaving how you think it should.
-
AuthorPosts
Find exclusive trading pro-tools on