The AlpacaZorroPlugin is a powerful integration between Alpaca, a brokerage providing real-time market data and trading capabilities, and Zorro, an institutional-grade development tool for financial research and automated trading. With this plugin, you can seamlessly trade using Alpaca within the Zorro environment.
To install the plugin, follow these steps:
Alpca.dll
and the websocket_proxy
folder into the plugin
folder under Zorro's root path.AlpacaBrokderCommands.h
file into the include
folder under Zorro's root path.To use the plugin, follow these steps:
Since version 1.0.0, the AlpacaZorroPlugin supports several optional Alpaca-specific configurations, which can be added to either the Zorro.ini
or ZorroFix.ini
file (preferred):
AlpacaPaidDataPlan = 1 // 0 = False (Use Basic Data Plan), 1 = True (Use Pro Data Plan). Default to 1.
AlpacaLogLevel = 0 // 0 = OFF, 1 = ERROR, 2 = WARNING, 3 = INFO, 4 = DEBUG, 5 = TRACE. Default to 0.
AlpacaLogType = 127 // 1 = DEFAULT, 2 = ACCOUNT, 4 = BALANCE, 8 = POSITION, 16 = ORDER, 32 = HISTORY, 64 = MD, 128 = WEB_SOCKET_DATA,
// 127 = DEFAULT | ACCOUNT | BALANCE | POSITION | ORDER | HISTORY | MD, 255 = ALL
AlpacaUseWebsocket = 0 // 0 = Not use Websocket, 1 = Use Websocket. Default to 1.
AlpacaFractionalLotAmount = 0.001 // The LotAmount for fractionable asset. Default to 1.
The Configurations are:
Support Alpaca MarketData V2 Websocket Real-Time Data. Multiple ZorroS instances can share one WebSocket connection through ZorroWebsocketProxy.
The Websocket can be turned off by the AlpacaUseWebsocket
configuration. if AlpacaUseWebsocket
is set to 0, the last price will be polled from the Snapshot REST endpoint.
NTOE: ZorroWebsocketProxy has a lock-free design, it spins on one CPU core. High CPU usage is normal and expected.
Support Limit, Market order types.
// By default, enterLong/enterShort places a Market order
enterLong(5); // long 5 lot at Market price
enterShort(5); // short 5 lot at Market price
// Place a limit
OrderLimit = 100.00; // set a Limit price first
enterLong(5); // place 5 lot at 100.00
Support FOK, IOC, GTC, DAY, OPG, CLS TimeInForce types. See Understand Orders for more info.
// By default, enterLong/enterShort places an order with FOK TimeInfoForce type
// Use borkerCommand(SET_ORDERTYPE, tif) to change TimeInForce.
// Valid TimeInForce value is
// ORDERTYPE_IOC - ICO
// ORDERTYPE_GTC - GTC
// ORDERTYPE_FOK - FOK (default)
// ORDERTYPE_DAY - DAY
// ORDERTYPE_OPG - OPG
// ORDERTYPE_CLS - CLS
//
// NOTE: brokerCommand(SET_ORDERTYPE, 0) will be ignored, this is because Zorro always calls brokerCommand(SET_ORDERTYPE, 0) before setting the limit price.
#include <AlpacaBrokerCommands.h>
brokerCommand(SET_ORDERTYPE, ORDERTYPE_DAY); // set TIF to Day
OrderLimit = 100.00;
enterShort(5); // Sell 5 lot Day order at limit price 100.00
OrderLimit = 0; // set order type back to Market
brokerCommand(SET_ORDERTYPE, ORDERTYPE_CLS); // set TIF to CLS
enteryLong(5); // Buy 5 lot MarketOnClose order
Support LastQuote/LastTrade price type.
// By default, it use ask/bid price mode
// Use brokerCommand(SET_PRICETYPE, 2) to change price type to trades
brokerCommand(SET_PRICETYPE, 2) // Set price type to trades
brokerCommand(SET_PRICETYPE, 1 /*or 0*/) // Set price type to ask/bid quote
Support custom order text
// Use borkderCommand(SET_ORDERTEXT, xxxx) to set a custom order text
brokerCommand(SET_ORDERTEXT, "MyAlpacaOrder"); // "MyAlpacaOrder" will be added into ClientOrderId
enterLong(5);
Support Position retrieval
// get position for specific Asset
brokerCommand(GET_POSITION, "AAPL");
Support Fractional Quantity To enable Franction Quantity support, add the AlpacaFractionalLotAmount config in Zorro.ini or ZorroFix.ini
// AlpacaFractionalLotAmount = 0.001 in ZorroFix.ini
#include <AlpacaBrokerCommands.h>
...
asset("TLSA");
brokerCommand(SET_ORDERTYPE, ORDERTYPE_DAY); // Fractional qty only for Market and Day order type
LotAmount = 5;
enterLong(); // Buy 0.005 shares TLSA
Generate AssetList file through custom borkerCommand
#include <AlpacaBrokerCommands.h>
brokerCommand(CREATE_ASSETLIST, char *symbols);
symbols - One or more symbols separated by a comma, or 0 for all tradeable symbols. An AssetAlpaca.csv file will be generated in the Log directory.
Exemple:
// GenerateAlpacaAssetList.c
#include <AlpacaBrokerCommands.h>
function main() {
brokerCommand(CREATE_ASSETLIST, "SPY,AAPL,MSFT,TSLA"); // Generate AssetsAlpaca.csv contains SPY, AAPL, MSFT, TSLA symbols
//brokerCommand(CREATE_ASSETLIST, 0); // Generate AssetsAplaca.csv contains all tradeable symbols /v2/assets endpoint.
}
Get properties of current Asset
#include <AlpacaBrokerCommands.h>
....
int fractionable = borkerCommand(IS_ASSERT_FRACTIONABLE, Asset);
int shortable = borkerCommand(IS_ASSERT_SHORTABLE, Asset);
int easyToBorrow = borkerCommand(IS_ASSERT_EASY_TO_BORROW, Asset);
int marginable = borkerCommand(IS_ASSERT_EASY_TO_MARGINABLE, Asset);
Set Historical Bar Adjustment
#include <AlpacaBrokerCommands.h>
....
brokerCommand(SET_ADJUSTMENT, ADJUSTMENT_RAW);
// Valid Adjustment value are
// ADJUSTMENT_RAW - 0
// ADJUSTMENT_SPLIT - 1
// ADJUSTMENT_DEVEDEND - 2
// ADJUSTMENT_ALL - 3
Following Zorro Broker API functions has been implemented:
If you find any issues or have any suggestions, please report them in GitHub issues.