Description
Add an overlay to your chart based on selected hours and minutes. Developed for myself, to overlay out-of-cash market hours, might be not perfect, but works.
Changelog:
- v0.1 - initial code
- v0.2 - covering area before reaching "new" end hour/minute, previously indicator was not painting
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class HoursOverlay : Indicator
{
[Parameter("Hour start", Group = "Start (UTC)", MinValue = 0, MaxValue = 23, Step = 1, DefaultValue = 9)]
public int hourStart { get; set; }
[Parameter("Minute start", Group = "Start (UTC)", MinValue = 0, MaxValue = 59, Step = 1, DefaultValue = 0)]
public int minuteStart { get; set; }
[Parameter("Hour end", Group = "End (UTC)", MinValue = 0, MaxValue = 23, Step = 1, DefaultValue = 17)]
public int hourEnd { get; set; }
[Parameter("Minute end", Group = "End (UTC)", MinValue = 0, MaxValue = 59, Step = 1, DefaultValue = 30)]
public int minuteEnd { get; set; }
[Parameter("Red", Group = "Color", MinValue = 0, MaxValue = 255, DefaultValue = 125)]
public int red { get; set; }
[Parameter("Green", Group = "Color", MinValue = 0, MaxValue = 255, DefaultValue = 125)]
public int green { get; set; }
[Parameter("Blue", Group = "Color", MinValue = 0, MaxValue = 255, DefaultValue = 125)]
public int blue { get; set; }
[Parameter("Alpha", Group = "Color", MinValue = 0, MaxValue = 255, DefaultValue = 120)]
public int alpha { get; set; }
private bool startedPainting;
private int paintStart;
private List<ChartRectangle> rectangles;
private bool repaintingRects;
private int repaintingStarted;
protected override void Initialize()
{
startedPainting = false;
rectangles = new List<ChartRectangle>();
Chart.ScrollChanged += onChangeRecalcRectangles;
}
public override void Calculate(int index)
{
if (timeFrameTooHigh(Chart.TimeFrame))
{
return;
}
if (!startedPainting)
{
startedPainting = Bars.OpenTimes[index].Hour == hourStart && Bars.OpenTimes[index].Minute == minuteStart;
paintStart = index;
}
if (startedPainting)
{
if (reachedEndOfRectangle(index) || reachedEndOfChart())
{
var r = Chart.DrawRectangle("i" + index, Bars.OpenTimes[paintStart], Chart.TopY, Bars.OpenTimes[index - 1], Chart.BottomY, Color.FromArgb(alpha, red, green, blue));
r.IsFilled = true;
rectangles.Add(r);
startedPainting = false;
}
}
}
void onChangeRecalcRectangles(Object args)
{
var now = System.DateTime.Now.Millisecond;
if (repaintingRects && (now - repaintingStarted) < 50)
{
return;
}
repaintingStarted = now;
repaintingRects = true;
foreach (var r in rectangles)
{
r.Y1 = Chart.TopY;
r.Y2 = Chart.BottomY;
}
repaintingRects = false;
}
private bool reachedEndOfRectangle(int index)
{
return Bars.OpenTimes[index].Hour == hourEnd && Bars.OpenTimes[index].Minute == minuteEnd;
}
private bool reachedEndOfChart()
{
return IsLastBar;
}
private bool timeFrameTooHigh(TimeFrame chartTimeFrame)
{
return chartTimeFrame >= TimeFrame.Daily;
}
}
}
KA
kamilsz
Joined on 23.12.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: HoursOverlay.algo
- Rating: 5
- Installs: 1478
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
Thank you for this. It seems to be working very well!