@backtest-kit/sidekick 14.1.0 → 15.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +180 -180
- package/content/config/source/timeframe_15m.pine +113 -113
- package/content/config/source/timeframe_4h.pine +56 -56
- package/content/config/symbol.config.cjs +460 -460
- package/content/docker/ollama/docker-compose.yaml +34 -34
- package/content/docker/ollama/watch.sh +2 -2
- package/content/scripts/cache/cache_candles.mjs +47 -47
- package/content/scripts/cache/cache_model.mjs +42 -42
- package/content/scripts/cache/validate_candles.mjs +46 -46
- package/content/scripts/run_timeframe_15m.mjs +77 -77
- package/content/scripts/run_timeframe_4h.mjs +68 -68
- package/package.json +68 -68
- package/scripts/init.mjs +304 -304
- package/src/classes/BacktestLowerStopOnBreakevenAction.mjs +17 -17
- package/src/classes/BacktestPartialProfitTakingAction.mjs +24 -24
- package/src/classes/BacktestPositionMonitorAction.mjs +57 -57
- package/src/config/ccxt.mjs +15 -15
- package/src/config/params.mjs +1 -1
- package/src/config/setup.mjs +45 -45
- package/src/config/validate.mjs +14 -14
- package/src/enum/ActionName.mjs +5 -5
- package/src/enum/ExchangeName.mjs +3 -3
- package/src/enum/FrameName.mjs +6 -6
- package/src/enum/RiskName.mjs +4 -4
- package/src/enum/StrategyName.mjs +3 -3
- package/src/index.mjs +8 -6
- package/src/logic/action/backtest_lower_stop_on_breakeven.action.mjs +9 -9
- package/src/logic/action/backtest_partial_profit_taking.action.mjs +9 -9
- package/src/logic/action/backtest_position_monitor.action.mjs +9 -9
- package/src/logic/exchange/binance.exchange.mjs +69 -69
- package/src/logic/frame/dec_2025.frame.mjs +10 -10
- package/src/logic/frame/feb_2024.frame.mjs +10 -10
- package/src/logic/frame/nov_2025.frame.mjs +10 -10
- package/src/logic/frame/oct_2025.frame.mjs +10 -10
- package/src/logic/index.mjs +15 -15
- package/src/logic/risk/sl_distance.risk.mjs +32 -32
- package/src/logic/risk/tp_distance.risk.mjs +32 -32
- package/src/logic/strategy/main.strategy.mjs +48 -48
- package/src/main/bootstrap.mjs +52 -52
- package/src/math/timeframe_15m.math.mjs +68 -68
- package/src/math/timeframe_4h.math.mjs +53 -53
- package/src/utils/getArgs.mjs +17 -17
- package/template/CLAUDE.mustache +421 -421
- package/template/README.mustache +257 -257
- package/template/env.mustache +2 -2
- package/template/gitignore.mustache +29 -29
- package/template/jsconfig.json.mustache +26 -26
- package/template/package.mustache +37 -37
- package/template/types.mustache +11 -11
package/README.md
CHANGED
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
# 🧿 @backtest-kit/sidekick
|
|
2
|
-
|
|
3
|
-
> The fastest way to start a [backtest-kit](https://www.npmjs.com/package/backtest-kit) trading bot — but the *full-control* one. Scaffolds a complete multi-timeframe crypto strategy where every wire (exchange, frames, risk, actions, runner) is editable source in **your** project: a 4H trend filter + 15m signal generator in Pine Script, partial profit taking, breakeven trailing stops, and risk validation.
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
-
[](https://npmjs.org/package/@backtest-kit/sidekick)
|
|
9
|
-
[](https://github.com/tripolskypetr/backtest-kit/blob/master/LICENSE)
|
|
10
|
-
|
|
11
|
-
📚 **[Docs](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html)** · 🌟 **[Reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example)** · 🐙 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npx -y @backtest-kit/sidekick my-trading-bot
|
|
15
|
-
cd my-trading-bot && npm start
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## Init vs. Sidekick — pick your level of control
|
|
21
|
-
|
|
22
|
-
`@backtest-kit/cli --init` keeps the boilerplate *inside* the CLI; your repo holds only strategy files. **Sidekick is the "eject":** it writes the entire wiring — exchange adapter, frames, risk rules, actions, bootstrap, runner — as plain, editable source in your project, with no CLI in the loop and nothing hidden. Choose Sidekick when you want to read and own every line, not just the strategy.
|
|
23
|
-
|
|
24
|
-
What you get out of the box: a working multi-timeframe Pine Script strategy (4H trend + 15m signals), SL/TP distance risk validation, partial profit taking + breakeven trailing stops, cache utilities and debug scripts, a `CLAUDE.md` for AI-assisted iteration, and environment config.
|
|
25
|
-
|
|
26
|
-
- 🚀 **Zero config** — one command, no setup.
|
|
27
|
-
- 📊 **Multi-timeframe** — 4H trend filter (RSI+MACD+ADX) + 15m entries (EMA crossover + volume spike + momentum).
|
|
28
|
-
- 📜 **Pine Script v5** — strategies run locally via `@backtest-kit/pinets`, no TradingView.
|
|
29
|
-
- 🛡️ **Risk management** — SL/TP distance validation, 33/33/34 partial profit, breakeven trailing.
|
|
30
|
-
- 🔄 **Full lifecycle** — scheduled/opened/closed/cancelled event logging.
|
|
31
|
-
- 🔌 **Binance via CCXT** — OHLCV, order-book depth, tick-precise formatting.
|
|
32
|
-
- 🕐 **Historical frames** — bull, sharp-drop, and sideways periods predefined.
|
|
33
|
-
- 🎨 **Web dashboard** — `@backtest-kit/ui` charting.
|
|
34
|
-
- 💾 **Crash-safe storage** — atomic persistence for backtest and live.
|
|
35
|
-
|
|
36
|
-
---
|
|
37
|
-
|
|
38
|
-
## The strategy it scaffolds
|
|
39
|
-
|
|
40
|
-
<details>
|
|
41
|
-
<summary>4H trend filter (timeframe_4h.pine)</summary>
|
|
42
|
-
|
|
43
|
-
Classifies the market regime from three indicators:
|
|
44
|
-
|
|
45
|
-
| Regime | Condition |
|
|
46
|
-
|--------|-----------|
|
|
47
|
-
| **AllowLong** | ADX > 25, MACD histogram > 0, DI+ > DI−, RSI > 50 |
|
|
48
|
-
| **AllowShort** | ADX > 25, MACD histogram < 0, DI− > DI+, RSI < 50 |
|
|
49
|
-
| **AllowBoth** | Strong trend, no clear bull/bear regime |
|
|
50
|
-
| **NoTrades** | ADX ≤ 25 (weak trend) |
|
|
51
|
-
|
|
52
|
-
</details>
|
|
53
|
-
|
|
54
|
-
<details>
|
|
55
|
-
<summary>15m signal generator (timeframe_15m.pine)</summary>
|
|
56
|
-
|
|
57
|
-
EMA crossover confirmed by volume and momentum:
|
|
58
|
-
|
|
59
|
-
- **Long** — EMA(5) crosses above EMA(13), RSI 40–65, price above EMA(50), volume spike (>1.5× MA), positive momentum.
|
|
60
|
-
- **Short** — EMA(5) crosses below EMA(13), RSI 35–60, price below EMA(50), volume spike, negative momentum.
|
|
61
|
-
- **SL/TP** — static 2% / 3% from entry. **Signal expiry** — 5 bars.
|
|
62
|
-
|
|
63
|
-
</details>
|
|
64
|
-
|
|
65
|
-
<details>
|
|
66
|
-
<summary>Risk filters & position management</summary>
|
|
67
|
-
|
|
68
|
-
**Risk filters:** reject signals with SL distance < 0.2% or TP distance < 0.2% (slippage protection); enforce trend alignment (longs rejected in a bear regime, shorts in a bull regime).
|
|
69
|
-
|
|
70
|
-
**Position management:** partial profit taking scales out at three levels — 33% at TP3, 33% at TP2, 34% at TP1; when breakeven is reached, the trailing stop is lowered by 3 points.
|
|
71
|
-
|
|
72
|
-
</details>
|
|
73
|
-
|
|
74
|
-
<details>
|
|
75
|
-
<summary>Backtest frames</summary>
|
|
76
|
-
|
|
77
|
-
| Frame | Period | Market note |
|
|
78
|
-
|-------|--------|-------------|
|
|
79
|
-
| `February2024` | Feb 1–29, 2024 | Bull run |
|
|
80
|
-
| `October2025` | Oct 1–31, 2025 | Sharp drop Oct 9–11 |
|
|
81
|
-
| `November2025` | Nov 1–30, 2025 | Sideways with downtrend |
|
|
82
|
-
| `December2025` | Dec 1–31, 2025 | Sideways, no clear direction |
|
|
83
|
-
|
|
84
|
-
</details>
|
|
85
|
-
|
|
86
|
-
---
|
|
87
|
-
|
|
88
|
-
## Generated project structure
|
|
89
|
-
|
|
90
|
-
Everything below lands as editable source in your project — this *is* the package's deliverable.
|
|
91
|
-
|
|
92
|
-
<details>
|
|
93
|
-
<summary>Full tree</summary>
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
my-trading-bot/
|
|
97
|
-
├── src/
|
|
98
|
-
│ ├── index.mjs # Entry point — loads config, logic, bootstrap
|
|
99
|
-
│ ├── main/bootstrap.mjs # Mode dispatcher (backtest / paper / live)
|
|
100
|
-
│ ├── config/
|
|
101
|
-
│ │ ├── setup.mjs # Logger, storage, notifications, UI server
|
|
102
|
-
│ │ ├── validate.mjs # Schema validation for all enums
|
|
103
|
-
│ │ ├── params.mjs # Environment variables (Ollama API key)
|
|
104
|
-
│ │ └── ccxt.mjs # Binance exchange singleton via CCXT
|
|
105
|
-
│ ├── logic/
|
|
106
|
-
│ │ ├── strategy/main.strategy.mjs # Main strategy — multi-TF signal logic
|
|
107
|
-
│ │ ├── exchange/binance.exchange.mjs # Exchange schema — candles, order book, formatting
|
|
108
|
-
│ │ ├── frame/*.frame.mjs # Backtest time frames (Feb 2024, Oct–Dec 2025)
|
|
109
|
-
│ │ ├── risk/sl_distance.risk.mjs # Stop-loss distance validation (≥0.2%)
|
|
110
|
-
│ │ ├── risk/tp_distance.risk.mjs # Take-profit distance validation (≥0.2%)
|
|
111
|
-
│ │ └── action/
|
|
112
|
-
│ │ ├── backtest_partial_profit_taking.action.mjs
|
|
113
|
-
│ │ ├── backtest_lower_stop_on_breakeven.action.mjs
|
|
114
|
-
│ │ └── backtest_position_monitor.action.mjs
|
|
115
|
-
│ ├── classes/
|
|
116
|
-
│ │ ├── BacktestPartialProfitTakingAction.mjs # Scale out at 3 TP levels
|
|
117
|
-
│ │ ├── BacktestLowerStopOnBreakevenAction.mjs # Trailing stop on breakeven
|
|
118
|
-
│ │ └── BacktestPositionMonitorAction.mjs # Position event logger
|
|
119
|
-
│ ├── math/
|
|
120
|
-
│ │ ├── timeframe_4h.math.mjs # 4H trend data — RSI, MACD, ADX, DI+/DI−
|
|
121
|
-
│ │ └── timeframe_15m.math.mjs # 15m signal data — EMA, ATR, volume, momentum
|
|
122
|
-
│ ├── enum/ # String constants for type-safe schema refs
|
|
123
|
-
│ └── utils/getArgs.mjs # CLI argument parser with defaults
|
|
124
|
-
├── config/source/
|
|
125
|
-
│ ├── timeframe_4h.pine # Pine Script v5 — Daily Trend Filter (RSI/MACD/ADX)
|
|
126
|
-
│ └── timeframe_15m.pine # Pine Script v5 — Signal Strategy (EMA/ATR/Volume)
|
|
127
|
-
├── scripts/
|
|
128
|
-
│ ├── run_timeframe_15m.mjs # Standalone 15m Pine Script runner
|
|
129
|
-
│ ├── run_timeframe_4h.mjs # Standalone 4H Pine Script runner
|
|
130
|
-
│ └── cache/
|
|
131
|
-
│ ├── cache_candles.mjs # Pre-download OHLCV candles (1m/15m/4h)
|
|
132
|
-
│ ├── validate_candles.mjs # Verify cached candle data integrity
|
|
133
|
-
│ └── cache_model.mjs # Pull Ollama LLM model with progress bar
|
|
134
|
-
├── docker/ollama/
|
|
135
|
-
│ ├── docker-compose.yaml # Ollama GPU container setup
|
|
136
|
-
│ └── watch.sh # nvidia-smi monitor
|
|
137
|
-
├── CLAUDE.md # AI strategy development guide
|
|
138
|
-
├── .env # Environment variables
|
|
139
|
-
└── package.json
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
</details>
|
|
143
|
-
|
|
144
|
-
---
|
|
145
|
-
|
|
146
|
-
## CLI options & dependencies
|
|
147
|
-
|
|
148
|
-
```bash
|
|
149
|
-
npx -y @backtest-kit/sidekick my-bot # named project
|
|
150
|
-
npx -y @backtest-kit/sidekick . # current directory (must be empty)
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
<details>
|
|
154
|
-
<summary>Dependencies installed</summary>
|
|
155
|
-
|
|
156
|
-
| Package | Purpose |
|
|
157
|
-
|---------|---------|
|
|
158
|
-
| [backtest-kit](https://www.npmjs.com/package/backtest-kit) | Core backtesting / trading framework |
|
|
159
|
-
| [@backtest-kit/pinets](https://www.npmjs.com/package/@backtest-kit/pinets) | Pine Script v5 runtime for Node.js |
|
|
160
|
-
| [@backtest-kit/ui](https://www.npmjs.com/package/@backtest-kit/ui) | Interactive charting dashboard |
|
|
161
|
-
| [@backtest-kit/ollama](https://www.npmjs.com/package/@backtest-kit/ollama) | LLM inference integration |
|
|
162
|
-
| [ccxt](https://github.com/ccxt/ccxt) | Binance exchange connectivity |
|
|
163
|
-
| [functools-kit](https://www.npmjs.com/package/functools-kit) | `singleshot`, `randomString` utilities |
|
|
164
|
-
| [pinolog](https://www.npmjs.com/package/pinolog) | File-based structured logging |
|
|
165
|
-
| [openai](https://www.npmjs.com/package/openai) | OpenAI API client |
|
|
166
|
-
| [ollama](https://www.npmjs.com/package/ollama) | Ollama local LLM client |
|
|
167
|
-
|
|
168
|
-
</details>
|
|
169
|
-
|
|
170
|
-
## 🔗 Links
|
|
171
|
-
|
|
172
|
-
[Documentation](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html) · [GitHub](https://github.com/tripolskypetr/backtest-kit) · [Reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example)
|
|
173
|
-
|
|
174
|
-
## 🤝 Contribute
|
|
175
|
-
|
|
176
|
-
Found a bug or want a feature? [Open an issue](https://github.com/tripolskypetr/backtest-kit/issues) or submit a PR.
|
|
177
|
-
|
|
178
|
-
## 📜 License
|
|
179
|
-
|
|
180
|
-
MIT © [tripolskypetr](https://github.com/tripolskypetr)
|
|
1
|
+
# 🧿 @backtest-kit/sidekick
|
|
2
|
+
|
|
3
|
+
> The fastest way to start a [backtest-kit](https://www.npmjs.com/package/backtest-kit) trading bot — but the *full-control* one. Scaffolds a complete multi-timeframe crypto strategy where every wire (exchange, frames, risk, actions, runner) is editable source in **your** project: a 4H trend filter + 15m signal generator in Pine Script, partial profit taking, breakeven trailing stops, and risk validation.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
+
[](https://npmjs.org/package/@backtest-kit/sidekick)
|
|
9
|
+
[](https://github.com/tripolskypetr/backtest-kit/blob/master/LICENSE)
|
|
10
|
+
|
|
11
|
+
📚 **[Docs](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html)** · 🌟 **[Reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example)** · 🐙 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx -y @backtest-kit/sidekick my-trading-bot
|
|
15
|
+
cd my-trading-bot && npm start
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Init vs. Sidekick — pick your level of control
|
|
21
|
+
|
|
22
|
+
`@backtest-kit/cli --init` keeps the boilerplate *inside* the CLI; your repo holds only strategy files. **Sidekick is the "eject":** it writes the entire wiring — exchange adapter, frames, risk rules, actions, bootstrap, runner — as plain, editable source in your project, with no CLI in the loop and nothing hidden. Choose Sidekick when you want to read and own every line, not just the strategy.
|
|
23
|
+
|
|
24
|
+
What you get out of the box: a working multi-timeframe Pine Script strategy (4H trend + 15m signals), SL/TP distance risk validation, partial profit taking + breakeven trailing stops, cache utilities and debug scripts, a `CLAUDE.md` for AI-assisted iteration, and environment config.
|
|
25
|
+
|
|
26
|
+
- 🚀 **Zero config** — one command, no setup.
|
|
27
|
+
- 📊 **Multi-timeframe** — 4H trend filter (RSI+MACD+ADX) + 15m entries (EMA crossover + volume spike + momentum).
|
|
28
|
+
- 📜 **Pine Script v5** — strategies run locally via `@backtest-kit/pinets`, no TradingView.
|
|
29
|
+
- 🛡️ **Risk management** — SL/TP distance validation, 33/33/34 partial profit, breakeven trailing.
|
|
30
|
+
- 🔄 **Full lifecycle** — scheduled/opened/closed/cancelled event logging.
|
|
31
|
+
- 🔌 **Binance via CCXT** — OHLCV, order-book depth, tick-precise formatting.
|
|
32
|
+
- 🕐 **Historical frames** — bull, sharp-drop, and sideways periods predefined.
|
|
33
|
+
- 🎨 **Web dashboard** — `@backtest-kit/ui` charting.
|
|
34
|
+
- 💾 **Crash-safe storage** — atomic persistence for backtest and live.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## The strategy it scaffolds
|
|
39
|
+
|
|
40
|
+
<details>
|
|
41
|
+
<summary>4H trend filter (timeframe_4h.pine)</summary>
|
|
42
|
+
|
|
43
|
+
Classifies the market regime from three indicators:
|
|
44
|
+
|
|
45
|
+
| Regime | Condition |
|
|
46
|
+
|--------|-----------|
|
|
47
|
+
| **AllowLong** | ADX > 25, MACD histogram > 0, DI+ > DI−, RSI > 50 |
|
|
48
|
+
| **AllowShort** | ADX > 25, MACD histogram < 0, DI− > DI+, RSI < 50 |
|
|
49
|
+
| **AllowBoth** | Strong trend, no clear bull/bear regime |
|
|
50
|
+
| **NoTrades** | ADX ≤ 25 (weak trend) |
|
|
51
|
+
|
|
52
|
+
</details>
|
|
53
|
+
|
|
54
|
+
<details>
|
|
55
|
+
<summary>15m signal generator (timeframe_15m.pine)</summary>
|
|
56
|
+
|
|
57
|
+
EMA crossover confirmed by volume and momentum:
|
|
58
|
+
|
|
59
|
+
- **Long** — EMA(5) crosses above EMA(13), RSI 40–65, price above EMA(50), volume spike (>1.5× MA), positive momentum.
|
|
60
|
+
- **Short** — EMA(5) crosses below EMA(13), RSI 35–60, price below EMA(50), volume spike, negative momentum.
|
|
61
|
+
- **SL/TP** — static 2% / 3% from entry. **Signal expiry** — 5 bars.
|
|
62
|
+
|
|
63
|
+
</details>
|
|
64
|
+
|
|
65
|
+
<details>
|
|
66
|
+
<summary>Risk filters & position management</summary>
|
|
67
|
+
|
|
68
|
+
**Risk filters:** reject signals with SL distance < 0.2% or TP distance < 0.2% (slippage protection); enforce trend alignment (longs rejected in a bear regime, shorts in a bull regime).
|
|
69
|
+
|
|
70
|
+
**Position management:** partial profit taking scales out at three levels — 33% at TP3, 33% at TP2, 34% at TP1; when breakeven is reached, the trailing stop is lowered by 3 points.
|
|
71
|
+
|
|
72
|
+
</details>
|
|
73
|
+
|
|
74
|
+
<details>
|
|
75
|
+
<summary>Backtest frames</summary>
|
|
76
|
+
|
|
77
|
+
| Frame | Period | Market note |
|
|
78
|
+
|-------|--------|-------------|
|
|
79
|
+
| `February2024` | Feb 1–29, 2024 | Bull run |
|
|
80
|
+
| `October2025` | Oct 1–31, 2025 | Sharp drop Oct 9–11 |
|
|
81
|
+
| `November2025` | Nov 1–30, 2025 | Sideways with downtrend |
|
|
82
|
+
| `December2025` | Dec 1–31, 2025 | Sideways, no clear direction |
|
|
83
|
+
|
|
84
|
+
</details>
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Generated project structure
|
|
89
|
+
|
|
90
|
+
Everything below lands as editable source in your project — this *is* the package's deliverable.
|
|
91
|
+
|
|
92
|
+
<details>
|
|
93
|
+
<summary>Full tree</summary>
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
my-trading-bot/
|
|
97
|
+
├── src/
|
|
98
|
+
│ ├── index.mjs # Entry point — loads config, logic, bootstrap
|
|
99
|
+
│ ├── main/bootstrap.mjs # Mode dispatcher (backtest / paper / live)
|
|
100
|
+
│ ├── config/
|
|
101
|
+
│ │ ├── setup.mjs # Logger, storage, notifications, UI server
|
|
102
|
+
│ │ ├── validate.mjs # Schema validation for all enums
|
|
103
|
+
│ │ ├── params.mjs # Environment variables (Ollama API key)
|
|
104
|
+
│ │ └── ccxt.mjs # Binance exchange singleton via CCXT
|
|
105
|
+
│ ├── logic/
|
|
106
|
+
│ │ ├── strategy/main.strategy.mjs # Main strategy — multi-TF signal logic
|
|
107
|
+
│ │ ├── exchange/binance.exchange.mjs # Exchange schema — candles, order book, formatting
|
|
108
|
+
│ │ ├── frame/*.frame.mjs # Backtest time frames (Feb 2024, Oct–Dec 2025)
|
|
109
|
+
│ │ ├── risk/sl_distance.risk.mjs # Stop-loss distance validation (≥0.2%)
|
|
110
|
+
│ │ ├── risk/tp_distance.risk.mjs # Take-profit distance validation (≥0.2%)
|
|
111
|
+
│ │ └── action/
|
|
112
|
+
│ │ ├── backtest_partial_profit_taking.action.mjs
|
|
113
|
+
│ │ ├── backtest_lower_stop_on_breakeven.action.mjs
|
|
114
|
+
│ │ └── backtest_position_monitor.action.mjs
|
|
115
|
+
│ ├── classes/
|
|
116
|
+
│ │ ├── BacktestPartialProfitTakingAction.mjs # Scale out at 3 TP levels
|
|
117
|
+
│ │ ├── BacktestLowerStopOnBreakevenAction.mjs # Trailing stop on breakeven
|
|
118
|
+
│ │ └── BacktestPositionMonitorAction.mjs # Position event logger
|
|
119
|
+
│ ├── math/
|
|
120
|
+
│ │ ├── timeframe_4h.math.mjs # 4H trend data — RSI, MACD, ADX, DI+/DI−
|
|
121
|
+
│ │ └── timeframe_15m.math.mjs # 15m signal data — EMA, ATR, volume, momentum
|
|
122
|
+
│ ├── enum/ # String constants for type-safe schema refs
|
|
123
|
+
│ └── utils/getArgs.mjs # CLI argument parser with defaults
|
|
124
|
+
├── config/source/
|
|
125
|
+
│ ├── timeframe_4h.pine # Pine Script v5 — Daily Trend Filter (RSI/MACD/ADX)
|
|
126
|
+
│ └── timeframe_15m.pine # Pine Script v5 — Signal Strategy (EMA/ATR/Volume)
|
|
127
|
+
├── scripts/
|
|
128
|
+
│ ├── run_timeframe_15m.mjs # Standalone 15m Pine Script runner
|
|
129
|
+
│ ├── run_timeframe_4h.mjs # Standalone 4H Pine Script runner
|
|
130
|
+
│ └── cache/
|
|
131
|
+
│ ├── cache_candles.mjs # Pre-download OHLCV candles (1m/15m/4h)
|
|
132
|
+
│ ├── validate_candles.mjs # Verify cached candle data integrity
|
|
133
|
+
│ └── cache_model.mjs # Pull Ollama LLM model with progress bar
|
|
134
|
+
├── docker/ollama/
|
|
135
|
+
│ ├── docker-compose.yaml # Ollama GPU container setup
|
|
136
|
+
│ └── watch.sh # nvidia-smi monitor
|
|
137
|
+
├── CLAUDE.md # AI strategy development guide
|
|
138
|
+
├── .env # Environment variables
|
|
139
|
+
└── package.json
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
</details>
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## CLI options & dependencies
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npx -y @backtest-kit/sidekick my-bot # named project
|
|
150
|
+
npx -y @backtest-kit/sidekick . # current directory (must be empty)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
<details>
|
|
154
|
+
<summary>Dependencies installed</summary>
|
|
155
|
+
|
|
156
|
+
| Package | Purpose |
|
|
157
|
+
|---------|---------|
|
|
158
|
+
| [backtest-kit](https://www.npmjs.com/package/backtest-kit) | Core backtesting / trading framework |
|
|
159
|
+
| [@backtest-kit/pinets](https://www.npmjs.com/package/@backtest-kit/pinets) | Pine Script v5 runtime for Node.js |
|
|
160
|
+
| [@backtest-kit/ui](https://www.npmjs.com/package/@backtest-kit/ui) | Interactive charting dashboard |
|
|
161
|
+
| [@backtest-kit/ollama](https://www.npmjs.com/package/@backtest-kit/ollama) | LLM inference integration |
|
|
162
|
+
| [ccxt](https://github.com/ccxt/ccxt) | Binance exchange connectivity |
|
|
163
|
+
| [functools-kit](https://www.npmjs.com/package/functools-kit) | `singleshot`, `randomString` utilities |
|
|
164
|
+
| [pinolog](https://www.npmjs.com/package/pinolog) | File-based structured logging |
|
|
165
|
+
| [openai](https://www.npmjs.com/package/openai) | OpenAI API client |
|
|
166
|
+
| [ollama](https://www.npmjs.com/package/ollama) | Ollama local LLM client |
|
|
167
|
+
|
|
168
|
+
</details>
|
|
169
|
+
|
|
170
|
+
## 🔗 Links
|
|
171
|
+
|
|
172
|
+
[Documentation](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html) · [GitHub](https://github.com/tripolskypetr/backtest-kit) · [Reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example)
|
|
173
|
+
|
|
174
|
+
## 🤝 Contribute
|
|
175
|
+
|
|
176
|
+
Found a bug or want a feature? [Open an issue](https://github.com/tripolskypetr/backtest-kit/issues) or submit a PR.
|
|
177
|
+
|
|
178
|
+
## 📜 License
|
|
179
|
+
|
|
180
|
+
MIT © [tripolskypetr](https://github.com/tripolskypetr)
|
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
//@version=5
|
|
2
|
-
indicator("Signal Strategy 15m v1", overlay=true)
|
|
3
|
-
|
|
4
|
-
plotcandle(open, high, low, close,
|
|
5
|
-
color=close > open ? color.new(color.green, 70) : color.new(color.red, 70),
|
|
6
|
-
wickcolor=color.new(color.gray, 70),
|
|
7
|
-
bordercolor=color.new(color.gray, 70))
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// === INPUTS ===
|
|
11
|
-
rsi_len = input.int(7, "RSI Length", minval=2)
|
|
12
|
-
ema_fast_len = input.int(5, "EMA Fast", minval=1)
|
|
13
|
-
ema_slow_len = input.int(13, "EMA Slow", minval=1)
|
|
14
|
-
ema_trend_len = input.int(50, "EMA Trend", minval=1)
|
|
15
|
-
atr_len = input.int(14, "ATR Length", minval=1)
|
|
16
|
-
vol_ma_len = input.int(20, "Volume MA Length", minval=1)
|
|
17
|
-
|
|
18
|
-
sl_mult = input.float(1.5, "SL ATR Multiplier", minval=0.5, step=0.1)
|
|
19
|
-
tp_mult = input.float(2.5, "TP ATR Multiplier", minval=0.5, step=0.1)
|
|
20
|
-
signal_valid_bars = input.int(5, "Signal Valid Bars", minval=1)
|
|
21
|
-
|
|
22
|
-
// === INDICATORS ===
|
|
23
|
-
rsi = ta.rsi(close, rsi_len)
|
|
24
|
-
ema_fast = ta.ema(close, ema_fast_len)
|
|
25
|
-
ema_slow = ta.ema(close, ema_slow_len)
|
|
26
|
-
ema_trend = ta.ema(close, ema_trend_len)
|
|
27
|
-
atr = ta.atr(atr_len)
|
|
28
|
-
|
|
29
|
-
// Volume filter - выше среднего
|
|
30
|
-
vol_ma = ta.sma(volume, vol_ma_len)
|
|
31
|
-
vol_spike = volume > vol_ma * 1.5
|
|
32
|
-
|
|
33
|
-
// Momentum confirmation
|
|
34
|
-
mom = ta.mom(close, 3)
|
|
35
|
-
mom_up = mom > 0
|
|
36
|
-
mom_down = mom < 0
|
|
37
|
-
|
|
38
|
-
// === TREND FILTER ===
|
|
39
|
-
trend_up = close > ema_trend and ema_fast > ema_trend
|
|
40
|
-
trend_down = close < ema_trend and ema_fast < ema_trend
|
|
41
|
-
|
|
42
|
-
// === ENTRY CONDITIONS ===
|
|
43
|
-
// Long: пересечение + RSI не перекуплен + тренд вверх + объём + momentum
|
|
44
|
-
long_cond = ta.crossover(ema_fast, ema_slow) and rsi > 40 and rsi < 65 and trend_up and vol_spike and mom_up
|
|
45
|
-
|
|
46
|
-
// Short: пересечение + RSI не перепродан + тренд вниз + объём + momentum
|
|
47
|
-
short_cond = ta.crossunder(ema_fast, ema_slow) and rsi < 60 and rsi > 35 and trend_down and vol_spike and mom_down
|
|
48
|
-
|
|
49
|
-
// === SIGNAL MANAGEMENT ===
|
|
50
|
-
var int bars_since_signal = 0
|
|
51
|
-
var int last_signal = 0
|
|
52
|
-
var float entry_price = na
|
|
53
|
-
var float signal_atr = na
|
|
54
|
-
|
|
55
|
-
if long_cond
|
|
56
|
-
last_signal := 1
|
|
57
|
-
bars_since_signal := 0
|
|
58
|
-
entry_price := close
|
|
59
|
-
signal_atr := atr
|
|
60
|
-
else if short_cond
|
|
61
|
-
last_signal := -1
|
|
62
|
-
bars_since_signal := 0
|
|
63
|
-
entry_price := close
|
|
64
|
-
signal_atr := atr
|
|
65
|
-
else
|
|
66
|
-
bars_since_signal += 1
|
|
67
|
-
|
|
68
|
-
// Signal expires faster on 15m
|
|
69
|
-
active_signal = bars_since_signal <= signal_valid_bars ? last_signal : 0
|
|
70
|
-
|
|
71
|
-
// === DYNAMIC SL/TP based on ATR ===
|
|
72
|
-
// sl = last_signal == 1 ? entry_price - signal_atr * sl_mult : last_signal == -1 ? entry_price + signal_atr * sl_mult : na
|
|
73
|
-
// tp = last_signal == 1 ? entry_price + signal_atr * tp_mult : last_signal == -1 ? entry_price - signal_atr * tp_mult : na
|
|
74
|
-
|
|
75
|
-
// === STATIC SL/TP for watch strategy ==
|
|
76
|
-
sl = last_signal == -1 ? close * 1.02 : close * 0.98
|
|
77
|
-
tp = last_signal == -1 ? close * 0.97 : close * 1.03
|
|
78
|
-
|
|
79
|
-
// === VISUALIZATION ===
|
|
80
|
-
line_color = active_signal == 1 ? color.green : active_signal == -1 ? color.red : color.gray
|
|
81
|
-
|
|
82
|
-
plot(ema_fast, "EMA Fast", color=color.new(color.blue, 50), linewidth=1)
|
|
83
|
-
plot(ema_slow, "EMA Slow", color=color.new(color.orange, 50), linewidth=1)
|
|
84
|
-
plot(ema_trend, "EMA Trend", color=color.new(color.white, 70), linewidth=2)
|
|
85
|
-
|
|
86
|
-
plotshape(long_cond, "Long", shape.triangleup, location.belowbar, color.green, size=size.small)
|
|
87
|
-
plotshape(short_cond, "Short", shape.triangledown, location.abovebar, color.red, size=size.small)
|
|
88
|
-
|
|
89
|
-
plot(close, "Active Signal", color=line_color, linewidth=6)
|
|
90
|
-
|
|
91
|
-
// === OUTPUTS FOR BOT ===
|
|
92
|
-
plot(close, "Close", display=display.data_window)
|
|
93
|
-
plot(active_signal, "Signal", display=display.data_window)
|
|
94
|
-
plot(sl, "StopLoss", display=display.data_window)
|
|
95
|
-
plot(tp, "TakeProfit", display=display.data_window)
|
|
96
|
-
plot(1440, "EstimatedTime", display=display.data_window) // 24 hour for 15m TF
|
|
97
|
-
|
|
98
|
-
// === DEBUG: INDICATOR DUMP ===
|
|
99
|
-
plot(rsi, "d_RSI", display=display.data_window)
|
|
100
|
-
plot(ema_fast, "d_EmaFast", display=display.data_window)
|
|
101
|
-
plot(ema_slow, "d_EmaSlow", display=display.data_window)
|
|
102
|
-
plot(ema_trend, "d_EmaTrend", display=display.data_window)
|
|
103
|
-
plot(atr, "d_ATR", display=display.data_window)
|
|
104
|
-
plot(volume, "d_Volume", display=display.data_window)
|
|
105
|
-
plot(vol_ma, "d_VolMA", display=display.data_window)
|
|
106
|
-
plot(vol_spike ? 1 : 0, "d_VolSpike", display=display.data_window)
|
|
107
|
-
plot(mom, "d_Mom", display=display.data_window)
|
|
108
|
-
plot(mom_up ? 1 : 0, "d_MomUp", display=display.data_window)
|
|
109
|
-
plot(mom_down ? 1 : 0, "d_MomDown", display=display.data_window)
|
|
110
|
-
plot(trend_up ? 1 : 0, "d_TrendUp", display=display.data_window)
|
|
111
|
-
plot(trend_down ? 1 : 0, "d_TrendDown", display=display.data_window)
|
|
112
|
-
plot(long_cond ? 1 : 0, "d_LongCond", display=display.data_window)
|
|
113
|
-
plot(short_cond ? 1 : 0, "d_ShortCond", display=display.data_window)
|
|
1
|
+
//@version=5
|
|
2
|
+
indicator("Signal Strategy 15m v1", overlay=true)
|
|
3
|
+
|
|
4
|
+
plotcandle(open, high, low, close,
|
|
5
|
+
color=close > open ? color.new(color.green, 70) : color.new(color.red, 70),
|
|
6
|
+
wickcolor=color.new(color.gray, 70),
|
|
7
|
+
bordercolor=color.new(color.gray, 70))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// === INPUTS ===
|
|
11
|
+
rsi_len = input.int(7, "RSI Length", minval=2)
|
|
12
|
+
ema_fast_len = input.int(5, "EMA Fast", minval=1)
|
|
13
|
+
ema_slow_len = input.int(13, "EMA Slow", minval=1)
|
|
14
|
+
ema_trend_len = input.int(50, "EMA Trend", minval=1)
|
|
15
|
+
atr_len = input.int(14, "ATR Length", minval=1)
|
|
16
|
+
vol_ma_len = input.int(20, "Volume MA Length", minval=1)
|
|
17
|
+
|
|
18
|
+
sl_mult = input.float(1.5, "SL ATR Multiplier", minval=0.5, step=0.1)
|
|
19
|
+
tp_mult = input.float(2.5, "TP ATR Multiplier", minval=0.5, step=0.1)
|
|
20
|
+
signal_valid_bars = input.int(5, "Signal Valid Bars", minval=1)
|
|
21
|
+
|
|
22
|
+
// === INDICATORS ===
|
|
23
|
+
rsi = ta.rsi(close, rsi_len)
|
|
24
|
+
ema_fast = ta.ema(close, ema_fast_len)
|
|
25
|
+
ema_slow = ta.ema(close, ema_slow_len)
|
|
26
|
+
ema_trend = ta.ema(close, ema_trend_len)
|
|
27
|
+
atr = ta.atr(atr_len)
|
|
28
|
+
|
|
29
|
+
// Volume filter - выше среднего
|
|
30
|
+
vol_ma = ta.sma(volume, vol_ma_len)
|
|
31
|
+
vol_spike = volume > vol_ma * 1.5
|
|
32
|
+
|
|
33
|
+
// Momentum confirmation
|
|
34
|
+
mom = ta.mom(close, 3)
|
|
35
|
+
mom_up = mom > 0
|
|
36
|
+
mom_down = mom < 0
|
|
37
|
+
|
|
38
|
+
// === TREND FILTER ===
|
|
39
|
+
trend_up = close > ema_trend and ema_fast > ema_trend
|
|
40
|
+
trend_down = close < ema_trend and ema_fast < ema_trend
|
|
41
|
+
|
|
42
|
+
// === ENTRY CONDITIONS ===
|
|
43
|
+
// Long: пересечение + RSI не перекуплен + тренд вверх + объём + momentum
|
|
44
|
+
long_cond = ta.crossover(ema_fast, ema_slow) and rsi > 40 and rsi < 65 and trend_up and vol_spike and mom_up
|
|
45
|
+
|
|
46
|
+
// Short: пересечение + RSI не перепродан + тренд вниз + объём + momentum
|
|
47
|
+
short_cond = ta.crossunder(ema_fast, ema_slow) and rsi < 60 and rsi > 35 and trend_down and vol_spike and mom_down
|
|
48
|
+
|
|
49
|
+
// === SIGNAL MANAGEMENT ===
|
|
50
|
+
var int bars_since_signal = 0
|
|
51
|
+
var int last_signal = 0
|
|
52
|
+
var float entry_price = na
|
|
53
|
+
var float signal_atr = na
|
|
54
|
+
|
|
55
|
+
if long_cond
|
|
56
|
+
last_signal := 1
|
|
57
|
+
bars_since_signal := 0
|
|
58
|
+
entry_price := close
|
|
59
|
+
signal_atr := atr
|
|
60
|
+
else if short_cond
|
|
61
|
+
last_signal := -1
|
|
62
|
+
bars_since_signal := 0
|
|
63
|
+
entry_price := close
|
|
64
|
+
signal_atr := atr
|
|
65
|
+
else
|
|
66
|
+
bars_since_signal += 1
|
|
67
|
+
|
|
68
|
+
// Signal expires faster on 15m
|
|
69
|
+
active_signal = bars_since_signal <= signal_valid_bars ? last_signal : 0
|
|
70
|
+
|
|
71
|
+
// === DYNAMIC SL/TP based on ATR ===
|
|
72
|
+
// sl = last_signal == 1 ? entry_price - signal_atr * sl_mult : last_signal == -1 ? entry_price + signal_atr * sl_mult : na
|
|
73
|
+
// tp = last_signal == 1 ? entry_price + signal_atr * tp_mult : last_signal == -1 ? entry_price - signal_atr * tp_mult : na
|
|
74
|
+
|
|
75
|
+
// === STATIC SL/TP for watch strategy ==
|
|
76
|
+
sl = last_signal == -1 ? close * 1.02 : close * 0.98
|
|
77
|
+
tp = last_signal == -1 ? close * 0.97 : close * 1.03
|
|
78
|
+
|
|
79
|
+
// === VISUALIZATION ===
|
|
80
|
+
line_color = active_signal == 1 ? color.green : active_signal == -1 ? color.red : color.gray
|
|
81
|
+
|
|
82
|
+
plot(ema_fast, "EMA Fast", color=color.new(color.blue, 50), linewidth=1)
|
|
83
|
+
plot(ema_slow, "EMA Slow", color=color.new(color.orange, 50), linewidth=1)
|
|
84
|
+
plot(ema_trend, "EMA Trend", color=color.new(color.white, 70), linewidth=2)
|
|
85
|
+
|
|
86
|
+
plotshape(long_cond, "Long", shape.triangleup, location.belowbar, color.green, size=size.small)
|
|
87
|
+
plotshape(short_cond, "Short", shape.triangledown, location.abovebar, color.red, size=size.small)
|
|
88
|
+
|
|
89
|
+
plot(close, "Active Signal", color=line_color, linewidth=6)
|
|
90
|
+
|
|
91
|
+
// === OUTPUTS FOR BOT ===
|
|
92
|
+
plot(close, "Close", display=display.data_window)
|
|
93
|
+
plot(active_signal, "Signal", display=display.data_window)
|
|
94
|
+
plot(sl, "StopLoss", display=display.data_window)
|
|
95
|
+
plot(tp, "TakeProfit", display=display.data_window)
|
|
96
|
+
plot(1440, "EstimatedTime", display=display.data_window) // 24 hour for 15m TF
|
|
97
|
+
|
|
98
|
+
// === DEBUG: INDICATOR DUMP ===
|
|
99
|
+
plot(rsi, "d_RSI", display=display.data_window)
|
|
100
|
+
plot(ema_fast, "d_EmaFast", display=display.data_window)
|
|
101
|
+
plot(ema_slow, "d_EmaSlow", display=display.data_window)
|
|
102
|
+
plot(ema_trend, "d_EmaTrend", display=display.data_window)
|
|
103
|
+
plot(atr, "d_ATR", display=display.data_window)
|
|
104
|
+
plot(volume, "d_Volume", display=display.data_window)
|
|
105
|
+
plot(vol_ma, "d_VolMA", display=display.data_window)
|
|
106
|
+
plot(vol_spike ? 1 : 0, "d_VolSpike", display=display.data_window)
|
|
107
|
+
plot(mom, "d_Mom", display=display.data_window)
|
|
108
|
+
plot(mom_up ? 1 : 0, "d_MomUp", display=display.data_window)
|
|
109
|
+
plot(mom_down ? 1 : 0, "d_MomDown", display=display.data_window)
|
|
110
|
+
plot(trend_up ? 1 : 0, "d_TrendUp", display=display.data_window)
|
|
111
|
+
plot(trend_down ? 1 : 0, "d_TrendDown", display=display.data_window)
|
|
112
|
+
plot(long_cond ? 1 : 0, "d_LongCond", display=display.data_window)
|
|
113
|
+
plot(short_cond ? 1 : 0, "d_ShortCond", display=display.data_window)
|
|
114
114
|
plot(bars_since_signal, "d_BarsSinceSignal", display=display.data_window)
|