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

UnManaged Orders

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

    UnManaged Orders

    Hello,

    I am using an Unmanaged Approach
    The compilation failed on this...... NinjaTrader.Cbi.IExecution does not contain a definition for OrderState...

    if (longOrder != null && longOrder == execution.Order && execution.OrderState == OrderState.Filled) // we need a short position Stop loss
    //(Position.MarketPosition == MarketPosition.Long && stopOrder == null) // not a call after Stop Loss has been created
    {

    stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice +1 * TickSize, tickPrice +1 * TickSize, "entry", "Short limit entry");


    }

    #2
    cocopod, please try with the execution.Order.OrderState here instead.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      ok... that compiled

      Hi Bertran,

      Thanks ...that compiled. Still a few problems though

      Consider this. In my "OnMarketData" section I create a bracket order. Please refer to code part 1. This works

      In the OnExecution section I will create a corresponding Stop Loss for the Long or short that was filled or if the Stop Loss was actually activated I would then null the holders for the long and short position and then hopefully back up to OnMarketData and recreate the Bracket. Please refer to Code part 2.. Not sure if it works








      *************** CODE PART 1 ***********************

      protected override void OnMarketData(MarketDataEventArgs e)
      {
      if
      ( longOrder == null || shortOrder == null)
      {
      // Create the Bracket on Initialization
      // Automatically recreates a Bracket when the previous one has run its course!!!

      longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +23 * TickSize, GetCurrentBid() +23 * TickSize, "Gold", "long limit entry");
      shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -23 * TickSize, GetCurrentBid() -23 * TickSize, "Gold", "Short limit entry");
      timer.Elapsed += OnTimedEvent;
      timer.Start();
      }

      }



      *************** End of CODE PART 1 ***********************




      *************** CODE PART 2 ***********************


      protected override void OnExecution(IExecution execution)
      {
      /* Ok.. we have an order
      1 ) is it long or short?
      2 ) Create corresponding Stop Loss
      3 )
      4 ) Has a Stop Loss been executed? We need to Close orders
      */


      pTickPrice = Close[0]; // Keep a record of the Execution TickPrice


      // bh 10-11 (1) Long or Short??
      // Print(execution.ToString());
      if
      (longOrder != null && longOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

      {
      // (2)
      stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice -2 * TickSize, tickPrice -2 * TickSize, "Gold", "Short limit entry");
      }

      else if (shortOrder != null && shortOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

      {
      // (2)
      stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, tickPrice +2 * TickSize, tickPrice +2 * TickSize, "Gold", "Stop loss short");
      }


      // (4) if stopOrder is executed then the long / short position must have been closed

      else if
      ( Position.MarketPosition == MarketPosition.Flat)

      {
      shortOrder = longOrder = null;
      }


      }
      *************** End of CODE PART 2 ***********************

      Comment


        #4
        Glad to hear it compiled, what's the issue with your code from testing? The nullifying of the orders I would not tied to the position state though but to the stop order execution, as the execution will always be seen before the Position update event.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Stop Loss and other things

          Hi Bertrand,

          Thanks for your response. To put things into perspective I have only been using NinjaScript for 4 days now ..oh.. and 26 minutes and therefore can be considered dangerous.

          First problem is that the StopLoss does not seem to be created properly -if at all.

          Second Problem.. when the Stop Loss is activated and all market positions are flat we do not create a new bracket.

          Please help

          Comment


            #6
            Then you would need to be cautious working in the most advanced area of order submission, the unmanaged environment. It was created for allowing ultimate flexibility, however there's less background checks done by NT as in managed.

            The bracket is probably not created since your IOrder objects are not nullified, only a debug print added could provide the needed details.

            For the stops, they seem to differ in name and stop type order, is that expected?

            'the StopLoss does not seem to be created properly -if at all.'

            I'm not sure what you exactly mean here, so you see only some positions getting a stop order sent by your order? Or do you see rejections or an unexpected price?

            Generally when debugging strategies the TraceOrders feature is very helpful.

            BertrandNinjaTrader Customer Service

            Comment


              #7
              TraceOrder

              Bertrand,

              I pasted part of the Output file below. Can send the entire thing if you wish.

              The Bracket Pending order was created
              The Position was filled
              The Stop Loss was created and eventually filled at what appears to be the creation level. It did not appear to move
              Another Bracket trade was created after the order was closed however its action was not as what I would have expected

              Changing unmanaged order at 14/11/2014 16:17:28: Order='3567b28b099f4767b696856b35a62f3f/Sim101' Name='Short limit entry' State=PendingCancel Instrument='CL 12-14' Action=SellShort Limit price=74.87 Stop price=74.84 Quantity=1 Strategy='TESTprog' Type=StopLimit Tif=Gtc Oco='Oil' Filled=0 Fill price=0 Token='3567b28b099f4767b696856b35a62f3f' Gtd='01/12/2099 00:00:00'
              14/11/2014 16:17:28 Entered internal SubmitOrder() method at 14/11/2014 16:17:28: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=74.89 StopPrice=74.89 OcoId='Oil' Name='Short limit entry'
              **NT** Strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
              14/11/2014 16:17:28 CancelAllOrders: BarsInProgress=0
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
              14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87

              Comment


                #8
                Hello cocopod,

                Thank you for your response.

                You have a stop order that is running the same name as the short entry, for the purpose of debugging I suggest changing the name of your long positions stop loss to something else.

                Your stop loss for a long position:
                Code:
                stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice -2 * TickSize, tickPrice -2 * TickSize, "Gold", "[B]Short limit entry[/B]");
                In addition, would it be possible to view the code in full?

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by popecapllc, 08-09-2023, 07:42 PM
                9 responses
                1,362 views
                0 likes
                Last Post BartMan
                by BartMan
                 
                Started by ETFVoyageur, 04-30-2024, 02:04 PM
                11 responses
                101 views
                0 likes
                Last Post ETFVoyageur  
                Started by bubblegum, 03-18-2024, 10:41 AM
                3 responses
                46 views
                0 likes
                Last Post vjsworld  
                Started by JamesK1, Today, 02:48 PM
                1 response
                13 views
                0 likes
                Last Post JamesK1
                by JamesK1
                 
                Started by llanqui, Today, 03:51 PM
                0 responses
                13 views
                0 likes
                Last Post llanqui
                by llanqui
                 
                Working...
                X