help with break-even code as a second position and second exit

Viewing 15 posts - 1 through 15 (of 39 total)
  • Author
    Posts
  • #151247 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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
    #151943 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    I think I posted this in the wrong place

    #151945 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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

    #151946 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average
    // 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
    #151949 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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)
    #151951 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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
    #151962 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    Nope that’s not right either i’ll try again with just the break-even

    #151966 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    I’ll try and explain what I’m doing better I want to buy or sell two lots or contracts or pounds per points,

    1. with a take profit condition for 1 lot      (AverageTrueRange[14](CLOSE))*1.5)
    2. with a stop loss condition  for 2 lots      (AverageTrueRange[14](CLOSE))*2)
    3. with a break-even condition  for 1 lot     after take profit is hit
    4. 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

    #151970 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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

    224.png 224.png
    #151973 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    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
    
    #151974 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    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.

    #151980 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    but I only want it to sell half

    #151986 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    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.

    #152011 quote
    Patrick K TemplarPatrick K Templar
    Participant
    Average

    Is it at all possible to fix it to the candle that open the trade

    #152024 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    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
    Kovit thanked this post
Viewing 15 posts - 1 through 15 (of 39 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

help with break-even code as a second position and second exit


ProOrder: Automated Strategies & Backtesting

New Reply
Summary

This topic contains 38 replies,
has 3 voices, and was last updated by robertogozzirobertogozzi
5 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/22/2020
Status: Active
Attachments: 11 files
ProRealCode ProRealCode
Loading...