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

SubmitOrderUnmanaged runs the first time but not the second time...

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

    SubmitOrderUnmanaged runs the first time but not the second time...

    Hello,

    I am using the Unmanaged approach but I am facing a bit of a struggle.
    I am testing a strategy on historic prices (not realtime)
    I am debugging with Visual studio my script

    For some reason, the first call to SubmitOrderUnmanaged works.
    however the second call does not (using F11 to debug).

    I used CancelOrder() to cancel the first two orders and checked they were null.

    When consition to submit new trades is met, the debugger does go onto the line calling for the order but does not actually do anything.

    OCO string is different on every call.

    Can you tell me if there is something special i should do ?

    #2
    Sorry, I forgot to say "thanks "

    Comment


      #3
      Hello guilhem,

      Thank you for the post.

      If you have a small sample that shows the logic you used that would be helpful, we can't really do anything with the information you observed in visual studio but we could review a small code sample to see if anything is out of place.

      You can also try using an existing unmanaged script to see if that works, there is one posted in this thread: https://ninjatrader.com/support/foru...269#post802269


      I look forward to being of further asssitance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Hello Jesse,
        I think i understand what s wrong, the unmanaged order starts working at the begining of the current bar (historic prices) rather than next bar like the managed ones...
        Is this right ?

        Best

        Comment


          #5
          Hello guilhem,

          That would depend on what you coded, if you mean for fills the same fill engine is being used so you should see similar results for filling. The prices could differ because you are submitting orders on your own without the managed approach and depending on the order type you may use a price you provide. From your provided details I don't have a lot of context of whats being done to know what should be happening.

          Starting to work at the beginning of a bar would require tick granularity and to use IsFirstTickOfBar, is that what you are referring to here?


          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hello Jesse,

            Here is the beast...

            protected override void OnBarUpdate()
            {
            if ((BarsInProgress == 0) && (CurrentBar > 50))
            {
            if ((longStopEntry != null) || (shortStopEntry != null))
            {
            if (((longStopEntry != null) && (longStopEntry.OrderState == OrderState.Filled)) &&
            ((shortStopEntry != null) && (shortStopEntry.OrderState != OrderState.Cancelled)))
            SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.Market, 1);
            if (((longStopEntry != null) && (longStopEntry.OrderState != OrderState.Filled)) && ((shortStopEntry != null) &&
            (shortStopEntry.OrderState == OrderState.Cancelled)))
            SubmitOrderUnmanaged(1, OrderAction.BuyToCover, OrderType.Market, 1);
            }
            orderOn = false;
            tradeLong = true;
            tradeShort = true;

            if ((tradeLong) || (tradeShort))
            {
            stopPriceLong = High[0] - 2 * TickSize;
            stopPriceShort = Low[0] + 2 * TickSize;
            orderOn = true; // only sort out positions if orderOn
            }
            }
            else if(orderOn)
            {
            if (ToTime(Time[0]) == 190000)
            return;
            else if (ToTime(Time[0]) == startTime) // derniere barre de la serie en minute
            {
            ocoString = string.Format("unmanagedOrder {0}", DateTime.Now.ToString("hhmmssffff"));
            if (tradeLong)
            SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.StopMarket, 1, 0, stopPriceShort, ocoString, "longStopEntry");
            if (tradeShort)
            SubmitOrderUnmanaged(1, OrderAction.SellShort, OrderType.StopMarket, 1, 0, stopPriceShort, ocoString, "shortStopEntry");
            }
            else if ((ToTime(Time[0]) > 80100) && (ToTime(Time[0]) >= 150000))
            {
            orderOn = false;
            if ((longStopEntry != null) && (longStopEntry.OrderState == OrderState.Working))
            CancelOrder(longStopEntry);
            if ((shortStopEntry != null) && (shortStopEntry.OrderState == OrderState.Working))
            CancelOrder(shortStopEntry);
            }
            }
            }

            main dataserie modified to be from 08:00:00 AM till 18:59:59 (1440 minutes)
            secondary dataserie is 1 minute during same session.

            I am trying to sell short previous low + 2 ticks and/or buy previous high - 2 ticks
            if only one trade happened, i exit on close (19.00 bar)
            if nothing happened before 15.00 then cancel orders
            if two trades happened then do nothing on the close

            I thought that would be pretty easy but seems to be a massive headache for me

            Best

            Comment


              #7
              Hello guilhem,

              Thanks for providing more detail.

              Were you able to try the other unmanaged sample that I had linked to? That would be helpful here to see if the problem relates to your logic or the other parts not provided in this sample.

              One item I can see is that you are checking if the time equals a specific time. Are you certain that your condition is being called? Have you tried adding prints into these areas to check if that logic was called again?

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I will try, meanwhile, I am getting this

                ------------
                Strategy 'ModOrderGeneration/216413763': An order has been ignored since the stop price ‘41.64’ near the bar stamped ‘12/11/2020 08:01:00’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.

                Enabling NinjaScript strategy 'ModOrderGeneration/216413763' : On starting a real-time strategy - StartBehavior=ImmediatelySubmit Position=CL 12-20 1L, CL 12-20 1L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On price change IsUnmanaged=True MaxRestarts=4 in 5 minutes
                ------------


                not quite sure what those messsages are meaning... will try to debug later today.
                could you tell me how to see error message sent back by the SubmitOrderUnmanaged ?

                strategy attached should run on a chart where session is as per screen capture

                best

                Attached Files

                Comment


                  #9
                  Hello guilhem,

                  Thank you for the reply.

                  It looks like you located the reason why the order is not appearing, the order is being ignored based on the price used. You are seeing what is being reported back from the submit order in this case. You could take a look at the prices you are using and calculating at that time to get a better idea of why that error would be generated. You can also take a look at the following forum post, it looks like you are trying to bracket entry which the following post has a complete sample of: https://ninjatrader.com/support/foru...579#post770579

                  Please let me know if I may be of additional assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    thanks
                    my algo is full of errors i just realised ...

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by puapwr, Today, 06:09 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post puapwr
                    by puapwr
                     
                    Started by franciscog21, Today, 05:27 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post franciscog21  
                    Started by hdge4u, 04-29-2024, 12:23 PM
                    5 responses
                    28 views
                    0 likes
                    Last Post MisterTee  
                    Started by llanqui, 01-25-2024, 06:15 PM
                    9 responses
                    65 views
                    0 likes
                    Last Post MisterTee  
                    Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
                    16 responses
                    67 views
                    0 likes
                    Last Post tradingnasdaqprueba  
                    Working...
                    X