Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

coloring ichimoku cloud/region according to a Trend Series

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

    coloring ichimoku cloud/region according to a Trend Series

    I have a Series<int>Trend which has +1 or -1 values in it according to the Up/Dw direction.
    I want to draw a region between two Series<double>firstSeries, Series<double>secondSeries but coloring it according to the Trend series.

    From the forum I see that in general support directs people to use the code from
    This indicator was ported from the Ichimoku Cloud indicator for NinjaTrader 7. The Cloud&#8217;s points are constructed identically to the NinjaTrader 7 indicator, however the points are then used to construct new SharpDX PathGeometry Figures. 3/24/2021: Modified the Adjust Chart Margins feature so users are prompted when the indicator wants to change the Chart&#8217;s Right [&#8230;]

    to teach them how to Draw Region Between two Series.

    But obviously there the coloring is based on the crossing of SenkouSpanA&B lines, i.e. SenkouSpanA being greater than SenkouSpanB line.


    I've been not able to modifying the code for what I need and I wander if someone can help. This is probably something other people also may need to resolve.
    Or maybe someone can suggest a simpler code or indicate me where a code that does what I am asking is already present or direct me to another pre-existent indicator.


    Thank you!
    Gio

    #2
    Alternative:
    If someone offers another solution great but if I should try to alter the ichimoku code then in my experiment below I show where I am at. Pretty lost.

    Original ichimoku rendering code::
    Code:
            private void DrawRegionBetweenSeries(ChartScale chartScale, Series<double> firstSeries, Series<double> secondSeries, string upperColor, string lowerColor, int displacement)
            {
                string BrushName = String.Empty;
                
                List<SharpDX.Vector2>     SeriesAPoints     = new List<SharpDX.Vector2>();
                List<SharpDX.Vector2>     SeriesBPoints     = new List<SharpDX.Vector2>();
                List<SharpDX.Vector2>     tmpPoints         = new List<SharpDX.Vector2>();
                List<SharpDXFigure>        SharpDXFigures    = new List<SharpDXFigure>();
                
                // Convert SeriesA and SeriesB to points
                int start     = ChartBars.FromIndex-displacement*2 > 0 ? ChartBars.FromIndex-displacement*2 : 0;
                int end     = ChartBars.ToIndex;
                
                float x0 = (float)ChartControl.GetXByBarIndex(ChartBars, 0);
                float x1 = (float)ChartControl.GetXByBarIndex(ChartBars, 1);
                
                if (ChartControl.Properties.EquidistantBarSpacing)
                    for (int barIndex = start; barIndex <= end; barIndex++)
                    {
                        if(firstSeries.IsValidDataPointAt(barIndex))
                        {
                            SeriesAPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(firstSeries.GetValueAt(barIndex))));
                            SeriesBPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(secondSeries.GetValueAt(barIndex))));
                        }
                    }
                else
                    for (int barIndex = start; barIndex <= end; barIndex++)
                    {
                        if(firstSeries.IsValidDataPointAt(barIndex))
                        {
                            SeriesAPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex) + displacement * (x1 - x0), (float)chartScale.GetYByValue(firstSeries.GetValueAt(barIndex))));
                            SeriesBPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex) + displacement * (x1 - x0), (float)chartScale.GetYByValue(secondSeries.GetValueAt(barIndex))));
                        }
                    }
                
                int lastCross = 0;
                bool isTouching = false;
                bool colorNeeded = true;
                
                for (int i = 0; i < SeriesAPoints.Count; i++)
                {    
                    if (colorNeeded)
                    {
                        colorNeeded = false;
                        
                        // Set initial color or wait until we need to start a shape
                        if (SeriesAPoints[i].Y < SeriesBPoints[i].Y)
                            BrushName = upperColor;
                        else if (SeriesAPoints[i].Y > SeriesBPoints[i].Y)
                            BrushName = lowerColor;
                        else
                        {
                            colorNeeded = true;
                            lastCross = i;
                        }
                        
                        if(!colorNeeded)
                            tmpPoints.Add(SeriesAPoints[i]);
                        
                        continue;
                    }    
                    
                    // Check if SeriesA and SeriesB meet or have crossed to loop back and close figure
                    if(( SeriesAPoints[i].Y == SeriesBPoints[i].Y && isTouching == false)
                        || (SeriesAPoints[i].Y > SeriesBPoints[i].Y && SeriesAPoints[i-1].Y < SeriesBPoints[i-1].Y)
                        || (SeriesBPoints[i].Y > SeriesAPoints[i].Y && SeriesBPoints[i-1].Y < SeriesAPoints[i-1].Y))
                    {                    
                        // reset isTouching
                        isTouching = false;
                        
                        // Set the endpoint
                        SharpDX.Vector2 endpoint = (SeriesAPoints[i].Y != SeriesBPoints[i].Y) ? FindIntersection(SeriesAPoints[i-1], SeriesAPoints[i], SeriesBPoints[i-1], SeriesBPoints[i]) : SeriesAPoints[i];
                        tmpPoints.Add(endpoint);
                        
                        // Loop back and add SeriesBPoints
                        for(int j = i-1; j >= lastCross; j--)
                            tmpPoints.Add(SeriesBPoints[j]);
                        
                        // Create figure
                        SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(),  (SeriesAPoints[i-1].Y < SeriesBPoints[i-1].Y) ? upperColor : lowerColor);
                        SharpDXFigures.Add(figure);
                        
                        // Clear Points
                        tmpPoints.Clear();
                        
                        // Start new figure if we crossed, otherwise we will wait until we need a new figure
                        if (SeriesAPoints[i].Y != SeriesBPoints[i].Y)
                        {
                            tmpPoints.Add(SeriesBPoints[i]);
                            tmpPoints.Add(endpoint);
                            tmpPoints.Add(SeriesAPoints[i]);
                        }
                        else
                            isTouching = true;
                        
                        // Set last cross
                        lastCross = i;                    
                    }
                    
                    // Check if we are at the end of our rendering pass to loop back to loop back and close figure
                    else if (i == SeriesAPoints.Count -1)
                    {                    
                        tmpPoints.Add(SeriesAPoints[i]);
                        
                        // Loop back and add SeriesBPoints
                        for(int j = i; j >= lastCross; j--)
                            tmpPoints.Add(SeriesBPoints[j]);
                        
                        // Create figure
                        SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (SeriesAPoints[i].Y < SeriesBPoints[i].Y) ? upperColor : lowerColor);
                        SharpDXFigures.Add(figure);
                        
                        // Clear Points
                        tmpPoints.Clear();
                        break;
                    }
                    
                    // Figure does not need to be closed. Add more points or open a new figure if we were touching
                    else if (SeriesAPoints[i].Y != SeriesBPoints[i].Y)
                    {
                        if (isTouching == true)
                        {
                            tmpPoints.Add(SeriesAPoints[i-1]);
                            lastCross = i-1;
                        }
                        
                        tmpPoints.Add(SeriesAPoints[i]);
                        
                        isTouching = false;
                    }            
                }
                
                // Draw figures
                foreach(SharpDXFigure figure in SharpDXFigures)
                    DrawFigure(figure);
            }
            #endregion
    ​

    Modified ichimoku rendering code:
    Code:
            private void DrawRegionBetweenSeries(ChartScale chartScale, Series<double> firstSeries, Series<double> secondSeries, Series<int> trend, string upperColor, string lowerColor, int displacement)
            {
                string BrushName = String.Empty;
    
                List<SharpDX.Vector2> SeriesAPoints = new List<SharpDX.Vector2>();
                List<SharpDX.Vector2> SeriesBPoints = new List<SharpDX.Vector2>();
                List<SharpDX.Vector2> tmpPoints = new List<SharpDX.Vector2>();
                List<SharpDXFigure> SharpDXFigures = new List<SharpDXFigure>();
                int[] trend_ = new int[5000];
                // Convert SeriesA and SeriesB to points
                int start = ChartBars.FromIndex - displacement * 2 > 0 ? ChartBars.FromIndex - displacement * 2 : 0;
                int end = ChartBars.ToIndex;
    
                float x0 = (float)ChartControl.GetXByBarIndex(ChartBars, 0);
                float x1 = (float)ChartControl.GetXByBarIndex(ChartBars, 1);
    
                if (ChartControl.Properties.EquidistantBarSpacing)
                    for (int barIndex = start; barIndex <= end; barIndex++)
                    {
                        if (firstSeries.IsValidDataPointAt(barIndex))
                        {
                            SeriesAPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(firstSeries.GetValueAt(barIndex))));
                            SeriesBPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(secondSeries.GetValueAt(barIndex))));
                            trend_[barIndex - start] = trend.GetValueAt(barIndex);
                        }
                    }
                else
                    for (int barIndex = start; barIndex <= end; barIndex++)
                    {
                        if (firstSeries.IsValidDataPointAt(barIndex))
                        {
                            SeriesAPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex) + displacement * (x1 - x0), (float)chartScale.GetYByValue(firstSeries.GetValueAt(barIndex))));
                            SeriesBPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex) + displacement * (x1 - x0), (float)chartScale.GetYByValue(secondSeries.GetValueAt(barIndex))));
                            trend_[barIndex - start] = trend.GetValueAt(barIndex);
                        }
                    }
    
                int lastCross = 0;
                bool isTouching = false;
                bool colorNeeded = true;
    
                for (int i = 0; i < SeriesAPoints.Count; i++)
                {
                    if (colorNeeded)
                    {
                        colorNeeded = false;
    
                        // Set initial color or wait until we need to start a shape
                        if (trend_[i] > 0) // if (SeriesAPoints[i].Y < SeriesBPoints[i].Y)
                            BrushName = upperColor;
                        else if (trend_[i] < 0) //if (SeriesAPoints[i].Y > SeriesBPoints[i].Y)
                            BrushName = lowerColor;
                        else
                        {
                            colorNeeded = true;
                            lastCross = i;
                        }
    
                        if (!colorNeeded)
                            tmpPoints.Add(SeriesAPoints[i]);
    
                        continue;
                    }
    
                    // Check if SeriesA and SeriesB meet or have crossed to loop back and close figure
    //                if ((SeriesAPoints[i].Y == SeriesBPoints[i].Y && isTouching == false)
    //                    || (SeriesAPoints[i].Y > SeriesBPoints[i].Y && SeriesAPoints[i - 1].Y < SeriesBPoints[i - 1].Y)
    //                    || (SeriesBPoints[i].Y > SeriesAPoints[i].Y && SeriesBPoints[i - 1].Y < SeriesAPoints[i - 1].Y))
    
                    if (trend_[i] != trend_[i-1])
                    {
                        // reset isTouching
                        isTouching = false;
    
                        // Set the endpoint
                        //SharpDX.Vector2 endpoint = (SeriesAPoints[i].Y != SeriesBPoints[i].Y) ? FindIntersection(SeriesAPoints[i - 1], SeriesAPoints[i], SeriesBPoints[i - 1], SeriesBPoints[i]) : SeriesAPoints[i];
                        SharpDX.Vector2 endpoint = (trend_[i] != trend_[i - 1]) ? FindIntersection(SeriesAPoints[i - 1], SeriesAPoints[i], SeriesBPoints[i - 1], SeriesBPoints[i]) : SeriesAPoints[i];
                        tmpPoints.Add(endpoint);
    
                        // Loop back and add SeriesBPoints
                        for (int j = i - 1; j >= lastCross; j--)
                            tmpPoints.Add(SeriesBPoints[j]);
    
                        // Create figure
                        //SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (SeriesAPoints[i - 1].Y < SeriesBPoints[i - 1].Y) ? upperColor : lowerColor);
                        SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (trend_[i - 1]>0) ? upperColor : lowerColor);
                        SharpDXFigures.Add(figure);
    
                        // Clear Points
                        tmpPoints.Clear();
    
                        // Start new figure if we crossed, otherwise we will wait until we need a new figure
                        if (SeriesAPoints[i].Y != SeriesBPoints[i].Y)
                        {
                            tmpPoints.Add(SeriesBPoints[i]);
                            tmpPoints.Add(endpoint);
                            tmpPoints.Add(SeriesAPoints[i]);
                        }
                        else
                            isTouching = true;
    
                        // Set last cross
                        lastCross = i;
                    }
    
                    // Check if we are at the end of our rendering pass to loop back to loop back and close figure
                    else if (i == SeriesAPoints.Count - 1)
                    {
                        tmpPoints.Add(SeriesAPoints[i]);
    
                        // Loop back and add SeriesBPoints
                        for (int j = i; j >= lastCross; j--)
                            tmpPoints.Add(SeriesBPoints[j]);
    
                        // Create figure
                        //SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (SeriesAPoints[i].Y < SeriesBPoints[i].Y) ? upperColor : lowerColor);
                        SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (trend_[i]>0) ? upperColor : lowerColor);
                        SharpDXFigures.Add(figure);
    
                        // Clear Points
                        tmpPoints.Clear();
                        break;
                    }
    
                    // Figure does not need to be closed. Add more points or open a new figure if we were touching
                    //else if (SeriesAPoints[i].Y != SeriesBPoints[i].Y)
                    else if (trend_[i] != trend_[i - 1])
                    {
                        if (isTouching == true)
                        {
                            tmpPoints.Add(SeriesAPoints[i - 1]);
                            lastCross = i - 1;
                        }
    
                        tmpPoints.Add(SeriesAPoints[i]);
    
                        isTouching = false;
                    }
                }
    
                // Draw figures
                foreach (SharpDXFigure figure in SharpDXFigures)
                    DrawFigure(figure);
            }
            #endregion  SharpDX Helper Classes & Methods
    ​
    ​​

    Comment


      #3
      Hello giogio1,

      The easiest way to shade a region between two plots is with Draw.Region().


      The Ichimoku Cloud Indicator uses custom rendering because Draw.Region() draws to the center of the bar and not to the cross-points. The custom rendering draws to the cross-point which just looks prettier.

      That said, I wouldn't expect the logic to change. I would just expect that you supply your series as firstSeries and secondSeries to the method call.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        In fact I was wandering why they do it so complicate over there. This simple instruction does the work perfectly..
        Thank you Chelsea

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X