Files
mika@stocksharp.com 5b9fd0bf96 Harden analytics and compilation scripts
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.
2026-09-14 18:22:25 +03:00
..
2025-02-04 18:00:35 +03:00
2026-01-11 19:41:41 +03:00

Algo.Analytics.CSharp

This project contains a set of C# analytics scripts designed for the StockSharp trading platform. Each script implements IAnalyticsScript and demonstrates how to process historical market data from S# storage and visualize results through charts, heatmaps or grids. The project is delivered as a class library (targeting .NET 6.0) and can be used inside the StockSharp Designer or any custom application that references StockSharp.Algo.Analytics.

These examples correspond to the Hydra analytics feature described in the documentation.

Overview

The project includes several sample scripts illustrating different analytical tasks:

  • EmptyAnalyticsScript – template script that simply checks input and serves as a starting point for new analytics.
  • BiggestCandleScript – identifies the largest candles (by volume and by length) for each selected security and displays them on separate charts.
  • ChartDrawScript – shows how to draw line and histogram series on charts using candle close prices and volumes.
  • Chart3DScript – demonstrates generation of a 3D chart showing how the biggest trading volume is distributed by hours across multiple securities.
  • IndicatorScript – uses the Rate of Change (ROC) indicator to illustrate adding technical indicators to candle data.
  • NormalizePriceScript – normalizes closing prices relative to the first candle and plots the series on a single chart.
  • PearsonCorrelationScript – computes Pearson correlation between securities (requires MathNet.Numerics) and visualizes the result in a heatmap.
  • PriceVolumeScript – calculates volume distribution by price levels and draws it as a histogram.
  • TimeVolumeScript – aggregates traded volume by hour and outputs the information in a sortable grid.

Each script reads candles from IStorageRegistry using a specified IMarketDataDrive, StorageFormats and time frame. The resulting visuals are created through IAnalyticsPanel which exposes methods for creating charts, grids, heatmaps and 3‑D plots.

Usage

  1. Load this DLL into your application or the S# Designer to run individual scripts.
  2. Provide the securities, time range and storage parameters required by each script. The output will be displayed through the analytics panel implementation used (e.g., charts or tables in the Designer).

Support

For help and discussion, join the StockSharp community chat.