Logical error I cannot see
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;
}
}
}
Replies
mihlali700
02 Jan 2024, 09:51
( Updated at: 03 Jan 2024, 06:51 )
RE: Logical error I cannot see
PanagiotisCharalampous said:
Hi there,
You cannot use the current index with the BarsDaily collection. You need to match the index with correct BarsDaily index. Try something like the following
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
Best regards,
Panagiotis
Wow, thank you . That solved my problem , I would just like to know is there a newbar function inbuilt into ctrader ? I would like to change my if statement to check if theres a new bar in the daily timeframe or weekly etc before drawing my supports and resistance
@mihlali700
PanagiotisCharalampous
03 Jan 2024, 07:31
RE: RE: Logical error I cannot see
mihlali700 said:
PanagiotisCharalampous said:
Hi there,
You cannot use the current index with the BarsDaily collection. You need to match the index with correct BarsDaily index. Try something like the following
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
Best regards,
Panagiotis
Wow, thank you . That solved my problem , I would just like to know is there a newbar function inbuilt into ctrader ? I would like to change my if statement to check if theres a new bar in the daily timeframe or weekly etc before drawing my supports and resistance
Hi there,
You can use the BarOpened event.
Best regards,
@PanagiotisCharalampous
PanagiotisCharalampous
02 Jan 2024, 06:42
Hi there,
You cannot use the current index with the BarsDaily collection. You need to match the index with correct BarsDaily index. Try something like the following
Best regards,
Panagiotis
@PanagiotisCharalampous