Spread Recorder Robot - Write spread to text file

Created at 06 Jun 2014, 17:19
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!
TE

tete13

Joined 06.05.2014

Spread Recorder Robot - Write spread to text file
06 Jun 2014, 17:19


I've just created a simple robot to write spread to text file. We'll get some thing like this in text file when run this robot

13:20:10 | Bid:1.59871 | Ask:1.59902 | Spread:3.1
13:20:10 | Bid:1.59875 | Ask:1.59906 | Spread:3.1
13:20:11 | Bid:1.59872 | Ask:1.59902 | Spread:3
13:20:11 | Bid:1.59871 | Ask:1.59902 | Spread:3.1
13:20:12 | Bid:1.59875 | Ask:1.59906 | Spread:3.1
13:20:12 | Bid:1.59876 | Ask:1.59907 | Spread:3.1
13:20:14 | Bid:1.59877 | Ask:1.59908 | Spread:3.1
13:20:15 | Bid:1.59878 | Ask:1.59908 | Spread:3
13:20:15 | Bid:1.59879 | Ask:1.5991 | Spread:3.1
13:20:16 | Bid:1.59876 | Ask:1.59907 | Spread:3.1
13:20:17 | Bid:1.5987 | Ask:1.59901 | Spread:3.1
13:20:18 | Bid:1.5987 | Ask:1.59897 | Spread:2.7
13:20:19 | Bid:1.5987 | Ask:1.59899 | Spread:2.9
13:20:19 | Bid:1.59869 | Ask:1.59899 | Spread:3

Source code


using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.IO;
namespace cAlgo.Robots
{
    [Robot()]
    public class SpreadRecorder : Robot
    {
        StreamWriter _fileWriter;

        protected override void OnStart()
        {
            var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            var filePath = Path.Combine(desktopFolder, Symbol.Code + "-" + Server.Time.Year + "-" + Server.Time.Month + "-" + Server.Time.Day + "_Spread Record.txt");


            _fileWriter = File.AppendText(filePath);
            //creating file 
            _fileWriter.AutoFlush = true;
            //file will be saved on each change 
        }
        protected override void OnTick()
        {
            _fileWriter.WriteLine(Server.Time.Hour + ":" + Server.Time.Minute + ":" + Server.Time.Second + " | Bid:" + Symbol.Bid + " | Ask:" + Symbol.Ask + " | Spread:" + Math.Round(Symbol.Spread / Symbol.PipSize, 2));
        }
        protected override void OnStop()
        {
            _fileWriter.Close();
        }
    }
}

 

Can you help me with these questions:

1. This robot does not do any trading function. Should we use indicator instead of cbot? How to convert to Ctrader Indicator?

2. How do we set specific path for text file (ex: C\MyDocuments\... instead of Desktop)?

3. How do we get full digit (5 digits for most and 3 digits for XXX/JPY) for Bid and Ask data?

Ex: I want Ask data shows 1.59910 instead of 1.5991

3:20:15 | Bid:1.59878 | Ask:1.59908 | Spread:3
13:20:15 | Bid:1.59879 | Ask:1.5991 | Spread:3.1
13:20:16 | Bid:1.59876 | Ask:1.59907 | Spread:3.1

 


@tete13
Replies

Spotware
10 Jun 2014, 08:16

1) Yes, you can use an Indicator instead of cBot. In order to do that you need to create new Indicator in cAlgo and copy your code there. Please note that Indicator doesn't have OnStart and OnTick methods. You will need to use Initialize and Calculate methods instead. In Calculate method please do not forget to check IsRealTime property in order to filter historical bars.

2)  var filePath = @"C:\my folder\my file.txt";

3) There is no such method in cAlgo.API, but you can code such formatting.


@Spotware

wes87
15 Jan 2019, 16:53

Can you show the finished code, please


@wes87