Skip to content

Working with files in cTrader

In this article and its corresponding video, we will explain how you can work with files in cTrader. On average, users are concerned about launching cBots or indicators requesting full access rights to their device. Especially when no source code is available, it is difficult to know what the algorithm is doing with these permissions. Luckily, cTrader provides several methods that allow algos to access files on their local machine without needing elevated permissions.

Write to the algo directory

cTrader allows all algos to access a folder with this address:

C:/Users/{username}/Documents/cAlgo/Data/{algoType}

, where algoType is cBots or Indicators.

We will create an example cBot and demonstrate how you can access this directory programmatically.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.IO;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class FileBot : Robot
    {

        protected override void OnStart()
        {
            File.WriteAllText("Hello World.txt", "Hello World!");
        }
    }
}

As usual, we save and build the cBot, then create its instance.

After the instance starts, we can navigate to the folder defined above. In it, we should see the Hello World.txt file containing the text we specified in our code.

Read from the algo directory

The next step is to read the contents of a file located in the algo directory. To do so, we can rewrite our OnStart() method as follows.

1
2
3
4
protected override void OnStart()
{
        Print(File.ReadAllText("Hello Word.txt"));
}

After we build and launch our cBot, we will see that the algo prints the string from the file to the cTrader log.

Access other directories

Note that none of the operations our cBot performed earlier required providing it with elevated access rights. In fact, access rights were explicitly set to None at the class level.

We will try to write to a file located in a different directory and see what happens. We will modify our code to write to a file located in the root Documents directory.

1
2
3
4
protected override void OnStart()
{
    File.WriteAllText("C://OneDrive//Documents//Hello World.txt", "Hello World!");
}

Once we build and run the cBot again, we will see an exception being thrown in the log indicating that we are not authorised to write in this specific directory.

To solve this, we can go back to the cBot code and change its access rights as follows.

1
[Robot(AccessRights = AccessRights.FullAccess)]

We build and run the cBot. Instead of throwing an exception, the bot will now access the specified directory and write to the correct file.

For one more demonstration of how access rights work, we will modify the OnStart() method as follows.

1
2
3
4
protected override void OnStart()
{
        Print(File.ReadAllText("C://OneDrive//Documents//Hello World.txt"));
}

We will also set access rights back to None.

1
[Robot(AccessRights = AccessRights.None)]

After the cBot starts running, we will see that it fails to read from the file.

Subscribe to our YouTube channel