server

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package server exposes pyrite over HTTP: a JSON API, a server-sent event stream for run progress, and the embedded single-page front end.

Index

Constants

This section is empty.

Variables

View Source
var Examples = []Example{
	{
		Tag:   "market cap",
		Title: "Own whatever is biggest",
		Prompt: "Buy $100 of the biggest company in the US by market cap every day, " +
			"and sell it when that company is no longer number one.",
	},
	{
		Tag:    "trend",
		Title:  "Golden cross with a trailing stop",
		Prompt: "Buy SPY when its 50 day moving average crosses above the 200 day, sell on the reverse cross, and use a 12% trailing stop.",
	},
	{
		Tag:    "momentum",
		Title:  "Monthly momentum rotation",
		Prompt: "Every month, hold an equal weight basket of the 3 best performing big tech stocks over the previous 6 months.",
	},
	{
		Tag:    "mean reversion",
		Title:  "Buy the dip on quality names",
		Prompt: "Buy any mega cap stock whose RSI drops below 30, size each position at 10% of the portfolio, and sell when RSI goes back above 55. Use an 8% stop loss.",
	},
	{
		Tag:    "allocation",
		Title:  "Classic 60/40, rebalanced quarterly",
		Prompt: "Hold 60% SPY and 40% AGG, rebalancing back to those weights every quarter.",
	},
	{
		Tag:    "risk",
		Title:  "Go to cash in a downtrend",
		Prompt: "Hold QQQ while it is above its 200 day moving average, and move entirely to cash whenever it closes below.",
	},
	{
		Tag:    "long/short",
		Title:  "Pairs trade Coke against Pepsi",
		Prompt: "Trade KO against PEP. When the ratio of their prices is more than 2 standard deviations below its 60 day average, go long KO and short PEP, and close both when the ratio returns to its average.",
		Note:   "Requires shorting.",
	},
	{
		Tag:    "sectors",
		Title:  "Rotate into the strongest sectors",
		Prompt: "Each month, hold the 2 strongest S&P 500 sector ETFs by 3 month momentum, equally weighted.",
	},
	{
		Tag:    "volatility",
		Title:  "Hide when volatility spikes",
		Prompt: "Hold SPY normally, but move to cash for the next month whenever the VIX closes above 30.",
	},
	{
		Tag:    "AI",
		Title:  "Trade on the weekly news mood",
		Prompt: "Once a week, read the latest news headlines about Apple and ask the AI whether the tone is positive or negative. Hold AAPL when it is positive and stay in cash when it is negative.",
		Note:   "Uses the model and the internet during the backtest. Read the lookahead warning before trusting the result.",
	},
	{
		Tag:    "AI",
		Title:  "Do the opposite of the headlines",
		Prompt: "Every Monday, look up the second article on Yahoo Finance about the S&P 500, ask the AI whether it is bullish or bearish, and take the opposite position in SPY for the week.",
		Note:   "A contrarian take on the same mechanism.",
	},
	{
		Tag:    "dollar cost averaging",
		Title:  "Buy a little every month",
		Prompt: "Invest $500 into VTI on the first trading day of every month and never sell.",
	},
}

Examples are curated to demonstrate the range of the platform, from a plain moving-average rule to strategies that call a model mid-backtest.

Functions

This section is empty.

Types

type Event

type Event struct {
	Type string `json:"type"` // status | progress | done | error | log
	Run  *Run   `json:"run,omitempty"`
	// Message carries human-readable detail for log events.
	Message string `json:"message,omitempty"`
}

Event is one server-sent update.

type Example

type Example struct {
	Title  string `json:"title"`
	Prompt string `json:"prompt"`
	Note   string `json:"note,omitempty"`
	Tag    string `json:"tag,omitempty"`
}

Example is a ready-made prompt shown in the UI to get people started.

type Run

type Run struct {
	ID        string    `json:"id"`
	Status    RunStatus `json:"status"`
	Prompt    string    `json:"prompt"`
	Label     string    `json:"label,omitempty"`
	CreatedAt time.Time `json:"created_at"`
	// Progress is 0..100.
	Progress int        `json:"progress"`
	Stage    string     `json:"stage,omitempty"`
	Day      market.Day `json:"day,omitempty"`

	Plan   *strategy.Plan `json:"plan,omitempty"`
	Result *engine.Result `json:"result,omitempty"`
	// Sweep is set instead of Result when this run was a parameter search.
	// A sweep shares the whole run lifecycle — progress, SSE, cancellation,
	// persistence — because it is the same thing many times over, and a
	// parallel store for it would be duplication rather than design.
	Sweep       *engine.SweepResult       `json:"sweep,omitempty"`
	WalkForward *engine.WalkForwardResult `json:"walk_forward,omitempty"`
	Error       string                    `json:"error,omitempty"`
	// contains filtered or unexported fields
}

Run is one backtest, tracked from request to result.

type RunStatus

type RunStatus string

RunStatus is the lifecycle state of a backtest.

const (
	StatusQueued    RunStatus = "queued"
	StatusCompiling RunStatus = "compiling"
	StatusRunning   RunStatus = "running"
	StatusDone      RunStatus = "done"
	StatusError     RunStatus = "error"
	StatusCancelled RunStatus = "cancelled"
)

type RunStore

type RunStore struct {

	// MaxInMemory bounds memory use; older runs stay on disk.
	MaxInMemory int
	// contains filtered or unexported fields
}

RunStore keeps runs in memory and persists finished ones to disk so a result survives a restart and can be reopened by URL.

func NewRunStore

func NewRunStore(dir string) (*RunStore, error)

NewRunStore creates the store, making its directory.

func (*RunStore) Create

func (s *RunStore) Create(prompt, label string) *Run

Create registers a new run.

func (*RunStore) Delete

func (s *RunStore) Delete(id string) error

Delete removes a run from memory and disk.

func (*RunStore) Get

func (s *RunStore) Get(id string) (*Run, bool)

Get returns a run, loading it from disk if it is not in memory.

func (*RunStore) List

func (s *RunStore) List(limit int) []*Run

List returns run metadata, newest first.

func (*RunStore) ListSaved

func (s *RunStore) ListSaved(limit int) []*Run

ListSaved enumerates run files on disk, newest first.

func (*RunStore) Save

func (s *RunStore) Save(r *Run) error

Save writes a finished run to disk.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server serves the API and the front end.

func New

func New(a *app.App) *Server

New builds a server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe starts the HTTP server and shuts it down when ctx is done.

func (*Server) UseDevAssets

func (s *Server) UseDevAssets(dir string) error

UseDevAssets serves the front end from dir instead of the embedded copy.

Jump to

Keyboard shortcuts

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