Information

Username: motalaabbas12
Member since: 04 Jan 2024
Last login: 13 Oct 2024
Status: Active

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

MO
motalaabbas12 · 2 months ago

there was an error with the new modified scripts so i fixed now.using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
   [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
   public class SwingHilov11 : Indicator
   {
       [Parameter(DefaultValue = 8)]
       public int PeriodFractale { get; set; }

       [Output("Higher High ", PlotType = PlotType.Points, LineColor = "Lime", Thickness = 2)]
       public IndicatorDataSeries HH { get; set; }

       [Output("Higher Low", PlotType = PlotType.Points, LineColor = "Orange", Thickness = 2)]
       public IndicatorDataSeries HL { get; set; }

       [Output("Lower High", PlotType = PlotType.Points, LineColor = "DeepSkyBlue", Thickness = 2)]
       public IndicatorDataSeries LH { get; set; }

       [Output("Lower Low", PlotType = PlotType.Points, LineColor = "Red", Thickness = 2)]
       public IndicatorDataSeries LL { get; set; }

       private Fractals fra5;
       private int newindex;
       private IndicatorDataSeries up, down, up2, down2;

       protected override void Initialize()
       {
           fra5 = Indicators.Fractals(PeriodFractale);
           up = CreateDataSeries();
           down = CreateDataSeries();
           up2 = CreateDataSeries();
           down2 = CreateDataSeries();
       }

       public override void Calculate(int index)
       {
           if (index > newindex)
           {
               up[index] = fra5.UpFractal.Last(0);
               down[index] = fra5.DownFractal.Last(0);

               // Handle NaN cases for up and down fractals
               up[index] = double.IsNaN(up[index]) ? up[index - 1] : up[index];
               down[index] = double.IsNaN(down[index]) ? down[index - 1] : down[index];

               var customindex = index - (int)(PeriodFractale / 2);

               up2[customindex] = up2[index];
               down2[customindex] = down2[index];

               // Higher High and Higher Low
               if (up[index] > up[index - 1])
               {
                   HH[customindex] = up[index];
                   HL[customindex] = double.NaN;
               }
               else if (up[index] < up[index - 1])
               {
                   HL[customindex] = up[index];
                   HH[customindex] = double.NaN;
               }

               // Lower Low and Lower High
               if (down[index] < down[index - 1])
               {
                   LL[customindex] = down[index];
                   LH[customindex] = double.NaN;
               }
               else if (down[index] > down[index - 1])
               {
                   LH[customindex] = down[index];
                   LL[customindex] = double.NaN;
               }

               // Handle NaN for all series
               HL[customindex] = double.IsNaN(HL[customindex]) ? HL[customindex - 1] : HL[customindex];
               HH[customindex] = double.IsNaN(HH[customindex]) ? HH[customindex - 1] : HH[customindex];
               LL[customindex] = double.IsNaN(LL[customindex]) ? LL[customindex - 1] : LL[customindex];
               LH[customindex] = double.IsNaN(LH[customindex]) ? LH[customindex - 1] : LH[customindex];

               // Ensure consistency for following indices
               for (int i = 1; i < index - customindex; i++)
               {
                   HL[customindex + i] = HL[customindex];
                   HH[customindex + i] = HH[customindex];
                   LL[customindex + i] = LL[customindex];
                   LH[customindex + i] = LH[customindex];
               }

               newindex = index;
               CreateLabel(customindex);
           }
       }

       private void CreateLabel(int customindex)
       {
           // Create chart labels only when value changes to avoid redundant drawings
           if (!double.IsNaN(HH[customindex]) && (double.IsNaN(HH[customindex - 1]) || HH[customindex - 1] != HH[customindex]))
           {
               var textHH = Chart.DrawText("HH" + customindex, "HH", customindex, HH[customindex], Color.Lime);
               textHH.VerticalAlignment = VerticalAlignment.Top;
               textHH.HorizontalAlignment = HorizontalAlignment.Center;
           }
           else if (!double.IsNaN(HL[customindex]) && (double.IsNaN(HL[customindex - 1]) || HL[customindex - 1] != HL[customindex]))
           {
               var textHL = Chart.DrawText("HL" + customindex, "HL", customindex, HL[customindex], Color.Orange);
               textHL.VerticalAlignment = VerticalAlignment.Top;
               textHL.HorizontalAlignment = HorizontalAlignment.Center;
           }
           else if (!double.IsNaN(LL[customindex]) && (double.IsNaN(LL[customindex - 1]) || LL[customindex - 1] != LL[customindex]))
           {
               var textLL = Chart.DrawText("LL" + customindex, "LL", customindex, LL[customindex], Color.Red);
               textLL.VerticalAlignment = VerticalAlignment.Bottom;
               textLL.HorizontalAlignment = HorizontalAlignment.Center;
           }
           else if (!double.IsNaN(LH[customindex]) && (double.IsNaN(LH[customindex - 1]) || LH[customindex - 1] != LH[customindex]))
           {
               var textLH = Chart.DrawText("LH" + customindex, "LH", customindex, LH[customindex], Color.DeepSkyBlue);
               textLH.VerticalAlignment = VerticalAlignment.Bottom;
               textLH.HorizontalAlignment = HorizontalAlignment.Center;
           }
       }
   }
}