I wrote this code below.
When a certain price level is reached I want to move the stop loss to the entry price. but once it is set trying to set it again does not work.
I saw that the code is reached but has no effect.
any advise would be highly appreciated
IF ONMARKET THEN
IF LONGONMARKET THEN
LongTriggered=1
IF(Close - POSITIONPRICE)>= ZeroRiskLevel THEN
State=13
SET STOP LOSS POSITIONPRICE//0
ENDIF
ELSIF SHORTONMARKET THEN
ShortTriggered=1
IF(POSITIONPRICE-Close)>= ZeroRiskLevel THEN
State=14
SET STOP LOSS POSITIONPRICE//0
ENDIF
ENDIF
//FOR EACH BAR
ELSIF NOT ONMARKET THEN
IF InitDailyProfit>=StrategyProfit THEN // if no profit was made today continues. else wait for next day
IF LongTriggered=0 THEN
BUY StakeSize CONTRACTS AT MyBuyprice stop
ENDIF
IF ShortTriggered=0 THEN
SELLSHORT StakeSize CONTRACTS AT MySellprice stop
ENDIF
SET STOP LOSS StopLoss
SET TARGET PROFIT StopLimit
ENDIF
ENDIF
I think the problem could be changing a stop loss when a buy/sell is already active.
how can this be done?
is there a workaround with trailing stop or other thoughts?
There are 2 solutions for u, search for the “breakeven” stop loss + the MFE trailing stop loss on this forum.
Breakeven stop loss is self explanatory
The MFE trailing waits for price to close over a certain amount of pips and then start trailing with XX trailing stop.
yes that is exactly what I was looking for thanks!
Now I realize that instead of setting a new stoploss I need to set a SELL/SHORTSELL instead.
Good to know
Thanks
PaulParticipant
Master
Hi here is an example
It uses % instead of points.
BreakEvenStop = 1 // BreakEvenStop and BreakEvenStop Minimum Gain
bs = 1.00 // % BreakEvenStop
bsm= 0.10 // % BreakEvenStop Minimum Gain
if tcShort and tcxShort then
if (s0 or s1 or s2 or s3 or s4 or s5 or s6 or s7 or s8 or s9) then
sellshort positionsize contract at market
shorttradecounter=shorttradecounter + 1
If BreakEvenStop then
SET STOP %LOSS bsm
endif
endif
endif
endif
// BreakEvenStop
If BreakEvenStop then
if not onmarket then
newSL=0
endif
If longonmarket and close-tradeprice(1)>=((tradeprice/100)*bs)*pipsize then
newSL = tradeprice(1)+((tradeprice/100)*bsm)*pipsize
endif
If shortonmarket and tradeprice(1)-close>=((tradeprice/100)*bs)*pipsize then
newSL = tradeprice(1)-((tradeprice/100)*bsm)*pipsize
endif
If newSL>0 then
sell at newSL Stop
exitshort at newSL Stop
endif
endif
Even after this change I don’t think my code works as expected. Looking at the charts I don’t see it exit when the “break even” price is reached.
Here is the part of the code that deals with “on market” case. what do you think?
the state is reached (state 13 and 14 in this example)
IF ONMARKET THEN
IF LONGONMARKET THEN
LongTriggered=1
IF(Close - POSITIONPRICE)>= ZeroRiskLevel THEN
State=13
//SET STOP LOSS POSITIONPRICE//0
SELL AT POSITIONPRICE STOP
ENDIF
ELSIF SHORTONMARKET THEN
ShortTriggered=1
IF(POSITIONPRICE-Close)>= ZeroRiskLevel THEN
State=14
//SET STOP LOSS POSITIONPRICE//0
EXITSHORT AT POSITIONPRICE STOP
ENDIF
ENDIF
Did u try the breakeven code from forum?
Copy pastaed the example with the breakeven code in it..
defparam cumulateorders = false
startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
c1 = RSI[14] crosses over 50
if c1 then
BUY 1 LOT AT MARKET
SET STOP PLOSS 50 //first stoploss
endif
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
Thanks
I used a similar code as I wrote above and specifically added this:
SELL AT POSITIONPRICE STOP
following the forum code you mentioned.
it seems to have not made a change in the charts though. If I put a breakeven of 100 pts or 40 pts the gains don’t change even though they should have
So it is either a problem with my code or a problem with the detailed report in ProRealCode (I am guessing it’s my code but I don’t know what the problem is)…
startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
Can u try to add this exact code at the bottom and check ur detailed report?
As soon as profit > 30 pips (on a closed candle, not the high of the candle), this should close your trade at the least 5 pips in profit (without calculating spread and cost ofc.)
how to make this code for the short market???
startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// — BUY SIDE —
//test if the price have moved favourably of “startBreakeven” points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// — end of BUY SIDE —
@frenqle
For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part!
Thank you!
This is the code (you could have found it already coded, with a slightly different approach, at https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/):
startBreakeven = 30 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// — BUY SIDE —
//test if the price have moved favourably of “startBreakeven” points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 AND LongOnMarket THEN
SELL AT breakevenLevel STOP
ENDIF
//
// — SHORT SIDE —
//test if the price have moved favourably of “startBreakeven” points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 AND ShortOnMarket THEN
EXITSHORT AT breakevenLevel STOP
ENDIF