Description
Follow my cTrader Telegram group at https://t.me/cTraderCommunity; it's a new community but it will grow fast, plus everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them.
This is an indicator similar to the hour stagionality indicator: https://ctrader.com/algos/show/1917, but, as the name says, work on monthly data.
Those who use cTrader are lucky since there is a minimum of 10 years of history for the majority of symbols.
For any bug or suggestion, comment below or talk to me directly by joining the group linked above.
There's no image for this indicator cause it's the same as the one cited above.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Threading;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class StagionalityMonthly : Indicator
{
[Parameter()]
public DataSeries source { get; set; }
[Parameter("TimeZone", DefaultValue = 2)]
public int tz { get; set; }
[Output("Main", PlotType = PlotType.Histogram, Thickness = 1, Color = Colors.White, LineStyle = LineStyle.DotsRare)]
public IndicatorDataSeries Result { get; set; }
public int startIndex, endIndex;
public bool ctrlClick = false, altClick = false;
public bool mouseDown = false;
protected override void Initialize()
{
Chart.MouseDown += OnChartMouseDown;
Chart.ScrollChanged += OnChartScrollChanged;
Chart.MouseUp += OnChartMouseUp;
}
void OnChartMouseUp(ChartMouseEventArgs obj)
{
if (ctrlClick && altClick)
_Calculate(Chart.BarsTotal);
mouseDown = false;
}
//alternative to the mouse-up refresh method
void OnChartScrollChanged(ChartScrollEventArgs obj)
{
if (ctrlClick && altClick && !mouseDown)
_Calculate(Chart.BarsTotal);
}
void OnChartMouseDown(ChartMouseEventArgs obj)
{
mouseDown = true;
if (obj.CtrlKey && !obj.AltKey && !obj.ShiftKey)
{
startIndex = (int)obj.BarIndex;
ctrlClick = true;
Chart.DrawVerticalLine("start", startIndex, Color.White, 1, LineStyle.DotsRare);
}
else if (obj.AltKey && !obj.CtrlKey && !obj.ShiftKey)
{
endIndex = (int)obj.BarIndex;
altClick = true;
Chart.DrawVerticalLine("end", endIndex, Color.White, 1, LineStyle.DotsRare);
}
if ((obj.CtrlKey || obj.AltKey) && !obj.ShiftKey && ctrlClick && altClick)
_Calculate(Chart.BarsTotal);
}
public override void Calculate(int index)
{
}
public void _Calculate(int index)
{
for (int i = 0; i < index; i++)
{
Result[i] = double.NaN;
//chart cleaning
}
double[] months = new double[12];
for (int i = startIndex; i < endIndex; i++)
{
int opentime = MarketSeries.OpenTime[i].Month - 1;
//gathering the data
months[opentime] += source[i] - source[i - 1];
}
int nOfBars = Chart.LastVisibleBarIndex - Chart.FirstVisibleBarIndex;
int segmentsWidth = nOfBars / 12;
double hoursMax = 0;
for (int i = 0; i < months.Length; i++)
{
hoursMax = Math.Max(hoursMax, months[i]);
}
//charting the data
for (int i = 0; i < 12; i++)
{
IndicatorArea.DrawVerticalLine("zone" + i, Chart.FirstVisibleBarIndex + i * segmentsWidth, Color.White);
for (int k = Chart.FirstVisibleBarIndex + i * segmentsWidth; k < Chart.FirstVisibleBarIndex + (i + 1) * segmentsWidth; k++)
{
if (k - Chart.FirstVisibleBarIndex - i * segmentsWidth == Math.Ceiling((double)segmentsWidth / 2))
{
int X = Chart.FirstVisibleBarIndex + i * segmentsWidth + (k - Chart.FirstVisibleBarIndex - i * segmentsWidth);
IndicatorArea.DrawText("label" + i, (i + 1).ToString(), X, hoursMax, Color.AliceBlue);
}
Result[k] = months[i];
}
}
}
}
}
cysecsbin.01
Joined on 10.11.2018 Blocked
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: Stagionality Monthly.algo
- Rating: 5
- Installs: 1389
- Modified: 13/10/2021 09:54