sending price to textfile

GenesisG since: 21 Oct 2018; 
 13 Aug 2019, 11:33
sending price to textfile

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?

Panagiotis Charalampous's avatar
Panagiotis Charalampous since: 13 Jan 2017; 
 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

        protected override void OnStop()
        {
            _fileWriter.Close();
        }

You do not seem to initialize or use this variable any where. What is the purpose of it?

Best Regards,

Panagiotis

Head of Community Management at cTrader
GenesisG since: 21 Oct 2018; 
 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 since: 21 Oct 2018; 
 13 Aug 2019, 12:39

The log i am getting on crashing is
13/08/2019 19:34:28.516 | Crashed in OnTick with IOException: The process cannot access the file because another process has locked a portion of the file. 
 

Panagiotis Charalampous's avatar
Panagiotis Charalampous since: 13 Jan 2017; 
 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

Head of Community Management at cTrader
GenesisG since: 21 Oct 2018; 
 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!