Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to create code that has section that is true, and a section that must be false

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

    How to create code that has section that is true, and a section that must be false

    I have automated trading idea that includes the following:

    - one section of that must be true, and
    - a section that must be false (not have occurred) during the previous 90 minutes.

    Both would need to be as described to trigger a valid trade.


    As an example, this code would need to be true:

    MACD(12, 26, 9).Diff[1] > 0
    &&
    Open[0] == 9.005
    &&
    Close[1] < 3


    And, the following code would have to be false during the previous 90 minutes.

    LinRegSlope(3)[1] * 100 / Close[1] < 3
    &&
    ADX(13)[1] > ADX(13)[2]


    Help, please! I am having difficulty in figuring how to make a "false" return a good thing.

    Thanks!
    Last edited by ArmKnuckle; 02-16-2016, 03:48 AM.

    #2
    Hello ArmKnuckle,

    Thanks for your post.

    I think a good approach here is to use a bool that is set true when your second condition set has met the 90 minute mark. You can then use that bool as a condition requirement for your first condition.

    For measuring time, if you are using time based bars then you can count the number of bars since the condition went false, so if using 3 minute bars then you need 60 bars to pass. You would use an int variable (call it savedBar for example) to hold the bar number when the condition first goes false and continue to check against the current bar. Once CurrentBar - savedBar >= 60 then you can set a bool true for your first condition to do its thing. Of course if the 2nd condition goes true in the middle of the 60 bars then you would need to reset savedBar to the CurrentBar to start the count over.

    Continuing the example you could then have for your first condition:

    if (MACD(12, 26, 9).Diff[1] > 0 && Open[0] == 9.005 && Close[1] < 3 && (CurrentBar - savedBar >= 60))
    {
    // execute here
    }

    if you are not using time based bars then you would want to work with TimeSpans to compare the amounts of time. Here is a link to C# timespans: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response.

      I have a couple more questions:

      1) How do I create an int variable (savedBar) to hold the bar number when the condition first goes false? I'm not sure how to extract and hold the time period information when the calculation goes false.

      2) As you stated, I am actually using 3 minute bars. The calculation:

      (CurrentBar - savedBar >= 60)

      ...I think references a time period of 60 bars @ 3 min. each. Is this a calculation for 180 minutes?

      Is 30 x 3 the 90 minute calculation I am looking for?

      Thanks again!

      Comment


        #4
        Hello ArmKnuckle,

        Thanks for your reply.

        Variables would be declared in the area in the source code just below the "public class (name) : Indicator"
        Typically you would see something like:

        Code:
        	
        #region Variables
        private int interval = 10;
        private int period  = 14;
        #endregion
        In the above you would add a statement like:

        private int savedBar = 0; // holds the time start bar.

        Regarding, "I'm not sure how to extract and hold the time period information when the calculation goes false." You'll have to figure out your logic for sure on that. Basically what you want to do here is to detect when your condition is false, save the currentbar and also be able to detect when your condition is true to reset (restart the count). Most likely you will want to add bool variables to assist. Like the int variable a bool would be declared in the region variables like private bool myCondition = false; // initially set false (or true as you need).

        Correct, my error, with 3 minute bars then you only want to test for >= 30.

        Programming can be challenging so take your time and test with print statements to make sure things are at the values expected. Here is a link to our debug guide: http://ninjatrader.com/support/forum...ead.php?t=3418 additionaly this may assist as well: http://ninjatrader.com/support/helpG...g_concepts.htm
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by geotrades1, Today, 10:02 AM
        1 response
        4 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by ender_wiggum, Today, 09:50 AM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by rajendrasubedi2023, Today, 09:50 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by bmartz, Today, 09:30 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by geddyisodin, Today, 05:20 AM
        3 responses
        26 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X