WHAT A SCRIPT PRINTS. The Python engine writes bytes and picks where to cut them, so a multi-byte
character arrives split between two writes. Decoding each write on its own turned such a character
into replacement marks - "тест 🚀" logged as "тест ???" - so the stream now decodes through a
stateful decoder that keeps an incomplete tail until the rest of its bytes turn up, guarded by a lock
because one stream serves concurrently running scripts. The buffer it decodes into is grown to fit
and then reused rather than allocated per write: this is the sink for everything a running script
prints, a line at a time. Only what the current write decoded is read back, so what a longer earlier
write left further along is never seen.
WHAT A CANCELLED RUN DOES. Init checked no token at all, and its blanket catch around each
common-file write swallowed the cancellation the file system raised - a caller who gave up was told
the environment was ready. It now checks before it starts and before each write, and lets
cancellation leave by the door it came in. The analytics scripts did the same thing differently:
they broke out of their loops on a cancelled token and then carried on to report a result computed
from half the data. They throw instead, in every language and at every level of the loop.
THE PYTHON BRIDGE TO AN ASYNC SEQUENCE. IronPython blocked on MoveNextAsync and DisposeAsync through
GetAwaiter().GetResult(). Those ValueTasks are backed by the async iterator's own state machine
rather than by a Task, and GetResult on such a ValueTask does not wait - it throws unless the
operation has already finished, which over an in-memory storage it has not. Both now bridge through
AsTask(), and DisposeAsync is invoked through IAsyncDisposable because IronPython cannot see an
explicit interface implementation as an instance member.
CORRELATION OVER TIME RATHER THAN OVER POSITION. Pearson collected each instrument's closes into a
flat list and lined the series up by index, so a minute missing from one instrument shifted
everything after it and the answer was a correlation of two series that were never contemporaneous.
The closes are keyed by candle time and correlated over the moments the instruments share.
AND A MATRIX THAT IS NOT ONE. The numpy bridge accepted ragged input and a None row, and produced
whatever fell out. It now says which row is the wrong length instead.
Algo.Analytics.Python
This folder contains sample analytics scripts for the StockSharp trading platform written in IronPython. Each script implements the IAnalyticsScript interface and can be executed inside StockSharp Designer or any application that supports analytics scripts.
These examples illustrate the Hydra analytics feature. See the Hydra documentation for usage details.
The examples demonstrate how to analyse market data and visualize the results using StockSharp API. They can be used as a starting point for developing your own analytics tools or trading algorithms in Python.
Requirements
- .NET 6 or higher
- IronPython 3 (the scripts are tested with version 3.4.2)
- StockSharp packages, including
StockSharp.Algo.Analytics
Directory Structure
Algo.Analytics.Python/
├── common/ # Helper modules used by the scripts
├── biggest_candle_script.py
├── chart3d_script.py
├── chart_draw_script.py
├── empty_analytics_script.py
├── indicator_script.py
├── normalize_price_script.py
├── pearson_correlation_script.py
├── price_volume_script.py
└── time_volume_script.py
common helpers
The common directory provides a set of helper functions that simplify interaction with StockSharp objects from Python:
- candle_extensions.py – utilities for working with candle messages (length, body, shadows, middle price, etc.).
- chart_extensions.py – wrappers to create 2D and 3D charts with correct .NET type mapping.
- datatype_extensions.py – convenience function to create
TimeFramevalues. - indicator_extensions.py – helpers to process StockSharp indicators from Python code.
- numpy_extensions.py – utility methods exposing NumPy.NET features to IronPython.
- orderbook_extensions.py – methods to get best bid/ask from order book messages.
- storage_extensions.py – extensions for loading market data from
IStorageRegistry.
Example Scripts
Below is a brief description of the provided scripts.
empty_analytics_script.py
Template demonstrating the minimal implementation of an analytics script. Use it as a starting point for your own logic.
biggest_candle_script.py
Finds the candles with the largest volume and range for each selected security and displays them on separate 3D charts.
chart3d_script.py
Aggregates traded volume by hour for several securities and visualizes the results on a 3D chart.
chart_draw_script.py
Shows how to draw different series styles (lines and histograms) on charts using closing prices and volumes.
indicator_script.py
Calculates the Rate of Change (ROC) indicator for the loaded candles and plots both the candle series and indicator values.
normalize_price_script.py
Normalizes closing prices of multiple securities to start from 1, allowing them to be compared on the same chart.
pearson_correlation_script.py
Loads closing prices for multiple securities, computes the Pearson correlation matrix using NumPy.NET, and displays the matrix as a heatmap.
price_volume_script.py
Calculates how trading volume is distributed across price levels for a single security and draws the distribution as a histogram.
time_volume_script.py
Analyses when the largest trading volume occurs by grouping candles by hour and listing the totals in a grid.
Running Scripts
- Ensure IronPython 3 is available on your
PATH. - Launch StockSharp Designer or your own host application.
- Load any of the
.pyfiles and execute the script. Most scripts expect candle data to be available in your storage; adjust the date range and security identifiers as needed.