code me pine script MACD for tradingview
- User
code me pine script MACD for tradingview
- Assistant
Certainly! Below is an example of a simple Moving Average Convergence Divergence (MACD) strategy coded in Pine Script for TradingView:
//@version=4 strategy("MACD Strategy", overlay=true) // MACD Inputs macdShort = input(12, title="MACD Short EMA") macdLong = input(26, title="MACD Long EMA") macdSignal = input(9, title="MACD Signal SMA") // Calculate MACD [macdLine, signalLine, _] = macd(close, macdShort, macdLong, macdSignal) // Determine strategy entry and exit conditions enterLong = crossover(macdLine, signalLine) exitLong = crossunder(macdLine, signalLine) enterShort = crossunder(macdLine, signalLine) exitShort = crossover(macdLine, signalLine) // Execute strategy strategy.entry("Long", strategy.long, when = enterLong) strategy.close("Long", when = exitLong) strategy.entry("Short", strategy.short, when = enterShort) strategy.close("Short", when = exitShort) // Plot MACD Lines plot(macdLine, color=color.blue, title="MACD Line") plot(signalLine, color=color.red, title="Signal Line") - Assistant
Certainly! Here's an updated version of the code that includes an adjustable stop loss and take profit:
//@version=4 strategy("MACD Strategy with Stop Loss and Take Profit", overlay=true) // MACD Inputs macdShort = input(12, title="MACD Short EMA") macdLong = input(26, title="MACD Long EMA") macdSignal = input(9, title="MACD Signal SMA") // Stop Loss and Take Profit Inputs stopLossPercent = input(1, title="Stop Loss (%)", minval=0.1) takeProfitPercent = input(1, title="Take Profit (%)", minval=0.1) // Calculate MACD [macdLine, signalLine, _] = macd(close, macdShort, macdLong, macdSignal) // Determine strategy entry and exit conditions enterLong = crossover(macdLine, signalLine) exitLong = crossunder(macdLine, signalLine) enterShort = crossunder(macdLine, signalLine) exitShort = crossover(macdLine, signalLine) // Execute strategy strategy.entry("Long", strategy.long, when = enterLong) strategy.close("Long", when = exitLong, comment="Exit Long") strategy.entry("Short", strategy.short, when = enterShort) strategy.close("Short", when = exitShort, comment="Exit Short") // Calculate Stop Loss and Take Profit levels stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100) takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100) // Apply Stop Loss and Take Profit levels strategy.exit("Exit Long", "Long", stop=stopLossLevel, limit=takeProfitLevel) strategy.exit("Exit Short", "Short", stop=stopLossLevel, limit=takeProfitLevel) // Plot MACD Lines plot(macdLine, color=color.blue, title="MACD Line") plot(signalLine, color=color.red, title="Signal Line") - User
code me pine script rsi for tradingview
- Assistant
Certainly! Below is an example of a simple Relative Strength Index (RSI) strategy coded in Pine Script for TradingView:
//@version=4 strategy("RSI Strategy", overlay=true) // RSI Inputs rsiLength = input(14, title="RSI Length") overboughtLevel = input(70, title="Overbought Level") oversoldLevel = input(30, title="Oversold Level") // Calculate RSI rsiValue = rsi(close, rsiLength) // Determine strategy entry and exit conditions enterLong = crossover(rsiValue, oversoldLevel) exitLong = crossunder(rsiValue, overboughtLevel) enterShort = crossunder(rsiValue, overboughtLevel) exitShort = crossover(rsiValue, oversoldLevel) // Execute strategy strategy.entry("Long", strategy.long, when = enterLong) strategy.close("Long", when = exitLong) strategy.entry("Short", strategy.short, when = enterShort) strategy.close("Short", when = exitShort) // Plot RSI value and levels plot(rsiValue, color=color.blue, title="RSI") plot(overboughtLevel, color=color.red, title="Overbought Level") plot(oversoldLevel, color=color.green, title="Oversold Level")