Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using divergence in a strategy

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

    Using divergence in a strategy

    Hello

    I am trying to use a divergence condition in my strategy, but I'm not really sure how to go about it.

    What I am trying to achieve is something like "when the price makes a new low, but the indicator makes a higher low... do this"

    I've created two series for the indicator's swing High and Low, but I'm not sure how to move on from here

    Also, is the Closes[2] the right approach, or should I use BarsInProgress[2] and what exactly is the difference between the two variants?

    Thankyou

    Code:
    private Series<double>        dmiHigh;
    private Series<double>        dmiLow;
    ....
    else if (State == State.Configure)                
                {
                    AddDataSeries(Data.BarsPeriodType.Tick, 1);
                    AddDataSeries(Data.BarsPeriodType.Minute, 5);             
                }
    
    else if (State == State.DataLoaded)
    {
    DMI1            = DMI(Closes[2], Convert.ToInt32(DmiPeriod));
    
    AddChartIndicator(DMI1);
    
    dmiHigh = new Series<double>(DMI(Closes[2], Convert.ToInt32(DmiPeriod)), MaximumBarsLookBack.TwoHundredFiftySix);
    dmiLow = new Series<double>(DMI(Closes[2], Convert.ToInt32(DmiPeriod)), MaximumBarsLookBack.TwoHundredFiftySix);
      }
    protected override void OnBarUpdate()
      if (DMI1[1] < DMI1[2] && DMI1[2] > DMI1[3])
             dmiHigh[2] = DMI1[2];
    
     if (DMI1[1] > DMI1[2] && DMI1[2] < DMI1[3])
           dmiLow[2] = DMI1[2];
    
    // What now?!?
    {

    #2
    Hello itrader46,

    Thank you for your note.

    I think you may be running into confusion regarding indexing, as well as some issues with adding the indicator.

    First, an indicator being added via AddChartIndicator() cannot use any additional data series hosted by the calling strategy, but can only use the strategy's primary data series. If you wish to use a different data series for the indicator's input, you can add the series in the indicator itself and explicitly reference it in the indicator code (please make sure though the hosting strategy has the same AddDataSeries() call included as well).

    In a nutshell, what that means is that visually, any indicator that you plot on the chart with the strategy using AddChartIndicator will use the primary data series unless you specifically program a version of the indicator that uses the desired series.

    What is the primary data series you're running this on?

    I would recommend reviewing this page from our help guide that goes into how the indexing works:



    I say this because I see that if you're detecting that the second bar back from the current bar's DMI value is either the lowest or highest DMI value out of the three bars previous to the current one, it currently would save that value to the DMI1 value of 2 bars ago. I'm unsure if you're meaning to assign this to that bar, or have this be the value of dmiHigh of the current bar.

    I'm not sure I quite follow what you're wanting to do with this comparison - would you be able to further elaborate?

    Thanks in advance; I look forward to assisting you further.



    Comment


      #3
      Well... I'm only tryng to use a price - indicator divergence în my order execution. I don't know how better to explain it.
      I'm not trying to plot the indicator on the chart from my code, as I know that can't be done, but by adding the indicator with Closes[2], I can print the swings High and Low of DMI applied to the 5min series. I rekon if I can do that, then I could probably compare those values with eachother to get my price - DMI divergence..... but I'm not sure how can I do that.
      I think if I can compare the last two consecutive DMI swings high or low with the corresponding price swings, I can get a true/false divergence condition, so I'll probably have to store those swing values, then delete old ones in case a new lower or higher swing appears and use the new one in determining the divergence

      Also, you didn't answer my question about the difference between Closes[2] and BarsInProgress[2] (or is it BarsArray[2]?)

      Comment


        #4
        Hello itrader46,

        Thank you for your reply.

        Yes, by saving an instance of the DMI indicator to the DMI1 variable, you can then utilize the same instance each time you wish to access the indicator based on that particular data series.

        Closes[2] refers to a dataseries of close data for the second data series added to the chart. BarsInProgress[2] isn't anything - BarsInProgress returns an index value of the current Bars object that has called the OnBarUpdate method. So, if we wanted to do something if we're running through OnBarUpdate for that series, we could say within OnBarUpdate:

        if (BarsInProgress == 2)
        {
        // do something
        }

        BarsArray[2] would refer to the entire bars series for the second data series added, not just the close prices. You're best off using Closes[2] in this particular case because you don't really need the whole bars array.

        I think you might be getting a little confused on what values you're saving where. I've created a small example strategy that takes your logic and assigns DMI1[2] to dmiHigh[0] and dmiLow[0] for each bar if a new value is set - if there's no new value it assigns the same value of dmiHigh or dmiLow the previous bar had. You want to make sure there's a value for each of those for each bar. Assigning the value to dmiHigh[0] or dmiLow[0] will save those values for the current bar. You can then access those values for previous bars if you want, by using an index representing the number of bars back.

        This strategy will print current values to the chart and also print values to the NinjaScript Output window so you can follow the logic.

        Please let us know if we may be of further assistance to you.
        Attached Files

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        20 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        119 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        63 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        45 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X