• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Specifying the order in which conditions occur

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

    Specifying the order in which conditions occur

    Hello all,

    I am building a strategy that triggers orders based upon price interacting with an overlay indicator that generates moving average-like bands.

    Like this...
    Click image for larger version

Name:	ConditionOrderExample.PNG
Views:	55
Size:	7.7 KB
ID:	1080877

    The first entry at Y is valid because it touched the Y-1 line last.

    The second entry at Y-1 is valid because it touched the Y line last.

    The third entry is valid because is touched the Y line last (even though it didn't trade at the Y line last).

    The fourth entry is NOT VALID and it would not be valid until it first touched the Y-1 line. Since Y1 was the last touched, only long at Y is valid, not Short!

    The fifth entry is again NOT VALID because it is a short, and it didn't touch the line below it before it touched Y-1, therefore, within my paradigm, it is considered a continuation and not a reversal.

    I apologize for the crude example, but I wanted to make it as clear as possible rather than have the concept muddled with chaotic price action.

    The current instantiation of my automated strategy enters at every line where the entry conditions are met (Low[0] <= Line, Close[0] & Open[0] > Line for Long, for example)

    So in logical terms using the Y-Line I want to say...

    if entry conditions are met and price touched Y1 BEFORE Y-1, then enter Long, or for a short it would be... if entry conditions are met and price touched Y-1 BEFORE Y1 then enter Short. In other words, for each entry (at each line) a time condition would need to be set referencing the lines immediately above and below it).

    My final word is that I am able to do this in a crude way by referencing the highest or lowest bar in the last 10 bars, or crossabove/crossbelow in the last 10 bars, but the problem with that is that it needs to be a generalized BEFORE rather than "in the last 10 bars" because sometimes it could be 10, sometimes it could be 50, and so using a specific lookback is really not a solution. I need to be able to specify the condition with respect to TIME.

    Thank you for your time, I know this was a lot of information, but I wanted to be as detailed as possible, I hope you can be as detailed in your response.

    Looking forward to hearing from you.

    #2
    Hello lunardiplomacy,

    Thank you for your note.

    This would certainly be possible, but you'd definitely need to unlock the code and manually code some of it if you're currently using the strategy builder.

    There's probably several ways to go about this, but what I'd suggest is running the strategy On Price Change, so your OnBarUpdate runs every time the price moves. When it hits one of the lines, save the Time[0] to a variable, then use those variables to make a time comparison and check which lines were hit the most recently.

    So I might do something like this:

    Code:
            private DateTime LastYHighPriceTime;
            private DateTime LastYLowPriceTime;
            private DateTime LastYPriceTime;
    
    protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) 
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                if (Close[0] == YPrice)
                {
                    LastYPriceTime = Time[0];
                }
                if (Close[0] == YHighPrice)
                {
                    LastYHighPriceTime = Time[0];
                }
                if (Close[0] == YLowPrice)
                {
                    LastYLowPriceTime = Time[0];
                }
    
                 // Set 1
                if ((Low[0] <= YHighPrice)
                     && (Close[0] > YHighPrice)
                     && (Open[0] > YHighPrice)
                     && (LastYHighPriceTime < LastYLowPriceTime))
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                }
    
                // further logic goes here
    }
    Obviously you'd have to substitute your own logic, but this should get you headed in the correct direction.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello lunardiplomacy,

      Thank you for your note.

      This would certainly be possible, but you'd definitely need to unlock the code and manually code some of it if you're currently using the strategy builder.

      There's probably several ways to go about this, but what I'd suggest is running the strategy On Price Change, so your OnBarUpdate runs every time the price moves. When it hits one of the lines, save the Time[0] to a variable, then use those variables to make a time comparison and check which lines were hit the most recently.

      So I might do something like this:

      Code:
       private DateTime LastYHighPriceTime;
      private DateTime LastYLowPriceTime;
      private DateTime LastYPriceTime;
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;
      
      if (CurrentBars[0] < 1)
      return;
      
      if (Close[0] == YPrice)
      {
      LastYPriceTime = Time[0];
      }
      if (Close[0] == YHighPrice)
      {
      LastYHighPriceTime = Time[0];
      }
      if (Close[0] == YLowPrice)
      {
      LastYLowPriceTime = Time[0];
      }
      
      // Set 1
      if ((Low[0] <= YHighPrice)
      && (Close[0] > YHighPrice)
      && (Open[0] > YHighPrice)
      && (LastYHighPriceTime < LastYLowPriceTime))
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), "");
      }
      
      // further logic goes here
      }
      Obviously you'd have to substitute your own logic, but this should get you headed in the correct direction.

      Please let us know if we may be of further assistance to you.
      Hello Kate,

      Thank you for getting back to me. Yes, I am already in manual coding, so no worries there, however clearly I'm still learning because the method "Time[0]" is almost obnoxiously straightforward, so thank you for so much for teaching me that.

      In your initial synopsis you mentioned that I should run the strategy OnPriceChange, yet in the code you so generously provided it looks like it's meant to be run OnBarUpdate. Are these not either/or? Just want to be sure there isn't something I am misunderstanding.

      Thank you so much for helping, Kate.

      Comment


        #4
        Hello lunardiplomacy,

        Thank you for your reply.

        I think you're confusing the method OnBarUpdate() with the Calculation Mode On Bar Close. The latter tells the OnBarUpdate() method how often it should run, and is what I'm referring to setting. You'd actually set this in the OnStateChange() method in the State == State.SetDefaults like so:

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Strategy here.";
        Name = "aExampleYLines";
        Calculate = Calculate.OnPriceChange;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;

        }
        }

        Please let us know if we may be of further assistance to you.
        Last edited by NinjaTrader_Kate; 12-13-2019, 02:03 PM.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by tvaughan4, Today, 04:37 PM
        0 responses
        3 views
        0 likes
        Last Post tvaughan4  
        Started by Convert, Today, 03:44 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Yannick, Today, 02:43 PM
        3 responses
        10 views
        0 likes
        Last Post Yannick
        by Yannick
         
        Started by brucerobinson, Today, 02:12 PM
        2 responses
        6 views
        0 likes
        Last Post brucerobinson  
        Started by gurdonzn, Today, 12:36 PM
        5 responses
        24 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Working...
        X