📜 ⬆️ ⬇️

How-to: robots and brokerage trading system APIs

image

In one of the past topics, we began to discuss the general structure of the Russian stock exchange market, and today we’ll take a closer look at one of its links - broker systems. The brokerage business is technologically intrinsic: customers submit bids using trading terminals often developed by brokers, and an automated system for collecting bids and billing them to the stock exchange is required for processing bids and sending them to the exchange. Such systems, equipped with authorization and limiting tools, which allow to smasshrutizirovat an application to the market and give the client information about its status and the current status of its portfolio, are usually called broker's trading systems.

Most applications on modern exchanges are not generated by humans, but by specially created trading robots, which operate according to a given algorithm. For these programs, the key factor is the overall speed of work, which depends on the efficiency of the robot itself, and on the communication channels, and on the power of the iron on which it works. Of course, to connect the robot to the trading system, it must have an API. We will talk about this today.
')

Why do you need it?


The work of the robot directly with broker servers (bypassing client interfaces) allows it to quickly receive data on trades (Market Data) and account status, process these data faster and, based on them, generate buy or sell orders and then track their execution. In this scheme, the trading speed depends only on the speed of the robot itself and the communication channels.

image

API Specs


The interface for connecting to our brokerage system is created using the component object model (COM). This means that robots developed on platforms that support this technology, from C ++ and Delphi to Visual Basic for Application from MS Excel, can be connected to trading servers.

Recently, a new version of the API (SmartCOM 3.0) has been released, which works with the trading system launched this year called MatriX (we used IBM Data Power technologies to create it — there will be a separate topic about this).

The key features of the open interface include the following:


Types of robots


image

Developers engaged in the development of robots for systems of different brokers form a rather active community, which includes both individual enthusiasts and traders who create robots for their needs, as well as companies professionally engaged in building automated trading systems to order.

The result is quite a lot of various programs, both paid and distributed freely. Here are a couple of examples:


Buy a trading robot or download a free program, of course, a good idea. But to do everything yourself - much more interesting and intriguing task, isn't it?

Make a robot


Let's try to make our own simplest robot that will connect to the trading system, request account information and quotes, and also place orders on the stock exchange:

using System; using System.Runtime.InteropServices; using SmartCOM3Lib; namespace SmartTest { class SmartCOMTest { StServer mSmartCOMInstance; /*  */ public SmartCOMTest() { try { /*    StServerClass */ mSmartCOMInstance = new StServerClass(); /*    */ mSmartCOMInstance.ConfigureClient("logLevel=5;"); /*    */ mSmartCOMInstance.Connected += new _IStClient_ConnectedEventHandler(mSmartCOMInstance_Connected); mSmartCOMInstance.Disconnected += new _IStClient_DisconnectedEventHandler(mSmartCOMInstance_Disconnected); mSmartCOMInstance.SetPortfolio += new _IStClient_SetPortfolioEventHandler(mSmartCOMInstance_SetPortfolio); mSmartCOMInstance.SetMyOrder += new _IStClient_SetMyOrderEventHandler(mSmartCOMInstance_SetMyOrder); mSmartCOMInstance.UpdateBidAsk += new _IStClient_UpdateBidAskEventHandler(mSmartCOMInstance_UpdateBidAsk); } catch (COMException ex) { /*   COM- */ } catch (Exception ex) { /*    */ } } /*  */ public void Run() { try { /*        */ /*        Connected */ mSmartCOMInstance.connect("mx.ittrade.ru", 8443, "BPXXXX", "xxxxxx"); /*      ,    Esc,      */ while (Console.ReadKey().Key != ConsoleKey.Escape); /*    */ mSmartCOMInstance.disconnect(); } catch (COMException ex) { /*   COM- */ } catch (Exception ex) { /*    */ } } void mSmartCOMInstance_SetPortfolio(string portfolio, double cash, double leverage, double comission, double saldo) { /*    */ } void mSmartCOMInstance_Disconnected(string reason) { /*            *      */ } void mSmartCOMInstance_Connected() { /*   ,      */ /*         SBER */ mSmartCOMInstance.ListenPortfolio("BPXXXX-MS-01"); mSmartCOMInstance.ListenBidAsks("SBER"); } void mSmartCOMInstance_UpdateBidAsk(string symbol, int row, int nrows, double bid, double bidsize, double ask, double asksize) { /*         */ /* ,         *         , *     : */ if (/*      */) /*     1      */ mSmartCOMInstance.PlaceOrder("BPXXXX-MS-01", "SBER", StOrder_Action.StOrder_Action_Buy, StOrder_Type.StOrder_Type_Market, StOrder_Validity.StOrder_Validity_Day, 0, 1, 0, 0); } void mSmartCOMInstance_SetMyOrder(int row, int nrows, string portfolio, string symbol, StOrder_State state, StOrder_Action action, StOrder_Type type, StOrder_Validity validity, double price, double amount, double stop, double filled, DateTime datetime, string id, string no, int cookie) { /*       */ } } class Program { /*   */ static void Main(string[] args) { SmartCOMTest smartCOMTest = new SmartCOMTest(); smartCOMTest.Run(); } } } 

A more complete list of available API functions can be found here .

In order not to lose real money due to errors in the work of the robot, first it is better to debug it on a special test account - a risk-free virtual exchange, with money, shares, futures and everything necessary for full-fledged trading.

Job


It’s quite difficult for the human eye to understand what’s what, that's why, usually, an ordinary trading terminal is also used to monitor the operation of the robot.

image

That's all for today. Next time we will talk about what is happening “inside” the brokerage system: how it accepts and processes applications, how they are put on the stock exchange, and what equipment and software are used for this work.

PS If you have any questions about the creation and use of trading robots, then ask them in the comments or write a letter to smartcom@itinvest.ru . In addition, all topics of interest related to the SmartCOM API can be discussed with the system developer at Brainstorge .

Source: https://habr.com/ru/post/201702/


All Articles