New Trading API

Spotware's avatar

Spotware since: 23 Sep 2013;

  22 Nov 2013, 12:59
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.

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
jeex's avatar

jeex since: 18 Oct 2013;

  22 Nov 2013, 13:23
Great job!

Great Job. Congrats.

hichem since: 17 Dec 2012;

  25 Nov 2013, 11:28

Is the new API available on all Brokers?

I have noticed that on the LqdMarkets cAlgo, the new API is not yet updated
 

Spotware's avatar

Spotware since: 23 Sep 2013;

  25 Nov 2013, 14:18
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.

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
Hyperloop's avatar

Hyperloop since: 23 Oct 2013;

  29 Nov 2013, 08:25

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!

Spotware's avatar

Spotware since: 23 Sep 2013;

  29 Nov 2013, 11:31
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.

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
Hyperloop's avatar

Hyperloop since: 23 Oct 2013;

  29 Nov 2013, 20:42

Great, I look forward to future updates.

lec0456's avatar

lec0456 since: 14 Nov 2012;

  13 Dec 2013, 03:56

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);
}

???

Spotware's avatar

Spotware since: 23 Sep 2013;

  13 Dec 2013, 09:25
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. 

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions

hermoso since: 23 Jul 2012;

  31 Dec 2013, 04:53
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?