Topics
Replies
mihlali700
06 Jan 2024, 08:38
RE: RE: RE: Why does printing this give me random number ?
PanagiotisCharalampous said:
mihlali700 said:
PanagiotisCharalampous said:
Hi there,
The close price changes on every tick therefore it is expected that the results would change. Can you explain to us what your expectation is?
Best regards,
Panagiotis
No I understand why it would be different at every tick but the way its different makes I unusable , I expect the numbers printing to be somewhat close to each other instead they are not, they are widely different with it even printing “NaN”
It's obviously a logical bug on your side but it's hard to tell by a code snippet. We are just guessing. You would need to provide more information in order to get more help. Post at least the following
- The full indicator code
- Screenshots of the values you get.
- The values you would expect to get instead and an explanation why you expect them.
What I'm trying to print out is the difference of pips between two prices , which is PPValue and the currentprice which gives Pppips but since it prints out like this I cant really nail down why theres a huge variance in the values printed
Here is the full code :
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
public class Pivots : Indicator
{
[Parameter(DefaultValue = 10)]
public int AlertPips { get; set; }
[Parameter("Pivot Calculation Method", DefaultValue = PivotCalculationMethod.Standard)]
public PivotCalculationMethod CalculationMethod { get; set; }
public enum PivotCalculationMethod
{
Standard,
Fibonacci,
Woodie,
}
private Bars BarsDaily;
protected override void Initialize()
{
BarsDaily = MarketData.GetBars(TimeFrame.Daily);
}
public override void Calculate(int index)
{
switch (CalculationMethod)
{
case PivotCalculationMethod.Standard:
// Standard
Standardpivotformula(index);
break;
case PivotCalculationMethod.Fibonacci:
FibonacciPivotformula(index);
break;
case PivotCalculationMethod.Woodie:
// Woodie forumla
WoodiePivotFormula(index);
break;
}
}
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;
}
private void Standardpivotformula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// 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[barsDailyIndex];
double LowValue = BarsDaily.LowPrices[barsDailyIndex];
double CloseValue = BarsDaily.ClosePrices[barsDailyIndex];
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);
var currentprice = Bars.ClosePrices[index-1];
var PPPips = Math.Abs((currentprice - PPValue) / Symbol.PipSize);
var S1Pips = Math.Abs((currentprice - S1Value) / Symbol.PipSize);
var S2Pips = Math.Abs((currentprice - S2Value) / Symbol.PipSize);
var S3Pips = Math.Abs((currentprice - S3Value) / Symbol.PipSize);
var R1Pips = Math.Abs((currentprice - R1Value) / Symbol.PipSize);
var R2Pips = Math.Abs((currentprice - R2Value) / Symbol.PipSize);
var R3Pips = Math.Abs((currentprice - R3Value) / Symbol.PipSize);
Print(PPPips);
if ( PPPips <= AlertPips ) {
Print("Price is above or below PPPis");
}
// 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 WoodiePivotFormula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// 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 prevHigh = BarsDaily.HighPrices[barsDailyIndex - 1];
double prevLow = BarsDaily.LowPrices[barsDailyIndex - 1];
double todayOpen = BarsDaily.OpenPrices[barsDailyIndex];
double prevRange = prevHigh - prevLow;
double PPValue = (prevHigh + prevLow + (todayOpen * 2)) / 4;
double R1Value = (PPValue * 2) - prevLow;
double R2Value = PPValue + prevRange;
double S1Value = (PPValue * 2) - prevHigh;
double S2Value = PPValue - prevRange;
double S3Value = (prevLow - 2 * (prevHigh - PPValue));
double S4Value = (S3Value - prevRange);
double R3Value = (prevHigh + 2 * (PPValue - prevLow));
double R4Value = (R3Value + prevRange);
// 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);
}
}
public void FibonacciPivotformula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
double prevHigh = BarsDaily.HighPrices[barsDailyIndex - 1];
double prevLow = BarsDaily.LowPrices[barsDailyIndex - 1];
double prevClose = BarsDaily.ClosePrices[barsDailyIndex - 1];
double prevRange = prevHigh - prevLow;
double Pivot = (prevHigh + prevLow + prevClose) / 3;
double R38 = Pivot + (prevRange * 0.382);
double R61 = Pivot + (prevRange * 0.618);
double R78 = Pivot + (prevRange * 0.786);
double R100 = Pivot + (prevRange * 1.000);
double R138 = Pivot + (prevRange * 1.382);
double R161 = Pivot + (prevRange * 1.618);
double R200 = Pivot + (prevRange * 2.000);
double S38 = Pivot - (prevRange * 0.382);
double S61 = Pivot - (prevRange * 0.618);
double S78 = Pivot - (prevRange * 0.786);
double S100 = Pivot - (prevRange * 1.000);
double S138 = Pivot - (prevRange * 1.382);
double S161 = Pivot - (prevRange * 1.618);
double S200 = Pivot - (prevRange * 2.000);
DateTime startOfDay = Bars.OpenTimes[index].Date;
DateTime endOfDay = startOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
// Draw trendlines at pivot levels
DrawHorizontalLine("Pivot Line", startOfDay, Pivot, endOfDay, Pivot, Color.Yellow);
DrawHorizontalLine("R38 Line", startOfDay, R38, endOfDay, R38, Color.Red);
DrawHorizontalLine("R61 Line", startOfDay, R61, endOfDay, R61, Color.Red);
DrawHorizontalLine("R78 Line", startOfDay, R78, endOfDay, R78, Color.Red);
DrawHorizontalLine("R100 Line", startOfDay, R100, endOfDay, R100, Color.Green);
DrawHorizontalLine("R138 Line", startOfDay, R138, endOfDay, R138, Color.Green);
DrawHorizontalLine("R161 Line", startOfDay, R161, endOfDay, R161, Color.Green);
DrawHorizontalLine("R200 Line", startOfDay, R200, endOfDay, R200, Color.Green);
DrawHorizontalLine("S38 Line", startOfDay, S38, endOfDay, S38, Color.Red);
DrawHorizontalLine("S61 Line", startOfDay, S61, endOfDay, S61, Color.Red);
DrawHorizontalLine("S78 Line", startOfDay, S78, endOfDay, S78, Color.Red);
DrawHorizontalLine("S100 Line", startOfDay, S100, endOfDay, S100, Color.Blue);
DrawHorizontalLine("S138 Line", startOfDay, S138, endOfDay, S138, Color.Blue);
DrawHorizontalLine("S161 Line", startOfDay, S161, endOfDay, S161, Color.Blue);
DrawHorizontalLine("S200 Line", startOfDay, S200, endOfDay, S200, Color.Blue);
}
}
}
}
@mihlali700
mihlali700
06 Jan 2024, 08:38
RE: RE: RE: Why does printing this give me random number ?
PanagiotisCharalampous said:
mihlali700 said:
PanagiotisCharalampous said:
Hi there,
The close price changes on every tick therefore it is expected that the results would change. Can you explain to us what your expectation is?
Best regards,
Panagiotis
No I understand why it would be different at every tick but the way its different makes I unusable , I expect the numbers printing to be somewhat close to each other instead they are not, they are widely different with it even printing “NaN”
It's obviously a logical bug on your side but it's hard to tell by a code snippet. We are just guessing. You would need to provide more information in order to get more help. Post at least the following
- The full indicator code
- Screenshots of the values you get.
- The values you would expect to get instead and an explanation why you expect them.
What I'm trying to print out is the difference of pips between two prices , which is PPValue and the currentprice which gives Pppips but since it prints out like this I cant really nail down why theres a huge variance in the values printed
Here is the full code :
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
public class Pivots : Indicator
{
[Parameter(DefaultValue = 10)]
public int AlertPips { get; set; }
[Parameter("Pivot Calculation Method", DefaultValue = PivotCalculationMethod.Standard)]
public PivotCalculationMethod CalculationMethod { get; set; }
public enum PivotCalculationMethod
{
Standard,
Fibonacci,
Woodie,
}
private Bars BarsDaily;
protected override void Initialize()
{
BarsDaily = MarketData.GetBars(TimeFrame.Daily);
}
public override void Calculate(int index)
{
switch (CalculationMethod)
{
case PivotCalculationMethod.Standard:
// Standard
Standardpivotformula(index);
break;
case PivotCalculationMethod.Fibonacci:
FibonacciPivotformula(index);
break;
case PivotCalculationMethod.Woodie:
// Woodie forumla
WoodiePivotFormula(index);
break;
}
}
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;
}
private void Standardpivotformula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// 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[barsDailyIndex];
double LowValue = BarsDaily.LowPrices[barsDailyIndex];
double CloseValue = BarsDaily.ClosePrices[barsDailyIndex];
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);
var currentprice = Bars.ClosePrices[index-1];
var PPPips = Math.Abs((currentprice - PPValue) / Symbol.PipSize);
var S1Pips = Math.Abs((currentprice - S1Value) / Symbol.PipSize);
var S2Pips = Math.Abs((currentprice - S2Value) / Symbol.PipSize);
var S3Pips = Math.Abs((currentprice - S3Value) / Symbol.PipSize);
var R1Pips = Math.Abs((currentprice - R1Value) / Symbol.PipSize);
var R2Pips = Math.Abs((currentprice - R2Value) / Symbol.PipSize);
var R3Pips = Math.Abs((currentprice - R3Value) / Symbol.PipSize);
Print(PPPips);
if ( PPPips <= AlertPips ) {
Print("Price is above or below PPPis");
}
// 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 WoodiePivotFormula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// 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 prevHigh = BarsDaily.HighPrices[barsDailyIndex - 1];
double prevLow = BarsDaily.LowPrices[barsDailyIndex - 1];
double todayOpen = BarsDaily.OpenPrices[barsDailyIndex];
double prevRange = prevHigh - prevLow;
double PPValue = (prevHigh + prevLow + (todayOpen * 2)) / 4;
double R1Value = (PPValue * 2) - prevLow;
double R2Value = PPValue + prevRange;
double S1Value = (PPValue * 2) - prevHigh;
double S2Value = PPValue - prevRange;
double S3Value = (prevLow - 2 * (prevHigh - PPValue));
double S4Value = (S3Value - prevRange);
double R3Value = (prevHigh + 2 * (PPValue - prevLow));
double R4Value = (R3Value + prevRange);
// 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);
}
}
public void FibonacciPivotformula(int index)
{
if (Bars.OpenTimes[index].Date != Bars.OpenTimes[index - 1].Date)
{
var barsDailyIndex = BarsDaily.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
double prevHigh = BarsDaily.HighPrices[barsDailyIndex - 1];
double prevLow = BarsDaily.LowPrices[barsDailyIndex - 1];
double prevClose = BarsDaily.ClosePrices[barsDailyIndex - 1];
double prevRange = prevHigh - prevLow;
double Pivot = (prevHigh + prevLow + prevClose) / 3;
double R38 = Pivot + (prevRange * 0.382);
double R61 = Pivot + (prevRange * 0.618);
double R78 = Pivot + (prevRange * 0.786);
double R100 = Pivot + (prevRange * 1.000);
double R138 = Pivot + (prevRange * 1.382);
double R161 = Pivot + (prevRange * 1.618);
double R200 = Pivot + (prevRange * 2.000);
double S38 = Pivot - (prevRange * 0.382);
double S61 = Pivot - (prevRange * 0.618);
double S78 = Pivot - (prevRange * 0.786);
double S100 = Pivot - (prevRange * 1.000);
double S138 = Pivot - (prevRange * 1.382);
double S161 = Pivot - (prevRange * 1.618);
double S200 = Pivot - (prevRange * 2.000);
DateTime startOfDay = Bars.OpenTimes[index].Date;
DateTime endOfDay = startOfDay.AddHours(23).AddMinutes(59).AddSeconds(59);
// Draw trendlines at pivot levels
DrawHorizontalLine("Pivot Line", startOfDay, Pivot, endOfDay, Pivot, Color.Yellow);
DrawHorizontalLine("R38 Line", startOfDay, R38, endOfDay, R38, Color.Red);
DrawHorizontalLine("R61 Line", startOfDay, R61, endOfDay, R61, Color.Red);
DrawHorizontalLine("R78 Line", startOfDay, R78, endOfDay, R78, Color.Red);
DrawHorizontalLine("R100 Line", startOfDay, R100, endOfDay, R100, Color.Green);
DrawHorizontalLine("R138 Line", startOfDay, R138, endOfDay, R138, Color.Green);
DrawHorizontalLine("R161 Line", startOfDay, R161, endOfDay, R161, Color.Green);
DrawHorizontalLine("R200 Line", startOfDay, R200, endOfDay, R200, Color.Green);
DrawHorizontalLine("S38 Line", startOfDay, S38, endOfDay, S38, Color.Red);
DrawHorizontalLine("S61 Line", startOfDay, S61, endOfDay, S61, Color.Red);
DrawHorizontalLine("S78 Line", startOfDay, S78, endOfDay, S78, Color.Red);
DrawHorizontalLine("S100 Line", startOfDay, S100, endOfDay, S100, Color.Blue);
DrawHorizontalLine("S138 Line", startOfDay, S138, endOfDay, S138, Color.Blue);
DrawHorizontalLine("S161 Line", startOfDay, S161, endOfDay, S161, Color.Blue);
DrawHorizontalLine("S200 Line", startOfDay, S200, endOfDay, S200, Color.Blue);
}
}
}
}
@mihlali700
mihlali700
05 Jan 2024, 14:36
RE: Why does printing this give me random number ?
PanagiotisCharalampous said:
Hi there,
The close price changes on every tick therefore it is expected that the results would change. Can you explain to us what your expectation is?
Best regards,
Panagiotis
No I understand why it would be different at every tick but the way its different makes I unusable , I expect the numbers printing to be somewhat close to each other instead they are not, they are widely different with it even printing “NaN”
@mihlali700
mihlali700
05 Jan 2024, 14:36
RE: Why does printing this give me random number ?
PanagiotisCharalampous said:
Hi there,
The close price changes on every tick therefore it is expected that the results would change. Can you explain to us what your expectation is?
Best regards,
Panagiotis
No I understand why it would be different at every tick but the way its different makes I unusable , I expect the numbers printing to be somewhat close to each other instead they are not, they are widely different with it even printing “NaN”
@mihlali700
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
mihlali700
06 Jan 2024, 08:38
RE: RE: RE: Why does printing this give me random number ?
PanagiotisCharalampous said:
What I'm trying to print out is the difference of pips between two prices , which is PPValue and the currentprice which gives Pppips but since it prints out like this I cant really nail down why theres a huge variance in the values printed
Here is the full code :
@mihlali700