Skip to content

Feature/golang port#1

Merged
bvasilenko merged 272 commits intomasterfrom
feature/golang-port
Jan 14, 2026
Merged

Feature/golang port#1
bvasilenko merged 272 commits intomasterfrom
feature/golang-port

Conversation

@bvasilenko
Copy link
Collaborator

@bvasilenko bvasilenko commented Nov 15, 2025

Golang Port PoC

Current Performance (Measured)

  • Total: 2792ms
  • Python parser: 2108ms (75%)
  • Runtime execution: 18ms (0.6%)
  • Data fetch: 662ms (23.7%)

Target Performance

  • Total: <50ms (excl. data fetch)
  • Go parser: 5-10ms
  • Go runtime: <10ms

License Safety

  • Current: pynescript v0.2.0 (LGPL 3.0 - VIRAL)
  • Current: escodegen v2.1.0 (BSD-2-Clause)
  • Current: pinets local (unknown)
  • Target: Go stdlib (BSD-3-Clause)
  • Target: participle/v2 (MIT)
  • Target: Pure Go TA

Phase 1: Go Parser + Transpiler

  • Create golang-port structure
  • Initialize Go module
  • Study pine-parser AST output
  • Install participle parser
  • Define PineScript v5 grammar
  • Implement lexer
  • Implement parser
  • Map AST nodes to Go structs
  • Implement codegen
  • Test parsing
  • Compare AST output
  • Generate executable Go code
  • Verify compilation

Phase 2: Go Runtime

  • Create runtime structure
  • Pure Go TA implementation
  • OHLCV context
  • NA value handling
  • Color constants
  • PlotCollector interface
  • Math functions
  • Input functions with overrides
  • SMA, EMA, RMA with warmup
  • RSI with RMA smoothing
  • TR, ATR calculation (security() support added)
  • Bollinger Bands
  • STDEV calculation (security() support added)
  • MACD
  • Stochastic oscillator
  • Strategy entry/close/exit
  • Trade tracking
  • Equity calculation
  • ChartData structure
  • JSON output

Phase 2.5: request.security() Module

Baseline

  • AST scanner (5/5 tests)
  • JSON reader (5/5 tests)
  • Context cache (8/8 tests)
  • Expression prefetch (3/3 tests)
  • Code injection (4/4 tests)
  • BB pattern tests (7/7 PASS)

ForwardSeriesBuffer Alignment

  • Extract AST utilities (SRP)
  • Fetch contexts only
  • Direct OHLCV access
  • Comprehensive edge case tests
  • 266/266 tests PASS

Inline TA States

  • Circular buffer warmup
  • Forward-only sliding window
  • 7/7 tests PASS
  • 82KB → 0B, O(N) → O(1)
  • 8/13 TA functions O(1)
  • SMA circular buffer optimization
  • Keep O(period) for window scans

Complex Expressions

  • BinaryExpression in security
  • Identifier in security
  • 5/5 codegen tests PASS
  • 7/7 baseline tests PASS
  • TernaryExpr in arguments
  • String literal quote trim
  • Parenthesized expressions
  • Visitor/transformer updates
  • Complex expression parsing
  • 10/10 integration tests (28+ cases)
  • Plot styling parameters (style, linewidth, transp, pane)

Integration

  • Builder pipeline integration
  • 10 test suites PASS
  • E2E with multi-timeframe data
  • SMA value verification
  • Timeframe conversion tests
  • Dynamic warmup calculation
  • Bar conversion formula
  • Automatic timeframe fetch
  • Timeframe normalization

Phase 3: Binary Template

  • Create template structure
  • Main template with imports
  • CLI flags
  • Data loading integration
  • Code injection
  • AST codegen
  • CLI entry point
  • Build pine-gen
  • Test code generation
  • Test binary compilation
  • Test execution
  • Verify JSON output
  • Execution <50ms (24µs for 30 bars with placeholder strategy)

Validation

  • Complete AST → Go code generation for Pine functions (ta.sma/ema/rsi/atr/bbands/macd/stoch, plot, if/ternary, Series[offset])
  • Implement strategy.entry, strategy.close, strategy.exit codegen (strategy.close lines 247-251, strategy.entry working)
  • ./bin/strategy on daily-lines-simple.pine validates basic features
  • ./bin/strategy on daily-lines.pine validates advanced features

