Why do I get a NaN?
Why do I get a NaN?
04 Aug 2020, 10:12
for (int i = 0; m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] <= m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time2))]; i++)
{
var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for loop: low for this time is" + PRICE);
}
There are time when a price is printed to the log, but sometimes I get a NaN. What are the reasons why I got a NaN?
Replies
fang0092
04 Aug 2020, 10:53
RE:
PanagiotisCharalampous said:
Hi fang0092,
Please provide the complete code so that we can reproduce the behavior.
Best Regards,
Panagiotis
- Run the code.
- Go to 4hour timeframe of WTI.
- Click on the button on the upper left.
- Drag green line to 40.676.
- Drag red line to 39.275.
- Hold ctrl and click on the chart to draw a rectangle with time 1 at July 9, 2020 17:00 to July 13,2020 05:00. User time is UTC+8.
- last print to log-> for: price_tobreak is 40.676 while low for this time isNaN
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewIndicator : Indicator
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
[Parameter(DefaultValue = "2F68D0F7")]
public string SelectedColor { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
ChartHorizontalLine RFinishLine;
ChartHorizontalLine GStartLine;
ChartRectangle Rect1;
ControlBase DrawingDialog;
protected override void Initialize()
{
Chart.MouseDown += OnChartMouseDown;
Chart.MouseMove += OnChartMouseMove;
Chart.MouseUp += OnChartMouseUp;
Chart.ObjectUpdated += OnChartObjectUpdated;
TheChoices();
}
void OnHor_LineClick(ButtonClickEventArgs obj)
{
RFinishLine = DrawRHL1();
GStartLine = DrawBHL2();
RFinishLine.IsInteractive = true;
GStartLine.IsInteractive = true;
Print(RFinishLine.Name + " was created at price " + RFinishLine.Y);
Print(GStartLine.Name + " was created at price " + GStartLine.Y);
}
void OnChartMouseDown(ChartMouseEventArgs obj)
{
if (obj.CtrlKey == false)
{
return;
}
if (obj.CtrlKey == true)
{
Rect1 = DrawRect1(obj.TimeValue);
Chart.IsScrollingEnabled = false;
}
}
void OnChartMouseMove(ChartMouseEventArgs obj)
{
if (Rect1 == null)
{
return;
}
Rect1.Time2 = obj.TimeValue;
}
void OnChartObjectUpdated(ChartObjectUpdatedEventArgs obj)
{
if (obj.ChartObject.Name == RFinishLine.Name)
{
Print(RFinishLine.Name + " was moved to price " + RFinishLine.Y);
}
if (obj.ChartObject.Name == GStartLine.Name)
{
Print(GStartLine.Name + " was moved to price " + GStartLine.Y);
}
}
private void DETm1bar_thatbreaksGStartLine_pos()
{
double priceTG_toreach = RFinishLine.Y;
double price_tobreak = GStartLine.Y;
// ONLY gets the bars of one minute timeframe
var m1Bars = MarketData.GetBars(TimeFrame.Minute);
if (RFinishLine.Y > GStartLine.Y)
{
Print("first if statement works");
for (int i = 0; m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i <= m1Bars.OpenTimes.GetIndexByTime(Rect1.Time2); i++)
{
var PRICE = m1Bars.HighPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for statement: price_tobreak is " + price_tobreak + " while high for this time is" + PRICE);
if (m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] > price_tobreak)
{
Print("2nd IF");
DateTime m1bar_thatbreaksGStartLine_pos = m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
Print("The position is at " + m1bar_thatbreaksGStartLine_pos + " with the price of " + m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i]);
ChartVerticalLine LineOfm1bar_thatbreaksStartLinepos = Chart.DrawVerticalLine("LineThatIndicates m1bar_thatbreaksGStartLine_pos" + m1bar_thatbreaksGStartLine_pos, m1bar_thatbreaksGStartLine_pos, Color.PaleGreen);
LineOfm1bar_thatbreaksStartLinepos.IsInteractive = true;
LineOfm1bar_thatbreaksStartLinepos.Comment = "high is at " + m1Bars.HighPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
break;
}
}
}
if (RFinishLine.Y < GStartLine.Y)
{
Print("first if statement works");
for (int i = 0; m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] <= m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time2))]; i++)
{
var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for: price_tobreak is " + price_tobreak + " while low for this time is" + PRICE);
if (m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i] < price_tobreak)
{
DateTime m1bar_thatbreaksGStartLine_pos = m1Bars.OpenTimes[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
Print("The position is at " + m1bar_thatbreaksGStartLine_pos + " with the price of " + m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i]);
ChartVerticalLine LineOfm1bar_thatbreaksStartLinepos = Chart.DrawVerticalLine("LineThatIndicates m1bar_thatbreaksGStartLine_pos" + m1bar_thatbreaksGStartLine_pos, m1bar_thatbreaksGStartLine_pos, Color.PaleGreen);
LineOfm1bar_thatbreaksStartLinepos.IsInteractive = true;
LineOfm1bar_thatbreaksStartLinepos.Comment = "high is at " + m1Bars.LowPrices[(m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1)) + i];
break;
}
}
}
}
void OnChartMouseUp(ChartMouseEventArgs obj)
{
Chart.IsScrollingEnabled = true;
if (Rect1 != null)
{
if (RFinishLine != null)
{
if (GStartLine != null)
{
DETm1bar_thatbreaksGStartLine_pos();
//DETm1bar_that_reachesTG();
Chart.RemoveObject(RFinishLine.Name);
Chart.RemoveObject(GStartLine.Name);
Chart.RemoveObject(Rect1.Name);
}
}
}
Rect1 = null;
return;
}
private void TheChoices()
{
var therealbox = new StackPanel
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Orientation = Orientation.Vertical,
Width = 160,
BackgroundColor = "Red"
};
// when clicked, these cause an event. Event handler attached to it, not in it,will run.
var HorLineButton = new Button
{
Text = "#1 HLs",
HorizontalContentAlignment = HorizontalAlignment.Center
};
therealbox.AddChild(HorLineButton);
// therealbox.AddChild(RealizeButton);
HorLineButton.Click += OnHor_LineClick;
DrawingDialog = therealbox;
Chart.AddControl(DrawingDialog);
// Chart.AddControl(therealbox);
}
//method to draw a horizontal line HL1
private ChartHorizontalLine DrawRHL1()
{
var RHL1 = Chart.DrawHorizontalLine("End Line", ((Chart.TopY - Chart.BottomY) * 2 / 3) + Chart.BottomY, Color.Red);
return RHL1;
}
//method to draw a horizontal line HL2
private ChartHorizontalLine DrawBHL2()
{
var BHL2 = Chart.DrawHorizontalLine("Start Line", ((Chart.TopY - Chart.BottomY) * 1 / 3) + Chart.BottomY, Color.Green);
return BHL2;
}
//method to draw a rectangle Rect1
private ChartRectangle DrawRect1(DateTime Timefrommouse)
{
var Rect1 = Chart.DrawRectangle("Rect1", Timefrommouse, Chart.BottomY, Timefrommouse, Chart.TopY, SelectedColor);
Rect1.IsFilled = true;
return Rect1;
}
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
}
}
}
@fang0092
PanagiotisCharalampous
04 Aug 2020, 11:13
Hi fang0092,
There is not enough data loaded in your m1 bars. You need to load more history to obtain the data. See below
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0)
m1Bars.LoadMoreHistory();
var PRICE = m1Bars.HighPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for statement: price_tobreak is " + price_tobreak + " while high for this time is" + PRICE);
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0)
m1Bars.LoadMoreHistory();
var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i];
Print("for: price_tobreak is " + price_tobreak + " while low for this time is" + PRICE);
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous
fang0092
04 Aug 2020, 11:31
RE:
PanagiotisCharalampous said:
Hi fang0092,
There is not enough data loaded in your m1 bars. You need to load more history to obtain the data. See below
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0) m1Bars.LoadMoreHistory(); var PRICE = m1Bars.HighPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i]; Print("for statement: price_tobreak is " + price_tobreak + " while high for this time is" + PRICE);
while (m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) < 0) m1Bars.LoadMoreHistory(); var PRICE = m1Bars.LowPrices[m1Bars.OpenTimes.GetIndexByTime(Rect1.Time1) + i]; Print("for: price_tobreak is " + price_tobreak + " while low for this time is" + PRICE);
Best Regards,
Panagiotis
Join us on Telegram
It works, but it take a while to load. If I load the m1 bars ahead of time (before using the indicator) will it significantly reduce the loading time? Do the m1 bars loaded remain until I close ctrader?
@fang0092
PanagiotisCharalampous
04 Aug 2020, 11:43
Hi fang0092,
. If I load the m1 bars ahead of time (before using the indicator) will it significantly reduce the loading time?
It will take the same time to load no matter where you call the function
Do the m1 bars loaded remain until I close ctrader?
They will stay in memory as long as you use the indicator.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous
fang0092
04 Aug 2020, 12:14
RE:
PanagiotisCharalampous said:
Hi fang0092,
. If I load the m1 bars ahead of time (before using the indicator) will it significantly reduce the loading time?
It will take the same time to load no matter where you call the function
Do the m1 bars loaded remain until I close ctrader?
They will stay in memory as long as you use the indicator.
Best Regards,
Panagiotis
Join us on Telegram
Ok thank you very much
@fang0092
PanagiotisCharalampous
04 Aug 2020, 10:27
Hi fang0092,
Please provide the complete code so that we can reproduce the behavior.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous