Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What happens after the Stop Loss is activated

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

    What happens after the Stop Loss is activated

    Hi,

    On execution of the Stop Loss. I should have no market position and then should promptly go and recreate one.

    The thing is that the new order appears to be ignored even though it is readily apparent in the chart



    protected override void OnExecution(IExecution execution)

    if
    (longOrder != null && longOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

    {
    stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice +2 * TickSize, tickPrice +2 * TickSize, "Oil", "Short limit entry");

    }


    else if (shortOrder != null && shortOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)
    {
    stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, tickPrice -2 * TickSize, tickPrice -2 * TickSize, "Oil", "Stop loss short");
    }


    // (if stopOrder is executed then the long / short position must have been closed

    else if
    ( Position.MarketPosition == MarketPosition.Flat)

    {
    shortOrder = longOrder = null;
    }

    #2
    Hi cocopod,

    I am somewhat confused by the code you have posted. Is this code involved with the issue you are describing?

    You mention that after a stop is detected a new order is submitted and this order is being ignored.

    However, there is no code detecting a stop loss has filled.

    There is code detecting when the position is flat, but there is no code to submit an order in this.

    There is also code to set the stop loss when a long or short entry fill is detected.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      Thanks for your considered response.

      >>However, there is no code detecting a stop loss has filled.<<

      This is actually inferred when

      Position.MarketPosition == MarketPosition.Flat

      Allowing me to recreate the order in code below

      protected override void OnMarketData(MarketDataEventArgs e)
      {
      if ( longOrder == null || shortOrder == null)
      {
      longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +5 * TickSize, GetCurrentBid() +23 * TickSize, "Oil", "long limit entry");

      shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -5 * TickSize, GetCurrentBid() -23 * TickSize, "Oil", "Short limit entry");

      Comment


        #4
        Hello cocopod,

        Ah, this makes more sense.

        So in OnExecution, you check that the position has updated though OnExecution and OnPositionUpdate are run asynchronously and the position may not be updated by that time.

        I would recommend you add the check for flat and reset to null to OnPositionUpdate so that the position is for sure updated before this is checked.

        Then, double check that the code in OnMarketData is evaluating as true by adding a print to the action block of that branching command.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          >>So in OnExecution, you check that the position has updated though OnExecution and OnPositionUpdate are run asynchronously and the position may not be updated by that time.<<

          I suppose so although my understanding is that OnExecution is effectively an event and as soon as the order is filled, the computer drops everything its doing and goes and runs the code contained therein.



          >>I would recommend you add the check for flat and reset to null to OnPositionUpdate so that the position is for sure updated before this is checked.<<

          I am not using OnPositionUpdate although I had initially added the check
          //(Position.MarketPosition == MarketPosition.Flat)
          to the OnMarketData section in place of
          ( longOrder == null || shortOrder == null) where the initial Bracket order is created.

          This proved a disaster with the system creating multiple Bracket orders as the Position was always Flat unless we hit the specific market price whereupon the "Pending" Order was filled. Look at the snippet of code below that shows how I tried to use it

          *********** Start of Code **********

          protected override void OnMarketData(MarketDataEventArgs e)
          {
          if
          ( longOrder == null || shortOrder == null)
          //(Position.MarketPosition == MarketPosition.Flat)
          {

          longOrder = SubmitOrder(.....);
          shortOrder = SubmitOrder(....);

          Comment


            #6
            Hi cocopod,

            Once OnExecution is called it runs the code in OnExecution.

            However, the Position.MarketPosition object you are calling does not get changed in OnExecution, it gets changed in OnPositionUpdate.

            So yes, when the order fills OnExecution is triggered and the code in OnExecution is run.

            You are not manually updating Position.MarketPosition, you are checking the value of this object. Position.MarketPosition cannot be updated by your code it is a read-only value. This means NinjaTrader is changing this object's value.

            I am suggesting that you do use OnPositionUpdate, I am not asking if you are already using it.


            When you mention "this proved a disaster with the system creating multiple Bracket orders as the Position was always Flat unless we hit the specific market price whereupon the "Pending" Order was filled. Look at the snippet of code below that shows how I tried to use it" I am not sure what you are trying to convey. I am seeing the code but I do not understand what issue you are having.
            Can you clarify this for me?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I did not initially understand the use of OnPositionUpdate and my referral to the code where I used
              (Position.MarketPosition == MarketPosition.Long)
              was my attempt to demonstrate that I had indeed attempted to use it - however wrongly

              I will update my code and get back to you.

              On a slight tangent I see that I am using stopOrder.LimitPrice. In the Output window this actually has a value. where did the value come from? I certainly didn't set it.

              On a further tangent, I only started to program in NinjaScript 4 days ago so its all rather new to me

              C

              Comment


                #8
                Hi cocopod,

                This is coming from the order from the first time you set it.

                In otherwords you are setting the limit price to the price the limit is already set to.

                So I would expect the fill to be at the same price as the fill will be at the limit price.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  ok... This OnPositionUpdate??

                  I have an Order and a corresponding Stop Loss. I wish to adjust the Stop loss however because it originated from a Bracket Trade I don't know if I'm Long or Short.

                  I therefore need OnPositionUpdate to give let me know...something along the lines of
                  if (Position.MarketPosition == MarketPosition.Long)...

                  the thing though is that I wiah to update the Stop Loss every Tick so need OnBarUpdate(). so what do I do?

                  Should I update a variable? something along the lines of
                  protected override void OnPositionUpdate()
                  if (Position.MarketPosition == MarketPosition.Long)
                  { WeAreLong = 1
                  else WeAreShort = 1}

                  Then use this variable in the OnBarUpdate? Please advise

                  This does explain why the ChangeOrder never updated

                  C

                  Comment


                    #10
                    Hello cocopod,

                    If you want to adjust the stop loss (evaluating on every tick) then add the code to OnMarketData.

                    This is ok because the entry order will have already filled by this time, so the Position.MarketPosition will have been updated to reflect the long position.

                    (OnPositionUpdate will also have already run by this point as well)

                    Then check that the position is long in OnMarketData and adjust the stop according to your logic.



                    It seems like you have multiple inquiries and you are trying to mix different things you are trying to achieve into this one inquiry.

                    If you are trying to detect a flat position after an order exits, do this in OnPositionUpdate to ensure that the position has updated.

                    If you are trying to move a stop on a tick by tick evaluation, do this in OnMarketData.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      The thing is that I'm trying to cram in a lot of learning into a short space of time.

                      Now in
                      protected override void OnMarketData(MarketDataEventArgs e)

                      are you saying it should be able to recognise the term

                      if (Position.MarketPosition == MarketPosition.Long)?

                      Because if so, I really have no need for OnBarUpdate

                      Comment


                        #12
                        Hello cocopod,

                        Yes, you can check the Position.MarketPosition in any method including OnMarketData.

                        However, you need to consider that these methods run asynchronously. So you need to pick the correct method to place your code in depending on what you are trying to accomplish.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          I must be close

                          Something is not quite right.

                          The Order is filled and the Stop Loss is pending ( I think it is updating its position ). The thing however is that the New Pending Bracket trade is no longer called. Look at the "ELSE" statement at the end of the code contained within OnExecution in Code Sample 1. This should reset the longOrder and shortOrder to null.

                          The corresponding code to Create the new Bracket order is shown in Code Sample 2. But it doesn't seem to do anything


                          **************** Code Sample 1 ***************

                          protected override void OnExecution(IExecution execution)
                          {

                          if
                          (longOrder != null && longOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

                          {
                          stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice +2 * TickSize, tickPrice +2 * TickSize, "Oil", "Short limit entry");
                          }


                          else if (shortOrder != null && shortOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

                          {
                          stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, tickPrice -2 * TickSize, tickPrice -2 * TickSize, "Oil", "Stop loss short");
                          }

                          else
                          {
                          longOrder = shortOrder = null; // We should create a new Bracket
                          }

                          **************** End of Code Sample 1 ***************


                          **************** Start of Code Sample 2 ***************



                          protected override void OnMarketData(MarketDataEventArgs e)
                          {
                          if
                          ( longOrder == null || shortOrder == null)
                          {
                          // Create the Bracket on Initialization
                          // Automatically recreates a Bracket when the previous one has run its course!!!

                          longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +2 * TickSize, GetCurrentBid() +2 * TickSize, "Oil", "long limit entry");
                          shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -2 * TickSize, GetCurrentBid() -2 * TickSize, "Oil", "Short limit entry");
                          timer.Elapsed += OnTimedEvent;

                          **************** End of Code Sample 2 ***************

                          Comment


                            #14
                            Hi cocopod,

                            Add a print where these variables are set to null that has the time in it so you know that the variables are being set to null when you expect.

                            Then add a print in OnMarketData outside of other conditions that checks to see if both of these variables are null.

                            For example:
                            if (longOrder == null)
                            {
                            Print("OnMarketData - longOrder is null");
                            }
                            else
                            {
                            Print("OnMarketData - longOrder is not null");
                            }

                            If it is null, the new entry orders should be triggered.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            633 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            364 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            105 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            567 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            568 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X