pine语言马丁格尔策略

Author: Zer3192, Created: 2023-10-16 17:50:01, Updated: 2023-10-19 15:10:47

``````//@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)

// 马丁格尔策略参数
useMartin = input(true, title="使用马丁格尔倍投")
//initialQty = input(0.001, title="初始仓位大小")
martinFactor = input(2, title="马丁格尔倍数")

// 买入条件满足时，产生买入信号，并设置止盈
if (longCondition)
if useMartin
if close<strategy.position_avg_price*0.95 and strategy.position_size >0
if strategy.position_size >0
strategy.exit("Take Profit/Stop Loss", "Buy", profit=strategy.position_avg_price  * 1.1)

// 卖出条件满足时，产生卖出信号，并设置止盈
if (shortCondition)
strategy.entry("Sell",strategy.short)
if useMartin
if close<strategy.position_avg_price*1.05 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  * 0.9)

``````

More