• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error Message in Strategy

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

    Error Message in Strategy

    When I run the Strategy it places a buy stop above the market and sell stop below the market correctly. But as soon as the stop is hit I get an error message and the Strategy terminates itself. It is placing the stop and taking it correctly too it seems.
    Thank you.

    This behavior I was observing happens when say we are in a long with a sell stop below. I get the error message when the strategy wants to reverse and go short and there is a sell stop order down below it. I guess it confuses it? I only have a stop and go long and short logic coded. So either I stop out or the strategy reverses on an opposite signal. If the stop is filled before getting an opposite signal it generates no error messages and works correctly. Any way to work around this?
    Thank you.
    Attached Files
    Last edited by Trader17; 10-25-2018, 07:37 PM.

    #2
    Hello Trader17,

    Thanks for the post, If this is related to the thread that we had here:

    https://ninjatrader.com/support/foru...top-loss/page2

    I noticed that we are not resetting the stop loss value, the attached script works well for me. I changed the OnBarUpdate method to check for the position and reset the SetStopLoss value before using it again.

    Please let me know if I can assist further.
    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I am still getting an error message when the strategy tries to reverse from long to short and vice versa and an existing stop is still in place. This is the code I have in the Strategy.

      //protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
      {

      if (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long)
      {
      //LongPosition = true; Use LongPosition elsewhere if needed
      SetStopLoss(CalculationMode.Price, MIN(Low,3)[0]-(TickSize*3));
      }
      else if (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short)
      {
      //ShortPosition = true; Use ShortPosition elsewhere if needed
      SetStopLoss(CalculationMode.Price, MAX(High,3)[0]+(TickSize*3));
      }


      Thank you.

      Also looks like the stop is moving like a trail stop. I am watching it move with the MIN or MAX I guess. And I am not using SetTrailStop either in the code.

      I think the problem happens when a stop is still in place and the strategy gets a reversal signal. And at that point it confuses the old stop with the new one that would be placed. And then does not place the new order and closes out and terminates itself.

      Thanks.
      Attached Files
      Last edited by Trader17; 10-26-2018, 12:59 PM.

      Comment


        #4
        Hello Trader17,

        Thanks for the reply.

        The strategy is throwing this error because you are not resetting the price of the stop loss before entering in the other direction. In the code sample I posted earlier, I found it better to do the logic in OnBarUpdate.because you would need to do additional checks in OnPositionUpdate.


        Code:
        protected override void OnBarUpdate()
                {
                    if (CurrentBar < BarsRequiredToTrade)
                        return;
        
                    if (CrossAbove(smaFast, smaSlow, 1))
                    {
                        if (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short)
                        {
                            SetStopLoss(CalculationMode.Ticks, 10);
                        }
                        EnterLong();
                        SetStopLoss(CalculationMode.Price, MIN(Low, 3)[0]-TickSize*2);
                    }
                    else if (CrossBelow(smaFast, smaSlow, 1))
                    {
                        if (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long)
                        {
                            SetStopLoss(CalculationMode.Ticks, 10);
                        }
                        EnterShort();
                        SetStopLoss(CalculationMode.Price, MAX(High, 3)[0]+TickSize*2);
                    }
                }
        Please let me know if you have any questions.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          So basically the above code will check if there is an existing stop order and cancel it before reversing the position? That is what I am trying to achieve since I am not using a profit target. So when there is a stop order out there and a reversal order at the same time does either take precedence? Or will the strategy make sure all open stop orders are cancelled first and then reverse? Why do I see 2 SetStopLoss in the code above? I am only using MIN or MAX.

          And if we use profit targets and stop losses then until one is hit Ninja trader will not take a reversal signal, correct?

          Is this the right way to achieve what I am trying to? Will this cancel the open stop loss order and then enter in the opposite direction and then place the new stop?

          Thank you.

          Last edited by Trader17; 10-27-2018, 11:41 AM.

          Comment


            #6
            Hello Trader17,

            Thanks for the reply.

            You are seeing two SetStopLosses because if you don't reset the price of SetStopLoss it will use the price from the previous use of SetStopLoss, so if you try to go long and enter a stop with the price that was used for the previous short order, then it will throw the error because you are trying to set a stop on an illegal side of the market. You can test this out by running the
            MyTestStrategy.cs strategy. To add the strategy in NinjaTrader, place the file within ..\Documents\NinjaTrader 8\bin\Custom\Strategies and compiling.

            Please let me know if I can assist further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thank you. So the SetStopLoss within the { } of 10 ticks will not matter then? Why is that in there? And it will still go ahead and place my regular MIN or MAX stop also?
              Thanks.

              Comment


                #8
                Hello Trader17,

                Thanks for the reply.

                That is there to reset the price of the SetStopLoss. Using Tick swill make the stop general for short and long orders, so you will not get an error while calling EnterLong/EnterShort. You can use the SetStopLoss with Min and Max after that with no issue.

                Please let me know if I can assist further.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  So instead of cancelling out the old stop it is moving it to where the new position would place a stop? And then on the next tick it will adjust the same stop to the MIN or MAX value? Correct? Will a Profit Target order be affected the same way too then? Like it also needs to be cancelled?

                  Is there a way in Ninja Script to be able to cancel out any stops associated with an open position about to be reversed directly?

                  And I got an error while compiling after inserting the code you provided above.

                  Thank you.
                  Attached Files
                  Last edited by Trader17; 10-29-2018, 12:59 PM.

                  Comment


                    #10
                    Hello Trader17,

                    Thanks for the reply.

                    The OnBarUpdate snippet that I posted relies heavily on the setup that occurs in the OnStateChange method. I would recommend just using the script I posted as a standalone example, then apply the same concept to your OnBarUpdate method.

                    There is not a way to cancel the stop in the managed approach. SetStopLoss does not return a value so you can't tie it to an Order object, it will place a stop for any order that is sumbitted if you don't specify a signal name. The stop resetting technique must be used to flp-flop between long and short positions.

                    Please let me know if you have any questions.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      So basically when the strategy reverses from long to short and vice versa on a signal and only a stop or target is used then use the method your snippet shows. Otherwise if a Stop and Target are used together in a strategy then it will not reverse until one of them is hit, correct? Will the fixed stop and MIN/MAX stop not conflict with each other on each bar update? Or the fixed stop will become the MIN/MAX stop on bar close?

                      And when I use the MIN or MAX what if I want it to be placed only once and not update with each close? I have seen it adjust with the close of each bar.

                      Thank you.
                      Last edited by Trader17; 10-29-2018, 01:26 PM.

                      Comment


                        #12
                        Hello Trader17,

                        Thanks for the reply.

                        The if you are going to use a calculation mode of price, then you need to perform the price reset for the stop and target if you are going from long to short or vice versa.

                        Here is why the strategy will fail if you do not reset the value (this is based off my example):

                        1. Short condition hit
                        2. Enter short.
                        3. Place stop loss above the market (This stop loss will be used for all orders from here on)
                        4. Long condition hit before stop loss was hit.
                        5. Enter Long (remember, we are not resetting the stop loss price)
                        6. Strategy tries to use SetStopLoss from step 3, causing an error.

                        If you call SetStopLoss before entering long here, it will place a legal stop loss when EnterLong is hit since the Tick calculation mode places the stop below the market for long orders and above the market for short orders. Have you tested the strategy that I provided?

                        I look forward to hearing of your results.

                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you very much. It is working and cancelling the old stop. Interesting as it does not try to place the stop twice as in the code. Is it possible to keep the MIN/MAX stop in place once placed and not move with the market. It moves itself at times and sometimes it stays in the original place.
                          Thank you.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by superkolleech, Today, 12:46 AM
                          1 response
                          11 views
                          0 likes
                          Last Post MojoJojo  
                          Started by davidwade, Today, 02:51 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post davidwade  
                          Started by aviat72, 04-26-2020, 09:41 PM
                          10 responses
                          92 views
                          0 likes
                          Last Post kissandfly  
                          Started by healthooa, Today, 01:28 AM
                          0 responses
                          8 views
                          0 likes
                          Last Post healthooa  
                          Started by harjinderjit, Yesterday, 08:24 PM
                          0 responses
                          20 views
                          0 likes
                          Last Post harjinderjit  
                          Working...
                          X