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 Order

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

    #16
    Hi Michael,

    Sorry I was out of the office yesterday .

    I have now got it to work using separate stoplosses and targets for each (both long and short) order. ( I used the OCO function in submiOrder too, which is very useful!)

    I now have my full code up and running.

    However, I can only do what I want to do if I do this off a fixed instrument open, as I am using:


    longEntry = CurrentDayOHL().CurrentOpen[0] + ('x' *TickSize);


    Let's say I want to have my 'open' price at 10:05 (something completely random), what could I write that would allow me to have the first tick at that time as the reference/open price for my longEntry and shortEntry?


    Thanks as always

    Comment


      #17
      Hello,

      Thank you for the question.

      For an arbitrary open time, you would need to use a secondary series.

      I believe what you would likely need to do would be add a 1 tick series using an Add() statement and then utilize BarsInProgress index to find the First tick after the specified time. I see that Michael has provided a sample of using BarsInProgress and also adding a 1 tick series in post #2: http://ninjatrader.com/support/forum...41&postcount=2

      The existing condition would basically be what you need:

      Code:
      if(BarsInProgress == 1 && FirstBarOfSession)
      You would need to add a Time check to this condition instead:

      Code:
      if(BarsInProgress == 1 && ToTime(Times[1][0]) == 100500)
      If there is a tick which time exactly equals 10:05:00, this would trigger the condition where you could set any variable needed to the price of 10:05. You could also do a range of time to ensure there is a tick or:

      Code:
      if(BarsInProgress == 1 && (ToTime(Times[1][0]) >= 100500 || ToTime(Times[1][0]) <= 100559))
      This would check from 10:05:00 to 10:05:59

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

      Comment


        #18
        Hello again

        I have been trying to write the code for the break of the first 60 mins, in any session (unmanaged again) with certain additions.

        Assuming the open of the session is at 0700, I would like to place stop entry orders 1 tick either side of the high and low made before 0800. I would like to only enter one trade per day. I would also like the code to work on any time frame, ie from 1 tick bars to 1 minute bars. A further condition for the orders to be submitted is that a variable (var) must be less than 15 ticks.

        Another issue I am having is cancelling orders. Let's say that a short stop entry is submitted. I would like to pull that order if the 60 minute high is broken before executed.

        At the moment all sorts of things are happening: trades are being entered when var > 15 ticks (but not always), sometimes stops are being triggered on the open, sometimes orders are not being submitted (something to do with how i am entering them with my current code:

        ToTime(Time[0]) > ToTime(8, 0, 0) && ToTime(Time[0]) < ToTime(8, 2, 0) && var < 15).


        Thank you in advance and look forward to hearing from you

        Comment


          #19
          I forgot to mention that the stop losses of any orders would be the other side of the 60 min bar...


          Also, while on this topic, what would I need to do for a strategy which trades more than once a day, entering positions when the previous position is stopped out?

          Ie. If we get long at the high of the first 60 mins + 1 and then get stopped out at the lo - 1, I would like to get short at the same level with a stop at the 60 min high + 1, and so on and so on until exit on close....


          Thanks so much again

          Comment


            #20
            This is my code:

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {

            if(ToTime(Time[0]) > ToTime(8, 00, 0) && ToTime(Time[0]) < ToTime(8, 02, 00) && var1 < 10)

            {

            longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentHigh[0] + (1*TickSize), First607amOHLC().CurrentHigh[0] + (1*TickSize), "Long", "longOrder");

            }

            else
            if(ToTime(Time[0]) > ToTime(8, 00, 00) && ToTime(Time[0]) < ToTime(8, 02, 00) && var2 < 10)

            {
            shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentLow[0] - (1*TickSize), First607amOHLC().CurrentLow[0] - (1*TickSize), "Short", "shortOrder");
            }


            else

            if(longOrder != null && CurrentDayOHL().CurrentLow[0] < First607amOHLC().CurrentLow[0])
            {
            CancelOrder(longOrder);
            }



            else
            if(shortOrder != null && CurrentDayOHL().CurrentHigh[0] > First607amOHLC().CurrentHigh[0])
            {
            CancelOrder(shortOrder);
            }


            }


            protected override void OnExecution(IExecution execution)
            {
            if ( execution.MarketPosition == MarketPosition.Long
            && execution.Order != null
            && execution.Order.Filled > 0 )
            {
            double stop = First607amOHLC().CurrentLow[0] - (1*TickSize);

            if ( shortOrder != null )
            CancelOrder(shortOrder);
            shortOrder = null;

            SubmitOrder(0, OrderAction.Sell, OrderType.Stop, DefaultQuantity, stop, stop, "Exit", "LongStop");
            }

            else
            if ( execution.MarketPosition == MarketPosition.Short
            && execution.Order != null
            && execution.Order.Filled > 0 )
            {
            double stop = First607amOHLC().CurrentHigh[0] + (1*TickSize);

            if ( longOrder != null )
            CancelOrder(longOrder);
            longOrder = null;

            SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, DefaultQuantity, stop, stop, "Exit", "ShortStop");

            }

            Sometimes it enters trades even when var1 or var2 are greater than 10 (very rarely) and sometimes it doesn't enter trades at all, and sometimes it enters loads of trades and only stops one out, the rest exit on close.

            I thought this might help you see where the problem lies...

            Comment


              #21
              Hello Griberit,

              I recommend implementing some counters to track how many open orders you have. It sounds like your open orders are racking up much quicker than your var 1 and var 2 variables and therefore you are ending up in more positions that you want.

              The code you provided does not show what is going on with your var 1 and var 2 variables. I would need to see this part of the code as well to further investigate.

              Essentially, right now the following part of your code:
              Code:
              if(ToTime(Time[0]) > ToTime(8, 00, 0) && ToTime(Time[0]) < ToTime(8, 02, 00) && var1 < 10)
              
              {
              
              longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentHigh[0] + (1*TickSize), First607amOHLC().CurrentHigh[0] + (1*TickSize), "Long", "longOrder");
              
              }
              
              else
              if(ToTime(Time[0]) > ToTime(8, 00, 00) && ToTime(Time[0]) < ToTime(8, 02, 00) && var2 < 10)
              
              {
              shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentLow[0] - (1*TickSize), First607amOHLC().CurrentLow[0] - (1*TickSize), "Short", "shortOrder");	
              }
              Allows for an indefinite number of orders to be submitted between 8:00 and 8:02. You are missing the logic to handle your condition:
              I would like to only enter one trade per day.
              I recommend trying the following to start:
              Code:
              if(longOrder == null && ToTime(Time[0]) > ToTime(8, 00, 0) && ToTime(Time[0]) < ToTime(8, 02, 00) && var1 < 10)
              
              {
              
              longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentHigh[0] + (1*TickSize), First607amOHLC().CurrentHigh[0] + (1*TickSize), "Long", "longOrder");
              
              }
              
              else
              if(shortOrder == null && ToTime(Time[0]) > ToTime(8, 00, 00) && ToTime(Time[0]) < ToTime(8, 02, 00) && var2 < 10)
              
              {
              shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, DefaultQuantity, First607amOHLC().CurrentLow[0] - (1*TickSize), First607amOHLC().CurrentLow[0] - (1*TickSize), "Short", "shortOrder");	
              }
              and retesting to see if the behavior has changed.

              Please let me know if I may be of further assistance.
              Michael M.NinjaTrader Quality Assurance

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by cls71, Today, 04:45 AM
              0 responses
              1 view
              0 likes
              Last Post cls71
              by cls71
               
              Started by mjairg, 07-20-2023, 11:57 PM
              3 responses
              213 views
              1 like
              Last Post PaulMohn  
              Started by TheWhiteDragon, 01-21-2019, 12:44 PM
              4 responses
              544 views
              0 likes
              Last Post PaulMohn  
              Started by GLFX005, Today, 03:23 AM
              0 responses
              3 views
              0 likes
              Last Post GLFX005
              by GLFX005
               
              Started by XXtrader, Yesterday, 11:30 PM
              2 responses
              12 views
              0 likes
              Last Post XXtrader  
              Working...
              X