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

Issue with Stop Loss

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

    Issue with Stop Loss

    Hey guys first of all reading the topics I'm grateful for your constant support and quick responses....
    I am fairly new to NinjaScript so I apologize if my question might sound naive

    I am building a strategy and during the execution part I have an issue with the stop loss

    inside protected override void OnBarUpdate()
    I am creating some calculations and once met I am using this block for execution:

    EnterLongLimit(1, entryPrice, "BullishEntry");
    SetStopLoss("BullishEntry", CalculationMode.Price, stopPrice, false);
    SetProfitTarget("BullishEntry", CalculationMode.Price, targetPrice);

    the issue that I encountered is that with this method sometimes when price reaches the stop loss and it tries to place the order, price already went below and I get an error "Orders cannot be placed above the market". One thing I noticed is that when manually placing an ATM strategy the stop loss is a solid red color (stop loss), however when using this strategy the stop loss is a pale pink color (Stop Market Order)....

    How can I change the StopLoss so that it places a stop loss at a defined price right away upon entry rather than placing a stop market order that gets triggered once price reaches my defined price?

    #2
    Hello alifarahani,

    as a side note the set methods should always come first to ensure the most recent calculated price is used.

    SetStopLoss("BullishEntry", CalculationMode.Price, stopPrice, false);
    SetProfitTarget("BullishEntry", CalculationMode.Price, targetPrice);
    EnterLongLimit(1, entryPrice, "BullishEntry");

    Using the set methods like this should allow you to place targets once the entry has filled. The orders wont be sent when the entry is submitted and working but once the entry fills the targets are submitted. Each time you call the Set methods it will update the prices. Is your entry condition becoming true for multiple bars in a row before it fills? if so the prices are likely being reset while the entry is working and once it fills the price has shifted from what it was for the original submission of the entry.

    To make a fixed offset you can use one of the other calculation modes like Ticks
    JesseNinjaTrader Customer Service

    Comment


      #3
      thanks for your reply, is there a way to change the stoploss so that it places a limit order, instead of a market order? the same way that it places stops on the ATM strategy?

      Comment


        #4
        Hello alifarahani,

        The Set methods specifically use stop market orders. To submit a stop limit order or other type of order you would need to either create a separate condition to wait for a long/short position or use OnExecutionUpdate to submit the targets when the entry has filled.

        You can see an example of submitting targets based on an entry fill here: https://ninjatrader.com/support/help...ub=onexecution
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you Jesse, can you help me understand the difference (see attached) between a Stop Loss and a Stop Market Order?
          I know in the ATM strategy it places a Stop Loss, however in my code it places a Stop market order...
          What I'd like to achieve is a Stop Loss instead of a Stop Market Order
          Attached Files

          Comment


            #6
            Hello alifarahani,

            Those are for when an ATM is used, the orders named named stoploss and profittarget use those colors. ATM's also use stop market orders for their stoploss's unless a stop limit is selected instead.

            You can use the control center orders tab to compare the types of orders different actions or tools use in the platform if you are trying to recreate something in your own scripts using the order methods.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks a lot Jesse, please see attached . . . the issue that we're trying to solve is not getting this type of errors, which happen quite frequently and the system disabling the strategy.... Do you have any recommendation on how to solve this type of behavior?
              I appreciate you being patient with me answering these weird requests
              Attached Files

              Comment


                #8
                Hello alifarahani,

                In that series of errors the OCO error can be ignored, that happens when a stoploss is rejected but a target with the same OCO is submitted.

                The actual error that needs addressed is the stop being placed above the market. That will cause a rejection which will terminate the strategy. That can be caused depending on the strategies settings, bars type being used and price that you calculated while using CalculationMode.Price. At the time the order was submitted the price that was calculated was no longer valid. You would need to look at the price you are calculating and make sure that is below the current market price when the order is submitted. You may need to add additional offset to the stop to ensure it is placed below market.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks a lot Jesse, seems like I was able to fix it using:

                  // Ensure the stop price is below the current market price
                  if (stopPrice >= roundedCurrentPrice)
                  {
                  stopPrice = roundedCurrentPrice - tickSize; // Adjust the stop price to be slightly below the current market price
                  }

                  one other issue that I'm facing is, if I enter a trade and it hits the target or the stop loss on the same candle, it places again the same order, I tried using a bool:
                  private bool orderPlacedThisCandle = false;

                  and then set it to true once it the entry is submitted but no luck... any ideas?​

                  Comment


                    #10
                    Hello alifarahani,

                    You can use bars since entry to help control that:

                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by naanku, Today, 07:25 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post naanku
                    by naanku
                     
                    Started by milfocs, Today, 07:23 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post milfocs
                    by milfocs
                     
                    Started by PaulMohn, Today, 06:59 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post PaulMohn  
                    Started by bortz, 11-06-2023, 08:04 AM
                    48 responses
                    1,754 views
                    0 likes
                    Last Post carnitron  
                    Started by Jonker, 04-27-2024, 01:19 PM
                    3 responses
                    24 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X