Writing scripts on the API
Last updated
Last updated
In order to write a script on the TSLab API, you must create a class inherited from the IExternalScript interface.
This interface has the following implementation:
As you can see, the interface has only one Execute method, which takes two parameters.
IContext - contains the general script context; it is needed for drawing graphs and caching.
ISecurity - allows you to work with the tool, contains candles, ticks, you can work with positions.
The Execute method is called each time TSLab starts a script recount.
You can also use other interfaces, they differ only in the number of tools in the parameters:
IExternalScript2 - two tools at the input (ISecurity)
IExternalScript3 - three tools at the input (ISecurity)
IExternalScript4 - four tools at the input (ISecurity)
IExternalScriptMultiSec - input array of tools (ISecurity)
To write scripts, you can use the following template:
Interface IContext |
IsLastBarClosed IsLastBarUsed IsFixedBarsCount | To determine the number of bars in the calculation. |
IsOptimization | Indicates that the optimization process is ongoing. |
TradeFromBar | Indicates which bar to start trading with. |
ScriptResult | Displays the result in an optimization table. |
GetData(...) | Work with a cache. Used to calculate indicators. |
LoadObject(...) StoreObject(...) | Work with the script cache, which is not erased between calculations. |
LoadGlobalObject(...) StoreGlobalObject(...) | Working with a global cache that is shared between various agents and scripts. Data in the cache may be erased if the program does not have enough memory. |
Log(...) | Log output. |
Recalc(...) | Run the script again. |
Interface ISecurity |
FinInfo | Current data on the security (bid, ask, price of the last transaction, open interest, etc.). |
Bars | List of candles. |
OpenPrices HighPrices LowPrices ClosePrices Volumes | List of opening prices. List of highs. List of lows. Closing price list. List of volumes. |
Interval IntervalBase IntervalInstance | The interval of the security. |
LotSize LotTick Margin Tick Decimals | Lot size for a security. Size of the lot change for a security. Margin ratio for a security. Minimum possible price change. The number of decimal places for the security price. |
Positions | List of items. |
Commission | The delegate to calculate the commission. |
InitDeposit | Initial deposit. |
IsRealtime | Indicates whether the agent is in real trading mode. |
Symbol | Security symbol. |
CompressTo(...) CompressToPriceRange(...) CompressToVolume(...) Decompress(...) | Squeeze and unclench candles. |
GetBuyQueue(...) GetSellQueue(...) UpdateQueueData(...) | Get the order queue. |
GetTrades(...) | Get a list of offers. |
RoundPrice(...) RoundShares(...) | Rounding the entry price to the minimum tick. Rounding the incoming quantity to the minimum step of the lot. |