Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

time limitations for automated strategies

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

    time limitations for automated strategies

    hello, everyone,



    i could use a lot of guidance with the subject of time limitations / filters for automated strategies. i searched these fora, but found no definitive answers for nt7.


    1) how could i limit an automated strategy so that it only enters new trades between 18:00 et and 16:00 et of the next day? say, from 18:00 on sunday on to 16:00 on monday, and then from 18:00 on monday on to 16:00 on tuesday and so on.


    2) also, if i wanted the strategy to close all the positions it has generated at 16:10 every day, what kind of commands would be useful?


    3) and finally, it seems to me that nt can handle this kind of time constraints on periods to place trades and times to close all positions irrespective of the kind of bars that the strategy is executed on (minute, range, kase, etc), am i right?


    thanks.

    #2
    Hello rtwave,

    With NinjaScript, you accomplish this with conditional logic, usually if statements. OnBarUpdate is a "clock signal", meaning that it will trigger over and over again as time goes on. Every tick or bar has a unique time that it triggers. You can test every bar's trigger time to see if it is within a range, and only perform tasks if this test passes.

    I am including a link to the ToTime() section of the Help Guide.



    I will also adapt its sample code to cover the three situations you mentioned.

    Code:
    // Only trade between 18:00 and 16:00 (so not between 16:00 and 18:00)
    if (! (ToTime(Time[0]) > 160000 && ToTime(Time[0]) < 180000))
    {
        if(MarketPosition == MarketPosition.Flat)
        {
            EnterLong();
        }
    }
    The first snippet done another way, as a more literal interpretation of what you were asking.

    Code:
    // Only trade between midnight and 16:00, or between 18:00 and midnight
    if (ToTime(Time[0]) <= 160000 || ToTime(Time[0]) >= 180000)
    {
        if(MarketPosition == MarketPosition.Flat)
        {
            EnterLong();
        }
    }
    Code:
    // Close all positions at 16:10
    if (ToTime(Time[0]) >= 161000)
    {
        ExitLong();
        ExitShort();
    }
    The second code snippet takes into account your third question. Since it is possible that some bar types may not update exactly at 16:10 - we may have an update, say, at 16:09 and another at 16:11 - by setting my condition so that it checks every time between 16:10 and midnight, and using methods that are safe to call from a flat position and will flatten any non-flat position, we have guaranteed that our position will flatten some time between 16:10 and midnight.

    If we need this to flatten exactly at 16:10, we can provide this guarantee by setting CalculationMode to Ticks (or CalculateOnBarClose = false in NT7).
    Last edited by NinjaTrader_JessicaP; 03-23-2016, 09:26 AM. Reason: Extra right paren
    Jessica P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by yertle, Yesterday, 08:38 AM
    7 responses
    28 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by bmartz, 03-12-2024, 06:12 AM
    2 responses
    20 views
    0 likes
    Last Post bmartz
    by bmartz
     
    Started by funk10101, Today, 12:02 AM
    0 responses
    4 views
    0 likes
    Last Post funk10101  
    Started by gravdigaz6, Yesterday, 11:40 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by MarianApalaghiei, Yesterday, 10:49 PM
    3 responses
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X