Bollinger Bands Strategy Profit Taking and Martingale Doubling
Pine Script strategy by Zer3192.
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1_close | 0 | Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
| v_input_2 | 20 | Length |
| v_input_3 | 2 | Multiplier |
| v_input_4 | true | Use Martingale doubling strategy |
| v_input_5 | 0.001 | Initial position size |
| v_input_6 | 2 | Martingale multiplier |
Source Pine Script
//@version=4
strategy("Bollinger Bands %B Crossover", overlay=true,pyramiding = 6)
// Load Bollinger Bands %B indicator
source = input(close, title="Source")
length = input(20, minval=1, title="Length")
mult = input(2.0, minval=0.001, maxval=50, title="Multiplier")
basis = sma(source, length)
dev = mult * stdev(source, length)
upper = basis + dev
lower = basis - dev
bb = (source - lower) / (upper - lower)
// Define long and short conditions
longCondition = crossover(bb, 0)
shortCondition = crossunder(bb, 1)
// Martingale Strategy Parameters
useMartin = input(true, title="Use Martingale doubling strategy")
initialQty = input(0.001, title="Initial position size")
martinFactor = input(2, title="Martingale multiplier")
// When the buying conditions are met, a buy signal is generated and a take-profit and stop-loss are set.
if (longCondition)
strategy.entry("Buy", strategy.long)
if useMartin
if close<strategy.position_avg_price*0.95 and strategy.position_size >0
strategy.order("exitBuy", "Buy",qty=strategy.position_size * martinFactor,when=strategy.long)
if strategy.position_size >0
strategy.exit("Take Profit/Stop Loss", "Buy", profit=strategy.position_avg_price * 1.1)
// When the selling conditions are met, a sell signal is generated and a take-profit and stop-loss are set.
if (shortCondition)
strategy.entry("Sell",strategy.short)
if useMartin
if close<strategy.position_avg_price*1.05 and strategy.position_size < 0
strategy.order( "exitSell","Sell", qty=strategy.position_size * martinFactor,when=strategy.short)
if strategy.position_size >0
strategy.exit("Take Profit/Stop Loss", "Sell", profit=strategy.position_avg_price * 1.1)
Published by Zer3192 on the FMZ strategy square · last modified 2023-10-19. 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/Bollinger-Bands-Strategy-Profit-Taking-and-Martingale-Doubling.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
- Modified Bollinger Bands Strategy Take Profit and Martingale DoublingPine Script · Martingale
- The Gunbot Bands StrategyPine Script · Martingale · 3m
- Bollinger Bands and RSI indicators strategyPine Script · Martingale · 2h
- Diversified Indicators Aggregation StrategyPine Script · Martingale · 1d
- MACD KDJ Combined Martingale Pyramiding Quantitative Trading StrategyPine Script · Martingale · 1h
- blackcat L1 MartinGale Scalping StrategyPine Script · Martingale