examples

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 5 Imported by: 0

README

Example strategies

Hand-written strategies in the pyrite JavaScript API. They serve three purposes: something to read when learning the API, a starting point to edit, and a check that the API can express these ideas cleanly without a model in the loop.

Running one

Paste the file into the Code tab in the web interface and press Re-run edited code, or post it directly:

curl -X POST http://127.0.0.1:8080/api/runs \
  -H 'Content-Type: application/json' \
  -d "$(jq -n --arg code "$(cat examples/golden-cross.js)" \
        '{code:$code, name:"Golden cross", universe:["SPY"], warmup:220,
          start:"2015-01-01", end:"2024-12-31", benchmarks:["SPY"]}')"

universe and warmup must be supplied when running code directly — those come from the compiler's plan when you use a plain-English prompt.

The files

File Idea Notes
biggest-company.js Own whichever US company is largest by market cap The flagship example
golden-cross.js 50/200 SMA crossover with a trailing stop Shows crossing detection via ctx.state
momentum-rotation.js Monthly rotation into the strongest names Shows ctx.rank and ctx.equalWeight
mean-reversion.js Buy oversold, exit on recovery Shows per-symbol loops and risk exits
sixty-forty.js 60/40 stocks and bonds, quarterly rebalance Shows ctx.rebalance
pairs-trade.js Long/short KO against PEP on their price ratio Requires allow_short
news-sentiment.js Weekly news read by a model Read the lookahead warning first

Writing your own

The full API reference is pyrite api, or the Strategy API button in the interface. Two rules cover most mistakes:

  1. Guard every indicator. They return null until enough history exists.
  2. Set warmup high enough. A 200-day average needs at least 200 bars before the strategy can do anything.

Documentation

Overview

Package examples ships the bundled strategies inside the binary.

The Go file lives here, beside the .js files, rather than under internal/ with a copy of them. Two copies would drift, and examples/ at the repository root is where anyone browsing on GitHub looks first.

Embedding is what makes `pyrite run --example golden-cross` work from a downloaded binary with nothing checked out, no API key and nothing else installed. That is the shortest path from finding the project to seeing a real result on real data, and it is worth a few kilobytes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Names

func Names() []string

Names lists the bundled example names, sorted.

Types

type Example

type Example struct {
	Name string `json:"name"`
	// Label is a short human name derived from the file name, for a chart
	// legend or a metrics column. Title is a full sentence and is documentation
	// rather than a label; using it as one produces a legend nobody can read.
	Label string `json:"label"`
	// Title and Summary are read from the leading comment block, so each file
	// stays the single source of truth rather than duplicating a description
	// into a Go table that will drift from it.
	Title   string `json:"title"`
	Summary string `json:"summary"`
	Code    string `json:"code"`
	// Universe, Benchmarks and Warmup come from header directives in the
	// file, so a bundled example runs correctly with no flags at all.
	Universe   []string `json:"universe,omitempty"`
	Benchmarks []string `json:"benchmarks,omitempty"`
	Warmup     int      `json:"warmup,omitempty"`
	AllowShort bool     `json:"allow_short,omitempty"`
	// NeedsModel marks an example that calls a model inside the backtest, so
	// the CLI can say why it will not run without one.
	NeedsModel bool `json:"needs_model,omitempty"`
}

Example is one bundled strategy.

func All

func All() []Example

All returns every bundled example.

func Get

func Get(name string) (Example, error)

Get loads one example by name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL