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 being finalized before reaching the second target

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

    Strategy being finalized before reaching the second target

    Hello good afternoon, I need some help if possible. The following error is occurring as per the attached image, after reaching the 1st target the operation is completed and there is still 1 contract sold. It should wait to reach the 2nd target or return to the Stop to end the operation. I've also included the code in case it makes the logic easier to see.
    Attached Files

    #2
    Hell WenisCR,

    Thank you for your post.

    Based on your screenshots alone, I am not able to understand what is happening. I see there are two contracts at the Buy STOP order for 10383.50 along with two profit Buy LMT orders. It appears that one contract was filled at 13078 and then the other order is potentially canceled.

    So I may accurately assist you, please answer all of the following questions:
    • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
    • Who are you connected to? This is displayed in green on the lower-left corner of the Control Center window.
    • Who is your broker?
    • What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
    • (For a chart) what interval is selected? For example, 5 minute, 1 day, 2000 volume, 4 Renko, etc.
    • Is this a script that you developed or was it imported from a third party?
    • Do you see a canceled order on the Log tab of the Control Center?
    • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?

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

    Comment


      #3
      Hi Emily, how are you doing?

      I put the code in a .txt file and attached it to this post so you can see the logic, it's quite simple.


      I put the code below so you can see the logic, it's something very simple. When an entry signal occurs, a buy or sell order is sent with 2 Mini NQ contracts, Stop is sent at the previous close and target1 and target2 are also sent. There is no failure in the log, I believe it is some missing or wrong parameter in the code, because as soon as target1 is successfully reached, the operation is finished and this could not happen, it should wait to reach the second target or return to Stop as shown in the image attached. Could you please check the attached code?
      Thanks for the help once again.​


      What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
      (Version 8.0.27.1 64-bit )
      Who are you connected to? This is displayed in green on the lower-left corner of the Control Center window.
      (Yes, connected.)
      Who is your broker?
      (ActiveTrades)
      What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
      (NQ 06-23)
      (For a chart) what interval is selected? For example, 5 minute, 1 day, 2000 volume, 4 Renko, etc.
      (5min)
      Is this a script that you developed or was it imported from a third party?
      (I am developing.)
      Do you see a canceled order on the Log tab of the Control Center?
      (In log, we have no error and the request is not cancelled, the operation is finished.)
      Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
      (No bugs or glitches.)


      Comment


        #4
        Hello WenisCR,

        Thank you for your reply.

        In the future, to share your scripts with us in the forums it is more friendly to export it by going to Control Center > Tools > Export > NinjaScript Add On. Select your script, then once it is successfully exported you may attach the .zip file to your post here in the forum. This allows us to see it as the .cs file and also import it into the platform on our side if needed.

        That said, I am seeing that you utilize order objects targerOrderShort1 and targetOrderShort2 in your strategy. I suggest reviewing the conditions that define these objects as well as when they are set to null. You can add print statements throughout your script to better understand the behavior. For more information about using prints to debug and understand your script:


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

        Comment


          #5
          Hi Emily,
          Sorry for attaching incorrectly.

          Here is the attachment as requested, yes I have already checked the points you reported, I need help if possible.
          Could you check the code?​

          Comment


            #6
            Hello WenisCR,

            Thank you for you reply.

            Please try enabling TraceOrders to see if there are any messages that help to understand what is happening. To enable TraceOrders, go to OnStateChange() in your script and when State == State.SetDefaults set TraceOrders to true. Then, review the NinjaScript Output window and observe the output regarding your entry and exit orders. For more information about TraceOrders:



            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Hello,

              I was analyzing, I'll try to exemplify and maybe you can help me.
              When target 1 is reached, the Stop and target 2 are also being finalized, as they are referencing ShortEntry​.

              Would you know how I can add this target 1 and target 2 and when target 1 is reached, not finish the Stop and target 2?


              11/04/2023 11:35:32 Strategy '286639487/Setup123PFR: Cancelled pending exit order, since associated position is closed,

              In this case of sale:

              EnterShortStopMarket(0, true, Convert.ToInt32(Qtd), Low[0] - (1*TickSize), "ShortEntry");

              protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
              {
              if (ShortOrder != null && ShortOrder == execution.Order)
              {
              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
              {

              if (execution.Order.OrderState == OrderState.PartFilled)
              {
              //targetOrder = ExitShortLimit(0, true, execution.Order.Filled, alvoShort, "MyTarget", "ShortEntry");
              //stopOrderShort = ExitShortStopMarket(0, true, sumFilled, execution.Order.AverageFillPrice + (StopTicks * TickSize), "MyStopShort", "ShortEntry");
              stopOrder = ExitShortStopMarket(0, true, (sumFilled), MyStopShort, "MyStopShort", "ShortEntry");
              targetOrder = ExitShortLimit(0, true, (sumFilled/2), execution.Order.AverageFillPrice - (AlvoTicks1 * TickSize), "MyTarget1", "ShortEntry");
              targetOrder = ExitShortLimit(0, true, (sumFilled/2), execution.Order.AverageFillPrice - (AlvoTicks2 * TickSize), "MyTarget2", "ShortEntry");
              }

              else
              if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
              {
              //targetOrder = ExitShortLimit(0, true, execution.Order.Filled, alvoShort, "MyTarget", "ShortEntry");
              //stopOrderShort = ExitShortStopMarket(0, true, sumFilled, execution.Order.AverageFillPrice + (StopTicks * TickSize), "MyStopShort", "ShortEntry​");
              stopOrder = ExitShortStopMarket(0, true, (sumFilled), MyStopShort, "MyStopShort", "ShortEntry");
              targetOrder = ExitShortLimit(0, true, (sumFilled/2), execution.Order.AverageFillPrice - (AlvoTicks1 * TickSize), "MyTarget1", "ShortEntry​");
              targetOrder = ExitShortLimit(0, true, (sumFilled/2), execution.Order.AverageFillPrice - (AlvoTicks2 * TickSize), "MyTarget2", "ShortEntry​");
              }

              if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
              {

              ShortOrder = null;
              sumFilled = 0;
              }
              }
              }​​​
              Attached Files

              Comment


                #8
                Hello WenisCR,

                Thank you for your reply.

                To prevent this from happening, it would be best to use separate, uniquely named entry orders to match up with each separate stop and target order. For example, ShortEntry1 and ShortEntry2 rather than just one ShortEntry order.

                When scaling in and scaling out, it is best to use unique signal names and separate orders for each quantity that will be scaled.

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

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Segwin, 05-07-2018, 02:15 PM
                14 responses
                1,789 views
                0 likes
                Last Post aligator  
                Started by Jimmyk, 01-26-2018, 05:19 AM
                6 responses
                838 views
                0 likes
                Last Post emuns
                by emuns
                 
                Started by jxs_xrj, 01-12-2020, 09:49 AM
                6 responses
                3,294 views
                1 like
                Last Post jgualdronc  
                Started by Touch-Ups, Today, 10:36 AM
                0 responses
                13 views
                0 likes
                Last Post Touch-Ups  
                Started by geddyisodin, 04-25-2024, 05:20 AM
                11 responses
                63 views
                0 likes
                Last Post halgo_boulder  
                Working...
                X