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
if not onmarket then
breakeven=0
endif
if longonmarket then
if close-tradeprice(1)>=400 then
breakeven = 1
endif
if breakeven then
sell at tradeprice(1) sto
endif
endif
Thank you
Chris
Please 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 🙂
I’ll assume that it is not just because there is no ‘p’ in STOP!
Try this:
if not longonmarket then
breakeven = 0
endif
if longonmarket and close-tradeprice>=400 then
breakeven = 1
endif
if longonmarket and breakeven then
sell at tradeprice stop
endif
Are you testing in tick by tick mode?
No that was my mistake. I have used the ‘p’ in the code.
Thank you for moving to correct forum
Vonasi, How would the code be written for short entries?
Yes I am in tick by tick mode
What I am trying to achieve is:
to have an initial stop of 200 pips, then once the trade movies in my favor by 400 pips I want to trail stop to break even. I have a limit order set at 600 pips to take profit.
Your code can be abridged like that
if longonmarket and close-tradeprice>=400 then
sell at tradeprice stop
endif
for SHORT trades
if shortonmarket and tradeprice-close>=400 then
exitshort at tradeprice stop
endif
This 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.
/ Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = ExponentialAverage[19](close)
indicator2 = ExponentialAverage[63](close)
c1 = (indicator1 CROSSES OVER indicator2)
IF c1 and Not OnMarket THEN
BUY 19 CONTRACT AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = ExponentialAverage[5](close)
indicator4 = ExponentialAverage[68](close)
c2 = (indicator3 CROSSES UNDER indicator4)
IF c2 and Not OnMarket THEN
SELLSHORT 19 CONTRACT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 20
SET TARGET pPROFIT 60
This is my basic code, and now I want it to trail to break even after 40 pips in profit.
Test this one, I added a few lines between 2 and 4
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
IF Not OnMarket THEN
BreakEven = 0
ELSIF LongOnMarket THEN
IF (close - TradePrice) >= (40 * pipsize) THEN
BreakEven = 1
ENDIF
IF BreakEven THEN
SELL AT TradePrice STOP
ENDIF
ELSIF ShortOnMarket THEN
IF (TradePrice - close) >= (40 * pipsize) THEN
BreakEven = 1
ENDIF
IF BreakEven THEN
EXITSHORT AT TradePrice STOP
ENDIF
ENDIF
// Conditions to enter long positions
indicator1 = ExponentialAverage[19](close)
indicator2 = ExponentialAverage[63](close)
c1 = (indicator1 CROSSES OVER indicator2)
IF c1 and Not OnMarket THEN
BUY 19 CONTRACT AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = ExponentialAverage[5](close)
indicator4 = ExponentialAverage[68](close)
c2 = (indicator3 CROSSES UNDER indicator4)
IF c2 and Not OnMarket THEN
SELLSHORT 19 CONTRACT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 20
SET TARGET pPROFIT 60
My 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?
Vonasi, 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.
My 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.
Vonasi, 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. 🙂
My 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.