can you write in a pinescode 4.0 strategy: long postion if price closes below the lower…
- User
can you write in a pinescode 4.0 strategy: long postion if price closes below the lower bollindger and close position if price above upper bolindger. for short position coditions are opposite. And use arbitrary take profit and stop loss.
- Assistant
//@version=4 strategy("Bollinger Bands Strategy", overlay = true)
// Strategy inputs takeProfit = input(1.5, title="Take Profit (%)", step=0.1) stopLoss = input(1.0, title="Stop Loss (%)", step=0.1)
// Bollinger Bands length = input(20, minval=1, title="BB Length") mult = input(2.0, minval=0.001, maxval=50, title="BB Std Dev") src = close
basis = sma(src, length) dev = mult * stdev(src, length) upperBB = basis + dev lowerBB = basis - dev
// Long position condition longCondition = close < lowerBB
// Short position condition shortCondition = close > upperBB
// Take Profit and Stop Loss levels longTakeProfit = close * (1 + takeProfit / 100) longStopLoss = close * (1 - stopLoss / 100)
shortTakeProfit = close * (1 - takeProfit / 100) shortStopLoss = close * (1 + stopLoss / 100)
// Entry and exit logic if (longCondition) strategy.entry("Long", strategy.long) strategy.exit("Exit Long", "Long", limit=longTakeProfit, stop=longStopLoss)
if (shortCondition) strategy.entry("Short", strategy.short) strategy.exit("Exit Short", "Short", limit=shortTakeProfit, stop=shortStopLoss)