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

StopLoss generating orders above market

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

    StopLoss generating orders above market

    The following logic often generates errors by submitting orders above market for a long exit, in real-time and playback. LongStop = Math.Min(SMA1[1],Math.Min(Low[1],SMA2[1])));

    SetStopLoss("Long", CalculationMode.Price,LongStop , false);
    Any help would be greatly appreciated. Thanks.

    #2
    Hello zrobfrank,

    For a sell stop order (exiting a long position) the stop price must be less than the current bid price.
    Code:
    LongStop = Math.Min(GetCurrentBid() - TickSize, Math.Min(SMA1[1],Math.Min(Low[1],SMA2[1]))));
    SetStopLoss("Long", CalculationMode.Price,LongStop , false);
    In addition, Set methods cannot be unset and will continue to hold their values for new entry orders.

    This means it is important to reset Set methods using a number of Ticks, Percent, or Currency before placing a new entry (or using a unique signalName for each new entry). (Setting to a specific price may be an invalid price when the entry order fills, especially for entry limit and stop orders)

    The help guide notes:

    “Notes:
    As Set method orders are submitted upon receiving an entry execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set.“

    “Tips:
    You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the stop loss price while in an open position
    Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position”

    NinjaScript > Language Reference > Strategy > Order Methods > Managed Approach > SetStopLoss()

    Before calling a new entry call the Set methods first, one line above the entry method:
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // call SetStopLoss() with CalculationMode.Ticks one line above calling EnterLong() to reset
    // this ensures the set method is not holding a price value from a previous order which may be invalid when the new entry fills
    SetStopLoss(CalculationMode.Ticks, 20)
    EnterLong();
    }
    
    // after the entry fills, the stop loss can be moved to any desired valid price with CalculationMode.Price
    if (Position.MarketPosition == MarketPosition.Long && Close[0] > Position.AveragePrice + 10 * TickSize)
    {
    // after 10 ticks of profit, set the stop loss to the maximum valid price
    SetStopLoss(CalculationMode.Price, GetCurrentBid() - TickSize);
    }
    ​​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Chelsea B.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by pibrew, Today, 06:37 AM
      0 responses
      4 views
      0 likes
      Last Post pibrew
      by pibrew
       
      Started by rbeckmann05, Yesterday, 06:48 PM
      1 response
      14 views
      0 likes
      Last Post bltdavid  
      Started by llanqui, Today, 03:53 AM
      0 responses
      6 views
      0 likes
      Last Post llanqui
      by llanqui
       
      Started by burtoninlondon, Today, 12:38 AM
      0 responses
      11 views
      0 likes
      Last Post burtoninlondon  
      Started by AaronKoRn, Yesterday, 09:49 PM
      0 responses
      16 views
      0 likes
      Last Post AaronKoRn  
      Working...
      X