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
andSystem.Text.Json.Serialization
namespaces, you can quickly serialize/deserialize 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 aGET
request to the URI specified in the passed stringuri
and returns anHttpsResponse
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 therequest.Uri
property of theHttpsRequest
object passed into this method. Afterward, returns anHttpsResponse
object containing the results of this request. The request type is set as the value of theequest.Method
property.
Network Access in Backtesting and Optimization
All methods in the Http
interface work as intended in backtesting and optimization.
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 same API athttps://forexApiExample.com/v1/exchangerate
. - If the
GET
response is successful, the cBot deserializes the response body into an object of a customSymbolPrice
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 Serialize/Deserialize 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 serializing and deserializing 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 38 39 40 41 |
|
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 a fake Forex API located athttps://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 thetoken
variable passed as a query parameter in the URI (https://anotherForexApiExample.com/commodities/v1/getprices
). - Using JSON serialization (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 48 49 50 51 |
|
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.
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.