This strategy uses price momentum indicators to determine the trading direction. Specifically, it calculates the moving average and mean price respectively. When the price crosses above the moving average and mean price, a buy signal is generated. To filter out false signals, it requires no similar previous signals. Then it saves the signal status and generates the final opening position signal in combination with the moving average. The strategy also contains stop loss and take profit settings.
The strategy mainly relies on price momentum indicators to judge the trend direction. First it calculates the moving average and mean price of the price:
swmaClose = swma(close)
vwapClose = vwap(close)
Where swma
is the smoothed moving average and vwap
is volume weighted average price. Both can reflect the average price level.
Then compare the price with the average to see if the price crosses above the moving average and mean price, to judge if it is a bullish signal:
swmaLong = close > swmaClose
vwapLong = close > vwapClose
To filter out false signals, it requires no previous signals from these two indicators:
triggerLong = vwapLong and not vwapLong[1] and not swmaLong and not swmaLong[1]
Next, save the bullish signal:
saveLong = false, saveLong := triggerLong ? true : not vwapLong ? false : saveLong[1]
Finally, when the saved crossing signal and the price crosses above the moving average again, generate the opening position signal:
startLong = saveLong and swmaLong
This can filter out some false signals and make the signals more reliable.
The strategy also contains stop loss and take profit settings. The stop loss distance is configurable, and the take profit is set to a certain multiple of the stop loss.
The strategy has the following advantages:
The strategy also has some risks:
Countermeasures:
The strategy can also be optimized in the following directions:
These optimizations can improve strategy flexibility, robustness and profitability.
Overall, this price momentum tracking strategy is a simple, straightforward and logical trend tracking strategy. The strategy uses price moving averages and mean prices to determine price momentum direction, and designs a multi-step verification mechanism to improve signal quality. The strategy also contains reasonable stop loss and take profit settings. In terms of code, the strategy logic is very concise, requiring only 20+ lines of Pine script to implement. In summary, this strategy is a very good learning example, beginners can use it as a very good starting point to understand quantitative trading strategies. Of course, the strategy itself also has practical trading value. Through parameter optimization and function expansion, it can become a practical trading system to avoid noise and track trends.
/*backtest start: 2023-12-03 00:00:00 end: 2024-01-02 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=4 strategy(title = "Simple Price Momentum", shorttitle = "SPM", overlay = true, initial_capital = 20000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity, commission_value = 0.025) // How To Create A Simple Trading Strategy With TradingView // https://docs.google.com/document/d/1fXxCtPuGgTXb-RuBJNbwlfgkeiLTK5060LfTrzRlr5k/view swmaClose = swma(close) vwapClose = vwap(close) swmaLong = close > swmaClose vwapLong = close > vwapClose triggerLong = vwapLong and not vwapLong[1] and not swmaLong and not swmaLong[1] saveLong = false, saveLong := triggerLong ? true : not vwapLong ? false : saveLong[1] startLong = saveLong and swmaLong startLong := input(false, "Consecutive Orders") ? startLong : startLong and not startLong[1] stopLoss = input(250, "Stop Loss", step = 50) takeProfit = input(10, "Reward/Risk") * stopLoss strategy.entry("Open Long", strategy.long, when = startLong) strategy.exit("Exit Long", "Open Long", profit = stopLoss, loss = takeProfit) // bgcolor(swmaLong ? color.blue : na) // bgcolor(vwapLong ? color.orange : na) // bgcolor(triggerLong ? color.purple : na) // bgcolor(saveLong ? color.yellow : na) bgcolor(startLong[1] ? color.green : na)