Skip to content

Network Access

This guide defines how you can create algorithms that can access the Internet. Below, we provide a one-minute summary of this API feature.

Network Access in One Minute!

  • Your algo solutions can source information from trading news websites or Forex-oriented web APIs. Use this feature to ensure that your cBots and indicators react to pressing real-life events and information!
  • By using methods in the System.Text.Json and System.Text.Json.Serialization namespaces, you can quickly serialise/deserialise objects to and from JSON files, allowing cBots to easily consume endpoints and process valuable information!
  • AccessRights.None is enough for network functions.

How to Use Network Access

The new Http interface contains several methods that, when implemented, should enable cBots and other types of algorithms to access the Internet. For an illustration, see the following.

  • HttpResponse Http.Get(string uri). Performs a GET request to the URI specified in the passed string uri and returns an HttpsResponse object containing the results of this request.

The HttpsRequest class allows for performing more complex requests including POST requests. To perform these requests, set the HttpsRequest.Method property to one of the values of the HttpMethod enum such as HttpMethod.Put or HttpMethod.Patch.

In turn, the HttpsRequest.Uri property contains the URI to which a request is sent while the HttpsRequest.Body property can be used to set the request body. See below for an example of how you can use an HttpsRequest object.

  • HttpResponse Http.Send(HttpRequest request). Performs a request to the URI specified as the value of the request.Uri property of the HttpsRequest object passed into this method. Afterward, returns an HttpsResponse object containing the results of this request. The request type is set as the value of the equest.Method property.

Network Access in Backtesting and Optimisation

All methods in the Http interface work as intended in backtesting and optimisation. Note that, when accessin a web resource in backtesting or optimisation, the up-to-date version of this resource will be requested instead of a historical one.

Creating Example cBots

Performing a GET Request

We will create a simple cBot that performs the following actions.

  • Sends a GET request and accesses a JSON file via the API at https://forexApiExample.com/v1/exchangerate (note that this API is entirely fake).
  • If the GET response is successful, the cBot deserialises the response body into an object of a custom SymbolPrice class (we have created this class for demonstration purposes).
  • Places a Sell limit order for a symbol at a price that is slightly higher than the price specified in the API.

Note that the cBot is entirely made up. While the code will build, it will not perform any meaningful actions.

How to Serialise/Deserialise Strings From JSON Files

The code of our cBot includes two important using statements, namely System.Text.Json and System.Text.Json.Serialization. These namespaces contain several methods that allow for easily serialising and deserialising strings from JSON files such as JsonSerializer.Deserialize<T>().

Here is the code of our example cBot.

 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
30
31
32
33
34
35
36
37
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.Text.Json;
using System.Text.Json.Serialization;

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

        protected override void OnStart()
        {

            var responseToGet = Http.Get("https://forexApiExample.com/v1/exchangerate");

            if (responseToGet.IsSuccessful)
            {
                var tradedSymbol = JsonSerializer.Deserialize<SymbolPrice>(responseToGet.Body);
                var result = PlaceLimitOrder(TradeType.Sell, tradedSymbol.SymbolName, 10000, tradedSymbol.ConversionRate + 0.15);

            }
        }
    }

    public class SymbolPrice
    {
        public string SymbolName { get; set; }
        public double ConversionRate { get; set; }
    }
}

If this cBot was to access a real API, it would have done so successfully.

Performing a POST Request

We can also create a more advanced cBot that achieves the following goals.

  • Sends a POST request to the same fake API at https://anotherForexApiExample.com/commodities/v1/getaccesstoken. This is done to attain an access token for this API.
  • Stores the token from the response body in the token variable.
  • Performs a GET request to the same API with the value of the token variable passed as a query parameter in the URI (https://anotherForexApiExample.com/commodities/v1/getprices).
  • Using JSON serialisation (discussed above), places a Buy stop order at a price that is slightly lower than the price received from the API.

Once again, note that this cBot will not perform any meaningful actions when compiled. It exists for example purposes only.

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.Text.Json;
using System.Text.Json.Serialization;

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

        protected override void OnStart()
        {
            var uriForAccessToken = new Uri("https://anotherForexApiExample.com/commodities/v1/getaccesstoken");

            var postRequest = new HttpRequest(uriForAccessToken);

            postRequest.Method = HttpMethod.Post;

            var responseToPost = Http.Send(postRequest);

            var token = responseToPost.Body;

            var response = Http.Get($"https://anotherForexApiExample.com/commodities/v1/getprices?token={token}");

            if (response.IsSuccessful)
            {
                var tradedSymbol = JsonSerializer.Deserialize<SymbolPrice>(response.Body);

                var result = PlaceStopOrder(TradeType.Buy, tradedSymbol.SymbolName, 10000, tradedSymbol.ConversionRate - 0.15);

            }
        }
    }

    public class SymbolPrice
    {
        public string SymbolName { get; set; }
        public double ConversionRate { get; set; }
    }
}

Network Access and the AccessRights Property

In brief, the AccessRights enum defines whether algorithms have access to data that is external to cTrader such as the file system of your local machine. You can learn more in this tutorial.

Setting AccessRights to AccessRights.FullAccess allows algos to perform advanced operations (e.g., sourcing information from the Windows registry). The end users, however, may find giving cBots or indicators full access risky as it may lead to a data breach.

As a result, AccessRights.None is usually the most sensible option if you want to share your algo products with others. However, even if you choose this option, your cTrader products are free to access the web! The addition of the network access functionality to the cTrader Algo API means that cBots no longer require the AccessRights property to be set to AccessRights.FullAccess to work with resources on the Internet.

This greatly expands the opportunities algo developers have when coding cTrader products that they intend to distribute to other traders.

Summary

In conclusion, network access is a powerful feature that allows for greatly expanding the number of operations that your algorithms can perform while introducing no additional risks.