sending price to textfile

Created at 13 Aug 2019, 11:33
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
GE

GenesisG

Joined 21.10.2018

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?


@GenesisG
Replies

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

        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


@PanagiotisCharalampous

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

GenesisG
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. 
 


@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