cBot Trading Operations¶
In this guide, we explain in detail the key trading operations you can execute using a cBot developed by means of the Automate API.
The Automate application of cTrader supports the following operations for cBots.
- Sending market orders
- Placing pending orders
- Modifying pending orders and open positions
- Closing positions and canceling orders
- Subscribing to trading events (i.e., positions and orders).
Sending Market Orders¶
In the cTrader Automate API, a market order is sent to the trading server due to the
ExecuteMarketOrder() method. After creating a new cBot, you can add this method with several defined properties of the market order (e.g., symbol, volume, take profit, stop loss, etc.). The below example places a market order when the cBot starts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
In the example, several order parameters were intentionally left undefined
“”. Check References for the signatures and order of all
ExecuteMarketOrder() parameters. You can find all other method parameters in the reference guide on the cTrader Help Center.
Upon building the cBot and adding an instance, you will see that a market order with the specified parameters was executed at the start. The ‘Positions’ and ‘Log’ tabs in the TradeWatch panel display the corresponding position and log entries.
Placing Pending Orders¶
cTrader Automate supports three types of pending orders, namely stop orders, limit orders, and stop limit orders. To place pending orders at the start, you can replace the market order with the following snippets of code in the earlier cBot example.
1 2 3
To quickly inspect the parameters of the method you are applying in cTrader Automate, open parenthesis at the start and you will see a tip.
If you click on a method/parameter in cTrader Automate and press F1, the help panel to the right of the code window will show the matching search results. If the text cursor remains outside of the code window and you press F1, the ‘Help Center’ window will appear to assist you.
You will see the following records in the TradeWatch panel after successful building and running the cBot.
Modifying Pending Orders and Open Positions¶
When the pending orders are placed, they become available in your cBot
PendingOrders collection. Through this collection, you can access and modify them. The existing pending orders can be modified by adjusting their stop loss level as follows.
1 2 3 4 5 6 7 8 9 10 11
The ‘Log’ tab of the TradeWatch panel displays that the three pending orders were successfully modified immediately after being placed.
After the different order types are executed successfully, positions will be opened for your account. All open positions are available in the
Positions collection of your cBot. Similarly to modifying the pending orders in the previous example, you can modify the open position. Since market orders are the fastest way to open positions, let’s add the
OnBar() method with a position modification action to the first cBot in this guide.
1 2 3 4 5 6 7
As reflected in the log, the open position was modified on the first opened bar.
Closing Positions and Canceling Orders¶
You can find a cBot example below that closes all open positions and cancels all pending orders on Friday at 11:00 (UTC) calling the
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
Subscribing to Trading Events¶
cTrader Automate API allows algorithm developers to subscribe to trading events and monitor trading activity, no matter if these have been initiated by a cBot or manually. It is achieved by listening to events available in the two collections demonstrated earlier,
There are three events available for positions:
They are triggered when you open, modify, and close the positions on your account, respectively.
In the below code snippet, the three events are declared in the
OnStart() method and the methods to be called are assigned. These will automatically create the code signatures for us to use. Afterward,
Print() statements are added to each of the event methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Whether it is you or the cBot who performs the
Closed events, the algorithm will reach to them each time with printed messages as shown in the Log below.
Similarly, you can subscribe to events related to pending orders. There are four available events for pending orders:
The four events are declared in the
OnStart() method, and the event handlers are added as follows.
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
If subscribed to the events related to pending orders, the cBot will react to both manual and programmed trading activities.
To conclude, the Automate API equips algorithm developers with an impressive arsenal of trading operations that can be executed by cBots. By skillfully applying them, you can pursue tailored and sophisticated trading strategies. The cTrader Help Center is available directly from the Automate application to effectively consult you about all available method parameters.