Phase 4: Additional Pine Features for Complex Strategies

  • Unary expressions (-1, +x, not x, !condition)
  • na constant for NaN value representation
  • timeframe.ismonthly, timeframe.isdaily, timeframe.isweekly built-in variables
  • timeframe.period built-in variable
  • input.float() with title and defval parameters (positional + named)
  • input.int(), input.bool(), input.string() for typed configuration
  • input.source() for selecting price source (close, open, high, low)
  • math.pow() with expression arguments (not just literals)
  • Variable subscript indexing src[variable] where variable is computed
  • Named parameter extraction: input.float(defval=1.4, title="X") fully supported
  • Comprehensive test coverage: input_handler_test.go (6 tests), math_handler_test.go (6 tests), subscript_resolver_test.go (8 tests)
  • Frontend config loading fix: metadata.strategy uses source filename instead of title

Phase 4.5: BB7 Strategy Prerequisites

  • input.session() for time range inputs (entry_time, trading_session)
  • time() function for session filtering
  • Session timezone support (America/New_York, Europe/Moscow, UTC)
  • syminfo.tickerid built-in variable (for security() calls) - Added to template
  • fixnan() function for forward-filling NaN values (pivothigh/pivotlow results)
  • pivothigh() function for resistance detection
  • pivotlow() function for support detection
  • Nested ternary expressions in parentheses (parser grammar fix)
  • math.min() and math.max() inline in conditions/ternaries
  • security() with complex TA function chains (sma, pivothigh/pivotlow, fixnan combinations)
  • barmerge.lookahead_on constant for security() lookahead parameter
  • security() with lookahead parameter support
  • wma() weighted moving average function (WMAHandler implemented and registered)
  • dev() function for deviation detection (DEVHandler implemented and registered)
  • strategy.position_avg_price built-in variable (StateManager + codegen sampling order fixed)
  • valuewhen() function for conditional value retrieval (66+ tests: handler validation, runtime correctness, integration scenarios)
  • valuewhen() runtime evaluation in security() contexts (StreamingBarEvaluator support, 7 test functions, 25 subtests, occurrence/boundary/expression/condition/validation/progression/state coverage)
  • Arrow function preamble extraction (ArrowVarInitResult, PreambleExtractor, module-level functions, 100+ tests, double-assignment syntax fixed)
  • Multi-condition strategy logic with session management
  • Visualization config system integration with BB7

PineScript Support Blockers (10)

  • Codegen TODO: alert, alertcondition, str.tostring, str.tonumber, str.split
  • Type: string variables
  • Runtime: multi-symbol security, syminfo.tickerid mapping
  • Parser: arrow functions, while loops, for loops (literals only), map generics, bitwise operators
  • Codegen: RSI inline

BB7 Dissected Components Testing

  • bb7-dissect-session.pine - manual validation PASSED
  • bb7-dissect-sma.pine - manual validation PASSED
  • bb7-dissect-bb.pine - manual validation PASSED
  • bb7-dissect-vol.pine - manual validation PASSED
  • bb7-dissect-potential.pine - manual validation PASSED
  • bb7-dissect-sl.pine - manual validation PASSED
  • bb7-dissect-tp.pine - manual validation PASSED
  • bb7-dissect-adx.pine - manual validation PASSED

Phase 5: Strategy Validation

  • Comprehensive test coverage: validation package with 28/41 tests passing (edge cases: exact minimum, insufficient data, multiple requirements)
  • ./bin/strategy on rolling-cagr.pine - manual validation PASSED
  • ./bin/strategy on rolling-cagr-5-10yr.pine - manual validation PASSED
  • Config management: Makefile targets (create-config, validate-configs, remove-config, clean-configs)
  • ./bin/strategy on BB7 - manual validation PASSED
  • ./bin/strategy on BB8 - manual validation PASSED
  • ./bin/strategy on BB9 - manual validation PASSED
  • time ./bin/strategy execution <50ms (49µs achieved with real SMA calculation)
  • ldd ./bin/strategy shows no external deps (static binary)
  • E2E: replace node src/index.js with ./bin/strategy in tests
  • E2E: 26/26 tests pass with Go binary

