Strategy vault / Trend following
OBV Pyramid Strategy Based on Coinrule Script
This strategy is called "OBV Pyramid". It designs opening positions based on the OBV indicator and adopts a pyramid increasing position approach to track trends for profit after they emerge.

Overview
This strategy is called "OBV Pyramid". It designs opening positions based on the OBV indicator and adopts a pyramid increasing position approach to track trends for profit after they emerge.
Principles
This strategy uses the OBV indicator to determine the trend direction. The OBV indicator judges price trends based on changes in trading volume, as shifts in volume reflect market participant attitudes. When the OBV line crosses above 0, it indicates strengthening buying power and an uptrend forming. When crossing below 0, it signals strengthening selling pressure and a downtrend.
This strategy confirms an uptrend by the OBV crossing above 0. When an uptrend forms, pyramid increasing position rules are set, allowing up to 7 additional buys. It aims to profit from the trend while setting take profit and stop loss exists.
Advantage Analysis
The biggest advantage of this strategy is catching trends using the pyramid approach to track trends and profit from them. Also, solid risk control is in place with take profit and stop loss settings.
Specifically, the main advantages are:
- Accurate trend judgement using OBV;
- Pyramid buying to track trends for profit;
- Take profit/stop loss controlling risk;
- Simple and clear logic.
Risk Analysis
The main risks come from two aspects:
- Inaccurate OBV signals leading to missed opportunities or wrong entries;
- Too many additional buys enlarging risk.
Solutions:
- Optimize OBV parameters to ensure accuracy;
- Reasonably limit additional buys for controllable risk.
Optimization Directions
Main optimization directions:
- OBV parameter optimization for higher accuracy;
- Optimization of number of additional buys and amounts;
- Take profit/stop loss optimization;
- Incorporating other indicators to avoid sole OBV reliance.
This can make the strategy more stable, controllable and extensible.
Conclusion
Overall this is a very practical strategy. It uses OBV to determine trend direction, then pyramids into the trend for profit. The logic is simple and clear for easy backtesting. It has applicability value and with further parameter, risk and money management optimization, performance can improve further, warranting additional research.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 0 | Strategy Direction: long | short | all |
| v_input_2 | 250 | Fast filter length |
| v_input_3 | 500 | Slow filter length |
| v_input_4 | 20 | LengthOBV |
| v_input_5 | 3 | ProfitTarget_Percent |
| v_input_6 | 10 | LossTarget_Percent |
Source Pine Script
/*backtest
start: 2023-11-07 00:00:00
end: 2023-12-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RafaelZioni
//@version=4
strategy(title = " OBV Pyr", overlay = true, pyramiding=5,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 20, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)
strat_dir_input = input(title="Strategy Direction", defval="long", options=["long", "short", "all"])
strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all
strategy.risk.allow_entry_in(strat_dir_value)
//
fastLength = input(250, title="Fast filter length ", minval=1)
slowLength = input(500,title="Slow filter length", minval=1)
source=close
v1=ema(source,fastLength)
v2=ema(source,slowLength)
//
filter=true
src = close
LengthOBV = input(20)
nv = change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume
c = cum(nv)
c_tb = c - sma(c,LengthOBV)
// Conditions
longCond = crossover(c_tb,0)
//shortCond =crossunder(cnv_tb,0)
//
longsignal = (v1 > v2 or filter == false ) and longCond
//shortsignal = (v1 < v2 or filter == false ) and shortCond
//set take profit
ProfitTarget_Percent = input(3)
Profit_Ticks = close * (ProfitTarget_Percent / 100) / syminfo.mintick
//set take profit
LossTarget_Percent = input(10)
Loss_Ticks = close * (LossTarget_Percent / 100) / syminfo.mintick
////Order Placing
//
strategy.entry("Entry 1", strategy.long, when=strategy.opentrades == 0 and longsignal)
//
strategy.entry("Entry 2", strategy.long, when=strategy.opentrades == 1 and longsignal)
//
strategy.entry("Entry 3", strategy.long, when=strategy.opentrades == 2 and longsignal)
//
strategy.entry("Entry 4", strategy.long, when=strategy.opentrades == 3 and longsignal)
//
strategy.entry("Entry 5", strategy.long, when=strategy.opentrades == 4 and longsignal)
//
strategy.entry("Entry 6", strategy.long, when=strategy.opentrades == 5 and longsignal)
//
strategy.entry("Entry 7", strategy.long, when=strategy.opentrades == 6 and longsignal)
//
//
//
if strategy.position_size > 0
strategy.exit(id="Exit 1", from_entry="Entry 1", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 2", from_entry="Entry 2", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 3", from_entry="Entry 3", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 4", from_entry="Entry 4", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 5", from_entry="Entry 5", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 6", from_entry="Entry 6", profit=Profit_Ticks, loss=Loss_Ticks)
strategy.exit(id="Exit 7", from_entry="Entry 7", profit=Profit_Ticks, loss=Loss_Ticks)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-08. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/OBV-Pyramid-Strategy-Based-on-Coinrule-Script.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Trend Following Strategy Based on OBV and MA Crossover SignalsPine Script · Trend following · 1d
- Five Minute Triple Confirmation Trend Trading Strategy with Risk Management SystemPine Script · Trend following · 1m
- Advanced Multi Indicator Trend Confirmation Trading StrategyPine Script · Trend following · 1d
- Trend Following Strategy Based on Time Series Decomposition and Volume Weighted Bollinger BandsPine Script · Trend following · 1h
- Kifiers Hidden MFI STOCH Divergence Trend Breaker Trading StrategyPine Script · Trend following · 1h
- Multi Indicator Integrated Trend Following Trading StrategyPine Script · Trend following · 2m