💰 The plan to optimize your strategies #106
Replies: 3 comments 2 replies
-
|
So here is the plan to make it all work together |
Beta Was this translation helpful? Give feedback.
-
|
GARCH is impossible in Pine Script because it requires iterative maximum likelihood optimization with unknown convergence steps, matrix operations for gradient descent, and dynamic parameter estimation from data—none of which Pine Script supports. Unlike EWMA or ATR which require manually chosen "magic constants" (smoothing factor, period length), GARCH estimates its parameters directly from the data through optimization, making it statistically rigorous but computationally demanding beyond Pine Script's capabilities. P.S. Monte Carlo optimization randomly samples parameter combinations from the search space, which is slow. GARCH takes 500ms to self-prepare in runtime |
Beta Was this translation helpful? Give feedback.
-
|
hey @tripolskypetr Thank you very much for this awesome contribution. Please note that I edited the discussion title to not look like a financial advice, since the main goal of this repo is to provide technical solutions for educational purposes. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Volatility-Filtered EMA Strategy
#77 (comment)
Responsibility:
pinetsThe Problem
EMA crossovers generate too many signals. Most of them occur during low-volatility sideways markets where price doesn't move enough to cover fees and slippage. The result: death by a thousand cuts.
The fix: before entering any trade, ask the model — "is there enough expected movement to justify this entry?"
Architecture
flowchart TD A[4h Candles] -->|EMA trend direction| B{LONG or SHORT?} B -->|direction decided| C[15m Candles] C -->|Pine Script| D[Entry Signal] D --> E{predict filter} E -->|sigma too low or unreliable| F[Skip] E -->|sigma covers costs| G[Enter Position] G --> H[Partial Profit via Kelly] H --> I[Trailing Stop after Breakeven] I --> J{4h timeout reached?} J -->|no| I J -->|yes| K[Close Remaining Position]Step 1 — Trend Direction (4h)
The higher timeframe decides one thing: do we only look for longs, or only for shorts.
No entries happen on this timeframe. It only sets the filter for step 2.
Why 4h: short enough to catch regime changes within days, long enough to avoid whipsaws. Daily works too but reacts slower.
Step 2 — Entry Signal (15m)
Pine Script runs on the 15m chart and generates candidate entry points in the direction decided by step 1.
This already eliminates ~50% of false signals (counter-trend entries).
Why 15m: gives precise entries within the 4h trend. Lower timeframes (1m, 5m) have too much microstructure noise. Higher (1h) lose precision.
Step 3 — Volatility Filter (GARCH)
This is where the
predictfunction comes in. Before executing any entry signal from step 2, we check if the expected price movement justifies the trade.flowchart LR A[Entry Signal] --> B["predict(candles, '15m')"] B --> C{reliable?} C -->|false| D[Skip — model inadequate] C -->|true| E{sigma * price > fees + slippage + min_profit?} E -->|no| F[Skip — not enough movement] E -->|yes| G[Execute Entry]What predict does
sigma— expected price movement for the next candle (as a fraction)reliable— whether the model converged and passed adequacy testsThe filter logic
minimum_profitis your decision, not the model's. It represents the smallest gain worth your time and risk. Without it, you enter trades with zero expected value after costs.What this catches
reliable = false— data doesn't fit GARCHStep 4 — Position Management
Once inside a trade, three rules govern the exit:
Partial profit (Kelly)
Take partial profit at a Kelly-optimal fraction of the position. Kelly criterion:
Use half-Kelly (
f*/2) until you have 100+ trades for stable win rate estimation. Full Kelly is mathematically optimal but practically fragile — overestimates edge when sample size is small.Recalculate Kelly parameters every N trades as your sample grows.
Trailing stop after breakeven
After partial profit is taken:
This converts a winning trade into a risk-free position. The worst outcome after partial profit is breaking even on the remainder.
Timeout: 16 candles
If TP is not reached within 16 candles of 15m (= 4 hours), close the remaining position at market.
Why 16 candles, not 4 hours: aligns with your entry timeframe. If you entered on candle N, you exit on candle N+16 regardless of wall clock time. Avoids edge cases where entering at the end of a 4h window gives you only minutes.
Why 4 hours at all: matches the higher timeframe. If the 4h trend is valid, a 15m entry should reach TP within one 4h candle. If it doesn't — the signal was likely noise.
flowchart TD A[Position Opened] --> B[Price hits partial TP?] B -->|yes| C[Take Partial Profit] C --> D[Move SL to Breakeven] D --> E[Trail Stop] E --> F{TP reached?} F -->|yes| G[Close Full Position] F -->|no| H{16 candles passed?} H -->|no| E H -->|yes| I[Close Remaining at Market] B -->|no| HWhat This Strategy Does NOT Do
Summary
predict()The core insight: EMA tells you where, GARCH tells you whether it's worth going there.
Beta Was this translation helpful? Give feedback.
All reactions