The Gandalf quantitative trading strategy is a mean reversion strategy based on median price lines. It determines the current trend direction by calculating weighted average price, median price line and body middle price, to find optimal entry points. When a trend reversal is detected, it will quickly cut losses and exit. The strategy combines the ideas of trend following and trend reversal strategies.
The core logic of Gandalf strategy is to compare the magnitude relationship between weighted average price, median price line and body middle price, to judge the current trend direction and strength.
Specifically, it calculates the following prices:
When entering a position, it compares the magnitude relationship between the weighted average price, median price line and body middle price of the last two bars, to determine whether it fits the characteristics of a starting trend.
For example, if the weighted average price is below the median price line, and the body middle price is also below the weighted average price, it indicates the price is falling, which presents a shorting opportunity.
When stopping loss, it continues to compare the magnitude relationship between these prices, to judge whether there are signs of trend reversal. If the weighted average price is above the body middle price, and the median price line is below the weighted average price, it indicates a trend reversal, and should cut loss immediately.
By comparing the price magnitude relationship, Gandalf strategy realizes the judgment and tracking of trends. It can find optimal entry timing, and also quickly detect trend reversals to stop loss.
The Gandalf strategy has the following advantages:
Using median price line to determine trend direction can effectively filter market noise and lock in the major trend.
The entry condition combining multiple price comparisons can more reliably determine the start of a trend.
The stop loss condition also uses price comparison to judge trend reversal, which allows fast stop loss and risk control.
Adopting conditional orders for entry can get in at ideal prices.
Preset maximum profit take times and holding period upper limit can lock in profits and control single trade risks.
The code structure is clear and simple, easy to understand and modify.
Parameters can be adjusted based on personal risk preference, easy to optimize.
Applicable to trending products, able to capture trending profits.
In summary, the Gandalf strategy utilizes median line to determine trend, sets profit taking and stop loss conditions, and can effectively control risks while tracking trends, making it a reliable trend following strategy.
The Gandalf strategy also has some risks to note:
As a trend following strategy, it will produce more small losses when trend is unclear or frequently reversing.
Unable to effectively determine trend reversal points, may lead to expanding losses.
Prone to being trapped in range-bound markets.
Relies on parameter settings, parameters need adjusting for different products.
Unidirectional holding, unable to profit from reverse trends.
High failure rate of conditional orders, may wait long for entry.
Risk management measures:
Adopt small position sizing, partial entry, to control single loss.
Set stop loss line, fast stop loss. Or adopt moving stop loss or trailing stop loss.
Optimize parameters to suit current product. Use other indicators to assist trend judgment.
Consider martingale to lower cost basis.
Trade products with obvious trends, higher profit confidence.
Relax entry criteria appropriately to improve entry probability.
The Gandalf strategy can also be improved in the following aspects:
Build trend judgment indicators to assist in determining timing of trend reversals, such as adding MACD, Bollinger Bands etc.
Add discrete optimization functions to auto optimize parameters and adapt to more products.
Increase machine learning algorithms, train neural networks or SVM models on historical data to judge trends.
Add more profit taking methods, like moving profit take, parabolic profit take.
Combine related products for spread trading or stat arb strategies.
Add state prediction based on Hidden Markov Model to judge market regime.
Construct combined strategies, like combining with moving average strategies for multi-strategy management.
Explore optimization of trading strategy combinations to find optimal portfolio weights.
In summary, the Gandalf strategy can be expanded and optimized in multiple dimensions like trend judgment, automatic optimization, risk management, to make the strategy more robust and reliable.
The Gandalf quantitative strategy is a simple yet effective strategy based on price comparison to determine trends. It combines the ideas of trend following and quick stop loss, and can effectively control risks. The strategy logic is clear and easy to understand, parameters can be adjusted based on personal risk preferences. But it also has some profit fluctuation and holding risks, requiring proper optimization and management. Overall, the Gandalf strategy is a reliable, easy to grasp and optimize trend following strategy, suitable for pursuing steady trend profits.
/*backtest start: 2023-10-22 00:00:00 end: 2023-10-29 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=3 // The GandalfProjectResearchSystem strategy, as discussed in // “System Development Using Artificial Intelligence” // by Domenico D’Errico and Giovanni Trombetta strategy("Gandalf Project Research System", overlay=true) // Inputs Quantity = input(0, title="Quantity (0 to auto calc)") Single_Trade_Money = input(10000, minval=1, title="Money to spend on single trade") MaxProfitCloses = input(6, minval=1, title="Max Profit Close") MaxBars = input(8, minval=1, title="Max Total Bars") Enter_Gap = input(-0.08, title="Distance from low price to place entry limit") AltExit = input(true, title="Use Alt Exit") // Calculate Order Quantity Ncon = Single_Trade_Money / close // Misc Variables src = close BarsSinceEntry = 0 MaxProfitCount = 0 MedBodyPrice = (open + close) / 2.0 Weighted = (high + low + close + close) / 4.0 Median = (high + low) / 2.0 // Enter Conditions Cond00 = strategy.position_size == 0 Cond01 = ((Weighted[1] < Median[1] and Median[2] <= Weighted[1] and MedBodyPrice[2] <= Weighted[3]) or (Weighted[1] < Median[3] and MedBodyPrice[0] < Median[2] and MedBodyPrice[1] < MedBodyPrice[2])) Entry01 = Cond00 and Cond01 // Update Exit Variables BarsSinceEntry := Cond00 ? 0 : nz(BarsSinceEntry[1]) + 1 MaxProfitCount := Cond00 ? 0 : (close > strategy.position_avg_price and BarsSinceEntry > 1) ? nz(MaxProfitCount[1]) + 1 : nz(MaxProfitCount[1]) // Exit Conditions eCond01 = BarsSinceEntry - 1 >= MaxBars eCond02 = MaxProfitCount >= MaxProfitCloses eCond03 = ((Weighted[1] < MedBodyPrice[1] and Median[2] == MedBodyPrice[3] and MedBodyPrice[1] <= MedBodyPrice[4]) or (Weighted[2] < MedBodyPrice[0] and Median[4] <= Weighted[3] and MedBodyPrice[1] <= Weighted[1]) or (Weighted[2] < MedBodyPrice[0] and Median[4] <= Weighted[3] and MedBodyPrice[1] <= Weighted[1])) eCond04 = AltExit ? true : close - strategy.position_avg_price < 0 Exit01 = not Cond00 and (eCond01 or eCond02 or (eCond03 and eCond04)) // Entries strategy.entry(id="L1", long=true, limit=low + Enter_Gap, qty=(Quantity > 0 ? Quantity : Ncon), when=Entry01) // Exits strategy.close("L1", Exit01)