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

1 Event Timer Code

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

    1 Event Timer Code

    I'm running strategies on range bars on 24 hour data, but have a START time and END time parameter. I've been, unsuccessfully, trying to get code to work which deals with scenarios where the START time is less than END (eg. start trading at 9:00 am and end at 14:00 pm ) and can handle START greater than END (eg. start trading at 9:00 am and end at 3:00 am next day).

    Basically, what I need to do is have a single event fire off at my END time, regardless of when I start the strategy or regardless of the BEGIN time.

    I was thinking in terms of a custom timer event, but I find it a bit overwhelming (been studying MSDN site) and would love an example or hint about how to turn a timer into essentially an alarm clock.

    THanks much in advance for any insights.

    #2
    I'm unclear what you're looking to do - you would like the strategy to trade between 9:00AM and 14:00PM as well as 09:00AM and 03:00AM the next day?

    What occurs at 14:00PM? What is occuring between 14:00PM and 03:00AM? What are you looking to do at 03:00AM? Does this need to occur at exactly 03:00AM?

    Can you give me an example of what you have tried so far and how it is failing?
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi Matthew,

      What I'd like to do is halt my strategy (similar to the advance halt sample code, which I have in part implemented) once my endTime parameter is reached. Here is the test I'm using in code:

      if((ToTime(Time[0])>=endTime)&& (ToTime(Time[0])<=endTime + 100)
      {
      //various tasks to halt trading
      }

      I can't just use the first condition due to the following example: Lets say startTime is 01:00 and my endTime is 07:00. I start the strategy up around 18:00. So, right off the bat, the current time is > than the endTime, so the strategy just halts immediately.

      Thus, I add in the second condition (after the &&) and just stick an arbitrary number of minutes onto endTime so that prevents the halting from occurring when it's not supposed to. However this extra condition leads to other issues, at least possible issues. This seems sort of a clunky way to effect the desired outcome.

      Bear in mind that that example I provided above is just one possible set of parameters. I'm trying to code this so that whatever the user inputs for startTime and endTime, will work as expected.

      EDIT: I guess I would add this: I still want to learn how to use timer as an alarm clock to trigger a single event, because there are other things I'd like to do with it than just halting. Using range bars, it looks like the only way to effect execution of a block of code at a specific time would be with a timer event.

      Thanks so much for your replies.
      Last edited by coolmoss; 07-24-2012, 11:42 AM.

      Comment


        #4
        You can put all of the logic for your events within a time filter, so it will exclude events outside of these times. Using an OR operator, you can account for various times (in my example I'm using midnight as the second start time)

        Code:
        if((ToTime(Time[0] >= 90000 && ToTime(Time[0]) < 14000) || ToTime(Time[0]) >  00000 && ToTime(Time[0]) < 30000)
        {
          // strategy logic goes here
        }
        Alternatively, what you could do is create a bool that will help ensure the event only triggers once

        Code:
        bool doOnce = true;
        
        if((Time[0]) > EndTime && myCondition))
        { 
        
          //myEvent
        
          doOnce = false;
        }
        
        // at the begnning of a new session, reset the doOnce bool back to true
        However you are correct with Range bars that the Time[0] might not ever == 30000, so you would need to create a custom TimerEvent to trigger at a specific time based on your PC clock, rather than the time of the bar.

        I'd suggest looking at the BarTimer indicator for an example of how to crate your own custom timer.
        MatthewNinjaTrader Product Management

        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