Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CalculationMode.Price giving errors

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

    CalculationMode.Price giving errors

    I am having a problem getting CalculationMode.Price to work with SetStopLoss. I attached the code and a screenshot of the errors, it seems to be ignoring the Lows[0][1] entry for the price, it will work if I type in an actual number. I'm not sure what I have wrong. This is the portion of the code that's erroring out.... SetStopLoss(@"Long Entry",CalculationMode.Price, Lows[0][1], false); Thanks
    Attached Files

    #2
    Hello mlarocco,

    Thank you for your inquiry.

    ""Sell stop or sell stop limit orders can't be placed below the market."
    "Strategy submitted an order that generated the following error: "Order rejected" Strategy has sent cancel request, attempted to close the position and terminated itself."

    This error may appear when submitting a stop order with SetStopLoss(), ExitLongStopMarket() / ExitShortStopMarket(), ExitLongStopLimit() / ExitShortStopLimit(), and SubmitOrderUnmanaged(), if the stop price is ahead of the market.

    For a sell stop order (such as when exiting a long position), the stop price must be below the bid.

    There are different approaches to avoiding the invalid order price.

    For the examples below, consider a variable holding your calculated stop price. This could be a price from a custom calculation, or one obtained from an indicator. In these examples 100 will be used as a place holder.
    Code:
    stopPrice = 100; // ← price you have calculated to use as the stop price
    Using Math.Max() / Math.Min() to ensure the calculated stopPrice is above/below the ask/bid:
    Code:
    // ensure sell stop order is below the bid
    stopPrice = Math.Min(stopPrice, GetCurrentBid() - 1 * TickSize);
    ExitLongStopMarket(stopPrice);
    
    // ensure buy stop order is above the ask
    stopPrice = Math.Max(stopPrice, GetCurrentAsk() + 1 * TickSize);
    ExitShortStopMarket(stopPrice);
    Using a condition to submit a market order to exit immediately, instead of a stop order, if the stopPrice is ahead of the market:
    Code:
    // if calculated exit sell stopPrice is less than the bid use stopPrice for stop
    // otherwise exit long position immediately with a market order
    if (Position.MarketPosition == MarketPosition.Long)
    {
        // if the stopPrice value is a valid price, use the stopPrice
        if (stopPrice < GetCurrentBid())
        {
            SetStopLoss(CalculationMode.Price, stopPrice);
        }
        // otherwise exit immediately with a market order
        else
        {
            ExitLong();
        }
    }
    
    // if calculated exit buy stopPrice is greater than the ask use stopPrice
    // other wise exit short position immediately with a market order
    if (Position.MarketPosition == MarketPosition.Short)
    {
        if (stopPrice > GetCurrentAsk())
        {
            SetStopLoss(CalculationMode.Price, stopPrice);
        }
        else
        {
            ExitShort();
        }
    }
    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)

    Before calling a new entry call the Set methods first, one line above the entry method.
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
        // reset the stop loss before entry, so this is no longer set to a specific price which may be invalid when the entry fills
        SetStopLoss(CalculationMode.Ticks, 20)
        EnterLong();
    }
    
    // after the entry fills, the stop loss can be moved to any desired valid price
    if (Position.MarketPosition == Market Position.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);
    }
    The help guide notes:

    “Notes:

    Since they are submitted upon receiving an 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()

    Please let us know if you have any other questions.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    29 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X