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.
StockSharp Algo.Analytics.FSharp
Overview
Algo.Analytics.FSharp contains a collection of analytic scripts written in F# for the StockSharp trading platform. Each script implements the IAnalyticsScript interface and can be executed inside any environment that provides an IAnalyticsPanel such as the S# Designer application. The scripts demonstrate how to read historical market data from IStorageRegistry, perform calculations, and render results using charts, grids, heatmaps, or 3‑D surfaces.
These scripts are examples for the Hydra analytics feature. Refer to the documentation for more information.
The project is distributed as a .NET 6 library and references the core Algo.Analytics project as well as MathNet.Numerics.FSharp for numerical calculations.
Project Structure
- Algo.Analytics.FSharp.fsproj – F# project file targeting .NET 6.
- EmptyAnalyticsScript.fs – template for custom analytics.
- PriceVolumeScript.fs – plots volume distribution by price levels.
- IndicatorScript.fs – demonstrates using the Rate Of Change indicator.
- TimeVolumeScript.fs – groups volume by hour and displays results in a grid.
- Chart3DScript.fs – renders hourly volume distribution for several securities in a 3‑D chart.
- ChartDrawScript.fs – showcases drawing of lines and histograms on charts.
- PearsonCorrelationScript.fs – calculates the Pearson correlation between securities and displays a heatmap.
- NormalizePriceScript.fs – normalizes close prices of different securities.
- BiggestCandleScript.fs – finds candles with the largest range and volume.
Scripts Overview
EmptyAnalyticsScript
A minimal implementation of IAnalyticsScript. It performs no analysis and can be used as a starting point for new scripts.
PriceVolumeScript
Processes candle data for a single instrument and builds a histogram showing how traded volume is distributed across price levels.
IndicatorScript
Loads candles for each selected security, calculates the Rate Of Change (ROC) indicator, and displays close prices together with ROC values on two separate charts.
TimeVolumeScript
Aggregates candle data by hour for the first selected instrument and fills a sortable grid with volumes. This highlights periods of the day with the most activity.
Chart3DScript
Extends the idea of TimeVolumeScript to multiple instruments. For every security it groups volumes by hour, then visualizes the result in a 3‑D surface chart where axes represent instruments and hours.
ChartDrawScript
Demonstrates different drawing styles available in IAnalyticsChart. For each security the script plots close prices as a dashed line and volumes as a histogram.
PearsonCorrelationScript
Uses MathNet.Numerics.Statistics to compute the Pearson correlation matrix of close prices between all selected securities. The resulting matrix is rendered as a heatmap via IAnalyticsPanel.
NormalizePriceScript
Shows how to normalize close prices by the first observed value so that several securities can be compared on the same scale in a single chart.
BiggestCandleScript
Loads candles for each security and identifies the candle with the greatest range and the candle with the highest volume. The results are displayed on two charts using bubble series.