Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Move stop to break even and beyond

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Move stop to break even and beyond

    Hi

    I'm trying to roll my initial stop to a small profit, then when price moves further in my favour, roll it again to secure a higher profit.

    Somehow it all seems to work fine on the first trade of the period I backtest, then the rolling of the stop does not seem to work. It's like I'm not "resetting" it correctly after I've exited the trade. I think I'm doing this "reset" under the marketposition= flat section of the code below, but I must not have it right

    Any help would be great!

    // Return Stop to flat & Set ATR based Stop & ATR based Profit
    if (Position.MarketPosition == MarketPosition.Flat)

    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    int stopShort= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    int Profit= Convert.ToInt32(Math.Round((ATR(14)[0]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Ticks,(s1*stopLong), false);
    SetStopLoss("GoShort", CalculationMode.Ticks,(s1*stopShort), false);
    SetProfitTarget("GoShort", CalculationMode.Ticks, (p1*Profit));
    SetProfitTarget("GoLong", CalculationMode.Ticks, (p1*Profit));
    rolA= true;
    rolB= false;
    rolC= false;
    rolD= false;
    rolE= false;


    // OPEN: Enter a long position

    {
    EnterLong(DefaultQuantity, "GoLong");
    }


    // ROLL 1: Secure Profit (Long)
    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] >= Position.AvgPrice +s1*0.4*(Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize))))*TickSize
    && rolA)
    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Price,Position.AvgPrice+0.25*stopL ong*TickSize, false);
    rolA=false;
    rolB=true;
    rolC=false;
    rolD=false;
    rolE=false;
    DrawTriangleUp("tag" + CurrentBar, true, 0, Low[0]-2*TickSize, Color.Red);

    }
    // ROLL 2: Reduce stop loss (Long)
    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] >= Position.AvgPrice +(s1*x1*1.75*(Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)))))*TickSize
    &&rolB)

    {
    int stopLong= Convert.ToInt32(Math.Round((ATR(14)[BarsSinceEntry("GoLong")]/TickSize)));
    SetStopLoss("GoLong", CalculationMode.Price,Position.AvgPrice-((s1*(1-(1.75*x1))*stopLong))*TickSize, false);
    rolA=false;
    rolB=false;
    rolC=true;
    rolD=false;
    DrawTriangleUp("tag2" + CurrentBar, true, 0, Low[0]-2*TickSize, Color.Orange);
    }

    #2
    Hello lancasterstephen,

    If you test in market replay, or with the simulated data feed, do you experience the same behavior?

    Have you added print statements to your script to check the values and that if your conditions are becoming true? I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CortexZenUSA, Today, 12:53 AM
    0 responses
    1 view
    0 likes
    Last Post CortexZenUSA  
    Started by CortexZenUSA, Today, 12:46 AM
    0 responses
    1 view
    0 likes
    Last Post CortexZenUSA  
    Started by usazencortex, Today, 12:43 AM
    0 responses
    5 views
    0 likes
    Last Post usazencortex  
    Started by sidlercom80, 10-28-2023, 08:49 AM
    168 responses
    2,266 views
    0 likes
    Last Post sidlercom80  
    Started by Barry Milan, Yesterday, 10:35 PM
    3 responses
    13 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X