Current Status

  • Parser: 40/40 Pine fixtures parse successfully (100% coverage)
  • Runtime: 15 packages (codegen, parser, chartdata, context, input, math, output, request, series, strategy, ta, value, visual, integration, validation)
  • Codegen: ForwardSeriesBuffer paradigm (ALL variables → Series storage, cursor-based, forward-only, immutable history, O(1) advance)
  • TA Functions: ta.sma/ema/rma/rsi/atr/bbands/macd/stoch/crossover/crossunder/stdev/change/pivothigh/pivotlow/valuewhen, wma, dev
  • TA Execution: Inline calculation per bar using ForwardSeriesBuffer, O(1) per-bar overhead
  • Strategy: entry/close/close_all, if statements, ternary operators, Series historical access (var[offset])
  • Binary: test-simple.pine → 2.9MB static binary (49µs execution for 30 bars)
  • Output: Unified chart format (metadata + candlestick + indicators + strategy + ui sections)
  • Visualization: Config system with filename-based loading (metadata.strategy = source filename)
  • Config Tools: Makefile integration (create-config, validate-configs, list-configs, remove-config, clean-configs)
  • Project structure: Proper .gitignore (bin/, testdata/*-output.json excluded)
  • Test Suite: 605+ tests (preprocessor: 48, chartdata: 22, builder: 18, codegen: 8+11 handlers, expression_analyzer: 10, temp_variable_manager: 11, inline_function_registry: 10, series_source_classifier_ast: 5, validation: 28/41, integration: 40, runtime, datafetcher: 5, security: 271 (74 timezone, 5 Pine-based integration), valuewhen: 66+7, pivot: 95, call_handlers: 35, plot: 127, parser: 40, preprocessor: 29, blockers: 14) - 100% pass rate
  • Handler Test Coverage: input_handler_test.go (6 tests, 14 subtests), math_handler_test.go (6 tests, 13 subtests), subscript_resolver_test.go (5 tests, 16 subtests), call_handler_.go (35 tests, 6 files, 1600+ lines), plot_.go (127 tests: 6 options, 6 buildOptions, 3 titleGen, 6 styleExtract, 20 new generalized tests)
  • Named Parameters: Full ObjectExpression extraction support (input.float(defval=1.4) → const = 1.40)
  • Warmup Validation: Compile-time analyzer detects subscript lookback requirements (close[252] → warns need 253+ bars)
  • Data Infrastructure: BTCUSDT_1D.json extended to 1500 bars (4+ years) supporting 5-year CAGR calculations
  • security() Module: ForwardSeriesBuffer alignment complete (271/271 tests) - ATR support added, dead code removed, AST utilities extracted, comprehensive edge case coverage, pivot runtime evaluation infrastructure (detector/cache/evaluator modules, 95 tests), pivot codegen integration complete, timezone-aware architecture (ExtractDateInTimezone, BuildMappingWithDateFilter, MOEX inference, 74 timezone tests, bar-count independence verified), Bug Feature/golang port #1 & feature/go-runner-extras #2 regression tests (Pine-based integration with output validation: first-bar lookahead, non-overlapping ranges, upscaling, downscaling, same-timeframe)
  • Call Handler Architecture: Strategy pattern refactoring (6 handlers: Meta, Plot, Strategy, TA, Unknown, Router), SOLID principles, 35 comprehensive tests (CanHandle, GenerateCode, Integration, EdgeCases)
  • Plot Module: Comprehensive test coverage (127 tests), all styling parameters (style, linewidth, transp, pane, color, offset, title), type handling (float64 ↔ int conversion), edge cases, generalization, deduplication

@bvasilenko bvasilenko self-assigned this Nov 15, 2025
@github-actions
Copy link

github-actions bot commented Jan 8, 2026

⏱️ Time Estimation

Scope: Branch commits only (5d12f70..4ee876c)

Based on commit timestamp analysis (2h session threshold)

  • Boris Vasilenko: 130.9h (258 commits)

Total: 130.9h across 258 commits

@github-actions
Copy link

⏱️ Time Estimation

Scope: Branch commits only (5d12f70..ed45b8d)

Based on commit timestamp analysis (0.5h session threshold)

  • Boris Vasilenko: 134.3h (266 commits)

Total: 134.3h across 266 commits

@github-actions
Copy link

⏱️ Time Estimation

Scope: Branch commits only (5d12f70..d8bf230)

Based on commit timestamp analysis (0.5h session threshold)

  • Boris Vasilenko: 139.2h (272 commits)

Total: 139.2h across 272 commits

@bvasilenko bvasilenko merged commit 28beb5b into master Jan 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant