how to add option for previos pivot
Created at 03 Mar 2023, 09:40
how to add option for previos pivot
03 Mar 2023, 09:40
in this code how could we add an option to have pivot just for present day?
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
public class AllPivots : Indicator
{
private DateTime _previousPeriodStartTime;
private int _previousPeriodStartIndex;
private TimeFrame PivotTimeFrame;
public string PP;
[Parameter("Pivot Color", DefaultValue = "Blue", Group = "Pivot")]
public Color PivotColor { get; set; }
[Parameter("Support Color", DefaultValue = "Red", Group = "Pivot")]
public Color SupportColor { get; set; }
[Parameter("Resistance Color", DefaultValue = "Red", Group = "Pivot")]
public Color ResistanceColor { get; set; }
[Parameter("Thicknes Pivot", DefaultValue = 1, Group = "Pivot")]
public int Thicknes { get; set; }
[Parameter("Pivot Line", DefaultValue = LineStyle.Solid, Group = "Pivot")]
public LineStyle PivotLine { get; set; }
[Parameter("Median Color", DefaultValue = "Gray", Group = "Median")]
public Color MedianColor { get; set; }
[Parameter("Thicknes Pivot Median", DefaultValue = 1, Group = "Median")]
public int ThicknesM { get; set; }
[Parameter(" Median Line", DefaultValue = LineStyle.Lines, Group = "Median")]
public LineStyle MedianLine { get; set; }
[Parameter("Show Labels", DefaultValue = true)]
public bool ShowLabels { get; set; }
protected override void Initialize()
{
if (TimeFrame <= TimeFrame.Hour12)
PivotTimeFrame = TimeFrame.Daily;
else if (TimeFrame < TimeFrame.Daily)
{
PivotTimeFrame = TimeFrame.Weekly;
}
else
PivotTimeFrame = TimeFrame.Monthly;
}
private DateTime GetStartOfPeriod(DateTime dateTime)
{
return CutToOpenByNewYork(dateTime, PivotTimeFrame);
}
private DateTime GetEndOfPeriod(DateTime dateTime)
{
if (PivotTimeFrame == TimeFrame.Monthly)
{
return new DateTime(dateTime.Year, dateTime.Month, 1).AddMonths(1);
}
return AddPeriod(CutToOpenByNewYork(dateTime, PivotTimeFrame), PivotTimeFrame);
}
public override void Calculate(int index)
{
var currentPeriodStartTime = GetStartOfPeriod(Bars.OpenTimes[index]);
if (currentPeriodStartTime == _previousPeriodStartTime)
return;
if (index > 0)
CalculatePivots(_previousPeriodStartTime, _previousPeriodStartIndex, currentPeriodStartTime, index);
_previousPeriodStartTime = currentPeriodStartTime;
_previousPeriodStartIndex = index;
}
private void CalculatePivots(DateTime startTime, int startIndex, DateTime startTimeOfNextPeriod, int index)
{
var high = Bars.HighPrices[startIndex];
var low = Bars.LowPrices[startIndex];
var close = Bars.ClosePrices[startIndex];
var i = startIndex + 1;
while (GetStartOfPeriod(Bars.OpenTimes[i]) == startTime && i < Bars.ClosePrices.Count)
{
high = Math.Max(high, Bars.HighPrices[i]);
low = Math.Min(low, Bars.LowPrices[i]);
close = Bars.ClosePrices[i];
i++;
}
var pivotStartTime = startTimeOfNextPeriod;
var pivotEndTime = GetEndOfPeriod(startTimeOfNextPeriod);
var pivot = (high + low + close) / 3;
var r1 = 2 * pivot - low;
var s1 = 2 * pivot - high;
var r2 = pivot + high - low;
var s2 = pivot - high + low;
var r3 = high + 2 * (pivot - low);
var s3 = low - 2 * (high - pivot);
var r4 = r3 + pivot - low;
var s4 = s3 + pivot - high;
var m0 = (s4 + s3) / 2;
var m1 = (s3 + s2) / 2;
var m2 = (s2 + s1) / 2;
var m3 = (s1 + pivot) / 2;
var m4 = (pivot + r1) / 2;
var m5 = (r1 + r2) / 2;
var m6 = (r2 + r3) / 2;
var m7 = (r3 + r4) / 2;
Chart.DrawTrendLine("pivot " + startIndex, pivotStartTime, pivot, pivotEndTime, pivot, PivotColor, Thicknes, PivotLine);
// Resistance line
double[] arrayResistnace = { r1, r2, r3, r4 };
foreach (var element in arrayResistnace)
{
Chart.DrawTrendLine(element.ToString() + startIndex, pivotStartTime, element, pivotEndTime, element, ResistanceColor, Thicknes, PivotLine);
}
//Support Line
double[] arraySupports = { s1, s2, s3, s4 };
foreach (var element in arraySupports)
{
Chart.DrawTrendLine(element.ToString() + startIndex, pivotStartTime, element, pivotEndTime, element, SupportColor, Thicknes, PivotLine);
}
//MedianLine
double[] arrayMedian = { m0, m1, m2, m3, m4, m5, m6, m7 };
foreach (var element in arrayMedian)
{
Chart.DrawTrendLine(element.ToString() + startIndex, pivotStartTime, element, pivotEndTime, element, MedianColor, Thicknes, PivotLine);
}
if (!ShowLabels)
return;
Chart.DrawText("Lpivot " + startIndex, "PP" + "=" + pivot.ToString("0.00000"), index, pivot, PivotColor);
// Resistance line
foreach (var element in arrayResistnace)
{
Chart.DrawText("L" + element.ToString() + startIndex, element + "=" + element.ToString("0.00000"), index, element, ResistanceColor);
}
//Support Line
foreach (var element in arraySupports)
{
Chart.DrawText("L" + element.ToString() + startIndex, element.ToString() + "=" + element.ToString("0.00000"), index, element, SupportColor);
}
//MedianLine
foreach (var element in arrayMedian)
{
Chart.DrawText("L" + element.ToString() + startIndex, element.ToString() + "=" + element.ToString("0.00000"), index, element, MedianColor);
}
}
private static DateTime CutToOpenByNewYork(DateTime date, TimeFrame timeFrame)
{
if (timeFrame == TimeFrame.Daily)
{
var hourShift = (date.Hour + 24 - 17) % 24;
return new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0, DateTimeKind.Unspecified).AddHours(-hourShift);
}
if (timeFrame == TimeFrame.Weekly)
return GetStartOfTheWeek(date);
if (timeFrame == TimeFrame.Monthly)
{
return new DateTime(date.Year, date.Month, 1, 0, 0, 0, DateTimeKind.Unspecified);
}
throw new ArgumentException(string.Format("Unknown timeframe: {0}", timeFrame), "timeFrame");
}
private static DateTime GetStartOfTheWeek(DateTime dateTime)
{
return dateTime.Date.AddDays((double)DayOfWeek.Sunday - (double)dateTime.Date.DayOfWeek).AddHours(-7);
}
public DateTime AddPeriod(DateTime dateTime, TimeFrame timeFrame)
{
if (timeFrame == TimeFrame.Daily)
{
return dateTime.AddDays(1);
}
if (timeFrame == TimeFrame.Weekly)
{
return dateTime.AddDays(7);
}
if (timeFrame == TimeFrame.Monthly)
return dateTime.AddMonths(1);
throw new ArgumentException(string.Format("Unknown timeframe: {0}", timeFrame), "timeFrame");
}
}
}