Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A switch in the strategy to turn time-based trading on / off

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

    A switch in the strategy to turn time-based trading on / off

    Hi everyone,

    I have some time parameters in the strategy (unlocked, working in code) and I'd like to be able to switch these on and off, particularly for the analyzer. What would the right way to do this be? As I'm not a programmer and just learning to code, I'd appreciate an "easy" explanation..

    I was thinking of using variables in the strategy builder, but I am not sure they would become visible in the analyzer as a parameter to select as on/off.
    Something like this:


    Code:
     // Set 3
    if ((CrossBelow(RSI1.Default, RSI_Min_Long_SL, 1))
    // Morning Hours
    && ((Times[0][0].TimeOfDay < new TimeSpan(8, 0, 0))
    || (Times[0][0].TimeOfDay < new TimeSpan(10, 0, 0)))
    && (TimeManagement == 1))
    So essentially, I'd have this trading rule twice: once with time management = 1 and once without time management (variable = 0), but then the "times..." instruction would be removed... It feels a bit clunky though and I'd duplicate the code of the trading strategy which inevitably will lead to me forgetting to update it in different locations in the future..

    So any help to make this a bit more slick is much appreciated!

    Click image for larger version  Name:	2021-01-22_0-00-23.png Views:	0 Size:	21.3 KB ID:	1137847

    PS: the above use of a variable still doesn't show up in the analyzer as a "switch" so maybe I am on a wrong track here... :-(
    Last edited by Oracletrades; 01-21-2021, 10:10 PM.

    #2
    Hello Oracletrades,

    Thanks for your post.

    Perhaps a simple solution would be to set the start and end times to be the same?

    You are correct, variables would not be visible in the Strategy analyzer parameter view. You would need to create "inputs" as these are what you would see. You could create bool inputs which then you can simply check off when you don't want them to be true and use the bool, in the applicable set.

    Taking a strategy builder only approach, what you could do would be to create 1 set with all of the time filters. Each time filter would be a group that would check if the input bool for that time is true and if the bar time is within the filter time range. You would set the "set" to "If any" and each group would be set to "If all" and this creates a set where you have multiple groups where any one of them can be true. The singular action then would be to enable a single bool (call it OkToTrade to make it easy). In each entry set, you then add the check of the bool OkToTrade as part of the entry conditions. However, one issue is that you also need something to turn the bool off. the easiest way to do this is to make set 1 have no conditions and the only action would be to set OkToTrade o false. So each time your strategy executes set 1 is done first setting the bool false, if set 2 is your time filters then if any of the filters are true, the bool is set to true and would then be true for any of your entry sets after that.

    I've attached a working example of this and when the times are selected and true, the strategy will draw the background color of tan to verify the time set.

    ExampleMultiTimeFilter.zip
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul, appreciate the effort. I was looking for something that doesn't require duplicating the code though... I to think that the input parameter is what I want to play with... Let me do some more digging. Setting the times to all the same time also crossed my mind, and that may be the easier option indeed..

      If any idea comes to mind or you see a code snipped to implement the "input" solution, I'd much appreciate if you could post a link here!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by smcllr, Today, 04:15 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ShawnB  
      Started by rtwave, Today, 02:33 PM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by mlprice12, Today, 01:43 PM
      2 responses
      6 views
      0 likes
      Last Post mlprice12  
      Started by coopgrafik, Today, 01:31 PM
      3 responses
      14 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by ARQERNES, Today, 12:08 PM
      2 responses
      20 views
      0 likes
      Last Post ARQERNES  
      Working...
      X