src/tzutrader/core

Core types and data structures for tzutrader.

This module defines the fundamental types used throughout the library:

  • Position: Trading position enum (Stay, Buy, Sell)
  • OHLCV: Open-High-Low-Close-Volume data structure
  • Signal: Trading signal representation
  • StrategyConfig: Strategy configuration
  • TzuTraderError: Base exception type

Types

DataError = object of TzuTraderError
Error related to data fetching or processing
OHLCV = object
  timestamp*: int64          ## Unix timestamp
  open*: float64             ## Opening price
  high*: float64             ## Highest price
  low*: float64              ## Lowest price
  close*: float64            ## Closing price
  volume*: float64           ## Trading volume
Open-High-Low-Close-Volume bar data
PortfolioError = object of TzuTraderError
Error related to portfolio operations
Position = enum
  Stay,                     ## Hold current position
  Buy,                      ## Enter or increase long position
  Sell                       ## Exit or decrease long position
Trading position action
Signal = object
  position*: Position        ## Position action (Stay, Buy, Sell)
  symbol*: string            ## Symbol to trade
  timestamp*: int64          ## Signal generation time (Unix timestamp)
  price*: float64            ## Price at signal generation
  reason*: string            ## Human-readable reason for signal
Trading signal generated by a strategy
StrategyConfig = object
  name*: string              ## Strategy name
  params*: Table[string, float64] ## Parameter key-value pairs
Strategy configuration parameters
StrategyError = object of TzuTraderError
Error related to strategy execution
Transaction = object
  timestamp*: int64          ## Transaction time (Unix timestamp)
  symbol*: string            ## Symbol traded
  action*: Position          ## Buy or Sell action
  quantity*: float64         ## Number of shares/units
  price*: float64            ## Execution price
  commission*: float64       ## Trading commission/fees
Record of an executed trade
TzuTraderError = object of CatchableError
Base exception type for tzutrader errors

Procs

proc `$`(ohlcv: OHLCV): string {....raises: [], tags: [], forbids: [].}
String representation of OHLCV data
proc `$`(signal: Signal): string {....raises: [], tags: [], forbids: [].}
String representation of Signal
proc `$`(tx: Transaction): string {....raises: [], tags: [], forbids: [].}
String representation of Transaction
proc change(ohlcv: OHLCV): float64 {....raises: [], tags: [], forbids: [].}
Calculate price change: close - open
proc changePercent(ohlcv: OHLCV): float64 {....raises: [], tags: [], forbids: [].}
Calculate percentage change: (close - open) / open * 100
proc fromJson(node: JsonNode; T: typedesc[OHLCV]): OHLCV
Parse OHLCV from JSON
proc fromJson(node: JsonNode; T: typedesc[Signal]): Signal
Parse Signal from JSON
proc isValid(ohlcv: OHLCV): bool {....raises: [], tags: [], forbids: [].}
Check if OHLCV data is valid Returns false if any price is NaN, negative, or high/low violate constraints
proc newSignal(position: Position; symbol: string; price: float64;
               reason: string = ""): Signal {....raises: [], tags: [TimeEffect],
    forbids: [].}
Create a new trading signal with current timestamp
proc newStrategyConfig(name: string; params: Table[string, float64] = initTable[
    string, float64]()): StrategyConfig {....raises: [], tags: [], forbids: [].}
Create a new strategy configuration
proc newTransaction(symbol: string; action: Position;
                    quantity, price, commission: float64): Transaction {.
    ...raises: [], tags: [TimeEffect], forbids: [].}
Create a new transaction record with current timestamp
proc toJson(config: StrategyConfig): JsonNode {....raises: [], tags: [],
    forbids: [].}
Convert StrategyConfig to JSON
proc toJson(ohlcv: OHLCV): JsonNode {....raises: [], tags: [], forbids: [].}
Convert OHLCV to JSON
proc toJson(signal: Signal): JsonNode {....raises: [], tags: [], forbids: [].}
Convert Signal to JSON
proc toJson(tx: Transaction): JsonNode {....raises: [], tags: [], forbids: [].}
Convert Transaction to JSON
proc trueRange(curr, prev: OHLCV): float64 {....raises: [], tags: [], forbids: [].}
Calculate true range for ATR calculation TR = max(high - low, |high - prevClose|, |low - prevClose|)
proc typicalPrice(ohlcv: OHLCV): float64 {....raises: [], tags: [], forbids: [].}
Calculate typical price: (High + Low + Close) / 3