Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best practice stop loss

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

    Best practice stop loss

    Hi Support, I am planning in using the following script in OnBarUpdate() to manage my position (trailstop or exit at market). I would like to ask you:
    1.if you recon the script is correctly coded or if the else statement highlighted in bold should be removed (and if yes, why exactly?);
    2. any suggestion on how to improve my script is welcome

    Code:
    if(Position.MarketPosition == MarketPosition.Long && stopOrderLong != null)
    {  
    if(BarsSinceEntry() > exitPeriod && Position.GetProfitLoss(Close[0], PerformanceUnit.Points) < stopLoss) 
    {
    	x = Close[0];
    }
    	[B]else x = stopOrderLong.StopPrice - TickSize;[/B]
    
    for(int i=1; i<10; i++)
    {
    	if(Close[0] > (Position.AvgPrice + i*stopLoss) && Close[0] < (Position.AvgPrice + (1+i)*stopLoss))
    {
    	y = Position.AvgPrice + i*stopLoss; 
    }	
    	[B]else y = stopOrderLong.StopPrice - TickSize;[/B]
    }
    
    double newStop = Math.Max(x,y); 	// calculate new stop loss
    if(newStop > stopOrderLong.StopPrice)                       // check if new stop loss is higher than current stop loss
    {
    if(newStop < Close[0])
    {
    	stopOrderLong = ExitLongStop(1,true,stopOrderLong.Quantity, newStop,"Stop","Long");		
    }
    else if(newStop >= Close[0])
    {
               ExitLong(1,stopOrderLong.Quantity,"Exit","Long");
    }
    }

    #2
    Hi Yades,

    Thank you for posting.

    I don't see any immediate errors, however have you tested out this code to see if works they way you want it to?

    What do you expect this strategy to do?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Cal
      once I am running a long position, what I expect this script to do is:
      1. exit after a specific number of bars if my profit target is not met (this apparently is working fine)
      2. trail my initial stop loss each time a bar close is above a multiple of my original stop loss;
      When I run the script I suspect the trailing stop part is not working properly, but I am not sure what exactly is causing the issue

      Originally posted by NinjaTrader_Cal View Post
      Hi Yades,

      Thank you for posting.

      I don't see any immediate errors, however have you tested out this code to see if works they way you want it to?

      What do you expect this strategy to do?

      Comment


        #4
        Yades,

        How are you setting the TrailStop and Profit targets in the code?

        Can you share a sample of them?
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          I don't set a target, i simply trail my stop as described and the initial stop is set onExecution with the same name and string as the one you see below:

          stopOrderLong = ExitLongStop(1,true,1, stop,"Stop","Long");

          Originally posted by NinjaTrader_Cal View Post
          Yades,

          How are you setting the TrailStop and Profit targets in the code?

          Can you share a sample of them?

          Comment


            #6
            Yades,

            I would suggest trying to print out the values of your trailing stop that you have adjusting to make sure they are the values you want to take place.

            These will print out to the output window which is accessible by Tools > Output Window..

            Here is a link on the Print() function with NinjaTrader.
            http://www.ninjatrader.com/support/h...html?print.htm
            Cal H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            671 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            379 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            111 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            582 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X