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

SetStopLoss in Live Account Error

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

    SetStopLoss in Live Account Error

    I have a managed strategy where I use use SetStopLoss and SetProfitTarget. I set them initially in State.Configure and update them in OnExecution to the correct levels. I then reset them in OnPositionUpdate when flat. Everything works perfect when backtesting, however in real-time (live account) the strategy tries to modify the stop loss (about 100 times in 1 second) and throws an error that it can't modify the order (I have the strategy set to ignore errors so once the error is closed, the strategy is managed correctly). I am completely out of ideas on how to fix this, so I it would be great to be able to send the strategy and trace log to someone so they can double check if there is an issue internally with handling stop and targets in the managed approach, or if there is some unique order of events that I am not adhering.

    #2
    Hello Jack22,

    Thank you for your note.

    I have attached 3 samples you could import and take a look at, perhaps they may shed light on what you could be doing wrong.

    If you are still unable to resolve your issue, send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader8>Log and My Documents>NinjaTrader8/Trace folders.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      **Update: support just stated this issue was fixed in 8.0.12.0**

      Some more information. Everything works correctly in a sim account. Live account I run into this issue. I added print statements today before every SetStopLoss and every SetProfitTarget.

      I SetStopLoss in State.Confiture (all good here--even though I add more Bars, I attach orders to BIP 0):
      Code:
      else if (State == State.Configure)
      {
      	if(this.iDebug) Print("Set Stop / Profit - Configure");
      	SetStopLoss(CalculationMode.Ticks, 20);
      	SetProfitTarget(CalculationMode.Ticks, Math.Max(4, this.iProfitTarget01));
      				
      	AddDataSeries(BarsPeriodType.Tick, 1); // 1
      	AddDataSeries(BarsPeriodType.Minute, 1); // 2
      	AddDataSeries(BarsPeriodType.Range, 1); // 3
      }
      I also reset the stop and target in OnPositionUpdate:
      Code:
      protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
      {
      	if(Position.MarketPosition == MarketPosition.Flat)
      	{		
      		if(this.iDebug) Print("Set Stop / Profit - OnPositionUpdate");
      		SetStopLoss(CalculationMode.Ticks, 20);
      		SetProfitTarget(CalculationMode.Ticks, Math.Max(4, this.iProfitTarget01));
      	}
      }
      After my Order is executed, I update the stop and target:
      Code:
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      	// Long
      	if(oLong01 != null && oLong01 == execution.Order)
      	{
      		if(execution.Order.OrderState == OrderState.Filled 
      			|| execution.Order.OrderState == OrderState.PartFilled 
      			|| (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
      		{	
      			if(this.iDebug) Print("Set Stop / Profit - oLong01");
      			SetStopLoss(CalculationMode.Price, this.swingL[iStopList] - this.iStopLossOffset * TickSize);
      			SetProfitTarget(CalculationMode.Ticks, this.iProfitTarget01);
      					
      			if(execution.Order.OrderState != OrderState.PartFilled)
      			{
      				oLong01 = null;
      			}
      		}
      	}
      	if(oLong02 != null && oLong02 == execution.Order)
      	{
      		if(execution.Order.OrderState == OrderState.Filled 
      			|| execution.Order.OrderState == OrderState.PartFilled 
      			|| (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
      		{	
      			if(this.iDebug) Print("Set Stop / Profit - oLong02");
      			SetStopLoss(CalculationMode.Price, this.swingL[iStopList] - this.iStopLossOffset * TickSize);
      			SetProfitTarget(CalculationMode.Ticks, this.iProfitTarget02);
      					
      			if(execution.Order.OrderState != OrderState.PartFilled)
      			{
      				oLong02 = null;
      			}
      		}
      	}
      }
      This is where the issue occurs for me, as soon as oLong01 is executed and the SetStopLoss is executed, I get the attached printout in my output window (my long execution was at 2800.50) and an error pops up that states the stop could not be modified (UpdateStateModify: order can not be modified affected Order: Sell 1 StopMarket @ 2795.5)

      This is how I assign an Order in OnOrderUpdate:
      Code:
      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
      			double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {
      	if(order.Name == "L01")
      		oLong01 = order;
      	
      	if(oLong01 != null && oLong01 == order)
      	{
      		if(oLong01.OrderState == OrderState.Cancelled
      			|| oLong01.OrderState == OrderState.Rejected)
      		{
      			oLong01 = null;
      		}
      	}
      			
      	if(order.Name == "L02")
      		oLong02 = order;
      			
      	if(oLong02 != null && oLong02 == order)
      	{
      		if(oLong02.OrderState == OrderState.Cancelled
      			|| oLong02.OrderState == OrderState.Rejected)
      		{
      			oLong02 = null;
      		}
      	}
      }
      My enter long limit looks like this: EnterLongLimit(x, true, this.iQuantity01, xP, "L01"); (x = 0)
      Attached Files
      Last edited by Jack22; 02-02-2018, 09:46 AM.

      Comment


        #4
        Hello,

        Thank you for the reply.

        Was this strategy able to run this live properly after upgrading to NT8 R12? I can confirm that this was fixed in R12.

        I look forward to hearing of your results.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hello,

          Thank you for the reply.

          Was this strategy able to run this live properly after upgrading to NT8 R12? I can confirm that this was fixed in R12.

          I look forward to hearing of your results.
          I tested on a live account today and it seems like the issue was fixed in R12. I am running the same strategy live all week, so if anything changes I'll post here; otherwise, all is working as it should.

          Comment


            #6
            Originally posted by Jack22 View Post
            I tested on a live account today and it seems like the issue was fixed in R12. I am running the same strategy live all week, so if anything changes I'll post here; otherwise, all is working as it should.
            Actually same error--the error just popped up on a different monitor and I didn't notice because it hit the profit target so fast. So, doesn't seem to be fixed in R12.

            Comment


              #7
              Hello Jack22,

              Thank you for your update.

              Please send me your log and trace files so we may look into this matter further.

              You can do this by going to the Control Center-> Help-> Email Support. Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

              Please list 'ATTN: Patrick H' in the subject line and reference this thread in the body of the email.

              I look forward to assisting you further.

              Comment


                #8
                Hello mates,

                Finally how it was fixed. I have exactly the same issue.
                • Everything works nicely in the Sim account but with errors in live account using SetTrailingStop for shorts and SetStopLoss for longs.

                Thanks in advance

                Bernard

                Comment


                  #9
                  Hello bcomas,

                  Thank you for your post.

                  What errors are you getting when running your strategy on a live account?

                  Do you see any errors occur in the Log tab of the Control Center? If so, what do these report?

                  Note that setting the stop loss would not be recommended to do in OnExecution/OnPositionUpdate.

                  Please ensure that in your strategy the SetTrailingStop and SetStopLoss orders are reset before entry orders are placed.

                  If you are calling SetStopLoss in OnBarUpdate(), you would need to reset the stop loss before the next entry so that it is set back to an initial level.

                  Please see the SamplePriceModification example in the help guide documentation below for how this could be accomplished. What are you doing differently from the example script?

                  SamplePriceModification — https://ninjatrader.com/support/help...of_stop_lo.htm

                  Thanks in advance, I look forward to your reply.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks a lot for the instructions. I have changed the code like the attached. Hoppefully in live it will work like in Sim.Could you review it?
                    I replicated my strategy for the management of the StopLoss and Trailing.

                    Bernard
                    Attached Files

                    Comment


                      #11
                      Hello bcomas,

                      Thank you for your note.

                      After reviewing your strategy, I do not see anything that would stop the strategy from working live.

                      Please let us know if we may assist further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello,

                        Again in Live trading a Log alert came up with the following message using exactly the previous StrategyTEST for orders management:

                        LiveAccount××××, Cannot change order '439192773' because current order values already match. affected Order: BuyToCover 1 StopMarket @ 13546

                        it happen only on the trailing side(shorts). In low and high volatility...

                        How can I fix it?? Only remove the messages.. the trailing works ok. ..

                        Many Thanks in advance

                        Bernard

                        Comment


                          #13
                          Hello bcomas,

                          Thank you for your note.

                          The error message means that a change order was submitted because the current order already matches the values you are trying to change the order to. This means that you would need to ensure that the change order is placed at a value that does not match the current order.

                          To clarify, are you wanting to stop the error messages from displaying on the screen or in the Log tab of the Control Center? If so, there is no documented or supported methods for stopping error messages from displaying.

                          Please let us know if we may assist further.
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you for your response.

                            This issue happened in 2018 ... It was solved but the solution is not clear.

                            All works perfect in Sim and in Live.
                            But in Live trading it sending several error messages that is annoying. For sure there is a simple solution. The SetTrailingStop() the cause of the messages ONLY in Live trading.

                            Bernard

                            Comment


                              #15
                              Hello bcomas,

                              Thank you for your note.

                              The error message means that SetTrailStop() is adjusting the order to the same level. Are you calling SetTrailStop() from OnBarUpdate() multiple times? If so, this could be fixed with a simple boolean flag. For example:

                              public bool ShouldUpdateStop = true;

                              if(ShouldUpdateStop)
                              {
                              Update();
                              ShouldUpdateStop = false;
                              }

                              Using Print Statements within your code is also very useful to see how the code is running. If you add a Print statement right before the order changing code, do you see the print statement twice? You should also turn on TraceOrders in the strategy to see what the trace orders are saying about the orders.

                              Prints - https://ninjatrader.com/support/help...nt8/?print.htm

                              TradeOrders - https://ninjatrader.com/support/helpGuides/nt8/?traceorders.htm

                              Please note that there is no way to disable the messages from the Log tab of the Control Center.

                              Let me know if I can assist any further.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rjm1903, Today, 04:14 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post rjm1903
                              by rjm1903
                               
                              Started by sidlercom80, 04-05-2021, 09:27 AM
                              7 responses
                              178 views
                              0 likes
                              Last Post Unsuitable  
                              Started by Zinger357, Today, 04:08 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Zinger357  
                              Started by sy2023, 09-15-2023, 08:51 PM
                              4 responses
                              248 views
                              0 likes
                              Last Post Crusade12  
                              Started by JosephHansen, Today, 12:35 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post JosephHansen  
                              Working...
                              X