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

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
    Kate W.NinjaTrader Customer Service

    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 judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Started by ScottWalsh, Today, 06:52 PM
        4 responses
        36 views
        0 likes
        Last Post ScottWalsh  
        Started by olisav57, Today, 07:39 PM
        0 responses
        7 views
        0 likes
        Last Post olisav57  
        Started by trilliantrader, Today, 03:01 PM
        2 responses
        21 views
        0 likes
        Last Post helpwanted  
        Started by cre8able, Today, 07:24 PM
        0 responses
        10 views
        0 likes
        Last Post cre8able  
        Working...
        X