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 keeps shutting itself down

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

    Strategy keeps shutting itself down

    Good afternoon,
    My programs keep shutting down.

    Click image for larger version

Name:	image.png
Views:	58
Size:	10.5 KB
ID:	1297424

    Is there way to prevent this from happening?

    I have a check to make sure that the position is in the correct Long/Short

    is there a way to add slippage or close at what ever the price is?

    Code:
    (Position.MarketPosition == MarketPosition.Short)

    Code:
    ExitShort(Convert.ToInt32(DefaultQuantity), @"OptimusCloseSell20", @"OptimusSell20");
    is what im using to close

    I dont see what the issue is.

    I would be grateful for any insite into why this happens every once in a while.

    Thank you!

    #2
    Hello Lele2k24,

    Thank you for your post.

    Is this a strategy created in the Strategy Builder or is it an unlocked script in the NinjaScript Editor? Do you have any stops or targets in your strategy, and if so how is that logic configured? The error message suggests there is a stop or stop limit order with a price on the incorrect side of the market, so this is not related to the ExitShort() order.

    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick response. It is unlocked:

      what would be the best way to try this:

      Code:
              //trying my hand at ATM (trailling SL)
                  if( AccountUnrealizedPL >=  1000  && AccountUnrealizedPL <=2000 )
                  {
                      Print("HIT 1k - moving to BE " + AccountUnrealizedPL);
                      
                      SetStopLoss(CalculationMode.Currency, 1);
                  }
                  
                  if( AccountUnrealizedPL >=  2000  && AccountUnrealizedPL <=3000 )
                  {
                      Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
                      
                      SetStopLoss(CalculationMode.Currency, 1000);
                  }
                  
                  if( AccountUnrealizedPL >=  3000  && AccountUnrealizedPL <=4000 )
                  {
                      Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
                      
                      SetStopLoss(CalculationMode.Currency, 2000);
                  }            
                  
                  if( AccountUnrealizedPL >=  4000  && AccountUnrealizedPL <=5000 )
                  {
                      Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
                      
                      SetStopLoss(CalculationMode.Currency, 3000);
                  }            
                      ​

      thanks again!

      Comment


        #4
        Originally posted by Lele2k24 View Post
        Thank you for the quick response. It is unlocked:

        what would be the best way to try this:

        Code:
         //trying my hand at ATM (trailling SL)
        if( AccountUnrealizedPL >= 1000 && AccountUnrealizedPL <=2000 )
        {
        Print("HIT 1k - moving to BE " + AccountUnrealizedPL);
        
        SetStopLoss(CalculationMode.Currency, 1);
        }
        
        if( AccountUnrealizedPL >= 2000 && AccountUnrealizedPL <=3000 )
        {
        Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
        
        SetStopLoss(CalculationMode.Currency, 1000);
        }
        
        if( AccountUnrealizedPL >= 3000 && AccountUnrealizedPL <=4000 )
        {
        Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
        
        SetStopLoss(CalculationMode.Currency, 2000);
        }
        
        if( AccountUnrealizedPL >= 4000 && AccountUnrealizedPL <=5000 )
        {
        Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
        
        SetStopLoss(CalculationMode.Currency, 3000);
        }
        ​

        thanks again!
        The Currency CalculationMode is calculated as the "PnL away from average entry. Calculated by the dollar per tick value for the order quantity used.​" Rather than setting the stop loss to a currency of 1 to breakeven, it is better to use CalculationMode.Price and set the price value to Position.AveragePrice:
        Code:
        SetStopLoss(CalculationMode.Price, Position.AveragePrice);
        For more details on SetStopLoss() and each calculation mode:


        As for moving the stop loss once certain amounts of profit have been hit, I suggest reviewing the following reference sample about modifying the price of stops and targets:


        That example modifies the stop loss to breakeven once the Close price hits the entry price plus 50 ticks:
        Code:
                    // 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+50 ticks, set stop loss to breakeven
                        if (Close[0] > Position.AveragePrice + 50 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                        }
                    }
        ​
        If you wanted to use an amount for unrealized PnL instead, you could consider using Position.GetUnrealizedProfitLoss() and checking if that is greater than or equal to the desired value:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you... so How would i set the stop loss to 1k of unlreazed profit?


          Code:
           // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
          if (Close[0] > Position.AveragePrice + 50 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice);
          }​
          this will set the SL to the entry price correct? so how would I move the stop loss to 1k in unrealized profit after it hits 2k of unrealized profit?

          thanks again!!!

          Comment


            #6
            Originally posted by Lele2k24 View Post
            Thank you... so How would i set the stop loss to 1k of unlreazed profit?


            Code:
             // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
            if (Close[0] > Position.AveragePrice + 50 * TickSize)
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
            }​
            this will set the SL to the entry price correct? so how would I move the stop loss to 1k in unrealized profit after it hits 2k of unrealized profit?

            thanks again!!!
            To check for 2000 of unrealized profit, you could use the following:
            if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) >= 2000)

            then to move the stop loss to 1000 in profit, that would be a currency value of -1000 since it is a PnL of 1000 on the other side of the entry:
            SetStopLoss(CalculationMode.Currency, -1000);

            Altogether, the logic would look like this:
            Code:
            // check if unrealized PnL is greater than or equal to 2000
            if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 2000)
            {
            // if so, move stop loss to 1000 in unrealized PnL using currency calculation mode:
            SetStopLoss(CalculationMode.Currency, -1000);
            }
            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              I just had the strat get the same issue:

              this is my updated code..

              Code:
                      //trying my hand at ATM (trailing SL)
                    if(Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
                        {
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 1000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 2000)
                          {
                              Print("HIT 1k - moving to BE " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                          }
                          
                      
                          if( Position.MarketPosition == MarketPosition.Long)
                          {
                          
                      //    if( AccountUnrealizedPL >=  2000  && AccountUnrealizedPL <=3000 )
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 2000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 3000)
                          {
                              Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, 1000);
                          }
                          
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 3000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 4000)
              
                          {
                              Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, 2000);
                          }            
                          
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 4000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 5000)
                          {
                              Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, 3000);
                          }            
                          
                          }//end if the market is long
                          
                          else if (Position.MarketPosition == MarketPosition.Short)    
                          {
                          
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 2000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 3000)
                          {
                              Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, -1000);
                          }
                          
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 3000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 4000)
              
                          {
                              Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, -2000);
                          }            
                          
                          if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 4000   && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 5000)
                          {
                              Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
                              
                              SetStopLoss(CalculationMode.Currency, -3000);
                          }            
                          
                          }//end if the market is short
                         }
                          ​
              the error came out just as the trade opened... there is no way any of the profit or loss level would have been hit..

              and the same code was applied to a different chat, and that opened the orders just fine - different instrument as well - this failed twice now on the ES while the NQ charts opened np. ( same short direction as well)

              I am lost.

              Please advise ..

              also wish NT* would not just disable the strat like that.. not opening the order with an error message would be enough


              thank you!!!
              Last edited by Lele2k24; 03-27-2024, 04:15 PM.

              Comment


                #8
                Hello Lele2k24,

                Thank you for your reply.

                What is your logic for the initial stop loss and profit target levels before you try your trailing stop logic? I suggest enabling TraceOrders in State.SetDefaults and then monitoring the NinjaScript Output window to see what price the rejected order was submitted at when it gets rejected. This could help you to better understand the error, such as if the order is being submitted at a price very close to the market, which could result in the order being submitted, then a large price movement before the order is received by your broker such that the submitted price is now on the incorrect side of the market, resulting in the order being rejected. Typically a way to prevent this is to use wider stops.

                More information about using prints and TraceOrders for debugging may be found in the following help article:


                As for the strategy being disabled when an order is rejected, that is due to the default RealtimeErrorHandling logic. You could modify this logic to ignore rejects or ignore all errors then set your own custom logic to handle rejected orders and other errors. This is an advanced concept for strategy development. More information may be found here:


                Please let us know if we may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Good afternoon,
                  I do not have any other TP or SL at all other than: ( not initials at all)

                  Code:
                   //trying my hand at ATM (trailing SL)
                  if(Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
                  {
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 1000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 2000)
                  {
                  Print("HIT 1k - moving to BE " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                  }
                  
                  
                  if( Position.MarketPosition == MarketPosition.Long)
                  {
                  
                  // if( AccountUnrealizedPL >= 2000 && AccountUnrealizedPL <=3000 )
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 2000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 3000)
                  {
                  Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, 1000);
                  }
                  
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 3000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 4000)
                  
                  {
                  Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, 2000);
                  }
                  
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 4000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 5000)
                  {
                  Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, 3000);
                  }
                  
                  }//end if the market is long
                  
                  else if (Position.MarketPosition == MarketPosition.Short)
                  {
                  
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 2000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 3000)
                  {
                  Print("HIT 2k - moving to 1k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, -1000);
                  }
                  
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 3000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 4000)
                  
                  {
                  Print("HIT 3k - moving to 2k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, -2000);
                  }
                  
                  if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= 4000 && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 5000)
                  {
                  Print("HIT 4k - moving to 3k " + AccountUnrealizedPL);
                  
                  SetStopLoss(CalculationMode.Currency, -3000);
                  }
                  
                  }//end if the market is short
                  }
                  ​​

                  the rest of the code is enterLong/Short and ExitLong/Short...

                  I will look at the output window and provide feed back on it..

                  thanks again

                  Comment


                    #10
                    Is there a way not to set up an initial SL and TP and still use my code to set one once 1k in profit and then move it?

                    Comment


                      #11
                      Originally posted by Lele2k24 View Post
                      Is there a way not to set up an initial SL and TP and still use my code to set one once 1k in profit and then move it?
                      This would require the use of Exit() orders rather than Set() methods, like SetStopLoss(). The Set() methods are intended to be called before entering a position so that they are "loaded up" and ready to trigger as soon as the entry is executed. If you'd like to have a protective order added only after 1000 in profit is hit, you would need to remove the Set() methods and instead use one of the Exit() methods to configure your desired exit order. The order methods available are listed in the help guide here:


                      Please feel free to reach out with any additional questions or concerns.
                      Emily C.NinjaTrader Customer Service

                      Comment


                        #12
                        thank you. Is there an example of using the exit as a stoploss instead of just closing the order right away? The goal is to have a stoploss if the price goes back into the entry not just close out the order the a take profit of 1000

                        as always.. thank you for all this.. I am learning a great deal!

                        Comment


                          #13
                          Originally posted by Lele2k24 View Post
                          thank you. Is there an example of using the exit as a stoploss instead of just closing the order right away? The goal is to have a stoploss if the price goes back into the entry not just close out the order the a take profit of 1000

                          as always.. thank you for all this.. I am learning a great deal!
                          The following example demonstrates using the OnOrderUpdate() and OnExecutionUpdate() methods to send Exit() orders as protective orders rather than using the Set() methods:


                          Thank you for your time and patience.
                          Emily C.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you. That did it.

                            Comment


                              #15
                              Hello,

                              I'm looking for some answer... I have a strategy with some tp and SL but sometime i don't understand why my stratégie shut down.... (order rejected)

                              You will find on attachement my code and just below my output rapport...

                              " vendre
                              RR is : 1,1395627804859
                              Close[0] is : 18338,25
                              TP is : 18310,9004932683
                              SL is : 18362,25
                              08/04/2024 15:28:05 Strategy 'Renkoko/322161493': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Price Value=18310,9004932683 IsSimulatedStop=False IsMarketIfTouched=False
                              08/04/2024 15:28:05 Strategy 'Renkoko/322161493': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=18362,25 IsSimulatedStop=False IsMarketIfTouched=False
                              08/04/2024 15:28:05 Strategy 'Renkoko/322161493': Entered internal SubmitOrderManaged() method at 08/04/2024 15:28:05: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Vente' FromEntrySignal=''
                              Strategy 'Renkoko/322161493' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
                              08/04/2024 15:28:06 CancelAllOrders: BarsInProgress=0
                              Disabling NinjaScript strategy 'Renkoko/322161493'
                              Enabling NinjaScript strategy 'Renkoko/322161493' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 300 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes"

                              If anaybody have an idea i will be really grateful, because i'm looking for since 2 day..


                              Have a nice day

                              PS : Sorry about my english, i'm french ​
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,406 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              98 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X