Skip to content

File Operations

Platform users express justified concerns about launching cBots and indicators with AccessRights.FullAccess. Especially when no source code is available, it is difficult to know for sure what changes will be introduced by the algorithm to the file system. In the access rights guide, we have already advised that cBots and indicators you are sharing with others should receive AccessRights.None.

This API guide explains how algorithms with limited AccessRights can securely conduct operations with local files.

File Operations in One Minute!

  • The file operations feature allows algorithms with restricted access rights to work with files and folders only inside a designated folder.
  • The folder name corresponds to the cBot/indicator name.
  • All other directories of the user’s file system remain securely protected from unauthorised access.
  • In the API, the file operations feature is supported only for .NET 6.0 algorithms.

Rules of File Operations

Any algorithm with restricted access rights is allowed to perform file operations only in a designated folder of the user’s file system. Each algorithm type locates its own folder as shown below (i.e., cBots, indicators, etc.), with a separate folder created for each algorithm name.

Documents
|-cAlgo
  |-Data
    |-cBots
      |-${cBot Name}
    |-Indicators
      |-${Indicator Name}

Note

The file operations feature is available for .NET 6.0 algorithms only.

The following rules determine how the file operations feature functions in the API.

1. Each algorithm has access only to the folder corresponding to its name (including subfolders, if any).

2. If there is no corresponding folder, such a folder is created in the file system according to the above scheme when an instance is first launched.

3. If a new folder cannot be created, the cBot/indicator will not start, and an error will be reported.

4. In the algorithm code, you do not need to specify the full path to the corresponding folder. Instead, algorithms use a relative path.

5. The System.IO namespace offers a variety of classes (e.g., File, FileInfo, Directory. DirectoryInfo, Path) for which relevant methods can be called to execute operations with files and folders.

Below, we are giving some examples of the methods included in the File and Directory classes.

Method Description
File.Create() Creates or overwrites a file in the designated directory.
File.WriteAllText() Creates a new file, writes the specified string to the file, and closes the file.
File.Copy() Copies the existing file to a new file. Overwriting a file with the same name is not allowed.
File.Delete() Deletes a file in the designated directory.
Directory.CreateDirectory() Creates a folder or a subfolder.
Directory.Delete() Deletes a folder or a subfolder.
Directory.Exists() Checks if the specified directory exists.
Directory.Move() Moves the existing directory to a new specified directory.

Use Case

When calling the TakeChartshot() method, you do not need to specify the full path to the destination directory after File.WriteAllBytes. Due to the file operations feature, the chartshot will be saved to the relevant algorithm directory even if the access rights are AccessRights.None.

Creating a cBot Example

The below cBot example has AccessRights.None, but it easily creates a text file in the corresponding folder named 'File Access Test' right after its initialisation. If a successful market order is placed OnStart(), the cBot writes a position ID in the created file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MycBot : Robot
    {
        protected override void OnStart()
        {
            TradeResult result = ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);

            if (result.IsSuccessful)
            {
                string positionId = result.Position.Id.ToString();
                {
                    File.WriteAllText("My positions.txt", $"{positionId}");
                } 
            }
        }
    }
}

Created File

Written Position ID

Summary

The file operations feature in the API still enables algorithms with restricted access rights to work with local files and folders. You can use methods from the System.IO namespace of C# to program how algorithms will perform such operations within the confines of the corresponding folder. Thus, users’ other directories of the file system remain fully protected even if the algorithm source code is unavailable.