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

Attach a StopLoss to a floating order without

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

    Attach a StopLoss to a floating order without

    Hello,

    I have some issues with an autostrategy when its working with several contracts.

    I dont use :
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    The strategy have 4 signals. Using a managed approach for the orders. I dont use when is flat before entry because its changing a lot the final performance of the strategy.
    But sometimes after change one signal to another an in-flight order or orders remains open without Stoploss.

    Is there a solution to attach a StopLoss when the strategy is working without automaticaly?
    I used MT4 as well and I remember some expert that is attaching the StopLoss and Takeprofit if the orders from the chart works without..
    Is there some solution for NinjaTrader? or some change with the code...

    Thanks!

    Attached a sample of how I use the autostrategy and his issues sometimes..
    Attached Files

    #2
    Hello bcomas,

    Thanks for your post.

    It looks like the stop loss is being reset to an initial level for "LongA" but I do not see any behaviors to associate a stop loss with "LongB" or reset a stop loss for short side orders.

    We always suggest ensuring that stop losses are reset before the entry order method is called. When you ensure that the stop is reset to an initial level for "LongB" before that entry is made, do you see your stop loss? We would recommend the same for any short side positions that you want protected.

    After minding the above, what orders are specifically do not have the stop loss applied?

    Keep in mind that SetStopLoss and SetTrailStop should not be mixed for the same entry signals. If you test with the TrailStop code removed, can you simplify your test?

    Exit orders, like ExitLongStopMarket, can be called after a position has been entered, but you may wish to look into why the stops and are not getting assigned when the entry order fills as a better solution.

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      Many thanks for your review.
      I will add into state.configure the SetTrailingStop (initialstop) for the LongB, ShortA,ShortB.

      I think it should be enough to reset the trailing before the position entries. Because when ....Flat the SetTrailingStops LongB, ShortA,ShortB are already on the code.


      Comment


        #4
        Hello bcomas,

        Just to be clear, if you are using Set methods in OnBarUpdate, they should always be reset in OnBarUpdate before the associated entry is submitted. If you call the Set method in State.Configure, and then later call the Set method in OnBarUpdate, it may not be reset to an initial level unless you make sure the Set method is called before the entry method is submitted.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          Many thanks for your help!!. I will update the strategy as your instructions.

          It should fix the issue..

          Comment


            #6
            Hello,

            The missing StopLoss and TakeProfit seems fixed after amend it with your instructions. But now sometimes the quantity of the StopLoss and Takeprofit doesnt macth with the position size sometimes. In market replay seems ok but after testing with Sim the bug appears. What can be the cause to fix it as well??
            Attached I replicated my strategy with the same order management.
            Apreciate some light in this

            Code:
             else if (State == State.Configure)
            {
            /// Initial Stop Loss / Take profit
            {
            SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
            SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
            SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
            SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
            SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
            SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
            }
            
            {//RealtimeErrorHandling - IgnoreAllErrors
            RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;/////////
            //RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
            }
            
            
            }
            else if (State == State.DataLoaded)
            {
            
            wasLoaded = true;
            }
            
            else if (State == State.Terminated) // Managed orders
            {
            if(wasLoaded){
            // Cancel Orders
            if (Account != null)
            foreach (Order actOrdr in Account.Orders)
            {
            Account.Cancel(new[] { actOrdr });
            }
            // Cancel Positions
            if (Position.MarketPosition == MarketPosition.Short)
            if (State != State.Historical)
            {
            ExitShort(Convert.ToInt32(Contracts),"",@"");
            ExitShort(Convert.ToInt32(Contracts),"",@"ShortA") ;
            ExitShort(Convert.ToInt32(Contracts),"",@"ShortB") ;
            }
            if (Position.MarketPosition == MarketPosition.Long)
            if (State != State.Historical)
            {
            ExitLong(Convert.ToInt32(Contracts),"",@"");
            ExitLong(Convert.ToInt32(Contracts),"",@"LongA");
            ExitLong(Convert.ToInt32(Contracts),"",@"LongB");
            }
            
            }
            }
            }
            
            protected override void OnBarUpdate()
            {
            
            {/// Reset Take Profit , Volatile market Times
            if (Bars.IsFirstBarOfSession)
            SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
            }
            
            if (BarsInProgress != 0)
            return;
            
            if (CurrentBars[0] < 1)
            return;
            
            if (CurrentBar < BarsRequiredToTrade)
            return;
            
            {
            ///Reset Stops when Flat
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
            SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
            SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
            SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
            }
            
            }
            
            /// BreakEven LONGA
            {
            // Resets the stop loss to the original value when all positions are closed
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            if (!canTradeTimeH()) /// Times
            SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
            }
            
            // 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+BreakEvenLong ticks, set stop loss to breakeven
            if (Close[0] > Position.AveragePrice + BreakEvenLong * TickSize)
            {
            if (!canTradeTimeH()) /// Times
            //Print("Move LongA to BE");
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice, SS1);
            }
            }
            
            }
            
            /// Trailing / SHORTA / SHORTB & LONGB
            {
            
            if (Position.MarketPosition == MarketPosition.Short)
            {
            // Update within the context of the 1 tick Bars
            SetTrailStop(@"ShortA",CalculationMode.Percent, 00.1,SS1);
            }
            else if (Position.MarketPosition == MarketPosition.Long)
            {
            // Update within the context of the 1 tick Bars
            SetTrailStop(@"LongB",CalculationMode.Percent, 00.1,SS1);
            }
            else if (Position.MarketPosition == MarketPosition.Short)
            {
            // Update within the context of the 1 tick Bars
            SetTrailStop(@"ShortB",CalculationMode.Percent, 00.1,SS1);
            }
            }
            
            ///////////////////////////////////////////////////////// SIGNALS
            
            /// Trailing LONGA , Volatile market Times
            
            //Reset Stops
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            if (canTradeTimeH()) /// Volatile market Times
            
            SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
            /*SetStopLoss(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);*/
            
            doOneTime = true;
            longOne = true;
            /*shortOne = true;*/
            longTwo = true;
            /*shortTwo = true;*/
            longThree = true;
            /*shortThree = true;*/
            longFour = true;
            /*shortFour = true;*/
            longStopOne = true;
            /*shortStopOne = true;*/
            
            }
            
            if (Position.MarketPosition == MarketPosition.Long)
            
            { //15
            if (Close[0] >= (Position.AveragePrice + 50 * TickSize) && longStopOne)
            {
            if (canTradeTimeH()) /// Volatile market Times
            //15
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice - 50 * TickSize, SS1);
            longStopOne = false;
            
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long stop -50" );
            doOneTime = true;
            }*/
            
            }
            //25
            if (Close[0] >= (Position.AveragePrice + 100 * TickSize) && longOne)
            {
            if (canTradeTimeH()) /// Volatile market Times
            //15
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 50 * TickSize, SS1);
            longOne = false;
            
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long stop +50" );
            doOneTime = true;
            }*/
            
            }
            //45
            if (Close[0] >= (Position.AveragePrice + 150 * TickSize) && longThree)
            {
            if (canTradeTimeH()) /// Volatile market Times
            //30
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 75 * TickSize, SS1);
            longThree = false;
            
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long stop +75" );
            doOneTime = true;
            }*/
            
            }
            //80
            if (Close[0] >= (Position.AveragePrice + 200 * TickSize) && longTwo)
            
            {
            if (canTradeTimeH()) /// Volatile market Times
            //60
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 100 * TickSize, SS1);
            longTwo = false;
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long stop +100" );
            doOneTime = false;
            }*/
            }
            //105
            if (Close[0] >= (Position.AveragePrice + 250 * TickSize) && longFour)
            
            {
            if (canTradeTimeH()) /// Volatile market Times
            //95
            SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 125 * TickSize, SS1);
            longFour = false;
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long stop +125" );
            doOneTime = false;
            }*/
            }
            }
            
            // Reset Targets
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            if (canTradeTimeH()) /// Volatile market Times
            
            SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);//150
            //SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);//100
            
            doOneTime = true;
            TargetLongOne = true;
            TargetLongTwo = true;
            TargetLongThree = true;
            /*TargetShortOne = true;
            TargetShortTwo = true;
            TargetShortThree = true;*/
            
            }
            
            // Long Target Moved Up
            else if (Position.MarketPosition == MarketPosition.Long)
            
            {
            // Once the price is greater than entry price+1 tick, SetProfitTarget to 300
            if (Close[0] > (Position.AveragePrice + 1 * TickSize) && TargetLongOne)
            {
            if (canTradeTimeH()) /// Volatile market Times
            
            SetProfitTarget(@"LongA", CalculationMode.Ticks, 300);
            TargetLongOne = false;
            
            /*if (doOneTime)
            {
            if (canTradeTimeH())
            Print ("Trailing Long Target moved up +300" );
            doOneTime = true;
            }*/
            
            }
            
            }
            
            // Check for buy condition
            if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
            
            {
            
            {
            ///Reset Stops before entry
            //SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
            //SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
            }
            if (State != State.Historical)
            EnterLongLimit(Convert.ToInt32(Contracts), Close[0], @"LongA");/////////////BUY
            
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert4.wav");
            BackBrush = Brushes.Green;
            
            }
            
            // Check for sell condition
            if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
            
            {
            
            {
            ///Reset Stops before entry
            SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
            SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
            }
            if (State != State.Historical)
            EnterShortLimit(Convert.ToInt32(Contracts), Close[0], @"ShortA");/////////////SELL
            
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
            BackBrush = Brushes.Maroon;
            }
            
            // Check for buy condition
            if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
            
            {
            
            {
            ///Reset Stops before entry
            SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
            }
            if (State != State.Historical)
            EnterLong(Convert.ToInt32(Contracts),@"LongB");
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
            
            BackBrush = Brushes.Lime;
            //Print (Time[0]+ " Large Trade - Long");
            
            }
            
            // Check for sell condition
            if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
            
            {
            
            {
            ///Reset Stops before entry
            SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
            }
            if (State != State.Historical)
            EnterShort(Convert.ToInt32(Contracts),@"ShortB");
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
            
            BackBrush = Brushes.Red;
            //Print (Time[0]+ " Large Trade - Short");
            
            }
            
            // Exit if reverse
            ///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exit BUY
            // sell condition
            if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
            
            if (Position.MarketPosition == MarketPosition.Long)
            {
            ExitLong(Convert.ToInt32(Contracts),"",@"");
            ExitLong(Convert.ToInt32(Contracts),"",@"LongA");
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
            BackBrush = Brushes.Gray;
            }
            
            ///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exit SELL
            // buy condition
            if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
            
            if (Position.MarketPosition == MarketPosition.Short)
            {
            ExitShort(Convert.ToInt32(Contracts),"",@"");
            ExitShort(Convert.ToInt32(Contracts),"",@"ShortA") ;
            
            //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
            BackBrush = Brushes.Gray;
            }
            
            }
            Attached Files

            Comment


              #7
              Originally posted by NinjaTrader_Jim View Post
              Hello bcomas,

              Just to be clear, if you are using Set methods in OnBarUpdate, they should always be reset in OnBarUpdate before the associated entry is submitted. If you call the Set method in State.Configure, and then later call the Set method in OnBarUpdate, it may not be reset to an initial level unless you make sure the Set method is called before the entry method is submitted.
              Hello,

              The missing StopLoss and TakeProfit seems fixed after amend it with your instructions. But now sometimes the quantity of the StopLoss and Takeprofit doesnt macth with the position size sometimes. In market replay seems ok but after testing with Sim the bug appears. What can be the cause to fix it as well??
              Attached I replicated my strategy with the same order management.
              Apreciate some light in this

              Code:
               else if (State == State.Configure)
              {
              /// Initial Stop Loss / Take profit
              {
              SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
              SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
              SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
              SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
              SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
              SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
              }
              
              {//RealtimeErrorHandling - IgnoreAllErrors
              RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;/////////
              //RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
              }
              
              
              }
              else if (State == State.DataLoaded)
              {
              
              wasLoaded = true;
              }
              
              else if (State == State.Terminated) // Managed orders
              {
              if(wasLoaded){
              // Cancel Orders
              if (Account != null)
              foreach (Order actOrdr in Account.Orders)
              {
              Account.Cancel(new[] { actOrdr });
              }
              // Cancel Positions
              if (Position.MarketPosition == MarketPosition.Short)
              if (State != State.Historical)
              {
              ExitShort(Convert.ToInt32(Contracts),"",@"");
              ExitShort(Convert.ToInt32(Contracts),"",@"ShortA") ;
              ExitShort(Convert.ToInt32(Contracts),"",@"ShortB") ;
              }
              if (Position.MarketPosition == MarketPosition.Long)
              if (State != State.Historical)
              {
              ExitLong(Convert.ToInt32(Contracts),"",@"");
              ExitLong(Convert.ToInt32(Contracts),"",@"LongA");
              ExitLong(Convert.ToInt32(Contracts),"",@"LongB");
              }
              
              }
              }
              }
              
              protected override void OnBarUpdate()
              {
              
              {/// Reset Take Profit , Volatile market Times
              if (Bars.IsFirstBarOfSession)
              SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
              }
              
              if (BarsInProgress != 0)
              return;
              
              if (CurrentBars[0] < 1)
              return;
              
              if (CurrentBar < BarsRequiredToTrade)
              return;
              
              {
              ///Reset Stops when Flat
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
              SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
              SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
              SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
              }
              
              }
              
              /// BreakEven LONGA
              {
              // Resets the stop loss to the original value when all positions are closed
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              if (!canTradeTimeH()) /// Times
              SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
              }
              
              // 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+BreakEvenLong ticks, set stop loss to breakeven
              if (Close[0] > Position.AveragePrice + BreakEvenLong * TickSize)
              {
              if (!canTradeTimeH()) /// Times
              //Print("Move LongA to BE");
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice, SS1);
              }
              }
              
              }
              
              /// Trailing / SHORTA / SHORTB & LONGB
              {
              
              if (Position.MarketPosition == MarketPosition.Short)
              {
              // Update within the context of the 1 tick Bars
              SetTrailStop(@"ShortA",CalculationMode.Percent, 00.1,SS1);
              }
              else if (Position.MarketPosition == MarketPosition.Long)
              {
              // Update within the context of the 1 tick Bars
              SetTrailStop(@"LongB",CalculationMode.Percent, 00.1,SS1);
              }
              else if (Position.MarketPosition == MarketPosition.Short)
              {
              // Update within the context of the 1 tick Bars
              SetTrailStop(@"ShortB",CalculationMode.Percent, 00.1,SS1);
              }
              }
              
              ///////////////////////////////////////////////////////// SIGNALS
              
              /// Trailing LONGA , Volatile market Times
              
              //Reset Stops
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              if (canTradeTimeH()) /// Volatile market Times
              
              SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
              /*SetStopLoss(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);*/
              
              doOneTime = true;
              longOne = true;
              /*shortOne = true;*/
              longTwo = true;
              /*shortTwo = true;*/
              longThree = true;
              /*shortThree = true;*/
              longFour = true;
              /*shortFour = true;*/
              longStopOne = true;
              /*shortStopOne = true;*/
              
              }
              
              if (Position.MarketPosition == MarketPosition.Long)
              
              { //15
              if (Close[0] >= (Position.AveragePrice + 50 * TickSize) && longStopOne)
              {
              if (canTradeTimeH()) /// Volatile market Times
              //15
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice - 50 * TickSize, SS1);
              longStopOne = false;
              
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long stop -50" );
              doOneTime = true;
              }*/
              
              }
              //25
              if (Close[0] >= (Position.AveragePrice + 100 * TickSize) && longOne)
              {
              if (canTradeTimeH()) /// Volatile market Times
              //15
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 50 * TickSize, SS1);
              longOne = false;
              
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long stop +50" );
              doOneTime = true;
              }*/
              
              }
              //45
              if (Close[0] >= (Position.AveragePrice + 150 * TickSize) && longThree)
              {
              if (canTradeTimeH()) /// Volatile market Times
              //30
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 75 * TickSize, SS1);
              longThree = false;
              
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long stop +75" );
              doOneTime = true;
              }*/
              
              }
              //80
              if (Close[0] >= (Position.AveragePrice + 200 * TickSize) && longTwo)
              
              {
              if (canTradeTimeH()) /// Volatile market Times
              //60
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 100 * TickSize, SS1);
              longTwo = false;
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long stop +100" );
              doOneTime = false;
              }*/
              }
              //105
              if (Close[0] >= (Position.AveragePrice + 250 * TickSize) && longFour)
              
              {
              if (canTradeTimeH()) /// Volatile market Times
              //95
              SetStopLoss(@"LongA",CalculationMode.Price, Position.AveragePrice + 125 * TickSize, SS1);
              longFour = false;
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long stop +125" );
              doOneTime = false;
              }*/
              }
              }
              
              // Reset Targets
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              if (canTradeTimeH()) /// Volatile market Times
              
              SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);//150
              //SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);//100
              
              doOneTime = true;
              TargetLongOne = true;
              TargetLongTwo = true;
              TargetLongThree = true;
              /*TargetShortOne = true;
              TargetShortTwo = true;
              TargetShortThree = true;*/
              
              }
              
              // Long Target Moved Up
              else if (Position.MarketPosition == MarketPosition.Long)
              
              {
              // Once the price is greater than entry price+1 tick, SetProfitTarget to 300
              if (Close[0] > (Position.AveragePrice + 1 * TickSize) && TargetLongOne)
              {
              if (canTradeTimeH()) /// Volatile market Times
              
              SetProfitTarget(@"LongA", CalculationMode.Ticks, 300);
              TargetLongOne = false;
              
              /*if (doOneTime)
              {
              if (canTradeTimeH())
              Print ("Trailing Long Target moved up +300" );
              doOneTime = true;
              }*/
              
              }
              
              }
              
              // Check for buy condition
              if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
              
              {
              
              {
              ///Reset Stops before entry
              //SetStopLoss(@"LongA", CalculationMode.Ticks, StopLoss, SS1);
              //SetProfitTarget(@"LongA", CalculationMode.Ticks, ProfitTarget);
              }
              if (State != State.Historical)
              EnterLongLimit(Convert.ToInt32(Contracts), Close[0], @"LongA");/////////////BUY
              
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert4.wav");
              BackBrush = Brushes.Green;
              
              }
              
              // Check for sell condition
              if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
              
              {
              
              {
              ///Reset Stops before entry
              SetTrailStop(@"ShortA", CalculationMode.Ticks, StopLoss, SS1);
              SetProfitTarget(@"ShortA", CalculationMode.Ticks, ProfitTarget2);
              }
              if (State != State.Historical)
              EnterShortLimit(Convert.ToInt32(Contracts), Close[0], @"ShortA");/////////////SELL
              
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
              BackBrush = Brushes.Maroon;
              }
              
              // Check for buy condition
              if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
              
              {
              
              {
              ///Reset Stops before entry
              SetTrailStop(@"LongB", CalculationMode.Ticks, StopLoss, SS1);
              }
              if (State != State.Historical)
              EnterLong(Convert.ToInt32(Contracts),@"LongB");
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
              
              BackBrush = Brushes.Lime;
              //Print (Time[0]+ " Large Trade - Long");
              
              }
              
              // Check for sell condition
              if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
              
              {
              
              {
              ///Reset Stops before entry
              SetTrailStop(@"ShortB", CalculationMode.Ticks, StopLoss, SS1);
              }
              if (State != State.Historical)
              EnterShort(Convert.ToInt32(Contracts),@"ShortB");
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
              
              BackBrush = Brushes.Red;
              //Print (Time[0]+ " Large Trade - Short");
              
              }
              
              // Exit if reverse
              ///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exit BUY
              // sell condition
              if (RVI(14)[0] > 50 && CrossBelow(SMA(9), SMA(14), 1))
              
              if (Position.MarketPosition == MarketPosition.Long)
              {
              ExitLong(Convert.ToInt32(Contracts),"",@"");
              ExitLong(Convert.ToInt32(Contracts),"",@"LongA");
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
              BackBrush = Brushes.Gray;
              }
              
              ///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> exit SELL
              // buy condition
              if (RVI(14)[0] > 50 && CrossAbove(SMA(9), SMA(14), 1))
              
              if (Position.MarketPosition == MarketPosition.Short)
              {
              ExitShort(Convert.ToInt32(Contracts),"",@"");
              ExitShort(Convert.ToInt32(Contracts),"",@"ShortA") ;
              
              //PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Alert3.wav");
              BackBrush = Brushes.Gray;
              }
              
              }
              Attached Files

              Comment


                #8
                Hello bcomas,

                The Set methods are being called in several spots in the script's life. You are calling the Set methods when the strategy is flat, and also in several other parts of the script. To move forward, I suggest reducing the code and functionality, and to add pieces back in sequentially until the issue comes up.

                For example, you can eliminate all stop/target modification, and just ensure you are resetting the stop loss and profit target before the associated entry is submitted.

                Once that is confirmed working, you can add code back to modify just the stop loss or profit target for one entry, and confirm that works.

                From there you can add the code to move stop loss or profit target for another entry.

                If you are using Set methods in OnBarUpdate, you just have to ensure they are reset before the associated entry is made. (It would not be necessary to include them in OnStateChange if they care properly reset in OnBarUpdate.) I suggest a Keep It Simple approach and to remove all Set methods except for those that are submitted before the associated entry, and those needed to modify your stop. That way you only have what is needed and there will be less ambiguity when troubleshooting.

                These steps will at least lead you to the problematic part. If you have gotten to this point and the matter is not clear, please implement just enough code to demonstrate the issue and attach it here for our comment.

                We look forward to assisting.

                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jim,

                  Finally everything is fixed just updating my NT to 8.0.23.2 version. (The last)

                  Now the quantity is correct and all SL and TP in place.

                  Also I ammended as per your instructions.

                  Tested in Live Trading

                  For some reason working with the previous NT version I had several issues using SetTrailingStop() and SetStopLoss() .

                  Many thanks for your help

                  Bernard

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by judysamnt7, 03-13-2023, 09:11 AM
                  4 responses
                  59 views
                  0 likes
                  Last Post DynamicTest  
                  Started by ScottWalsh, Today, 06:52 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by olisav57, Today, 07:39 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post olisav57  
                  Started by trilliantrader, Today, 03:01 PM
                  2 responses
                  21 views
                  0 likes
                  Last Post helpwanted  
                  Started by cre8able, Today, 07:24 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X