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

Start strategy at specific (minute) time

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

    Start strategy at specific (minute) time

    Hi,
    Before I start modifying my strats I have a question about the start and end time of strategies.
    At the moment I use this construction:
    if ((ToTime(Time[0]) >= t1start && ToTime(Time[0]) < t1eind)
    || (ToTime(Time[
    0]) >= t2start && ToTime(Time[0]) < t2eind)
    )
    {do something;
    }
    else
    {do something else;}

    When I use this on my (30 min and 60 min) charts, I always have the problem that the 30 min strategy starts at an half hour (for example 10.00 or 10.30) and the 60 min charts start at the full hour (f.e. 10.00 or 11.00). Even with
    CalculateOnBarClose=false.
    But I want certain strategies to start at lets say 10.05, 10.25 or 10.35. So exactly at a certain minute.

    Should I make the construction with DateTime.Now.Hour+DateTime.Now.Minute?

    My question is: How can I do this the best way?


    #2
    Hi trail, using DateTime.Now (your current PC clock) would be one approach then or you add in for example a minute series and have access to it's Times and bar updates for more precise control -
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,
      Thanks for the quick reply.

      I think I prefer the DateTime.Now method, because adding a Minute series will cost my PC more capacity.
      So you say, when I use the DateTime.Now method I can send an order from a 60 minutes strategy at f.e. 10.25, even if I use the Managed Approach?
      And what if the end time is 17.14 with a Managed Approach? Is the order then deleted at 17.14 or will it be kept alive till the next bar?

      Comment


        #4
        Correct, it would then trigger of the DateTimeNow condition and not be working with your bar timestamps only (which would be done on closing bar time only). If you place a non market order in the managed approach it would expire per default on the next bar update seen, however you could control that finer via setting the liveUntilCancelled to true. Then the order persists throughout the session and you could CancelOrder when desired.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          OK, thanks.
          - I use the time format 102500 (for 10:25) in my variables. Can you tell me how I can format the DateTimeNow in the same way?
          - about liveUntilCancelled : if I set liveUntilCancelled to True, do I always have to send the command CancelOrder? Doesn't this give more risk? What if my internetconnection gets lost, then the order won't be cancelled, correct?
          Should I use TimeInForce instead? Because then the time limit is already given with the order. And if correct, how should I apply this? (The help is pretty short about this.)

          Comment


            #6
            trail, you can use the ToTime as well on DateTime.Now (just a regular DateTime) to work via the integer representation.



            liveUntilCancelled will mean the order can persist until the end of session handling takes over, if you don't want to have it working anymore > correct then you would need to CancelOrder() it.

            For the end of session handling, here comes the TIF into play - the order would be either last only the DAY(session) or be GTC, good till (you) cancelled it.

            The order would be working still if you lost connection or disconnected.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              I use ToTime like the link you gave (see my first post) but it doesn't work in minutes. The smallest time unit is then the time frame units...
              Or am I doing something wrong?

              Comment


                #8
                Yes, here you pass in Time[0] > which is the closing bar timestamp.

                You would need to pass in DateTime.Now to get the current PC time.

                Like if (ToTime(DateTime.Now) == 100037) ...

                That would need to be worked live on CalculateOnBarClose = false.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  The DateTime.Now solution worked fine. So now I can control per minute the start of my orders. Thanks.

                  But now about the termination of orders: cancelling orders by the minute is a lot more difficult.
                  The solution in your post 4 about CancelOrder is not as simple as it looks I believe.
                  Because then I have to set my strats to Unmanaged=true. So all my Stoploss and Trail-constructions don't work anymore.... So that would be a lot of work and time to make his work good.
                  - Is there a good example of what elements I have to put in which places in my script to work with CancelOrder?
                  - Or maybe the solution of adding a Minute series is better in this case?
                  - Or another more simple solution?
                  Can you give me some advise about this? I would like to know in advance the solution with the least amount of effort.

                  Comment


                    #10
                    You're welcome, you could check into this reference sample on working with the CancelOrder() method - http://www.ninjatrader.com/support/f...ad.php?t=18890

                    I'm not sure I understand your unmanaged comment fully? The CancelOrder() call is definitely possible in managed mode as well, so you could keep your Set methods for exit.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Ok, I thought CancelOrder only works with Unmanaged.
                      Is the Sample you provided also Unmanaged? Because I can't see an Unmanaged=true.

                      Comment


                        #12
                        No, the sample would be done in managed mode.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Ok, thanks Bertrand.
                          I'll try this method.
                          With CancelOrder, is it always needed to check if it is null etc. or can I just fire the command and the order is cancelled?

                          Comment


                            #14
                            You're welcome, as CancelOrder works via the IOrder interface the proper object handling would be needed.
                            BertrandNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Jonafare, 12-06-2012, 03:48 PM
                            5 responses
                            3,985 views
                            0 likes
                            Last Post rene69851  
                            Started by Fitspressorest, Today, 01:38 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post Fitspressorest  
                            Started by Jonker, Today, 01:19 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post Jonker
                            by Jonker
                             
                            Started by futtrader, Today, 01:16 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post futtrader  
                            Started by Segwin, 05-07-2018, 02:15 PM
                            14 responses
                            1,792 views
                            0 likes
                            Last Post aligator  
                            Working...
                            X