Hello
help with break-even code as a second position and second exit
I have conditions to go long and conditions to go short and I have exit conditions which is a stop loss and take profit but what i would like to try and do, If the take profit is activated is to have a second contract (pound per point) which would have a break-even at entry and a second exit condition to specifically for the second contract (pound per point). C3 and C6 Should be the exit for the second contract I would like this to happen if the TP is hit not before
I have seen and read (Breakeven code for your automated trading strategy) but I do not know how to implement it
Hopefully I’ve explained it well enough 2 contracts one gets sold at a Take profit the second one is allowed to continue until the macd crosses or break even is hit
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = CALL HLAroon[70]
c1 = (indicator1 > 0)
indicator2 = CALL HLAroon[19]
c2 = (indicator2 CROSSES OVER 0)
IF c1 AND c2 THEN
BUY 2 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator3 = MACDline[6,19,9](close)
indicator4 = ExponentialAverage[9](MACDline[6,19,9](close))
c3 = (indicator3 CROSSES UNDER indicator4)
IF NOT ShortOnMarket AND c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = CALL HLAroon[70]
c4 = (indicator5 < 0)
indicator6 = CALL HLAroon[19]
c5 = (indicator6 CROSSES UNDER 0)
IF NOT LongOnMarket AND c4 AND c5 THEN
SELLSHORT 2 PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
indicator7 = MACDline[6,19,9](close)
indicator8 = ExponentialAverage[9](MACDline[6,19,9](close))
c6 = (indicator7 CROSSES OVER indicator8)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
SET STOP LOSS St
SET TARGET PROFIT Tp
I think I posted this in the wrong place
i have been trying with the Breakeven code but i find it hard to do what i want,
On the second position I would like it to exit with an indicator after Break even has been activated
// Conditions to enter long positions
indicator1 = AroonUp[19]
indicator2 = AroonDown[19]
c1 = (indicator1 CROSSES OVER indicator2)
if c1 then
BUY 2 LOT AT MARKET
SET STOP PLOSS Tp //first stoploss
endif
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND TP>=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 ---
// Conditions to exit 2nd Long positions
indicator3 = AroonUp[19]
indicator4 = AroonDown[19]
c2 = (indicator3 CROSSES UNDER indicator4)
IF c2 THEN
SELL 1 LOT AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
SET STOP LOSS St
SET TARGET PROFIT Tp
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
That one above is wrong sorry i’ve tried it this way it below but it’s still not right
defparam cumulateorders = false
startBreakeven = Tp //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)
// Conditions to enter long positions
indicator1 = AroonUp[19]
indicator2 = AroonDown[19]
c1 = (indicator1 CROSSES OVER indicator2)
// Conditions to exit 2nd Long positions
indicator3 = AroonUp[19]
indicator4 = AroonDown[19]
c2 = (indicator3 CROSSES UNDER indicator4)
if c1 then
BUY 2 LOT AT MARKET
SET STOP PLOSS St //first stoploss
endif
IF LongOnMarket AND c2 THEN
SELL 1 LOT AT MARKET
ENDIF
IF LongOnMarket AND Tp THEN
SELL 1 LOT AT MARKET
ENDIF
IF LongOnMarket AND St THEN
SELL 2 LOT AT MARKET
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 ---
// Stops and targets : Enter your protection stops and profit targets here
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
i think iv got
startBreakeven = Tp //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)
// Conditions to enter long positions
indicator1 = AroonUp[19]
indicator2 = AroonDown[19]
c1 = (indicator1 CROSSES OVER indicator2)
// Conditions to exit 2nd Long positions
indicator3 = AroonUp[19]
indicator4 = AroonDown[19]
c2 = (indicator3 CROSSES UNDER indicator4)
if c1 then
BUY 2 LOT AT MARKET
SET STOP PLOSS St //first stoploss
endif
IF LongOnMarket AND c2 THEN
SELL 1 LOT AT MARKET
ENDIF
IF LongOnMarket AND Tp THEN
SELL 1 LOT AT MARKET
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 1 LOT AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// Stops and targets : Enter your protection stops and profit targets here
St = ROUND((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
SET STOP LOSS St
Nope that’s not right either i’ll try again with just the break-even
I’ll try and explain what I’m doing better I want to buy or sell two lots or contracts or pounds per points,
- with a take profit condition for 1 lot (AverageTrueRange[14](CLOSE))*1.5)
- with a stop loss condition for 2 lots (AverageTrueRange[14](CLOSE))*2)
- with a break-even condition for 1 lot after take profit is hit
- With a exit condition to let the 2nd lot run for more profit
Below is all the code that I have for this and is not in working condition I’ve
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)
// Conditions to enter long positions 2 lots
indicator1 = AroonUp[19]
indicator2 = AroonDown[19]
c1 = (indicator1 CROSSES OVER indicator2)
// Conditions to exit 1st Long positions breakeven should be Activated at this point
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
// Conditions to exit 2nd Long positions should be the average from entry candel
indicator2 = MACDline[6,19,9](close)
indicator3 = ExponentialAverage[9](MACDline[6,19,9](close))
c2 = (indicator2 CROSSES UNDER indicator3)
// Conditions stop loss for 2 lots Long positions should be the average from entry candel
St = ROUND((AverageTrueRange[14](CLOSE))*2)
// Conditions for breakeven Long positions
if c1 then
BUY 2 LOT AT MARKET
SET STOP PLOSS st
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 1 LOT AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
just laid it out to make it simple
Giving it my best shot and I’m still falling short,
I don’t know if it’s the break-even
defparam cumulateorders = false
startBreakeven = Tp //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 10 //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 2 lots
indicator1 = AroonUp[19]
indicator2 = AroonDown[19]
c1 = (indicator1 CROSSES OVER indicator2)
if c1 then
BUY 2 LOT AT MARKET
SET STOP LOSS st
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 1 LOT AT breakevenLevel STOP
ENDIF
// Conditions to exit 1st Long positions breakeven should be Activated at this point
IF LongOnMarket AND Tp THEN
SELL 1 LOT AT MARKET
ENDIF
// Conditions to exit 2nd Long positions
indicator4 = MACDline[22,46,9](close)
indicator5 = ExponentialAverage[9](MACDline[6,19,9](close))
c2 = (indicator4 CROSSES UNDER indicator5)
IF LONGONMARKET AND c2 THEN
SELL AT MARKET
ENDIF
// --- end of BUY SIDE ---
// Stops and targets : Enter your protection stops and profit targets here
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
St = ROUND((AverageTrueRange[14](CLOSE))*2)
or all the take profit or the stop loss that’s causing the trade to end too soon
I’ve tried a more simpler system without the break-even i’m still having issues it turns out it’s the take profit that’s causing the issue I don’t know what to do from here
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = CALL HLAroon[19]
c1 = (indicator1 CROSSES OVER 0)
IF c1 THEN
BUY 2 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator2 = MACDline[22,46,9](close)
indicator3 = ExponentialAverage[9](MACDline[22,46,9](close))
c2 = (indicator2 CROSSES UNDER indicator3)
IF c2 THEN
SELL AT MARKET
ENDIF
IF Tp THEN
SELL 1 PERPOINT AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
St = ((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
SET STOP LOSS St
Yes, Take Profit is the issue, you forgot to add
SET TARGET PROFIT Tp
line 21 doesn’t make sense, you are making a logical comparison with TP which does not retain a logical value. Use the above line as line 29, instead.
but I only want it to sell half
Then change line line 21 to:
IF Close >= (TradePrice + Tp) THEN
but beware that your SL and TP changes every candle. I don’t know if that is what you want.
Is it at all possible to fix it to the candle that open the trade
Yes, you need to place their calculation within the conditions to enter a trade, along with the keyword NOT ONMARKET to make sure they are not changed while the trade is open:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = CALL HLAroon[19]
c1 = (indicator1 CROSSES OVER 0)
IF c1 AND Not OnMarket THEN
// Stops and targets : Enter your protection stops and profit targets here
St = ((AverageTrueRange[14](CLOSE))*2)
Tp = ROUND((AverageTrueRange[14](CLOSE))*1.5)
BUY 2 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator2 = MACDline[22,46,9](close)
indicator3 = ExponentialAverage[9](MACDline[22,46,9](close))
c2 = (indicator2 CROSSES UNDER indicator3)
IF c2 AND LongOnMarket THEN
SELL AT MARKET
ENDIF
IF Close >= (TradePrice + Tp) THEN
SELL 1 PERPOINT AT MARKET
ENDIF
SET STOP LOSS St