Writing a strategy
strategies/demo/go/smacross is the reference “real” strategy: a moving-average crossover that goes long one share when a fast SMA crosses above a slow SMA and short one share on the cross below. Each cross trades the difference between one share on its side and the position the fills have built, so the first cross trades one share and every later cross reverses through flat with two. This chapter walks through it as a tour of the SDK. Run it first:
bin/algo run \
--adapter bin/databento-adapter \
--adapter-inputs Path=testdata/xnas-sample/xnas-itch-20220610.trades.csv \
--strategy bin/smacross \
--symbol MSFT --bar-interval 1m[smacross] INFO: smacross: fast SMA(3) crossed below slow SMA(8); selling 1 MSFT
[smacross] INFO: smacross: fill smacross_1654865160000000000: sold 1 MSFT @ 261.85
[smacross] INFO: smacross: fast SMA(3) crossed above slow SMA(8); buying 2 MSFT
[smacross] INFO: smacross: fill smacross_1654865220000000000: bought 2 MSFT @ 262.17
[smacross] INFO: smacross: fast SMA(3) crossed below slow SMA(8); selling 2 MSFT
[smacross] INFO: smacross: fill smacross_1654865460000000000: sold 2 MSFT @ 261.09
[smacross] [smacross] 3 fills (1 buys, 2 sells)
[smacross] [smacross] final position MSFT -1 @ 261.09
...
simulator: resolution=tick bracket_ambiguity=conservative limit_fill=conservative slippage_ticks=0
3 fills, commission 0.00, realized PnL -1.40
open MSFT -1 @ 261.0900 (last 261.0300, unrealized 0.06)
equity 99998.66 (capital 100000.00, return -0.0013%)
...
Data notes 2 notes
short warm-up MSFT@1m 0 of 9 bars
cache disabled: end 2100-01-01T00:00:00Z not historical; pass an explicit --date-to
(The short warm-up is harmless here: the strategy asked for 9 bars of history before the first decision, and the small sample has none to give. With a larger data file the engine pre-rolls that history automatically so your very first in-range bar already has a full lookback behind it.)