sending price to textfile
sending price to textfile
13 Aug 2019, 11:33
So I am quite new to c# . The script is to update a price to a text file every time price changes. This is working correctly. On Excel I am importing the data and updating it by pressing a button. This works, but every so often the script in ctrader crashes. I'm thinking this is because excel and this script cannot view the textfile at the same time. Below is the log in ctrader and the script itself. What is the proper was for me to do this?
This is the log in Ctrader
13/08/2019 17:18:43.915 | cBot "ICMARKETS (2)" was stopped for US30, t1.
13/08/2019 17:18:43.900 | Crashed in OnStop with NullReferenceException: Object reference not set to an instance of an object.
This is the script.
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; using System.IO;
namespace cAlgo.Robots { [Robot()] public class ICMARKETS : Robot { StreamWriter _fileWriter;
protected override void OnStart() { }
protected override void OnTick() { var lastIndex = MarketSeries.Close.Count - 1; double close = MarketSeries.Close[lastIndex - 1]; StreamWriter SW = new StreamWriter("C:\\Users\\Gary\\Desktop\\hi.txt");
SW.WriteLine(Symbol.Ask); SW.WriteLine(Symbol.Bid); SW.Close(); }
protected override void OnStop() { _fileWriter.Close(); } } }
Ive been working on this and I dont have the knowledge to fix this. Any help?
Replies
GenesisG
13 Aug 2019, 12:38
Thanks for your response,
I have removed the onstop and onstart completely. But when refreshing in excel if excel and this bot read and write at the same time the cbot crashes still. I want this cbot to write to a text file. I have another bot writing to another text file. Then in excel I am importing both textfiles into the sheet and making calculations in excel.
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; using System.IO; namespace cAlgo.Robots { [Robot()] public class ICMARKETS : Robot { StreamWriter _fileWriter; protected override void OnTick() { var lastIndex = MarketSeries.Close.Count - 1; double close = MarketSeries.Close[lastIndex - 1]; StreamWriter SW = new StreamWriter("C:\\Users\\Gary\\Desktop\\hi.txt"); SW.WriteLine(Symbol.Ask); SW.WriteLine(Symbol.Bid); SW.Close(); } } }
@GenesisG
PanagiotisCharalampous
13 Aug 2019, 12:41
Hi GenesisG.
If the file is being used by another process then you will get an exception. Please read this discussion, it should be helpful.
Best Regards,
Panagiotis
@PanagiotisCharalampous
GenesisG
13 Aug 2019, 12:47
RE:
Panagiotis Charalampous said:
Hi GenesisG.
If the file is being used by another process then you will get an exception. Please read this discussion, it should be helpful.
Best Regards,
Panagiotis
Thanks!
@GenesisG
PanagiotisCharalampous
13 Aug 2019, 12:14
Hi GenesisG,
Thanks for posting in our forum. The reason you are getting this exception is because you are trying to use a variable that has not been initialized in OnStop
You do not seem to initialize or use this variable any where. What is the purpose of it?
Best Regards,
Panagiotis
@PanagiotisCharalampous