內建策略¶
給 oneshot 回測和 engine 的 bot 使用的 class 型策略。參數用 params={"<前綴>__<名稱>": 值} 傳入。
Warning
窗口類參數以 K 棒數計算,所以同一個值在 1m 和 1d 上是不同的策略。各 interval 的預設值見策略參數。
cjtrade.pkgs.strategy.bb
¶
pkgs/strategy/bb_1m.py ~~~~~~~~~~~~~~~~~~~~~~~ Bollinger Band mean-reversion strategy using TA-Lib (matches arenax.py).
Copy this file, rename the class, and implement your own logic in on_bar().
Run it: from cjtrade.apps.oneshot_backtest import run_oneshot from cjtrade.pkgs.strategy.bb_1m import BollingerStrategy
run_oneshot(symbol="2330", start="2023-01-01", strategy=BollingerStrategy())
TALibWrapper
¶
Pine Script style API
sma(real, timeperiod=20)
¶
Simple Moving Average
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
8 9 10 | |
ema(real, timeperiod=20)
¶
Exponential Moving Average
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
12 13 14 | |
wma(real, timeperiod=20)
¶
Weighted Moving Average
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
16 17 18 | |
bb(real, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
¶
Bollinger Bands
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
21 22 23 24 | |
rsi(real, timeperiod=14)
¶
Relative Strength Index
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
27 28 29 | |
macd(real, fastperiod=12, slowperiod=26, signalperiod=9)
¶
MACD - Moving Average Convergence Divergence
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
31 32 33 34 35 36 | |
stoch(high, low, close, fastk_period=5, slowk_period=3, slowd_period=3, slowk_matype=0, slowd_matype=0)
¶
Stochastic
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
38 39 40 41 42 43 44 45 46 47 | |
atr(high, low, close, timeperiod=14)
¶
Average True Range
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
50 51 52 | |
adx(high, low, close, timeperiod=14)
¶
Average Directional Index
Source code in src/cjtrade/pkgs/analytics/technical/talib_wrapper.py
54 55 56 | |
BollingerStrategy
¶
Bases: BaseStrategy
Bollinger Band mean-reversion strategy using TA-Lib.
Matches the behavior of arenax.py's calculate_bollinger_bands_mock(). Buys when price touches lower band, sells when it touches upper band. Keeps at most one position (no pyramiding).
Warnings
bb__window_size is counted in bars, so the same value means a different
time span on each bar interval.
on_start(ctx)
¶
Read parameters from ctx.params and reset monitor state.
Reads bb__window_size (default 20), bb__min_width_pct (0.01),
bb__std_dev (2.0) and risk__max_position_pct (0.05); the older
single-underscore names are accepted as fallbacks.
Source code in src/cjtrade/pkgs/strategy/bb.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
plots()
¶
The three bands, to be drawn against the price.
The same numbers monitors() reports, named for the chart rather than for the table. Separate because a runner samples these once per bar and keeps the series, while monitors are read as they are.
Empty until the bands exist, so the chart has no leading run of zeros that squashes the price into the top of the frame.
Source code in src/cjtrade/pkgs/strategy/bb.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
monitors()
¶
See BaseStrategy.monitors. Plain floats, ints and strings only.
Source code in src/cjtrade/pkgs/strategy/bb.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
on_bar(bar, ctx)
¶
Buy on a lower-band touch, sell on an upper-band touch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bar
|
Kbar
|
The bar that just closed. |
required |
ctx
|
StrategyContext
|
Account and price history as of this bar. |
required |
Returns:
| Type | Description |
|---|---|
list
|
At most one signal; empty while warming up, in a squeeze, or when no touch is actionable. |
Source code in src/cjtrade/pkgs/strategy/bb.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
cjtrade.pkgs.strategy.adx
¶
pkgs/strategy/adx.py ~~~~~~~~~~~~~~~~~~~~~ Regime-switching strategy: Donchian breakout in trends + SNR in consolidation.
What measures the trend
An efficiency ratio on a 0-100 scale (net displacement / total path travelled
over adx__period closes), NOT the Wilder ADX, which needs a history of each
bar's high and low that this strategy does not keep. See _calculate_adx, which
also records what it replaced and why the 40/20 thresholds still mean
something.
Behavior
- Measures trend strength to pick a regime
- Splits into 3 regimes:
- Strong Trend (strength > adx__strong_threshold, e.g. 40): Donchian breakout
- Consolidation (strength < adx__weak_threshold, e.g. 20): Support & Resistance
-
Neutral (mid-range): Conservative mixed mode
-
Donchian Breakout: BUY on breakout above N-day high, SELL below N-day low
- SNR Mode: BUY near support, SELL near resistance (like SupportResistanceStrategy)
Parameters (via ctx.params or constructor defaults) - adx__period: ADX calculation period (default: 14) - adx__strong_threshold: ADX value to enter strong trend mode (default: 40) - adx__weak_threshold: ADX value to enter consolidation mode (default: 20) - adx__donchian_period: Donchian lookback in trend mode (default: 20) - adx__snr_lookback_days: Support/Resistance lookback in consolidation mode (default: 20) - adx__snr_threshold_pct: How close to S/R before trading (default: 0.01) - risk__max_position_pct: Max position size (default: 0.05)
Usage
from cjtrade.pkgs.strategy.custom import ADXAdaptiveStrategy strategy = ADXAdaptiveStrategy( adx_period=14, adx_strong_threshold=40, adx_weak_threshold=20, ) run_oneshot(symbol="2330", start="2023-01-01", strategy=strategy)
ADXAdaptiveStrategy(adx_period=14, adx_strong_threshold=40.0, adx_weak_threshold=20.0, adx_donchian_period=20, adx_snr_lookback_days=20, adx_snr_threshold_pct=0.01, max_position_pct=0.05)
¶
Bases: BaseStrategy
Adaptive strategy using ADX to switch between Donchian breakout and SNR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adx_period
|
int
|
Trend-strength window, in bars. |
14
|
adx_strong_threshold
|
float
|
Strength above which the TREND regime is used. |
40.0
|
adx_weak_threshold
|
float
|
Strength below which the CONSOLIDATION regime is used. |
20.0
|
adx_donchian_period
|
int
|
Donchian channel lookback, in bars. |
20
|
adx_snr_lookback_days
|
int
|
Support/resistance lookback, in days (converted to bars via the interval hint). |
20
|
adx_snr_threshold_pct
|
float
|
How close to support/resistance before trading. |
0.01
|
max_position_pct
|
float
|
Max position size as a fraction of equity. |
0.05
|
Notes
All of these can be overridden by ctx.params in on_start.
Warnings
adx__period and adx__donchian_period are counted in bars and change
meaning with the bar interval. adx__snr_lookback_days is converted with
a fixed bars-per-day table keyed on adx__interval (default 1m).
Source code in src/cjtrade/pkgs/strategy/adx.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
on_start(ctx)
¶
Initialize parameters from ctx.params.
Also sets bars-per-day from adx__interval (falling back to
sr__interval / sr_interval, default "1m").
Source code in src/cjtrade/pkgs/strategy/adx.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
plots()
¶
Whatever levels the active regime is trading against.
Which keys appear changes with the regime, because the levels do: a Donchian channel in a trend, support and resistance in a range. The chart draws what it is given rather than expecting fixed names.
Source code in src/cjtrade/pkgs/strategy/adx.py
188 189 190 191 192 193 194 195 | |
monitors()
¶
See BaseStrategy.monitors. Plain floats, ints and strings only.
trend_strength is the efficiency ratio this strategy computes, not
the Wilder ADX -- see _calculate_adx. Reported anyway because the
regime switch is made against this number, so it is the one that
explains the behaviour.
Source code in src/cjtrade/pkgs/strategy/adx.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
on_bar(bar, ctx)
¶
Process incoming bar and generate signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bar
|
Kbar
|
The bar that just closed. |
required |
ctx
|
StrategyContext
|
Account and price history as of this bar. |
required |
Returns:
| Type | Description |
|---|---|
list of Signal
|
At most one signal; empty while warming up or when no level is hit. |
Source code in src/cjtrade/pkgs/strategy/adx.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
on_fill(fill, ctx)
¶
Called after a fill is executed.
Source code in src/cjtrade/pkgs/strategy/adx.py
474 475 476 | |
on_end(ctx)
¶
Called at end of backtest.
Source code in src/cjtrade/pkgs/strategy/adx.py
478 479 480 | |
cjtrade.pkgs.strategy.dca
¶
pkgs/strategy/dca.py ~~~~~~~~~~~~~~~~~~~~~ Monthly dollar-cost averaging.
Behavior
- Buys a fixed NT$ amount of each symbol it sees, once per calendar month, on the first bar of that month this strategy is given.
- Never sells. This is the accumulate-and-hold baseline, so there is no exit.
Why calendar months and not "30 days since the last buy"
This used to wait timedelta(days=30), which drifts: buy on 5 January and the
next purchase is 4 February, then 6 March, and by December the instalment has
slid a week and a half earlier. Over a multi-year backtest that silently
changes how many instalments fit in the run -- 12.2 a year rather than 12 --
and the comparison against a real monthly savings plan stops holding.
"The first bar of the month" also needs no trading calendar. The month rolls over on whatever bar arrives first, so a 1st that is a Sunday, a holiday or a typhoon day resolves itself: the bot buys on the first bar it is actually given in the new month. The alternative -- "buy on the 1st, or the next trading day if the 1st is closed" -- would need holiday data this codebase does not have (TradingCalendar.is_trading_day is weekday-only; see trading_calendar.py).
Usage
from cjtrade.pkgs.strategy.dca import DCA_Monthly
strategy = DCA_Monthly(amount=10_000)
DCA_Monthly(amount=DCA_MONTHLY_AMOUNT, max_position_pct=0.0, buy_frequency_months=1)
¶
Bases: BaseStrategy
Buy a fixed amount of each symbol, once per calendar month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
NT$ to spend per instalment, per symbol ( |
DCA_MONTHLY_AMOUNT
|
max_position_pct
|
float
|
Size the instalment as this fraction of current equity instead of a
fixed sum ( |
0.0
|
buy_frequency_months
|
int
|
Months between instalments ( |
1
|
Notes
The same values can be given via ctx.params in on_start, plus
dca__last_buy_time: optional {symbol: ISO timestamp} of the last
purchase made before this run, so a resumed run does not buy twice in one
month.
Source code in src/cjtrade/pkgs/strategy/dca.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
on_start(ctx)
¶
Read parameters from ctx.params and seed last-buy months.
Warnings
dca__buy_frequency_days is not used; only a warning is logged.
Source code in src/cjtrade/pkgs/strategy/dca.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
plots()
¶
The running average cost, which is the only line this strategy has.
A DCA chart's whole question is where the average sits against the price, so that is what gets drawn. Nothing until the first purchase, because an average of no purchases is not zero -- it does not exist.
Source code in src/cjtrade/pkgs/strategy/dca.py
158 159 160 161 162 163 164 165 166 167 | |
monitors()
¶
See BaseStrategy.monitors. Plain floats, ints and strings only.
Source code in src/cjtrade/pkgs/strategy/dca.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
on_bar(bar, ctx)
¶
Buy one instalment on the first bar of a due month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bar
|
Kbar
|
The bar that just closed. |
required |
ctx
|
StrategyContext
|
Account and price history as of this bar; |
required |
Returns:
| Type | Description |
|---|---|
list of Signal
|
One BUY, or empty if already bought this period or unaffordable. |
Source code in src/cjtrade/pkgs/strategy/dca.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
on_fill(fill, ctx)
¶
Log BUY fills at debug level.
Source code in src/cjtrade/pkgs/strategy/dca.py
280 281 282 283 284 | |
last_buy(symbol)
¶
When this strategy last bought symbol, or None.
Exposed so a caller can hand it back as dca__last_buy_time and resume without buying twice in the same month.
Source code in src/cjtrade/pkgs/strategy/dca.py
286 287 288 289 290 291 292 | |
cjtrade.pkgs.strategy.donchian
¶
pkgs/strategy/donchian.py ~~~~~~~~~~~~~~~~~~~~~~~~~ Pure Donchian Breakout Strategy.
Behavior
- Calculate highest high and lowest low over the last N days
- BUY when price breaks above the highest high (uptrend confirmation)
- SELL when price breaks below the lowest low (downtrend confirmation)
- Trend-following approach (works best in trending markets)
Parameters (via ctx.params or constructor defaults) - donchian__period: Lookback in BARS, excluding the current one (default: 20) - donchian__breakout_mode: 'close' (price closes above/below) or 'touch' (touches) (default: 'close') - donchian__trailing_stop_pct: Trailing stop as % below the peak since entry (default: 0.0 = disabled) - risk__max_position_pct: Max position size (default: 0.05)
Usage
from cjtrade.pkgs.strategy.donchian import DonchianBreakoutStrategy strategy = DonchianBreakoutStrategy(period=20, breakout_mode='close')
run_oneshot(symbol="2330", start="2023-01-01", strategy=strategy)
DonchianBreakoutStrategy(period=20, breakout_mode='close', trailing_stop_pct=0.0, max_position_pct=0.05)
¶
Bases: BaseStrategy
Pure Donchian Channel breakout trending strategy.
Tracks the highest high and lowest low over a lookback period. - BUY when price breaks above the highest high (new N-day high) - SELL when price breaks below the lowest low (new N-day low)
This is a classic trend-following approach that works best in strongly trending markets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
int
|
Bars to look back for the channel, current bar excluded (default: 20). |
20
|
breakout_mode
|
str
|
'close' = close above/below, 'touch' = touches (default: 'close'). |
'close'
|
trailing_stop_pct
|
float
|
Stop as % below the peak since entry (0.0 = disabled). |
0.0
|
max_position_pct
|
float
|
Max position size as fraction of equity (default: 0.05). |
0.05
|
Notes
All can also be set via ctx.params (donchian__*,
risk__max_position_pct).
Warnings
period is counted in bars, so its time span changes with the interval.
'touch' mode reads bar.high/bar.low. Both runners now deliver real bars, so it means the same in the trading engine as in BacktestEngine. Runs of the trading engine from before that change fed snapshots, whose high and low were the whole trading DAY so far, and are not comparable in 'touch' mode. 'close' mode reads only closes and is unaffected.
Source code in src/cjtrade/pkgs/strategy/donchian.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
on_start(ctx)
¶
Initialize parameters from ctx.params.
Source code in src/cjtrade/pkgs/strategy/donchian.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
on_bar(bar, ctx)
¶
Process incoming bar and generate signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bar
|
Kbar
|
The bar that just closed. |
required |
ctx
|
StrategyContext
|
Account and price history as of this bar. |
required |
Returns:
| Type | Description |
|---|---|
list of Signal
|
Breakout or trailing-stop signals; empty while warming up. |
Source code in src/cjtrade/pkgs/strategy/donchian.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
on_fill(fill, ctx)
¶
Called after a fill is executed.
Source code in src/cjtrade/pkgs/strategy/donchian.py
265 266 267 | |
on_end(ctx)
¶
Called at end of backtest.
Source code in src/cjtrade/pkgs/strategy/donchian.py
269 270 271 | |
cjtrade.pkgs.strategy.snr
¶
pkgs/strategy/support_resistance.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support & Resistance Level strategy.
Behavior
- Calculate the highest high (resistance) and lowest low (support) over the last N days.
- BUY near support levels (with breakout confirmation).
- SELL near resistance levels.
- Optional: BUY on breakout above resistance (trend following).
Usage
from cjtrade.pkgs.strategy.support_resistance import SupportResistanceStrategy strategy = SupportResistanceStrategy(lookback_days=20, breakout_mode=True)
run_oneshot(symbol="2330", start="2023-01-01", strategy=strategy)
SupportResistanceStrategy(lookback_days=20, support_threshold_pct=0.01, resistance_threshold_pct=0.01, breakout_mode=False, max_position_pct=0.05)
¶
Bases: BaseStrategy
Support & Resistance levels strategy.
Tracks highest high and lowest low over a lookback period. - BUY when price approaches support (from above) - SELL when price approaches resistance (from below) - Optional: BUY on resistance breakout (if breakout_mode=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lookback_days
|
int
|
Number of days to look back for S/R levels (default: 20). |
20
|
support_threshold_pct
|
float
|
How close to support before buying (e.g. 0.01 = 1%). |
0.01
|
resistance_threshold_pct
|
float
|
How close to resistance before selling. |
0.01
|
breakout_mode
|
bool
|
If True, also buy when price breaks above resistance. |
False
|
max_position_pct
|
float
|
Max position size as fraction of equity. |
0.05
|
Notes
All can also be set via ctx.params (sr__*, risk__max_position_pct).
Warnings
lookback_days is converted to bars with a fixed bars-per-day table keyed
on sr__interval (default 1m); a wrong hint gives a wrong window.
Source code in src/cjtrade/pkgs/strategy/snr.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
on_start(ctx)
¶
Read parameters from ctx.params and set bars-per-day from sr__interval.
Source code in src/cjtrade/pkgs/strategy/snr.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
on_bar(bar, ctx)
¶
Emit signals based on support/resistance levels.
Returns:
| Type | Description |
|---|---|
list of Signal
|
Empty until |
Source code in src/cjtrade/pkgs/strategy/snr.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
on_fill(fill, ctx)
¶
Log fills for debugging.
Source code in src/cjtrade/pkgs/strategy/snr.py
217 218 219 220 221 222 223 224 225 226 227 228 | |
on_end(ctx)
¶
Called at end of backtest.
Source code in src/cjtrade/pkgs/strategy/snr.py
230 231 232 233 234 235 236 | |
cjtrade.pkgs.strategy.baseline_0050
¶
pkgs/strategy/baseline_0050.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Baseline strategy that monthly buys 0050 (Taiwan Top 50 ETF) via DCA.
This strategy serves as a benchmark to compare against other trading strategies. It performs Dollar-Cost Averaging (DCA) on 0050 only, investing a fixed amount (NT$10,000) every 30 days, regardless of what symbols are in the backtest kbars. This allows fair comparison when testing multiple strategies on different symbols.
Behavior
- BUY: Fixed NT$10,000 every 30 days on 0050 only
- HOLD: No selling (pure accumulation)
- IGNORE: All other symbols
Usage
from cjtrade.pkgs.strategy.baseline_0050 import BaselineStrategy
run_compare_strategies( symbol=["2330", "2454", "3008"], # Test symbols strategies={ "DCA_Monthly": DCA_Monthly(), "BollingerBands": BollingerStrategy(), "Baseline_0050": BaselineStrategy(), # DCA on 0050 only } )
Note: The baseline symbol (0050) will be automatically added to the kbars if it's not already in the symbol list, so you don't need to include it manually.
BaselineStrategy(baseline_symbol=BASELINE_SYMBOL, monthly_amount=BASELINE_MONTHLY_AMOUNT)
¶
Bases: BaseStrategy
DCA baseline strategy that monthly buys 0050 (Taiwan Top 50 ETF).
This strategy is used as a benchmark to compare active trading strategies against a simple buy-and-hold DCA approach on 0050. It ignores all other symbols in the backtest and only trades 0050.
Behavior: - Every 30 days: buy NT$10,000 worth of 0050 - All other symbols: ignored - No selling: pure accumulation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
baseline_symbol
|
str
|
Override the hardcoded symbol (default: "0050"). Also read from ctx.params["baseline_symbol"]. |
BASELINE_SYMBOL
|
monthly_amount
|
float
|
Amount to invest per month in NT$ (default: 10000). Also read from ctx.params["baseline_monthly_amount"]. |
BASELINE_MONTHLY_AMOUNT
|
Warnings
The code does not match the text above: the default amount is BASELINE_MONTHLY_AMOUNT (4,888,888) and on_bar waits 2001 days, not 30, between buys.
Source code in src/cjtrade/pkgs/strategy/baseline_0050.py
83 84 85 86 87 88 89 90 91 | |
on_start(ctx)
¶
Read baseline_symbol and baseline_monthly_amount from ctx.params.
Source code in src/cjtrade/pkgs/strategy/baseline_0050.py
93 94 95 96 97 98 99 100 101 102 | |
on_bar(bar, ctx)
¶
Monthly DCA: buy NT$10,000 worth of 0050 every 30 days.
Returns:
| Type | Description |
|---|---|
list of Signal
|
One BUY when due, otherwise empty. Other symbols are ignored. |
Source code in src/cjtrade/pkgs/strategy/baseline_0050.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
on_fill(fill, ctx)
¶
Log fills for debugging.
Source code in src/cjtrade/pkgs/strategy/baseline_0050.py
151 152 153 154 | |
on_end(ctx)
¶
Called at end of backtest.
Source code in src/cjtrade/pkgs/strategy/baseline_0050.py
156 157 158 | |