• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Keep Orders Valid for X Bars

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

    Keep Orders Valid for X Bars

    Hi,

    I am learning to code in Ninja. I am devising a strategy which enters a trade based on the closing of last candle Close[0]. I am using Managed Approach and code is unlocked and I am editing the strategy. I am using only one primary series on the chart . fyi

    I am aware that , in the next candle(current), if the placed order is not filled, order gets cancelled. I would like the order to be valid for next 2 (or 3) candles.
    I would like to be able to control how many bars order should stay valid.

    I have written the attached code , but its not working . Can someone please point out what mistakes I am doing and possibly assist in correcting the code.

    Thanks,
    Nag
    Attached Files

    #2
    Hello ebnag,

    Thanks for your post.

    In the managed approach, orders that are not filled are automatically canceled. You have two choices on how to proceed, one is to use the advanced order method which allows you to place the order with a bool to specify IsLiveUntilCanceled. Please see the help guide for whichever entry order method you are using and it will detail the advanced order syntax for that order. The other is to simply resubmit the order for as many bars in a row as you wish. Here is a simple pseudo code example:

    if (your condition to enter && doitonce == true)
    {
    placetheorder = true; // use a bool to trigger that we want to place an order
    doitonce = false; // reset this bool so we only do this once per order condition
    savedBar = CurrentBar //save the bar number we are starting at
    }

    if (placetheorder == true && CurrentBar - savedBar < 3 && Position.MarketPosition == MarketPosition.Flat)
    {
    // place actual order here if less than 3 bars and we are flat.
    }


    You would also need code to reset the bools for the next order block.

    Note: I see that you have created two different threads for what appears to be the same relative question of keeping an order valid after the initial submittal. We ask that you only post once to avoid any confusion. I will provide a link in your other thread to this response.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the update, Paul. At the moment, I have not used sLiveUntilCanceled. I tried to code it using same example you provided above. Please refer to the code attached to my original request. Only difference is , I am not using condition - Position.MarketPosition == MarketPosition.Flat, as there could be an open order in opposite direction and in that case, I expect the code to close open position and submit a new order , based on current trigger. If I add this condition, since its not flat, I think it wont submit the order.

      You gave me above code sample when i contacted ninja platform support. Thank you. However, when I did the actual coding, its not working, as expected. Its obvious, I am missing something

      For this reason, I posted the code so that some one may help in correcting the code.

      Thank you
      Nag

      Comment


        #4
        Hello ebnag,

        Thanks for your reply.

        I will review and response but it will not be until Monday as I am at the end of my day here.

        Thanks in advance for your patience.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul, I will gladly wait.

          Thanks,
          Nag

          Comment


            #6
            Hello Nag,

            Thanks for your patience.

            I see that you have EntriesPerDirection set to 4, this means that your strategy could allow four single orders to be placed in the same direction. You are also resetting the bools to true on each OnBarUpdate() which will invalidate saving the bar number if you have more than one successive entry condition (Close[0] > Close[1]) for example.

            If you want to have multiple entries, you would need to use multiple bools as well as multiple int variables to save the bar number when the entry condition, for each separate entry, is true. Basically, this means that you would need to create 4 blocks of code (one for each entry in the same direction). You will also need to create additional bools so then when Entry #1 is placed that you do not use that again, for at least 3 bars. This will certainly add a new level of complexity to your code as you will again need to create a condition to reset the bools as resetting on each OnbarUpdate() will be incorrect.

            It may be easier for you to create new code and work with just 1 entry per direction and making sure that the single entry coding works as you expect. Once that works as expected, then you can add a block (with new int variable and new bools) for your second entry. Once that tests out correctly then add the 3rd and so on until you are up to your 4 entries per direction.

            As you build your code, continue to use print statements to help you follow what is happening as the complexity increases.

            Please also keep in mind that if you would like your code professional developed we can provide references to 3rd party programmers when requested.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul, Thanks for the update. I will set EntriesPerDirection parameter to 1. It was supposed to be for one order. Will change it. I did test the code with print statements and noticed that bools are not entering actual IF loop , because they are getting reset within onbarupdate. Same holds good for saved bar and current bar number. Saved and current bar value are always same. Can you advise where to set the variable so as not to reset it everytime.

              Thanks,
              Nag

              Comment


                #8
                Hello Nag,

                Thanks for your reply.

                I would create and declare the bools in the area called #Region Variables using private bool bdoitonce = true; and private bool sdoitonce = true;

                Your next challenge will be to determine when to reset those bools back to true after they have been used. You might consider something like:

                if (CurrentBar - bfirstbar > 10)
                {
                bdoitonce = true; // re-enable
                }

                and of course another set for sdoitonce.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Paul, Thanks. Thats exactly how I defined other variables - bfirstbar and sfirstbar and after if loop, reset them within onbarupdate . It didnt work as expected , so defined bdoitonce and sdoitonce within onbarupdate. Will define doitonce bools in region section

                  In the below code, it processes the first IF loop, but never second loop. when the value of bdoitonce is set to false after buy logic is evaluated to true (in first if loop), it never enters into second IF loop. But this was the approach you suggested.

                  When the saved and current bar number is always same, then i think it would not enter the new IF loop that you mentioned.

                  Can you point to what I am missing here ? Thank you !

                  ##Region Variables section

                  private int bfirstbar;
                  private int sfirstbar;


                  ##Onbarupdate Method

                  //Higher or Lower from Previous Bar
                  bool buySetup = Close[0] > Close[1];
                  bool sellSetup = Close[0] < Close[1];

                  bool bdoitonce = true;
                  bool sdoitonce = true;



                  if (buySetup && bdoitonce)
                  {
                  //Print(Time[0]+" Entering Long trade initialization" );
                  bfirstbar = CurrentBar; //save the bar number
                  bdoitonce = false; // prevent resetting.
                  lentryprice=High[0] + 10*TickSize; // Save the entry price into a variable.
                  Print(Time[0]+" bdoionce value: "+ bdoitonce );

                  }

                  if (bdoitonce == false && CurrentBar-bfirstbar < 3)
                  {
                  Print(Time[0]+" Submit Long trade");
                  SetStopLoss("Long-Entry", CalculationMode.Ticks,stopTicks,false);
                  EnterLongStop(1, lentryprice,"Long-Entry");
                  SetProfitTarget ("Long-Entry",CalculationMode.Ticks,profitTicks);

                  }




                  Comment


                    #10
                    Hello Nag,

                    Thanks for your reply.

                    Please create and set your bools in Region Variable, remove the OnBarUpdate() bools = true as this will not work and it is not what I suggested.

                    You are correct that with the declaration made in the region variables the bools will only be true once and that is why I further suggested to you that you need a reset condition which I showed an example of with:

                    if (CurrentBar - bfirstbar > 10)
                    {

                    bdoitonce = true; // re-enable
                    }


                    I did not explain very well this section so it goes in the onBarUpdate() and basically will reset the buy bool bfirstbar when 10 bars have gone by. You would need to duplicate that code and change the bfirstbar with the sfirstbar so that both bools will be independently reset 10 bars after they have been used. You can make that however many bars as you wish, I just suggested 10 to not conflict with your 3 bar wait.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Bouzayen, Yesterday, 08:31 PM
                    0 responses
                    14 views
                    0 likes
                    Last Post Bouzayen  
                    Started by IanC28, 03-27-2020, 10:20 AM
                    6 responses
                    30 views
                    0 likes
                    Last Post jim1234
                    by jim1234
                     
                    Started by aramtrader, 04-01-2020, 03:05 PM
                    7 responses
                    40 views
                    0 likes
                    Last Post woodie1
                    by woodie1
                     
                    Started by renehtx, Yesterday, 03:51 PM
                    2 responses
                    16 views
                    0 likes
                    Last Post renehtx
                    by renehtx
                     
                    Started by eidog2020, Yesterday, 03:31 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post eidog2020  
                    Working...
                    X