Additional materials
Export to UDP Port
The Moonbot terminal can stream market data (5-minute candles and all trades across all markets) to a local UDP port at IP address 127.0.0.1. Data is updated only for active pairs (BTC, USDT, or ETH — depending on the selected terminal configuration).
The terminal can also receive Buy trading signals via a UDP port.
To enable this feature, go to Settings → Advanced → System and enable the UDP Export checkbox.
Data transmission is performed in binary format in packets containing multiple elements. In the UDP parameters, you must specify port 2000 and buffer size 65000.
The candle array is transmitted in full with each update (once every 5 minutes). Trade data is transmitted incrementally (only new trades), but a single packet may contain multiple trades.
Signal reception is performed by the terminal on port 1999 in text format. Received signals are processed by a strategy of type UDP (see figure below):
Example of a signal: “Key=Test1 Coin=NEO Order=buy BuyPrice=0.0071”.
Parameter description:
-
Key = the key used to select the strategy in the terminal with the corresponding ChannelKey field (in the example above — Test1).
-
Coin = coin
-
Order =
-
buy — command to buy
-
sell — triggers selling for a coin previously bought in the terminal (by any strategy, not necessarily UDP)
-
BuyPrice = the price at which the Buy order will be placed. If not specified, the strategy settings will be used.
Example code demonstrating data reception and processing: (download archive with source code and ready example
🔗 https://moonbot.eu/files/udpTest.zip):
// structures
TOrderType = (O_SELL,O_BUY,O_BuyStop);
TUpdateKind = (UK_Candles, UK_Trades);
TUpdateHeader = packed record
Version: byte; // 1 byte - packet version, currently 1
TimeStamp: dword; // 4 bytes - Unix timestamp
Kind: TUpdateKind; // 1 byte - candles (0) or trades (1) inside
Coin: string[7]; // 7 bytes - coin ticker name
Count: word; // 2 bytes - elements count in the data array
reserved1: dword; // 4 bytes currently unused
reserved2: dword; // 4 bytes currently unused
end;
TTradeOrder = record // 8-bytes aligned
ID: integer;
Time: TDateTime;
Price: double;
Quantity: double;
BuyerID: integer;
reserved: integer;
OrderType: TOrderType;
FillType: byte; // 0 - PARTIAL, 1 - FULL
end;
TCandle = record
OpenP,CloseP,MaxP,MinP: double;
Vol: double;
Time: TDateTime;
end;
TTrades = array of TTradeOrder;
TCandles = array of TCandle;
// Data reading and handling
procedure TfrmUDPTest.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
hdr: TUpdateHeader;
Trades: TTrades;
Candles: TCandles;
begin
move(AData[0], hdr, SizeOf(hdr));
If hdr.Kind = UK_Trades then begin
SetLength(Trades, hdr.Count);
move(AData[SizeOf(hdr)], Trades[0], hdr.Count * SizeOf(TTradeOrder));
If SelectedCoin = hdr.Coin
then lPrice.Caption:=hdr.Coin + ‘ Last: ‘ + FloatToStr(Trades[hdr.Count - 1].Price);
end;
If hdr.Kind = UK_Candles then begin
SetLength(Candles, hdr.Count);
move(AData[SizeOf(hdr)], Candles[0], hdr.Count * SizeOf(TCandle));
lLastCandle.Caption:=’ Last candle: ‘ + hdr.Coin + ‘ 5m vol: ‘ + FloatToStr(Candles[hdr.Count - 1].Vol);
end;
lLastTrade.Caption:=Format(‘Last update: %s time: %d’, [hdr.Coin, hdr.TimeStamp]);
end;