Fibonacci Bollinger Bands DCA Martingale Strategy

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

//@version=5

strategy("Fibonacci Bollinger Bands DCA Martingale Strategy with Scaling,


Reinvesting Profits, and EMA Conditions", overlay=true)

// Input parameters
length = input.int(200, minval=1, title="Length")
src = input.source(hlc3, title="Source")
mult = input.float(3.0, minval=0.001, maxval=50, title="Multiplier")
initialTradePercent = input.float(10, title="Initial Trade Percentage (%)") / 100
scalingFactor = input.float(1.2, title="Scaling Factor")
shortEMA_length = input.int(50, title="Short EMA Length")
longEMA_length = input.int(200, title="Long EMA Length")

// Calculate EMAs
shortEMA = ta.ema(close, shortEMA_length)
longEMA = ta.ema(close, longEMA_length)

// Calculate Bollinger Bands


basis = ta.vwma(src, length)
dev = mult * ta.stdev(src, length)
upper_1 = basis + (0.236 * dev)
upper_2 = basis + (0.382 * dev)
upper_3 = basis + (0.5 * dev)
upper_4 = basis + (0.618 * dev)
upper_5 = basis + (0.764 * dev)
upper_6 = basis + (1 * dev)
lower_1 = basis - (0.236 * dev)
lower_2 = basis - (0.382 * dev)
lower_3 = basis - (0.5 * dev)
lower_4 = basis - (0.618 * dev)
lower_5 = basis - (0.764 * dev)
lower_6 = basis - (1 * dev)

// Plotting the EMAs


plot(shortEMA, color=color.yellow, linewidth=2, title="50 EMA")
plot(longEMA, color=color.blue, linewidth=2, title="200 EMA")

// Plotting the Bands


plot(basis, color=color.fuchsia, linewidth=2)
plot(upper_1, color=color.white, linewidth=1, title="Upper 0.236")
plot(upper_2, color=color.white, linewidth=1, title="Upper 0.382")
plot(upper_3, color=color.white, linewidth=1, title="Upper 0.5")
plot(upper_4, color=color.white, linewidth=1, title="Upper 0.618")
plot(upper_5, color=color.white, linewidth=1, title="Upper 0.764")
plot(upper_6, color=color.red, linewidth=2, title="Upper 1")
plot(lower_1, color=color.white, linewidth=1, title="Lower 0.236")
plot(lower_2, color=color.white, linewidth=1, title="Lower 0.382")
plot(lower_3, color=color.white, linewidth=1, title="Lower 0.5")
plot(lower_4, color=color.white, linewidth=1, title="Lower 0.618")
plot(lower_5, color=color.white, linewidth=1, title="Lower 0.764")
plot(lower_6, color=color.green, linewidth=2, title="Lower 1")

// Strategy Conditions
shortCondition1 = ta.crossunder(close, upper_1)
shortCondition2 = ta.crossunder(close, upper_2)
shortCondition3 = ta.crossunder(close, upper_3)
shortCondition4 = ta.crossunder(close, upper_4)
shortCondition5 = ta.crossunder(close, upper_5)
shortCondition6 = ta.crossunder(close, upper_6)
longCondition1 = ta.crossover(close, lower_1)
longCondition2 = ta.crossover(close, lower_2)
longCondition3 = ta.crossover(close, lower_3)
longCondition4 = ta.crossover(close, lower_4)
longCondition5 = ta.crossover(close, lower_5)
longCondition6 = ta.crossover(close, lower_6)

// Calculate available capital


equity = strategy.equity
initialTradeAmount = equity * initialTradePercent

// Track used capital and trade amounts


var float usedCapital = 0.0
var float tradeAmount1 = initialTradeAmount
var float tradeAmount2 = initialTradeAmount * scalingFactor
var float tradeAmount3 = tradeAmount2 * scalingFactor
var float tradeAmount4 = tradeAmount3 * scalingFactor
var float tradeAmount5 = tradeAmount4 * scalingFactor
var float tradeAmount6 = tradeAmount5 * scalingFactor

// Track open positions


var bool hasShort1 = false
var bool hasShort2 = false
var bool hasShort3 = false
var bool hasShort4 = false
var bool hasShort5 = false
var bool hasShort6 = false

var bool hasLong1 = false


var bool hasLong2 = false
var bool hasLong3 = false
var bool hasLong4 = false
var bool hasLong5 = false
var bool hasLong6 = false

// Determine trend based on EMA crossovers


goldenCross = ta.crossover(shortEMA, longEMA)
deathCross = ta.crossunder(shortEMA, longEMA)
longTrend = shortEMA > longEMA
shortTrend = shortEMA < longEMA

// Short Entries
if (shortTrend)
if (shortCondition1 and usedCapital + tradeAmount1 <= equity and not hasShort1)
strategy.entry("Short 0.236", strategy.short, qty=tradeAmount1/close)
usedCapital += tradeAmount1
hasShort1 := true

if (shortCondition2 and usedCapital + tradeAmount2 <= equity and not hasShort2)


strategy.entry("Short 0.382", strategy.short, qty=tradeAmount2/close)
usedCapital += tradeAmount2
hasShort2 := true

if (shortCondition3 and usedCapital + tradeAmount3 <= equity and not hasShort3)


strategy.entry("Short 0.5", strategy.short, qty=tradeAmount3/close)
usedCapital += tradeAmount3
hasShort3 := true
if (shortCondition4 and usedCapital + tradeAmount4 <= equity and not hasShort4)
strategy.entry("Short 0.618", strategy.short, qty=tradeAmount4/close)
usedCapital += tradeAmount4
hasShort4 := true

if (shortCondition5 and usedCapital + tradeAmount5 <= equity and not hasShort5)


strategy.entry("Short 0.764", strategy.short, qty=tradeAmount5/close)
usedCapital += tradeAmount5
hasShort5 := true

if (shortCondition6 and usedCapital + tradeAmount6 <= equity and not hasShort6)


strategy.entry("Short 1", strategy.short, qty=tradeAmount6/close)
usedCapital += tradeAmount6
hasShort6 := true

// Long Entries
if (longTrend)
if (longCondition1 and usedCapital + tradeAmount1 <= equity and not hasLong1)
strategy.entry("Long 0.236", strategy.long, qty=tradeAmount1/close)
usedCapital += tradeAmount1
hasLong1 := true

if (longCondition2 and usedCapital + tradeAmount2 <= equity and not hasLong2)


strategy.entry("Long 0.382", strategy.long, qty=tradeAmount2/close)
usedCapital += tradeAmount2
hasLong2 := true

if (longCondition3 and usedCapital + tradeAmount3 <= equity and not hasLong3)


strategy.entry("Long 0.5", strategy.long, qty=tradeAmount3/close)
usedCapital += tradeAmount3
hasLong3 := true

if (longCondition4 and usedCapital + tradeAmount4 <= equity and not hasLong4)


strategy.entry("Long 0.618", strategy.long, qty=tradeAmount4/close)
usedCapital += tradeAmount4
hasLong4 := true

if (longCondition5 and usedCapital + tradeAmount5 <= equity and not hasLong5)


strategy.entry("Long 0.764", strategy.long, qty=tradeAmount5/close)
usedCapital += tradeAmount5
hasLong5 := true

if (longCondition6 and usedCapital + tradeAmount6 <= equity and not hasLong6)


strategy.entry("Long 1", strategy.long, qty=tradeAmount6/close)
usedCapital += tradeAmount6
hasLong6 := true

// Reset used capital and position trackers after closing all positions
if ((strategy.position_size > 0 and close >= basis) or (strategy.position_size < 0
and close <= basis))
strategy.close_all()
usedCapital := 0.0
hasShort1 := false
hasShort2 := false
hasShort3 := false
hasShort4 := false
hasShort5 := false
hasShort6 := false
hasLong1 := false
hasLong2 := false
hasLong3 := false
hasLong4 := false
hasLong5 := false
hasLong6 := false

You might also like