Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLoss and Close Position Error.

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

    StopLoss and Close Position Error.

    Hallo.
    If the strategy are on position and StopLoss price is same as the price when strategy should reverse position. Strategy generate StopLoss order and ClosePosition order same time and this is case of error. How to set condition to avoid this error??
    Czarek

    #2
    For clarification...

    - What method is used to submit the stop loss order
    - What method is used to reverse the position
    RayNinjaTrader Customer Service

    Comment


      #3
      Her is my strategy mostly made by strategy creator. Can I fix this problem in creator?

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class NQlimitbreakeven : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int stop = 34; // Default setting for Stop
      private int limit = 142; // Default setting for Limit
      private int breakeven = 33; // Default setting for Breakeven
      private int offset = 34; // Default setting for Offset
      // User defined variables (add any user defined variables below)
      #endregion
      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      SetProfitTarget("", CalculationMode.Ticks, Limit);
      SetStopLoss("", CalculationMode.Ticks, Stop, false);
      CalculateOnBarClose = true;
      }
      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Resets the stop loss to the original value when all positions are closed
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Ticks, stop);
      }

      // If a long position is open, allow for stop loss modification to breakeven
      else if (Position.MarketPosition == MarketPosition.Long)
      {
      // Once the price is greater than entry price+breakeven ticks, set stop loss to breakeven
      if (Close[0] > Position.AvgPrice + breakeven * TickSize)
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      }
      }
      // If a short position is open, allow for stop loss modification to breakeven
      else if (Position.MarketPosition == MarketPosition.Short)
      {
      // Once the price is lower than entry price-breakeven ticks, set stop loss to breakeven
      if (Close[0]+ breakeven * TickSize < Position.AvgPrice)
      {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      }
      }
      // Condition set 1
      if (Close[1] < Close[0]
      && Close[2] < Close[0]
      && Close[3] < Close[0]
      && Close[4] < Close[0]
      && Close[5] < Close[0]
      && Close[6] < Close[0]
      && Close[7] < Close[0]
      && Close[8] < Close[0]
      && Close[9] < Close[0]
      && Close[10] < Close[0]
      && Open[0] < Close[0]
      && Open[1] < Close[0]
      && Open[2] < Close[0]
      && Open[3] < Close[0]
      && Open[4] < Close[0]
      && Open[5] < Close[0]
      && Open[6] < Close[0]
      && Open[7] < Close[0]
      && Open[8] < Close[0]
      && Open[9] < Close[0]
      && Open[10] < Close[0]
      && Open[0] + Offset * TickSize > Close[0]
      && ToTime(Time[0]) <= ToTime(15, 0, 0)
      && ToTime(Time[0]) >= ToTime(08, 30, 0))
      {
      EnterLong(1, "");
      }


      // Condition set 2
      if (Close[1] > Close[0]
      && Close[2] > Close[0]
      && Close[3] > Close[0]
      && Close[4] > Close[0]
      && Close[5] > Close[0]
      && Close[6] > Close[0]
      && Close[7] > Close[0]
      && Close[8] > Close[0]
      && Close[9] > Close[0]
      && Close[10] > Close[0]
      && Open[0] > Close[0]
      && Open[1] > Close[0]
      && Open[2] > Close[0]
      && Open[3] > Close[0]
      && Open[4] > Close[0]
      && Open[5] > Close[0]
      && Open[6] > Close[0]
      && Open[7] > Close[0]
      && Open[8] > Close[0]
      && Open[9] > Close[0]
      && Open[10] > Close[0]
      && Open[0] < Close[0] + Offset * TickSize
      && ToTime(Time[0]) <= ToTime(15, 0, 0)
      && ToTime(Time[0]) >= ToTime(08, 30, 0))
      {
      EnterShort(1, "");
      }

      }
      #region Properties
      [Description("")]
      [Category("Parameters")]
      public int Stop
      {
      get { return stop; }
      set { stop = Math.Max(1, value); }
      }
      [Description("")]
      [Category("Parameters")]
      public int Limit
      {
      get { return limit; }
      set { limit = Math.Max(1, value); }
      }
      [Description("")]
      [Category("Parameters")]
      public int Breakeven
      {
      get { return breakeven; }
      set { breakeven = Math.Max(1, value); }
      }

      [Description("")]
      [Category("Parameters")]
      public int Offset
      {
      get { return offset; }
      set { offset = Math.Max(1, value); }
      }
      #endregion
      }
      }

      Comment


        #4
        Unfortunately there is no current way to get around this since as you reverse, the market fills the stop before it can be cancelled. We have on our list for our next major version to deal with this issue by confirming cancellation of existing orders prior to submitting the reverse entry order.
        RayNinjaTrader Customer Service

        Comment


          #5
          Please let me know if you fix this problem. If this error happend my strategy terminate it self and it could be reason of potential loss.

          Now new problem appeared. Strategy generate this message: Account write permission nenied Affected Order: Buy 2 Stop@ 920
          What does it mean I only play with 1 future and price 920 is strange
          Rgds
          Czarek
          Czarek

          Comment


            #6
            Who is your broker, this is a message that comes from a broker server.
            RayNinjaTrader Customer Service

            Comment


              #7
              Data feed Zen-Fire, broker Proactive Direct this happend in simulation mode.

              Comment


                #8
                Just ignore this. The simulation account you are using can be used by multiple people and sometimes orders are placed into this account externally. That's what generates this message.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  My strategy still generate error and terminate is self.
                  The error message is: Sell Stop order can't be placed above the market. Affected order: Sell @ 1850.25
                  Strategy should only revese position do not put stop loss in this place.
                  I can not find where the problem is??
                  Czarek

                  Comment


                    #10
                    The problem is that when your strategy submits a stop sell order, the price of the stop order is above the current market price which is an invalid order.

                    --> You need to submit a valid stop order price
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      StopLoss have been generated by starategy generator and looks OK. Could you tell me some more how to fix it?

                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
                      SetProfitTarget("", CalculationMode.Ticks, Limit);
                      SetStopLoss("", CalculationMode.Ticks, Stop, false);
                      CalculateOnBarClose = true;
                      }
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      // Resets the stop loss to the original value when all positions are closed
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetStopLoss(CalculationMode.Ticks, stop);
                      }

                      // If a long position is open, allow for stop loss modification to breakeven
                      else if (Position.MarketPosition <= MarketPosition.Long)
                      {
                      // Once the price is greater than entry price+breakeven ticks, set stop loss to breakeven
                      if (Close[0] > Position.AvgPrice + breakeven * TickSize)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1 * TickSize);
                      }
                      }
                      // If a short position is open, allow for stop loss modification to breakeven
                      else if (Position.MarketPosition >= MarketPosition.Short)
                      {
                      // Once the price is lower than entry price-breakeven ticks, set stop loss to breakeven
                      if (Close[0]+ breakeven * TickSize < Position.AvgPrice)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice - 1 * TickSize);
                      }

                      Comment


                        #12
                        Unfortunately I can't debug it but your strategy looks simple. At some point, you are modifying the stop and its an invalid price.
                        RayNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        168 responses
                        2,262 views
                        0 likes
                        Last Post sidlercom80  
                        Started by Barry Milan, Yesterday, 10:35 PM
                        3 responses
                        10 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by WeyldFalcon, 12-10-2020, 06:48 PM
                        14 responses
                        1,429 views
                        0 likes
                        Last Post Handclap0241  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        2 responses
                        9 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by jeronymite, 04-12-2024, 04:26 PM
                        3 responses
                        41 views
                        0 likes
                        Last Post jeronymite  
                        Working...
                        X