Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetProfitTarget() and SetStopLoss() not working

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

    SetProfitTarget() and SetStopLoss() not working

    When running a strategy in real time, the strategy completely ignored my TP and SL

    #2
    Hello samish18,

    May I confirm SetProfitTarget() and SetStopLoss() being called before the entry order method and not after?

    Enable TraceOrders and print the order.ToString() in OnOrderUpdate().

    This will let us know what is happening with the orders.

    Below is a link to a support article on using prints and TraceOrders to understand behavior.


    Save the output to a text file (right-click the output window, select Save as)
    Attach the output text file to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      SetProfitTarget() and SetStopLoss() are called in the State.Configure, and seem to execute sometimes, but not consistently. I therefore run into the issue where the strategy takes a position and that position will remain open indefinitely

      Comment


        #4
        Hello samish18,

        I look forward to receiving the requested debugging output and assisting further.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Attached is the output. I had to shorten it quite a bit as I am running on a 1 tick chart. The strategy entered at 3:54:40 (per the execution tab) and then went past both the take profit and stop loss in the next minute (no take profit or stop loss were evidently in place)
          Attached Files

          Comment


            #6
            Hello samish18,

            It appears this strategy is attempting to submit an order thousands of times in the span of a few seconds, but is not able due to the entries per direction and re-using signal names.

            That said, it looks like the stop order is being submitted and then immediately cancelled.

            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=Submitted instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:40' gtd='2099-12-01' statementDate='1800-01-01'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=CancelPending instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:41' gtd='2099-12-01' statementDate='2024-10-10'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=CancelPending instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:41' gtd='2099-12-01' statementDate='2024-10-10'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=Working instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:40' gtd='2099-12-01' statementDate='1800-01-01'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=CancelPending instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:41' gtd='2099-12-01' statementDate='2024-10-10'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=CancelSubmitted instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:40' gtd='2099-12-01' statementDate='1800-01-01'
            orderId='131299008318' account='DEMO3680918' name='Stop loss' orderState=Cancelled instrument='NQ DEC24' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=20443.5 quantity=1 tif=Gtc oco='131299008122_1' filled=0 averageFillPrice=0 onBehalfOf='' id=149 time='2024-10-10 15:54:40' gtd='2099-12-01' statementDate='1800-01-01'

            So something in the logic is causing the entry order method to be called over and over again on each new tick and something is causing the stop order to be cancelled when submitted.

            May I have an export of the script to investigate further?

            To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
            1. Click Tools -> Export -> NinjaScript Add-on...
            2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
            3. Click the 'Export' button
            4. Enter the script name in the value for 'File name:'
            5. Choose a save location -> click Save
            6. Click OK to clear the export location message
            By default your exported file will be in the following location:
            • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
            Below is a link to the help guide on Exporting NinjaScripts.


            Once exported, please attach the file as an attachment to your reply.​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The strategy is attached
              Attached Files

              Comment


                #8
                Hello samish18,

                The script appears to be attempting to cancel an order and submit a new order using the same signal name on every tick.

                I suspect that attempting to cancel the order at the same time the order is filling is causing the stop and target to be cancelled.

                If you remove the call to CancelOrder() can the behavior be reproduced?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  When I remove it, I eventually get the error: "Strategy submitted an order that generated the following error 'Unable to submit order'. Strategy has sent cancel requests, attempted to close the position and terminated itself" and "Order can't be submitted: order status is cancelPending."

                  Comment


                    #10
                    Hello samish18,

                    To confirm, you have only removed line 62, is this correct?

                    Try removing the instance of the strategy from the chart in the Available list of the Strategies window, and add a new instance from the Configured area and enable this.

                    Does the error still occur?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Yes, I only removed line 62 and that is exactly what I did

                      Comment


                        #12
                        Hello samish18,

                        Are you able to place a manual order to the account?

                        This appears to be tested on a live demo, is this correct?

                        Can you reproduce using the Sim101 account or in Playback?

                        If so, may I test the script in it's current state?

                        Please provide an export.

                        To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                        1. Click Tools -> Export -> NinjaScript Add-on...
                        2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                        3. Click the 'Export' button
                        4. Enter the script name in the value for 'File name:'
                        5. Choose a save location -> click Save
                        6. Click OK to clear the export location message
                        By default your exported file will be in the following location:
                        • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                        Below is a link to the help guide on Exporting NinjaScripts.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          The strategy appears to work for a bit (it enters and exits several orders before getting stuck). I am slightly confused: what would you like me to export?

                          Comment


                            #14
                            Hello samish18,

                            To confirm, the orders are getting stuck testing the Sim101 account or on the Playback101 account?
                            (I suspect you may having high latency causing the orders to not be transmitted to the live demo account)

                            I was not able to confirm, you are able to place manual stop orders and these are becoming working without issue?

                            I would like to you export the NinjaScript. Tools > Export > NinjaScript.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              It is a demo account. I am able to manually place orders, and the strategy is able to enter and exit orders, until one gets stuck.
                              I already exported the ninjascript in message 7.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              50 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              68 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X