Create me a simple algorithm using tradingviews pine script
- User
Create me a simple algorithm using tradingviews pine script
Assistant
Below is a simple algorithm created using TradingView's Pine Script. This example will illustrate a basic strategy that:
- Plots two Moving Averages (MA): a short-term MA (50-period) and a long-term MA (200-period).
- Generates Buy/Sell Signals:
- Buy Signal: When the short-term MA crosses above the long-term MA.
- Sell Signal: When the short-term MA crosses below the long-term MA.
Pine Script Algorithm: Simple MA Crossover Strategy
pinescript//@version=5 indicator("Simple MA Crossover Strategy", overlay=true) // Define input parameters for MA periods and types lengthShort = input(50, title="Short MA Period") lengthLong = input(200, title="Long MA Period") maType = input.string("SMA", title="MA Type", options=["SMA", "EMA"]) // Calculate Moving Averages based on selected type maShort = maType == "SMA" ? ta.sma(close, lengthShort) : ta.ema(close, lengthShort) maLong = maType == "SMA" ? ta.sma(close, lengthLong) : ta.ema(close, lengthLong) // Plot Moving Averages on the chart plot(maShort, color=color.red, linewidth=2, title="Short MA") plot(maLong, color=color.blue, linewidth=2, title="Long MA") // Conditions for Buy & Sell Signals with optional filtering filterSignals = input(false, title="Filter Signals") if (filterSignals) // Only generate signals when both MAs are trending in the same direction buyCondition = ta.crossover(maShort, maLong) and maShort > maShort[1] and maLong > maLong[1] sellCondition = ta.crossunder(maShort, maLong) and maShort < maShort[1] and maLong < maLong[1] else buyCondition = ta.crossover(maShort, maLong) sellCondition = ta.crossunder(maShort, maLong) // Plot labels for buy/sell signals if (buyCondition) label.new(bar_index, low, style=label.style_label_up, color=color.green, text="Buy", yloc=yloc.belowbar) alertcondition(buyCondition, title="Buy Signal", message="Short MA crossed above Long MA!") if (sellCondition) label.new(bar_index, high, style=label.style_label_down, color=color.red, text="Sell", yloc=yloc.abovebar) alertcondition(sellCondition, title="Sell Signal", message="Short MA crossed under Long MA!")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.