Keep getting NaN Values on CSV file
Keep getting NaN Values on CSV file
04 Sep 2022, 18:48
Hi.. Since i upgraded to VS2022, and i guess it changed some settings and added .Net 6, also the cTrader updated itself to 4.2.22 version, i'm keep getting a NaN values on all the indocators i use to Dump to CSV bot, which of course prior to this changes it was all good.. this is the code-
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.IsraelStandardTime, AccessRights = AccessRights.FullAccess)]
public class a2DumptoCSVJan2022 : Robot
{
private WeightedMovingAverage WMA0;
private WeightedMovingAverage WMA1;
private WeightedMovingAverage WMA2;
private WeightedMovingAverage WMA50;
private WeightedMovingAverage WMA96;
private DirectionalMovementSystem ADXR;
/***********/public DataSeries AOpn;
private ParabolicSAR PSAR;
private RelativeStrengthIndex RSI;
double W1,W2,W3,W4,W5,W6,
W7,W8,W9,W10,WX1,WX0,
Adv,Dec,//cDec,
AVGGain,AVGLos,GAlpha,LAlpha,
RS,RSIa;
/*Added Feb 4*/double t0Bdy,t0OpnWMA,OpnWMA,WmaSma,RSIVal,RSIDel,T0VolDel,AVG,SMA;
int iAD = 0;
readonly int WMAp0 = 5;
readonly int WMAp1 = 10;
readonly int WMAp2 = 21;
readonly int WMAp50 = 50;
readonly int WMAp96 = 96;
int iCount; //iBarsBack;
string Dir = "";
string Dir9650 = "";
protected override void OnStart()
{
AOpn = Bars.OpenPrices;
WMA0 = Indicators.WeightedMovingAverage(Bars.OpenPrices,WMAp0);
WMA1 = Indicators.WeightedMovingAverage(Bars.OpenPrices,WMAp1);
WMA2 = Indicators.WeightedMovingAverage(Bars.OpenPrices,WMAp2);
WMA50 = Indicators.WeightedMovingAverage(Bars.OpenPrices,50);
WMA96 = Indicators.WeightedMovingAverage(Bars.OpenPrices,96);
ADXR = Indicators.DirectionalMovementSystem(15);
PSAR = Indicators.ParabolicSAR(0.02,0.2);
RSI = Indicators.RelativeStrengthIndex(Bars.OpenPrices,24);
//---------------
Timer.Start(TimeSpan.FromMilliseconds(1000));
Print("W95@0 "+WMA96.Result[0]+" W96@1 "+WMA96.Result[1]);
}
protected override void OnTimer()
{
if(DateTime.Now.Hour == 1 && DateTime.Now.Minute == 5 && DateTime.Now.Second == 00)
{
//sdsd
}
}
//public interface Symbol;
//public double Ask{ get; }
protected override void OnStop()
{
Print("W95@0 "+WMA96.Result[0]+" W96@1 "+WMA96.Result[1]);
iAD = 0;
iCount = Bars.OpenPrices.Count;
//iBarsBack = iCount;
W1 = (double)1/55; W2 = (double)2/55; W3 = (double)3/55; W4 = (double)4/55; W5 = (double)5/55;
W6 = (double)6/55; W7 = (double)7/55; W8 = (double)8/55; W9 = (double)9/55; W10 = (double)10/55;
DateTime DT1 = DateTime.Now;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var folderPath = Path.Combine(desktopFolder, "PS-OCT19");
Directory.CreateDirectory(folderPath);
var filePath = Path.Combine(folderPath, Symbol.Name + " " + TimeFrame + " - "+
DT1.Day+"."+DT1.Month+"."+DT1.Year+" "+
DT1.Hour+"."+DT1.Minute+"."+DT1.Second+".csv");
using (var writer = File.CreateText(filePath))
{
writer.WriteLine(
ConcatWithComma(
"Date[OpnTime-t-1]",//Bars.OpenTimes[i],
"",
"Day",
"",
"Open",//Bars.OpenPrices[i],
"High", //MarketSeries.High[i],
"Low",//MarketSeries.Low[i],
"Close",//MarketSeries.Close[i],
"",
"BDY t-1",
"OW[t]",
"OW[t-1",
"WSt",
"RSI",
"RSI Del",
"VolDel/t-1",
"",
"WMA"+WMAp0,
"WMA"+WMAp1,
"WMA"+WMAp2,
"WMA"+WMAp50,
"WMA"+WMAp96,
"",
"PSAR",
"",
"AVG",
"",
"RSI",
"",
"VOL"));
for (var i = 1; i <= Bars.ClosePrices.Count; i++) /***********Make sure i starts with Aug 30 2007************/
{
SMA = (Bars.OpenPrices[i-1]+ Bars.OpenPrices[i-2]+ // Current = i-1
Bars.OpenPrices[i-3]+ Bars.OpenPrices[i-4]+
Bars.OpenPrices[i-5]+ Bars.OpenPrices[i-6]+
Bars.OpenPrices[i-7]+ Bars.OpenPrices[i-8]+
Bars.OpenPrices[i-9]+ Bars.OpenPrices[i-10])/10;
WX1 = Bars.OpenPrices[i-1]*W10+ Bars.OpenPrices[i-2]*W9+
Bars.OpenPrices[i-3]*W8+ Bars.OpenPrices[i-4]*W7+
Bars.OpenPrices[i-5]*W6+ Bars.OpenPrices[i-6]*W5+
Bars.OpenPrices[i-7]*W4+ Bars.OpenPrices[i-8]*W3+
Bars.OpenPrices[i-9]*W2+ Bars.OpenPrices[i-10]*W1;
WX0 = Bars.OpenPrices[i-2]*W10+ Bars.OpenPrices[i-3]*W9+
Bars.OpenPrices[i-4]*W8+ Bars.OpenPrices[i-5]*W7+
Bars.OpenPrices[i-6]*W6+ Bars.OpenPrices[i-7]*W5+
Bars.OpenPrices[i-8]*W4+ Bars.OpenPrices[i-9]*W3+
Bars.OpenPrices[i-10]*W2+ Bars.OpenPrices[i-11]*W1;
if(WX1-WX0>0) { Adv = WX1-WX0; } else{ Adv = 0;}
if(WX1-WX0<0) { Dec = -(WX1-WX0); } else{ Dec = 0; }
//if(iAD < 10) { cAdv += Adv; cDec += Dec; }
if(iAD == 10) { GAlpha = (AVGGain/10); LAlpha = (AVGLos/10); }
if(iAD > 10)
{
AVGGain = ((GAlpha*9)+Adv)/10;
AVGLos = ((LAlpha*9)+Dec)/10;
RS = AVGGain/AVGLos;
if(AVGLos == 0) { RSIa = 0; } else{
RSIa = 100-(100/(1+RS));}
}
GAlpha = AVGGain;
LAlpha = AVGLos;
iAD++;
t0Bdy = (Bars.ClosePrices[i-2]-Bars.OpenPrices[i-2]);
t0OpnWMA = (Bars.OpenPrices[i-2] - WX0);
OpnWMA = (Bars.OpenPrices[i-1] - WX1);
WmaSma = (WX1 - SMA);
//RSIVal = t1RSI;
//RSIDel = t1RSI;
T0VolDel = Bars.TickVolumes[i-2] - Bars.TickVolumes[i-3];
if(i == iCount-5){
DateTime A1 = Bars.OpenTimes[i-1];}
AVG = ((t0Bdy+t0OpnWMA+OpnWMA+WmaSma)+RSIVal+RSIDel+T0VolDel)/4;
writer.WriteLine(ConcatWithComma(
Bars.OpenTimes[i-1]+"."+Bars.OpenTimes[i-1].Millisecond,
"",
Bars.OpenTimes[i-1].DayOfWeek,
"",
Bars.OpenPrices[i-1],
Bars.HighPrices[i-1],
Bars.LowPrices [i-1],
Bars.ClosePrices [i-1],
"",
t0Bdy,
t0OpnWMA,
OpnWMA,
WmaSma,
RSIVal,
RSIDel,
T0VolDel,
"",
WMA0.Result[i-1],
WMA1.Result[i-1],
WMA2.Result[i-1],
WMA50.Result[i-1],
WMA96.Result[i-1],
"",
PSAR.Result[i-1],
"",
AVG,
"",
RSI.Result[i-1],
"",
Bars.TickVolumes[i-1]));
iAD++;
}
}
}
private string ConcatWithComma(params object[] parameters)
{
return string.Join(",", parameters.Select(p => p.ToString()));
}
}
}
And i did tried some things, like changing the indicators from private to public, it did asked to change the int's to readonly, Also to run it on .Net 4 and 6, but nothing fixed the NaN values im getting to all the indicators values...
Any help would be much appreciated...
Thanks ahead...
Replies
YC1186
06 Sep 2022, 10:45
RE:
PanagiotisCharalampous said:
Hi there,
If you can simplify the code, we can check what happens. At the moment it is very complicated and will take a long time for somebody to figure out what is going on.
Best Regards,
Panagiotis
Hi.. Thanks for your reply..
Actually i don't think it's about the code, since it's the same code that i have in my Laptop which there it works but it runs the cTrader 4.1 which i guess it's because it doesn't have the .net 6, so it's most probably not with the code but i added it cause any expert on the platform know where to look for the error causing code, like there's no way it's double not assigned as private or public or so.. and i saw bigger and more complex code snips so i think it's not that big / complex, i don't know how to simplify it, but the NaN values i get is in all the indicators like WMA0.Result[i-1],
WMA1.Result[i-1],
WMA2.Result[i-1],
WMA50.Result[i-1],
WMA96.Result[i-1] Throws NaN values..
@YC1186
PanagiotisCharalampous
06 Sep 2022, 12:06
Hi there,
Obviously something has changed in the API between 4.1 and 4.2 but this does not mean it's a bug. But in order to advise where the change is, evaluate if it is a bug or an impreovement, and how address it, I need a small code sample that reproduces the problem. Unfortunately I cannot spend hours undestanding and debugging your code to figure out what is calcuated differently.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
YC1186
06 Sep 2022, 12:42
RE:
PanagiotisCharalampous said:
Hi there,
Obviously something has changed in the API between 4.1 and 4.2 but this does not mean it's a bug. But in order to advise where the change is, evaluate if it is a bug or an impreovement, and how address it, I need a small code sample that reproduces the problem. Unfortunately I cannot spend hours undestanding and debugging your code to figure out what is calcuated differently.
Best Regards,
Panagiotis
What do you mean by small code sample? How do you "Shrink" a code without affecting of it's functionality and what it is actually? i think it can be very simple by sort of Reverse Engineering it, like we know where the problem is, at the WMAX.Result[i-1], so from here back, maybe the declaration of the private WeightedMovingAverage WMA0; should be readonly and/or public there's many possibilities but we know where the problem is..
And again, if the problem is at the code at all, based on the facts that this exact code working perectly on CT4.1 machine, and it does throws good values on specific WMA96 series, implies strongly again that the problem isn't at the code at all, any maybe at the API or the server i don't know but it's really odd one..
@YC1186
PanagiotisCharalampous
06 Sep 2022, 12:46
Hi there,
What do you mean by small code sample? How do you "Shrink" a code without affecting of it's functionality and what it is actually?
A code example with 10-20 lines ago that can reproduce this discrepancy between 4.1 and 4.2.
And again, if the problem is at the code at all, based on the facts that this exact code working perectly on CT4.1 machine, and it does throws good values on specific WMA96 series, implies strongly again that the problem isn't at the code at all, any maybe at the API or the server i don't know but it's really odd one..
I addressed this im my response above.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
YC1186
06 Sep 2022, 14:31
RE:
PanagiotisCharalampous said:
Hi there,
What do you mean by small code sample? How do you "Shrink" a code without affecting of it's functionality and what it is actually?
A code example with 10-20 lines ago that can reproduce this discrepancy between 4.1 and 4.2.
And again, if the problem is at the code at all, based on the facts that this exact code working perectly on CT4.1 machine, and it does throws good values on specific WMA96 series, implies strongly again that the problem isn't at the code at all, any maybe at the API or the server i don't know but it's really odd one..
I addressed this im my response above.
Best Regards,
Panagiotis
Still don't understand what you mean... How can i write a code that reproduce the discrepancy if i don't even know if there's any discrepancy [let alone the relevant one] between 4.1 and 4.2? and for sure i can't know what it is.. And what you mean by 10-20 line ago?.. You are making a lot of non sense here.. and BTW, the main first version of this code i downloaded long ago from this very website, called Dump to CSV... So there's shouldn't be any complexity or weird stuff here..
@YC1186
PanagiotisCharalampous
06 Sep 2022, 14:41
Hi there,
I am not sure how to make it simpler. You ask me to reverse engineer and debug a 250 lines cBot to understand if there is a problem with the code or with cTrader. Unfortunately I cannot. If you can provide a simpler cBot that has the same problem, I am happy to look at it.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
YC1186
06 Sep 2022, 15:08
RE:
PanagiotisCharalampous said:
Hi there,
I am not sure how to make it simpler. You ask me to reverse engineer and debug a 250 lines cBot to understand if there is a problem with the code or with cTrader. Unfortunately I cannot. If you can provide a simpler cBot that has the same problem, I am happy to look at it.
Best Regards,
Panagiotis
Not actually Reverse engineering it, but conceptualy how to figure it... I guess that whatwe need here is an expert with the cTrader systems more than in C# coding or programming, cause again, maybe the bug isn't even in my code.. which is most likely, as it works perfectly on 4.1 system that doesn't have the .net 6 or other versions of it that may cause the problem..
@YC1186
prosteel1
07 Sep 2022, 11:49
RE:
PanagiotisCharalampous said:
Hi there,
Obviously something has changed in the API between 4.1 and 4.2 but this does not mean it's a bug. But in order to advise where the change is, evaluate if it is a bug or an impreovement, and how address it, I need a small code sample that reproduces the problem. Unfortunately I cannot spend hours undestanding and debugging your code to figure out what is calcuated differently.
Best Regards,
Panagiotis
Hi Panagiotis and YC1186,
Small code sample below. The following code works, but breaks when the OnBar line is commented out.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class a2DumptoCSVJan2022Works : Robot
{
[Parameter("Periods", Group = "RSI", DefaultValue = 24)]
public int Periods { get; set; }
private RelativeStrengthIndex RSI;
double _RSI;
protected override void OnStart()
{
RSI = Indicators.RelativeStrengthIndex(Bars.OpenPrices,Periods);
}
protected override void OnBar()
{
// Comment out following line to produce NaN
_RSI = RSI.Result.LastValue;
}
protected override void OnStop()
{
for (var i = Periods; i <= Bars.ClosePrices.Count; i++)
{
Print(i + " " + RSI.Result[i-1]);
}
}
}
}
@prosteel1
PanagiotisCharalampous
07 Sep 2022, 12:01
Ηι prosteel1,
This happens because the Result series is never populated. Result will be populated only when LastValue or Last() are called. Read about lazy loading here.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
YC1186
14 Sep 2022, 05:12
( Updated at: 14 Sep 2022, 05:14 )
RE: RE:
prosteel1 said:
PanagiotisCharalampous said:
Hi there,
Obviously something has changed in the API between 4.1 and 4.2 but this does not mean it's a bug. But in order to advise where the change is, evaluate if it is a bug or an impreovement, and how address it, I need a small code sample that reproduces the problem. Unfortunately I cannot spend hours undestanding and debugging your code to figure out what is calcuated differently.
Best Regards,
Panagiotis
Hi Panagiotis and YC1186,
Small code sample below. The following code works, but breaks when the OnBar line is commented out.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class a2DumptoCSVJan2022Works : Robot { [Parameter("Periods", Group = "RSI", DefaultValue = 24)] public int Periods { get; set; } private RelativeStrengthIndex RSI; double _RSI; protected override void OnStart() { RSI = Indicators.RelativeStrengthIndex(Bars.OpenPrices,Periods); } protected override void OnBar() { // Comment out following line to produce NaN _RSI = RSI.Result.LastValue; } protected override void OnStop() { for (var i = Periods; i <= Bars.ClosePrices.Count; i++) { Print(i + " " + RSI.Result[i-1]); } } } }
Freakin finally! that's seal the deal.. Great.. Don't know why it doesn't populate or initiate the values with the old good index call like [t-50] or so but it's ridiculous if for every indicator or oscillator used we gonna need to used that Result.LastValue call, so i hope there's some system guys here to fix that very annoying bug that bugs me for like a month and a half and of course nowhere to be found a fix for it..
Many thanks Pro-steel1 for solving that issue..
@YC1186
PanagiotisCharalampous
05 Sep 2022, 14:47
Hi there,
If you can simplify the code, we can check what happens. At the moment it is very complicated and will take a long time for somebody to figure out what is going on.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous