Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Controlling stoploss in a multiinstrument strategy

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

    Controlling stoploss in a multiinstrument strategy

    Hello,
    I am trying to control the adjusting of my stoploss based on the price from the main instrument. ES -> MES.
    MES is on BarsinProgress 3. Hence the below gets me into the MES based on the action of the ES.
    EnterLong(3,TradeSize, "MIMIC");

    Can you tell me how to keep adjusting the stoploss for the instrument on Bars array 3 in a trailing stop manner.
    Thanks. My regular setstoplossfor MIMIC doesn't seem to control the instrument in Barsarray 3 when called under bars in progress ==0.

    #2
    Hello richa61416,

    Thank you for your post.

    The sample strategy attached is a simple example of how to set trailing stops for multiple instruments.

    You could modify this script to set your stop to whatever you like programmatically based on your needs.

    Please note that adjusting the stop price an cause the calculated level to be placed on the other side of the market depending on the movement of the market.

    As this can be a real world possibility, I may suggest to add code to trap rejections and failures to change orders so you can take a different action when the order is error is seen. This involves using Order objects, OnOrderUpdate, and RealtimeErrorHandling. I have included documentation items below and an example strategy that demonstrates how Order objects can be used.

    RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

    OnOrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Thanks in advance; I look forward to assisting you further.
    Attached Files

    Comment


      #3
      Sorry I mean to place a trailing stop using the setstoploss command. E.g. below is my code snippet that I need to affect the second instrument. I am not using a fixed trailstop but a variation based on how much profit has been attained.

      if(SDTrail== false && SMATrail== false && High[1] -Position.AveragePrice >20 * TickSize)
      {
      Print("SMA Set");
      SMATrail=true;
      }
      if (DayType =="RANGE" )
      {

      RangeLong=Math.Min(RangeLong,amaCurrentDayVWAP1.Up perBand1[1] - TickSize);
      if (RangeLong> Position.AveragePrice)
      SetProfitTarget("my1Trail", CalculationMode.Price,RangeLong);

      }
      if( SDTrail== false && High[1] -Position.AveragePrice > 24 * TickSize)
      {
      SDTrail=true;
      SMATrail = false;
      }
      if (SMATrail && SMA(20)[1] - 2 * TickSize >Position.AveragePrice && Close[1] > SMA(20)[1] && Close[2] > SMA(20)[2])
      {
      Print("Set");
      SetStopLoss("my1Trail",CalculationMode.Price, SMA(20)[1] - 2 * TickSize,false);
      }
      if ((SDTrail || High[1] -Position.AveragePrice >12 * TickSize) && Close[1] > KAMA(2,10,30)[1] && Close[2] > KAMA(2,10,30)[2])
      {
      Print("SetSD");
      SetStopLoss("my1Trail",CalculationMode.Price, KAMA(2,10,30)[1]- 2 * TickSize,false);
      }

      Comment


        #4
        Ok. I figured it out. I need to use (Positions[3].MarketPosition == MarketPosition.Short) to determine if I am long or short.
        I was using Position.MarketPosition which was not entering the script since the BarsInProgress 0 would have a flat position.

        Thanks. I looked back at what you sent an realised nothing special was needed to reference the stoploss so I looked elsewhere. Thanks very much.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        52 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        43 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        47 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X