Modified Bollinger Bands Strategy Take Profit 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 |
| v_input_7 | 1.5 | Take-profit multiple |
| v_input_8 | 0.95 | Stop loss multiple |
Source Pine Script
//@version=4
strategy("Bollinger Bands %B Crossover", overlay=true,pyramiding = 5)
// 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")
// take profit and stop loss
profitTarget = input(1.5, title="Take-profit multiple")
stopLoss = input(0.95, title="Stop loss multiple")
//Define the percentage to trigger adding positions
dropPercentage=10
//Calculate current total position cost
totalCost=strategy.position_avg_price*strategy.position_size
//Calculate current position profit and loss
profitLoss=(close-totalCost)/totalCost*100
// When the buying conditions are met, a buy signal is generated, and take-profit is set
if (longCondition)
strategy.entry("Buy", strategy.long)
if useMartin
//Determine whether to add position
if profitLoss<=-dropPercentage and strategy.position_size >0
strategy.order("Buy1", "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 *profitTarget )
// When the selling conditions are met, a sell signal is generated, and take-profit is set
if (shortCondition)
strategy.entry("Sell",strategy.short)
if useMartin
//Determine whether to add position
if profitLoss<= -dropPercentage and strategy.position_size <0
strategy.order( "Sell2","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 *stopLoss )
Published by Zer3192 on the FMZ strategy square · last modified 2023-10-29. 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/Modified-Bollinger-Bands-Strategy-Take-Profit-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
- Bollinger Bands Strategy Profit Taking 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