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

Strategy has sent cancel requests, attempted to close the position and terminated its

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

    Strategy has sent cancel requests, attempted to close the position and terminated its

    Dear Ninjatrader-Team,

    I coded a strategy, which works in backtesting. But everytime I try to let it run realtime with realtime market data (FXCM demo) I get two error warnings at the first trade:

    1)
    Strategy 'SwingTrading/91980897' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.

    2)
    Sim101, Sell stop or sell stop limit orders can't be placed above the market. affected Order: Sell 1 StopMarket @ 10644,71

    What could that mean?
    Kind regards,
    Harvard

    #2
    Hello Harvard,

    Thank you for your post.

    Orders could be rejected due to price, quantity, etc. Can you send us your log and trace files by going to Help > Email Support? Please list 'ATTN: Patrick H - 1617732' in the subject line and reference this thread in the body of the email.

    Comment


      #3
      Thank you ones again. The problem is solved now with your help. My Stop Loss was to small. I copy & paste the answer for all other users:

      The order is for a Forex instrument. The Stop Loss is submitting only 1 pip below the entry. The entry is a buy at the Ask, and the Stop Loss would need to be submitted to the Bid. In Forex the Bid and Ask spread is usually larger than 1 pip. You would need to factor this into your Stop Loss so that it would not be rejected.


      You are using SetStopLoss to submit the protected Stop order. I would recommend increasing the number of pips from the entry for your Stop Loss. You can also submit the SetStopLoss in OnBarUpdate using CalculationMode.Price and use the Bid price with GetCurrentBid.


      Kind regards,
      Harvard

      Comment


        #4
        hello friends I made an automated strategy and the profit and stop are based on ticks, when I place the stop very low, he sends me an "order rejected" message which is the smallest tick stop so that I do not reject the order ???

        Comment


          #5
          Hello steventnt,

          For a buy stop order the GetCurrentAsk() + TickSize would be the closest valid price, for a sell stop order GetCurrentBid() - TickSize would be the closest valid price.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6

            Chelsea, can you tell me if my code to do a stop is correct? I was getting the same error when doing a strategy trading on Apex.

            Code:
            protected override void OnBarUpdate()
                    {
                        if (BarsInProgress != 0)
                            return;
            
                        if (CurrentBars[0] < 1)
                            return;
            
                        if(State == State.Historical)
                            return;
            
                        if (Position.MarketPosition == MarketPosition.Flat)
                            {
                         // Set 1
                        if (CrossAbove(LinReg1, VWMA1, 1))
                        {
                            EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                            SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                            SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);
                        }
            
                         // Set 2
                        if (CrossBelow(LinReg1, VWMA1, 1))
                        {
                            EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                            SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                            SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);
                        }
            
                         // For Long Positions
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                            // If the trade has moved in favor by profitTrigger ticks
                            if (Close[0] - Position.AveragePrice >= TickSize * profitTrigger)
                            {
                                // Adjust the stop loss to entry price plus the plusTicks value
                                SetStopLoss(CalculationMode.Price, Position.AveragePrice + (TickSize * plusTicks));
                            }
                        }
            
                        // For Short Positions
                        if (Position.MarketPosition == MarketPosition.Short)
                        {
                            // If the trade has moved in favor by profitTrigger ticks
                            if (Position.AveragePrice - Close[0] >= TickSize * profitTrigger)
                            {
                                // Adjust the stop loss to entry price minus the plusTicks value
                                SetStopLoss(CalculationMode.Price, Position.AveragePrice - (TickSize * plusTicks));
                            }
                        }    
                        }
                    }​
            Attached Files

            Comment


              #7
              Here is the whole code

              Code:
              namespace NinjaTrader.NinjaScript.Strategies
              {
              
                  public class HeikenAshiRealTime : Strategy
                  {
                      private LinReg LinReg1;
                      private VWMA VWMA1;
                      private int profitTrigger;
                      private int plusTicks;
                      private int stopLossTicks;
                      private int takeProfitTicks;
              
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Run in Real time - Heiken Ashi 1-Min - 13/20/10/-40/20/60 settings";
                              Name                                        = "HeikenAshiRealTime";
                              Calculate                                    = Calculate.OnBarClose;
                              EntriesPerDirection                            = 1;
                              EntryHandling                                = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = true;
                              ExitOnSessionCloseSeconds                    = 30;
                              IsFillLimitOnTouch                            = false;
                              MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                              OrderFillResolution                            = OrderFillResolution.High;
                              OrderFillResolutionType                        = BarsPeriodType.Minute;
                              OrderFillResolutionValue                    = 1;
                              Slippage                                    = 0;
                              StartBehavior                                = StartBehavior.WaitUntilFlat;
                              TimeInForce                                    = TimeInForce.Gtc;
                              TraceOrders                                    = false;
                              RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                              StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                            = 20;
                              IsInstantiatedOnEachOptimizationIteration    = true;
                              LinRegres                                    = 13;
                              VMARes                                        = 20;
                              ProfitTrigger                                 = 10;
                              PlusTicks                                     = -40;
                              StopLossTicks                                 = 20;
                              TakeProfitTicks                             = 50;
                          }
                          else if (State == State.Configure)
                          {
                              profitTrigger = ProfitTrigger;
                              plusTicks = PlusTicks;
                              stopLossTicks = StopLossTicks;
                              takeProfitTicks = TakeProfitTicks;
                          }
                          else if (State == State.DataLoaded)
                          {                
                              LinReg1                = LinReg(Close, Convert.ToInt32(LinRegres));
                              VWMA1                = VWMA(Close, Convert.ToInt32(VMARes));
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          if (BarsInProgress != 0)
                              return;
              
                          if (CurrentBars[0] < 1)
                              return;
              
                          if(State == State.Historical)
                              return;
              
                          if (Position.MarketPosition == MarketPosition.Flat)
                              {
                           // Set 1
                          if (CrossAbove(LinReg1, VWMA1, 1))
                          {
                              EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                              SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                              SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks);
                          }
              
                           // Set 2
                          if (CrossBelow(LinReg1, VWMA1, 1))
                          {
                              EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
                              SetStopLoss("ShortEntry", CalculationMode.Ticks, stopLossTicks, false);
                              SetProfitTarget("ShortEntry", CalculationMode.Ticks, takeProfitTicks);
                          }
              
                           // For Long Positions
                          if (Position.MarketPosition == MarketPosition.Long)
                          {
                              // If the trade has moved in favor by profitTrigger ticks
                              if (Close[0] - Position.AveragePrice >= TickSize * profitTrigger)
                              {
                                  // Adjust the stop loss to entry price plus the plusTicks value
                                  SetStopLoss(CalculationMode.Price, Position.AveragePrice + (TickSize * plusTicks));
                              }
                          }
              
                          // For Short Positions
                          if (Position.MarketPosition == MarketPosition.Short)
                          {
                              // If the trade has moved in favor by profitTrigger ticks
                              if (Position.AveragePrice - Close[0] >= TickSize * profitTrigger)
                              {
                                  // Adjust the stop loss to entry price minus the plusTicks value
                                  SetStopLoss(CalculationMode.Price, Position.AveragePrice - (TickSize * plusTicks));
                              }
                          }    
                          }
                      }
                      #region Properties
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="LinRegres", Order=1, GroupName="Parameters")]
                      public int LinRegres
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="VMARes", Order=2, GroupName="Parameters")]
                      public int VMARes
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="Profit Trigger", Order=3, GroupName="Parameters")]
                      public int ProfitTrigger
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(-50, 0)]
                      [Display(Name="Plus Ticks", Order=4, GroupName="Parameters")]
                      public int PlusTicks
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="Stop Loss Ticks", Order=5, GroupName="Parameters")]
                      public int StopLossTicks
                      { get; set; }
              
                      [NinjaScriptProperty]
                      [Range(1, int.MaxValue)]
                      [Display(Name="Take Profit Ticks", Order=6, GroupName="Parameters")]
                      public int TakeProfitTicks
                      { get; set; }
              
                      #endregion
              ​

              Comment


                #8
                Hello zayone,

                Set methods should be called before calling the entry.

                "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."


                Change
                EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks)​

                To
                SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks)​
                EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");

                And ensure that stopLossTicks, takeProfitTicks is 2 or greater to try and avoid a rejection in a fast market.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,

                  Can we do same with 2 long entries and different Profit target?

                  SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                  SetStopLoss("LongEntry1", CalculationMode.Ticks, stopLossTicks, false);

                  SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks)​
                  SetProfitTarget("LongEntry1", CalculationMode.Ticks, takeProfitTicks2)​

                  EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
                  ​EnterLong(Convert.ToInt32(Default Quantity), "LongEntry1");


                  In OnBarUpdate change Stoploss to BE

                  if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1 && Close[0] >= Position.AveragePrice + takeProfitTick* TickSize)
                  {
                  SetStopLoss("LongEntry1",CalculationMode.Price, Position.AveragePrice + (TickSize * plusTicks));
                  }


                  Is above is possible for multiple entry and BE after First hit?

                  Comment


                    #10
                    Hello jenishij,

                    Yes, that would be fine as they use different signal names. The EntryHandling should be UniqueEntries (or the EntriesPerDirection should be set to the total number of entries you want in the position)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Chelsea, From the above eg: I am not seeing the setstoploss updated the second time for the "LongEntry1". Still taking first Stoploss Value. Can we combine CalculationMode.Tick and CalculationMode.Price in StopLoss for same entry?

                      Initial:
                      SetStopLoss("LongEntry1", CalculationMode.Ticks, stopLossTicks, false);

                      After BE Condition:-
                      SetStopLoss("LongEntry1",CalculationMode.Price, Position.AveragePrice + (TickSize * plusTicks));

                      Comment


                        #12
                        Hello jenishij,

                        Yes, you can change from ticks to price and back and often this is required to reset a set method before placing a new entry.

                        Below is a link to an example ProfitChaseStopTrailSetMethodsExample that does this to reset the stop before placing a new entry.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          I did setstoploss and setprofittargett in onbarupdate(). So it will reset everytime right?

                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                          SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                          SetStopLoss("LongEntry1", CalculationMode.Ticks, stopLossTicks, false);
                          SetProfitTarget("LongEntry", CalculationMode.Ticks, takeProfitTicks)​
                          SetProfitTarget("LongEntry1", CalculationMode.Ticks, takeProfitTicks2)​​
                          }

                          Comment


                            #14
                            Hello jenishij,

                            It would depend on if this code is before or after the entry is submitted, but yes this code would be resetting stops and targets on each bar update.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              ​When i check the strategy performance, Looks second stoploss is not updated. setstoploss will take wrong value in historic data? i didnt put below lines in script.

                              if (State == State.Historical)
                              // return;

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Kaledus, Today, 01:29 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Kaledus
                              by Kaledus
                               
                              Started by PaulMohn, Today, 12:36 PM
                              1 response
                              16 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by yertle, Yesterday, 08:38 AM
                              8 responses
                              36 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by rdtdale, Today, 01:02 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X