- Home
- Forum
- cTrader News & Announcements
- New Trading API
New Trading API
New Trading API
In the new version of cAlgo we added a new trading API. The main advantage of this new trading API is the ability to choose between Synchronous and Asynchronous trade methods. The previous API allowed asynchronous methods only and although it is rather flexible, it is difficult to deal with on occasion, because one needs to implement more methods in order to handle some situations.
Example: Market Order
For example, if we want to execute a market order and print the newly created positions entry price or error message if execution failed, to the log, the code looked like this in the old API:
protected override void OnStart() { var request = new MarketOrderRequest(TradeType.Buy, 10000) { Label = "My Label" }; Trade.Send(request); } protected override void OnPositionOpened(Position openedPosition) { Print("Position created entry price: {0}", openedPosition.EntryPrice); } protected override void OnError(Error error) { Print("Failed to create position :("); }
Now, we can use the synchronous method, which means that our program will not continue execution until the operation is completed. So, everything is implemented in a single method:
protected override void OnStart() { var result = ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "My Label"); if (result.IsSuccessful) { Print("Position created entry price: {0}", result.Position.EntryPrice); } else { Print("Failed to create position :("); } }
Alternatively one can choose asynchronous methods that require us to implement several methods and in general the program becomes more complex. But sometimes it can be useful, for example if we want to execute several commands in parallel:
protected override void OnStart() { ExecuteMarketOrderAsync(TradeType.Buy, Symbol, 10000, "My Label", OnExecuted); } private void OnExecuted(TradeResult result) { if (result.IsSuccessful) { Print("Position created entry price: {0}", result.Position.EntryPrice); } else { Print("Failed to create position :("); } }
You can also notice that the code using new methods is shorter than the code using old requests.
Supported Methods
Right now cAlgo supports the following methods (as well as their Async analogues):
- ExecutemarketOrder
- PlaceLimitOrder
- PlaceStopOrder
- ModifyPosition
- ModifyPendingOrder
- ClosePosition
- CancelPendingOrder
Partial Close
Note that ClosePosition supports partial close now. Volume to close must be specified in a second parameter:
ClosePosition(position, 50000);
Positions and PendingOrders collections
Another change that was made in this version, is the replacement of Account.Positions and Account.PendingOrders collections with Positions and PendingOrders properties of a Robot. We have changed an iteration behavior of those collections so that they become compatible with the new API. For example, you can use code such as the following, to close all positions:
foreach (var position in Positions) { ClosePosition(position); }
If we used Account.Position in the code snippet above, it wouldn't work since the synchronous method ClosePosition closes a position and removes it from the collection. The new collections allow to continue iteration in this case.
We've also added new events to Positions collection: Opened and Closed
protected override void OnStart() { Positions.Opened += OnPositionsOpened; } private void OnPositionsOpened(PositionOpenedEventArgs args) { Print("Position #{0} is opened", args.Position.Id); }
In contrast to the overridden OnPositionOpened(position) method, this is called for any position of an account, not only for positions opened by the current robot instance. If you need to handle the same robots positions opening only, you can use the position's label. The Positions.Closed event works in the same way.
Old API support
The old API will be supported for an infinitely long time, so you don't need to rewrite your existing robots, although we recommend the use of the new API because of it's simplicity. The new features will be added in the new API only.
Is the new API available on all Brokers?
I have noticed that on the LqdMarkets cAlgo, the new API is not yet updated
RE:
hichem said:
Is the new API available on all Brokers?
I have noticed that on the LqdMarkets cAlgo, the new API is not yet updated
Not yet. It will be available on the next update.
Hi,
Are there currently future plans to release additional events for both the Positions AND PendingOrders?
Ex) PendingOrders.Placed, PendingOrders.Cancelled, Positions.Modified, PendingOrders.Modified
Being able to detect global changes like these would be very helpful especially for who run multiple robots simultaneously.
PS. So far the new API is great; love it!
RE:
Hyperloop said:
Hi,
Are there currently future plans to release additional events for both the Positions AND PendingOrders?
Ex) PendingOrders.Placed, PendingOrders.Cancelled, Positions.Modified, PendingOrders.Modified
Being able to detect global changes like these would be very helpful especially for who run multiple robots simultaneously.
PS. So far the new API is great; love it!
Thank you for your kind words. Yes, there are plans for additional events such as you have described.
isn't the code in the example wrong? It reads:
foreach (var position in Positions) { ClosePosition(positions); }
There is o variable positions, right? shouldn't it be:
foreach (var position in Positions) { ClosePosition(position); }
???
RE:
lec0456 said:
isn't the code in the example wrong? It reads:
foreach (var position in Positions) { ClosePosition(positions); }There is o variable positions, right? shouldn't it be:
foreach (var position in Positions) { ClosePosition(position); }???
Yes, thank you for pointing that out.
RE:
lec0456 said:
isn't the code in the example wrong? It reads:
foreach (var position in Positions) { ClosePosition(positions); }There is o variable positions, right? shouldn't it be:
foreach (var position in Positions) { ClosePosition(position); }???
isn't the thing you wrote wrong? It reads:
There is o variable positions, right? shouldn't it be:
There is no variable positions, right?