0bps

Strategy vault / Martingale

Bollinger Bands Strategy Profit Taking and Martingale Doubling

Pine Script strategy by Zer3192.

Author
Zer3192
Complexity
Starter · 44 lines
Parameters
6

The original spec has no written description. Read the source below.

Parameters

NameDefaultWhat it controls
v_input_1_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_220Length
v_input_32Multiplier
v_input_4trueUse Martingale doubling strategy
v_input_50.001Initial position size
v_input_62Martingale 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.

in Claude Code, Cursor, Codex & 4 more
/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
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open