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 algospoke, Today, 06:53 PM
      0 responses
      2 views
      0 likes
      Last Post algospoke  
      Started by mlprice12, 12-21-2021, 04:55 PM
      3 responses
      291 views
      0 likes
      Last Post paypachaysa  
      Started by lorem, 04-25-2024, 09:18 AM
      20 responses
      85 views
      0 likes
      Last Post lorem
      by lorem
       
      Started by xepher101, 05-10-2024, 12:19 PM
      4 responses
      52 views
      0 likes
      Last Post xepher101  
      Started by DawnTreader, 05-08-2024, 05:58 PM
      22 responses
      82 views
      0 likes
      Last Post DawnTreader  
      Working...
      X