mark the start of each month on the chart

Created at 02 Aug 2022, 20:13
AlgoCreators's avatar

AlgoCreators

Joined 16.01.2022

mark the start of each month on the chart
02 Aug 2022, 20:13


I need a code to mark the start of each month on the chart

Markings should be applied for the last twelve months


please guide me


@AlgoCreators
Replies

paolo.panicali
03 Aug 2022, 02:47

maybe this can help you

// Month Separator by ppanicali2022//last 12 month  separator

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Globalization;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.CentralEuropeanStandardTime, AccessRights = AccessRights.None)]
    public class MonthSeparator : Indicator
    {
        public override void Calculate(int index)
        {
            if (Bars.OpenTimes[index - 1].Month == Bars.OpenTimes[index].AddMonths(-1).Month && Bars.OpenTimes[index - 1] >= DateTime.Now.AddYears(-1))
            {
                Chart.DrawVerticalLine("MonthStart" + index, index, Color.Red);
                Chart.DrawText("MonthName" + index, DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(Bars.OpenTimes[index].Month), index, Bars.ClosePrices[index], Color.Green);
            }
        }
    }
}


@paolo.panicali

AlgoCreators
03 Aug 2022, 08:06 ( Updated at: 21 Dec 2023, 09:22 )

RE: maybe this can help you

paolo.panicali said:

// Month Separator by ppanicali2022//last 12 month  separator

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Globalization;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.CentralEuropeanStandardTime, AccessRights = AccessRights.None)]
    public class MonthSeparator : Indicator
    {
        public override void Calculate(int index)
        {
            if (Bars.OpenTimes[index - 1].Month == Bars.OpenTimes[index].AddMonths(-1).Month && Bars.OpenTimes[index - 1] >= DateTime.Now.AddYears(-1))
            {
                Chart.DrawVerticalLine("MonthStart" + index, index, Color.Red);
                Chart.DrawText("MonthName" + index, DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(Bars.OpenTimes[index].Month), index, Bars.ClosePrices[index], Color.Green);
            }
        }
    }
}

Thank you
Your code works fine
I myself came to the answer in a different way

Bars bars_D1 = MarketData.GetBars(TimeFrame.Daily, Symbol.Name);
                Color color = "#24FE00FD";
                for (int i = 0; i < 200; i++)
                {
                    if (bars_D1.Last(i).OpenTime.Month != bars_D1.Last(i+1).OpenTime.Month)
                    {
                        rect = Chart.DrawRectangle("Candle_D1" + bars_D1.Last(i).OpenTime, bars_D1.Last(i).OpenTime, bars_D1.Last(i).High, bars_D1.Last(0).OpenTime, bars_D1.Last(i).Low, color);
                        rect.Comment = "Candle_D1";
                        rect.IsInteractive = true;
                        rect.IsFilled = true;
                    }
                }

 


@AlgoCreators