@backtest-kit/sidekick 0.1.2 β 3.0.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 +120 -13
- package/content/config/source/timeframe_15m.pine +114 -0
- package/content/config/source/timeframe_4h.pine +57 -0
- package/content/config/symbol.config.cjs +460 -0
- package/content/docker/ollama/docker-compose.yaml +34 -0
- package/content/docker/ollama/watch.sh +2 -0
- package/content/scripts/cache/cache_candles.mjs +47 -0
- package/content/scripts/cache/cache_model.mjs +42 -0
- package/content/scripts/cache/validate_candles.mjs +46 -0
- package/content/scripts/run_timeframe_15m.mjs +77 -0
- package/content/scripts/run_timeframe_4h.mjs +68 -0
- package/package.json +2 -2
- package/scripts/init.mjs +40 -9
- package/src/classes/BacktestLowerStopOnBreakevenAction.mjs +18 -0
- package/src/classes/BacktestPartialProfitTakingAction.mjs +5 -0
- package/src/classes/BacktestPositionMonitorAction.mjs +4 -0
- package/src/config/setup.mjs +27 -1
- package/src/enum/ActionName.mjs +1 -1
- package/src/enum/FrameName.mjs +1 -0
- package/src/enum/RiskName.mjs +1 -1
- package/src/logic/action/backtest_lower_stop_on_breakeven.action.mjs +9 -0
- package/src/logic/exchange/binance.exchange.mjs +12 -2
- package/src/logic/frame/feb_2024.frame.mjs +10 -0
- package/src/logic/index.mjs +3 -2
- package/src/logic/risk/sl_distance.risk.mjs +32 -0
- package/src/logic/risk/tp_distance.risk.mjs +5 -3
- package/src/logic/strategy/main.strategy.mjs +29 -12
- package/src/main/bootstrap.mjs +1 -1
- package/src/math/timeframe_15m.math.mjs +68 -0
- package/src/math/timeframe_4h.math.mjs +53 -0
- package/template/CLAUDE.mustache +421 -0
- package/template/README.mustache +232 -24
- package/template/env.mustache +1 -17
- package/template/jsconfig.json.mustache +1 -0
- package/template/package.mustache +8 -5
- package/src/classes/BacktestTightenStopOnBreakevenAction.mjs +0 -13
- package/src/func/market.func.mjs +0 -46
- package/src/logic/action/backtest_tighten_stop_on_breakeven.action.mjs +0 -9
- package/src/logic/risk/rr_ratio.risk.mjs +0 -39
- /package/{types/backtest-kit.d.ts β template/types.mustache} +0 -0
package/template/README.mustache
CHANGED
|
@@ -1,48 +1,256 @@
|
|
|
1
1
|
# {{PROJECT_NAME}}
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Multi-timeframe crypto trading strategy built on [backtest-kit](https://libraries.io/npm/backtest-kit) with Pine Script indicators via [PineTS](https://github.com/QuantForgeOrg/PineTS) runtime. Uses a 4H trend filter + 15m signal generator to open long/short positions on Binance spot market with partial profit taking, breakeven trailing stops, and risk validation.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+

|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[](https://deepwiki.com/tripolskypetr/backtest-kit)
|
|
8
|
+
[](https://npmjs.org/package/backtest-kit)
|
|
9
|
+
[]()
|
|
10
|
+
[](https://github.com/tripolskypetr/backtest-kit/actions/workflows/webpack.yml)
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
## β¨ Features
|
|
13
|
+
|
|
14
|
+
- π **Multi-timeframe analysis** β 4H daily trend filter (RSI + MACD + ADX) combined with 15m entry signals (EMA crossover + volume spike + momentum)
|
|
15
|
+
- π **Pine Script indicators** β strategies written in TradingView Pine Script v5, executed locally via `@backtest-kit/pinets`
|
|
16
|
+
- π‘οΈ **Risk management** β SL/TP distance validation, Kelly-optimized partial profit taking (33/33/34%), breakeven trailing stop
|
|
17
|
+
- π **Position lifecycle** β full monitoring with scheduled/opened/closed/cancelled event logging
|
|
18
|
+
- π **Binance integration** β OHLCV candles, order book depth, tick-precise price/quantity formatting via CCXT
|
|
19
|
+
- π **Historical frames** β predefined backtest periods covering bull runs, sharp drops, and sideways markets
|
|
20
|
+
- π¨ **Web UI dashboard** β interactive charting via `@backtest-kit/ui`
|
|
21
|
+
- πΎ **Persistent storage** β crash-safe state with atomic persistence for both backtest and live modes
|
|
22
|
+
|
|
23
|
+
## ποΈ Architecture
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
src/
|
|
27
|
+
βββ index.mjs # Entry point β loads config, logic, bootstrap
|
|
28
|
+
βββ main/bootstrap.mjs # Mode dispatcher (backtest / paper / live)
|
|
29
|
+
βββ config/
|
|
30
|
+
β βββ setup.mjs # Logger, storage, notifications, UI server
|
|
31
|
+
β βββ validate.mjs # Schema validation for all enums
|
|
32
|
+
β βββ params.mjs # Environment variables (Ollama API key)
|
|
33
|
+
β βββ ccxt.mjs # Binance exchange singleton via CCXT
|
|
34
|
+
βββ logic/
|
|
35
|
+
β βββ strategy/main.strategy.mjs # Main strategy β multi-TF signal logic
|
|
36
|
+
β βββ exchange/binance.exchange.mjs # Exchange schema β candles, order book, formatting
|
|
37
|
+
β βββ frame/*.frame.mjs # Backtest time frames (Feb 2024, OctβDec 2025)
|
|
38
|
+
β βββ risk/sl_distance.risk.mjs # Stop-loss distance validation (β₯0.2%)
|
|
39
|
+
β βββ risk/tp_distance.risk.mjs # Take-profit distance validation (β₯0.2%)
|
|
40
|
+
β βββ action/
|
|
41
|
+
β βββ backtest_partial_profit_taking.action.mjs
|
|
42
|
+
β βββ backtest_lower_stop_on_breakeven.action.mjs
|
|
43
|
+
β βββ backtest_position_monitor.action.mjs
|
|
44
|
+
βββ classes/
|
|
45
|
+
β βββ BacktestPartialProfitTakingAction.mjs # Scale out at 3 TP levels
|
|
46
|
+
β βββ BacktestLowerStopOnBreakevenAction.mjs # Trailing stop on breakeven
|
|
47
|
+
β βββ BacktestPositionMonitorAction.mjs # Position event logger
|
|
48
|
+
βββ math/
|
|
49
|
+
β βββ timeframe_4h.math.mjs # 4H trend data β RSI, MACD, ADX, DI+/DI-
|
|
50
|
+
β βββ timeframe_15m.math.mjs # 15m signal data β EMA, ATR, volume, momentum
|
|
51
|
+
βββ enum/ # String constants for type-safe schema refs
|
|
52
|
+
β βββ ExchangeName.mjs
|
|
53
|
+
β βββ StrategyName.mjs
|
|
54
|
+
β βββ FrameName.mjs
|
|
55
|
+
β βββ RiskName.mjs
|
|
56
|
+
β βββ ActionName.mjs
|
|
57
|
+
βββ utils/getArgs.mjs # CLI argument parser with defaults
|
|
58
|
+
|
|
59
|
+
config/source/
|
|
60
|
+
βββ timeframe_4h.pine # Pine Script v5 β Daily Trend Filter (RSI/MACD/ADX)
|
|
61
|
+
βββ timeframe_15m.pine # Pine Script v5 β Signal Strategy (EMA/ATR/Volume)
|
|
62
|
+
|
|
63
|
+
scripts/
|
|
64
|
+
βββ run_timeframe_15m.mjs # Standalone 15m Pine Script runner β outputs signal markdown
|
|
65
|
+
βββ run_timeframe_4h.mjs # Standalone 4H Pine Script runner β outputs trend markdown
|
|
66
|
+
βββ cache/
|
|
67
|
+
βββ cache_candles.mjs # Pre-download OHLCV candles (1m/15m/4h) for a date range
|
|
68
|
+
βββ validate_candles.mjs # Verify cached candle data integrity and completeness
|
|
69
|
+
βββ cache_model.mjs # Pull Ollama LLM model (glm-4.7-flash:q4_K_M) with progress bar
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## π‘ Strategy Overview
|
|
73
|
+
|
|
74
|
+
### π― 4H Trend Filter (`timeframe_4h.pine`)
|
|
75
|
+
|
|
76
|
+
Determines the market regime using three indicators:
|
|
77
|
+
|
|
78
|
+
| Regime | Condition |
|
|
79
|
+
|--------|-----------|
|
|
80
|
+
| **AllowLong** | ADX > 25, MACD histogram > 0, DI+ > DI-, RSI > 50 |
|
|
81
|
+
| **AllowShort** | ADX > 25, MACD histogram < 0, DI- > DI+, RSI < 50 |
|
|
82
|
+
| **AllowBoth** | Strong trend but no clear bull/bear regime |
|
|
83
|
+
| **NoTrades** | ADX β€ 25 (weak trend) |
|
|
84
|
+
|
|
85
|
+
### β‘ 15m Signal Generator (`timeframe_15m.pine`)
|
|
86
|
+
|
|
87
|
+
Generates entry signals with EMA crossover confirmed by volume and momentum:
|
|
88
|
+
|
|
89
|
+
- **Long**: EMA(5) crosses above EMA(13), RSI 40β65, price above EMA(50), volume spike (>1.5x MA), positive momentum
|
|
90
|
+
- **Short**: EMA(5) crosses below EMA(13), RSI 35β60, price below EMA(50), volume spike, negative momentum
|
|
91
|
+
- **SL/TP**: Static 2%/3% from entry price
|
|
92
|
+
- **Signal expiry**: 5 bars
|
|
93
|
+
|
|
94
|
+
### π‘οΈ Risk Filters
|
|
95
|
+
|
|
96
|
+
- Reject signals where SL distance < 0.2% (slippage protection)
|
|
97
|
+
- Reject signals where TP distance < 0.2% (slippage protection)
|
|
98
|
+
- Trend alignment: long signals rejected in bear regime, short signals rejected in bull regime
|
|
99
|
+
|
|
100
|
+
### πΉ Position Management
|
|
101
|
+
|
|
102
|
+
- **Partial profit taking**: Scale out at 3 levels β 33% at TP3, 33% at TP2, 34% at TP1
|
|
103
|
+
- **Breakeven trailing stop**: When breakeven is reached, lower trailing stop by 3 points
|
|
104
|
+
|
|
105
|
+
## π Backtest Frames
|
|
106
|
+
|
|
107
|
+
| Frame | Period | Market Note |
|
|
108
|
+
|-------|--------|-------------|
|
|
109
|
+
| `February2024` | Feb 1β29, 2024 | Bull run |
|
|
110
|
+
| `October2025` | Oct 1β31, 2025 | Sharp drop Oct 9β11 |
|
|
111
|
+
| `November2025` | Nov 1β30, 2025 | Sideways with downtrend |
|
|
112
|
+
| `December2025` | Dec 1β31, 2025 | Sideways, no clear direction |
|
|
113
|
+
|
|
114
|
+
## π¦ Cache Utilities
|
|
115
|
+
|
|
116
|
+
Standalone scripts in `scripts/cache/` for preparing data before running backtests:
|
|
10
117
|
|
|
11
118
|
```bash
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
CC_BINANCE_API_SECRET=your_api_secret_here
|
|
119
|
+
# Download 1m, 15m, 4h candles for BTCUSDT (Feb 2024) from Binance
|
|
120
|
+
node scripts/cache/cache_candles.mjs
|
|
15
121
|
|
|
16
|
-
#
|
|
17
|
-
|
|
122
|
+
# Validate downloaded candle data β check for gaps and missing intervals
|
|
123
|
+
node scripts/cache/validate_candles.mjs
|
|
124
|
+
|
|
125
|
+
# Pull the Ollama LLM model (glm-4.7-flash:q4_K_M) with progress bar
|
|
126
|
+
node scripts/cache/cache_model.mjs
|
|
18
127
|
```
|
|
19
128
|
|
|
20
|
-
|
|
129
|
+
`cache_candles.mjs` and `validate_candles.mjs` register a temporary `ccxt-exchange` schema and call `warmCandles` / `checkCandles` from `backtest-kit` to populate and verify the local candle cache. `cache_model.mjs` pulls the `glm-4.7-flash:q4_K_M` model to a local Ollama instance at `http://127.0.0.1:11434`.
|
|
130
|
+
|
|
131
|
+
## π¬ Debug Scripts
|
|
132
|
+
|
|
133
|
+
Standalone scripts in `scripts/` for testing Pine Script indicators outside of backtest mode:
|
|
21
134
|
|
|
22
135
|
```bash
|
|
23
|
-
|
|
136
|
+
# Run 15m signal strategy on BTCUSDT and print markdown output
|
|
137
|
+
node scripts/run_timeframe_15m.mjs
|
|
138
|
+
|
|
139
|
+
# Run 4H trend filter on BTCUSDT and print markdown output
|
|
140
|
+
node scripts/run_timeframe_4h.mjs
|
|
24
141
|
```
|
|
25
142
|
|
|
26
|
-
|
|
143
|
+
Both scripts register a temporary Binance exchange, execute the corresponding `.pine` file via `@backtest-kit/pinets`, and output a markdown table with all indicator values (RSI, EMA, MACD, ADX, volume, momentum, etc.) for a fixed historical timestamp. Useful for verifying indicator logic and signal schema mapping without running a full backtest.
|
|
27
144
|
|
|
28
|
-
|
|
29
|
-
- [GitHub Repository](https://github.com/tripolskypetr/backtest-kit)
|
|
145
|
+
## π Dump Directory Structure
|
|
30
146
|
|
|
31
|
-
|
|
147
|
+
After running a backtest, all output is written to the `dump/` directory:
|
|
32
148
|
|
|
33
149
|
```
|
|
34
|
-
|
|
35
|
-
βββ
|
|
36
|
-
β βββ
|
|
37
|
-
β βββ
|
|
38
|
-
βββ
|
|
39
|
-
|
|
40
|
-
βββ
|
|
150
|
+
dump/
|
|
151
|
+
βββ data/
|
|
152
|
+
β βββ candle/{exchange}/{symbol}/{timeframe}/ # Cached OHLCV candles
|
|
153
|
+
β β βββ {timestamp_ms}.json # One candle per file
|
|
154
|
+
β βββ notification/backtest/ # Trading event notifications
|
|
155
|
+
β β βββ {uuid}.json
|
|
156
|
+
β βββ storage/backtest/ # Signal/position state
|
|
157
|
+
β βββ {signalId}.json
|
|
158
|
+
βββ report/ # JSONL event logs
|
|
159
|
+
β βββ backtest.jsonl
|
|
160
|
+
β βββ performance.jsonl
|
|
161
|
+
β βββ schedule.jsonl
|
|
162
|
+
β βββ heat.jsonl
|
|
163
|
+
β βββ breakeven.jsonl
|
|
164
|
+
β βββ partial.jsonl
|
|
165
|
+
βββ ta/ # Technical analysis markdown
|
|
166
|
+
βββ math_15m/{signalId}.md
|
|
167
|
+
βββ math_4h/{signalId}.md
|
|
41
168
|
```
|
|
42
169
|
|
|
43
|
-
|
|
170
|
+
### JSONL Reports (`dump/report/`)
|
|
171
|
+
|
|
172
|
+
Each JSONL file is an append-only event log β one JSON object per line with a `reportName`, `data`, and context metadata (`symbol`, `strategyName`, `exchangeName`, `frameName`, `timestamp`).
|
|
173
|
+
|
|
174
|
+
| File | Content |
|
|
175
|
+
|------|---------|
|
|
176
|
+
| `backtest.jsonl` | Strategy execution events (`idle`, `signal`, etc.) with `currentPrice` |
|
|
177
|
+
| `performance.jsonl` | Execution timing metrics (`metricType`, `duration`) |
|
|
178
|
+
| `schedule.jsonl` | Trade scheduling β `signalId`, `position`, `priceOpen`, SL/TP levels |
|
|
179
|
+
| `heat.jsonl` | Position closures β `closeReason` (`time_expired`, `stop_loss`, `take_profit`), `pnl` |
|
|
180
|
+
| `breakeven.jsonl` | Breakeven events β triggered when price reaches entry level |
|
|
181
|
+
| `partial.jsonl` | Partial profit taking β `level`, `partialExecuted`, `action` (`profit`/`loss`) |
|
|
182
|
+
|
|
183
|
+
### Markdown TA Dumps (`dump/ta/`)
|
|
184
|
+
|
|
185
|
+
Generated by `dumpPlotData()` from `@backtest-kit/pinets`. Each file is a markdown table with all Pine Script indicator values for the corresponding timeframe:
|
|
186
|
+
|
|
187
|
+
- `dump/ta/math_15m/{signalId}.md` β 15m signal indicators (EMA, ATR, Volume, Momentum)
|
|
188
|
+
- `dump/ta/math_4h/{signalId}.md` β 4H trend filter indicators (RSI, MACD, ADX, DI+/DI-)
|
|
189
|
+
|
|
190
|
+
## π¦ Installation
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
npm install
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## βοΈ Configuration
|
|
197
|
+
|
|
198
|
+
Copy `.env.example` to `.env` and fill in the required values:
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
cp .env.example .env
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
```env
|
|
205
|
+
# Ollama Configuration (for AI strategies)
|
|
206
|
+
CC_OLLAMA_API_KEY=your_ollama_key_here
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## π Usage
|
|
210
|
+
|
|
211
|
+
### π§ͺ Run Backtest
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
npm start
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
This runs the default backtest with:
|
|
218
|
+
- **Symbol**: BTCUSDT
|
|
219
|
+
- **Exchange**: Binance
|
|
220
|
+
- **Strategy**: MainStrategy
|
|
221
|
+
- **Frame**: October 2025
|
|
222
|
+
|
|
223
|
+
### π§ CLI Arguments
|
|
224
|
+
|
|
225
|
+
Override defaults via command-line flags (parsed by `backtest-kit`):
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
node ./src/index.mjs --backtest --symbol ETHUSDT --frameName nov_2025_frame
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
| Flag | Default | Description |
|
|
232
|
+
|------|---------|-------------|
|
|
233
|
+
| `--backtest` | β | Run in backtest mode |
|
|
234
|
+
| `--paper` | β | Run in paper trading mode (not yet implemented) |
|
|
235
|
+
| `--live` | β | Run in live trading mode (not yet implemented) |
|
|
236
|
+
| `--symbol` | `BTCUSDT` | Trading pair |
|
|
237
|
+
| `--strategyName` | `main_strategy` | Strategy to use |
|
|
238
|
+
| `--frameName` | `oct_2025_frame` | Backtest time frame |
|
|
239
|
+
| `--exchangeName` | `binance_exchange` | Exchange connector |
|
|
240
|
+
|
|
241
|
+
## π Dependencies
|
|
44
242
|
|
|
45
|
-
|
|
243
|
+
| Package | Purpose |
|
|
244
|
+
|---------|---------|
|
|
245
|
+
| [backtest-kit](https://libraries.io/npm/backtest-kit) | Core backtesting/trading framework |
|
|
246
|
+
| [@backtest-kit/pinets](https://github.com/QuantForgeOrg/PineTS) | Pine Script v5 runtime for Node.js |
|
|
247
|
+
| [@backtest-kit/ui](https://libraries.io/npm/backtest-kit) | Interactive charting dashboard |
|
|
248
|
+
| [@backtest-kit/ollama](https://libraries.io/npm/backtest-kit) | LLM inference integration |
|
|
249
|
+
| [ccxt](https://github.com/ccxt/ccxt) | Binance exchange connectivity |
|
|
250
|
+
| [functools-kit](https://www.npmjs.com/package/functools-kit) | `singleshot`, `randomString` utilities |
|
|
251
|
+
| [pinolog](https://www.npmjs.com/package/pinolog) | File-based structured logging |
|
|
252
|
+
| [openai](https://www.npmjs.com/package/openai) | OpenAI API client |
|
|
253
|
+
| [ollama](https://www.npmjs.com/package/ollama) | Ollama local LLM client |
|
|
46
254
|
|
|
47
255
|
## π License
|
|
48
256
|
|
package/template/env.mustache
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
# Exchange API Keys (for live trading)
|
|
2
|
-
# CC_BINANCE_API_KEY=your_api_key_here
|
|
3
|
-
# CC_BINANCE_API_SECRET=your_api_secret_here
|
|
4
|
-
|
|
5
|
-
# LLM API Keys (for AI strategies)
|
|
6
|
-
# CC_OPENAI_API_KEY=your_openai_key_here
|
|
7
|
-
# CC_ANTHROPIC_API_KEY=your_anthropic_key_here
|
|
8
|
-
# CC_DEEPSEEK_API_KEY=your_deepseek_key_here
|
|
9
|
-
# CC_GROK_API_KEY=your_grok_key_here
|
|
10
|
-
|
|
11
1
|
# Ollama Configuration
|
|
12
|
-
|
|
13
|
-
# CC_OLLAMA_BASE_URL=https://cloud.ollama.com
|
|
14
|
-
|
|
15
|
-
# Trading Configuration
|
|
16
|
-
# CC_PERCENT_SLIPPAGE=0.1
|
|
17
|
-
# CC_PERCENT_FEE=0.1
|
|
18
|
-
# CC_SCHEDULE_AWAIT_MINUTES=120
|
|
2
|
+
CC_OLLAMA_API_KEY=
|
|
@@ -5,16 +5,19 @@
|
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Backtest Kit trading bot project",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"start": "node ./src/index.mjs --backtest"
|
|
8
|
+
"start": "node ./src/index.mjs --backtest",
|
|
9
|
+
"postinstall": "npm run postinstall:bun",
|
|
10
|
+
"postinstall:bun": "npm list -g bun || npm install -g bun"
|
|
9
11
|
},
|
|
10
12
|
"dependencies": {
|
|
11
|
-
"@backtest-kit/ollama": "^0.
|
|
12
|
-
"@backtest-kit/
|
|
13
|
+
"@backtest-kit/ollama": "^3.0.6",
|
|
14
|
+
"@backtest-kit/pinets": "^3.0.11",
|
|
15
|
+
"@backtest-kit/ui": "^3.0.8",
|
|
13
16
|
"@huggingface/inference": "^4.7.1",
|
|
14
17
|
"@langchain/core": "^0.3.57",
|
|
15
18
|
"@langchain/xai": "^0.0.2",
|
|
16
|
-
"agent-swarm-kit": "^1.
|
|
17
|
-
"backtest-kit": "^
|
|
19
|
+
"agent-swarm-kit": "^1.2.4",
|
|
20
|
+
"backtest-kit": "^3.0.16",
|
|
18
21
|
"ccxt": "^4.4.41",
|
|
19
22
|
"dotenv": "^16.4.7",
|
|
20
23
|
"functools-kit": "^1.0.95",
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ActionBase, commitTrailingStop } from "backtest-kit";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Tightens trailing-stop by 3% when breakeven is reached
|
|
5
|
-
*/
|
|
6
|
-
export class BacktestTightenStopOnBreakevenAction extends ActionBase {
|
|
7
|
-
async breakevenAvailable({ symbol, currentPrice }) {
|
|
8
|
-
// Tighten trailing-stop by 3% (negative value brings stop-loss closer to entry)
|
|
9
|
-
await commitTrailingStop(symbol, -3, currentPrice);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default BacktestTightenStopOnBreakevenAction;
|
package/src/func/market.func.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
commitFifteenMinuteHistory,
|
|
3
|
-
commitHourHistory,
|
|
4
|
-
commitLongTermMath,
|
|
5
|
-
commitMicroTermMath,
|
|
6
|
-
commitOneMinuteHistory,
|
|
7
|
-
commitShortTermMath,
|
|
8
|
-
commitSwingTermMath,
|
|
9
|
-
commitThirtyMinuteHistory,
|
|
10
|
-
} from "@backtest-kit/signals";
|
|
11
|
-
import { formatPrice, getAveragePrice, getDate } from "backtest-kit";
|
|
12
|
-
import { str } from "functools-kit";
|
|
13
|
-
|
|
14
|
-
const commitHistorySetup = async (symbol, history) => {
|
|
15
|
-
// Candle histories across timeframes
|
|
16
|
-
{
|
|
17
|
-
await commitOneMinuteHistory(symbol, history);
|
|
18
|
-
await commitFifteenMinuteHistory(symbol, history);
|
|
19
|
-
await commitThirtyMinuteHistory(symbol, history);
|
|
20
|
-
await commitHourHistory(symbol, history);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Technical indicators across timeframes
|
|
24
|
-
{
|
|
25
|
-
await commitMicroTermMath(symbol, history);
|
|
26
|
-
await commitShortTermMath(symbol, history);
|
|
27
|
-
await commitSwingTermMath(symbol, history);
|
|
28
|
-
await commitLongTermMath(symbol, history);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const displayName = await String(symbol).toUpperCase();
|
|
32
|
-
|
|
33
|
-
const currentPrice = await getAveragePrice(symbol);
|
|
34
|
-
const currentData = await getDate();
|
|
35
|
-
|
|
36
|
-
await history.push({
|
|
37
|
-
role: "system",
|
|
38
|
-
content: str.newline(
|
|
39
|
-
`Trading symbol: ${displayName}`,
|
|
40
|
-
`Current price: ${await formatPrice(symbol, currentPrice)} USD`,
|
|
41
|
-
`Current time: ${currentData.toISOString()}`
|
|
42
|
-
),
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export { commitHistorySetup };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { addActionSchema } from "backtest-kit";
|
|
2
|
-
import ActionName from "../../enum/ActionName.mjs";
|
|
3
|
-
import { BacktestTightenStopOnBreakevenAction } from "../../classes/BacktestTightenStopOnBreakevenAction.mjs";
|
|
4
|
-
|
|
5
|
-
addActionSchema({
|
|
6
|
-
actionName: ActionName.BacktestTightenStopOnBreakevenAction,
|
|
7
|
-
handler: BacktestTightenStopOnBreakevenAction,
|
|
8
|
-
note: "Tighten trailing-stop by 3% when breakeven is reached",
|
|
9
|
-
});
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { addRiskSchema } from "backtest-kit";
|
|
2
|
-
import RiskName from "../../enum/RiskName.mjs";
|
|
3
|
-
|
|
4
|
-
addRiskSchema({
|
|
5
|
-
riskName: RiskName.RiskRewardRatioRisk,
|
|
6
|
-
validations: [
|
|
7
|
-
{
|
|
8
|
-
validate: ({ pendingSignal, currentPrice }) => {
|
|
9
|
-
const {
|
|
10
|
-
priceOpen = currentPrice,
|
|
11
|
-
priceTakeProfit,
|
|
12
|
-
priceStopLoss,
|
|
13
|
-
position,
|
|
14
|
-
} = pendingSignal;
|
|
15
|
-
if (!priceOpen) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
// Calculate reward (TP distance)
|
|
19
|
-
const reward =
|
|
20
|
-
position === "long"
|
|
21
|
-
? priceTakeProfit - priceOpen
|
|
22
|
-
: priceOpen - priceTakeProfit;
|
|
23
|
-
// Calculate risk (SL distance)
|
|
24
|
-
const risk =
|
|
25
|
-
position === "long"
|
|
26
|
-
? priceOpen - priceStopLoss
|
|
27
|
-
: priceStopLoss - priceOpen;
|
|
28
|
-
if (risk <= 0) {
|
|
29
|
-
throw new Error("Invalid SL: risk must be positive");
|
|
30
|
-
}
|
|
31
|
-
const rrRatio = reward / risk;
|
|
32
|
-
if (rrRatio < 2) {
|
|
33
|
-
throw new Error(`RR ratio ${rrRatio.toFixed(2)} < 2:1`);
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
note: "Risk-Reward ratio must be at least 1:2",
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
});
|
|
File without changes
|