Logical error I cannot see
Created at 01 Jan 2024, 18:20
MI
Logical error I cannot see
01 Jan 2024, 18:20
Goal : Create support and resistance lines using a formula and drawing them using trendlines . Works when timeframe is let to be default but when try and change it to a highertime frame which is the initial goal the lines are all over the place and not in the right area
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
public class NewIndicator : Indicator
{
private Bars BarsDaily;
protected override void Initialize()
{
BarsDaily = MarketData.GetBars(TimeFrame.Daily);
}
public override void Calculate(int index)
{
// Check if a new day has started
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
// Find the start and end of the current day using Bars
DateTime startOfDay = Bars.OpenTimes[index].Date;
DateTime endOfDay = startOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
// Calculate pivot points for the current day
double HighValue = BarsDaily.HighPrices[index];
double LowValue = BarsDaily.LowPrices[index];
double CloseValue = BarsDaily.ClosePrices[index];
double PPValue = (HighValue + LowValue + CloseValue) / 3;
double S1Value = (PPValue * 2) - HighValue;
double S2Value = PPValue - (HighValue - LowValue);
double S3Value = LowValue - 2 * (HighValue - PPValue);
double R1Value = (PPValue * 2) - LowValue;
double R2Value = PPValue + (HighValue - LowValue);
double R3Value = HighValue + 2 * (PPValue - LowValue);
// Draw trendlines at pivot levels
DrawHorizontalLine("PP Line", startOfDay, PPValue, endOfDay, PPValue, Color.Yellow);
DrawHorizontalLine("S1 Line", startOfDay, S1Value, endOfDay, S1Value, Color.Red);
DrawHorizontalLine("S2 Line", startOfDay, S2Value, endOfDay, S2Value, Color.Red);
DrawHorizontalLine("S3 Line", startOfDay, S3Value, endOfDay, S3Value, Color.Red);
DrawHorizontalLine("R1 Line", startOfDay, R1Value, endOfDay, R1Value, Color.Green);
DrawHorizontalLine("R2 Line", startOfDay, R2Value, endOfDay, R2Value, Color.Green);
DrawHorizontalLine("R3 Line", startOfDay, R3Value, endOfDay, R3Value, Color.Green);
}
}
private void DrawHorizontalLine(string name, DateTime startTime, double startY, DateTime endTime, double endY, Color color)
{
var trendLine = Chart.DrawTrendLine(name, startTime, startY, endTime, endY, color, 2, LineStyle.Solid);
trendLine.IsInteractive = true;
}
}
}