File operations
Platform users express justified concerns about launching cBots and indicators with AccessRights.FullAccess. This is especially true 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 or 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 (such as cBots and indicators), 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 or 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 (File, FileInfo, Directory. DirectoryInfo, Path, etc.) for which relevant methods can be called to execute operations with files and folders.
Below are 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.
Create a cBot example
The below cBot example has AccessRights.None, but it creates a text file in the corresponding folder named "File Access Test" immediately 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 | |
File parameter
The File parameter type allows algorithms to accept file inputs from users via API. It enables algos to securely read from and write to a user-selected file on the local machine.
When defined in an algorithm:
- In the cTrader UI, the parameter is displayed as a text field with a Browse button that opens a file selection dialog.
- In cTrader CLI, users can pass a file path directly as the parameter value.
- The algorithm receives a strongly typed
Fileobject with built-in read and write methods.
Note
File access is limited to the file explicitly provided by the user and does not grant unrestricted access to the local file system.
The File parameter type can be used to do the following in algorithms:
| Operation | Examples |
|---|---|
| External data input | Read CSV or text files, load custom datasets. |
| Persistent output | Write logs to a file, export results. |
| Configuration handling | Load external config files, maintain large parameter sets. |
To define a file parameter, declare a property of type File and annotate it with the [Parameter] attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
DefaultValue can be used to prepopulate the parameter with a file path, and the File type exposes a set of high-level methods for interacting with the file contents. You can read the entire file as text or as individual lines.
1 2 | |
Writing operations overwrite the existing file contents.
1 | |
You can append text or multiple lines without overwriting existing content.
1 2 | |
Before performing operations, you can verify whether the file exists.
1 2 3 4 | |
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, other directories in the user's file system remain fully protected even if the algorithm source code is unavailable.

