0% found this document useful (0 votes)
167 views2 pages

Custom Moving Average Strategy Code

The document contains two Pine Script codes for TradingView. The first code implements a Simple Moving Average (SMA) indicator, while the second code establishes a crossover strategy using 20 and 50 period SMAs for trading signals. The strategy includes conditions for entering buy and sell trades based on the crossover of the fast and slow moving averages.

Uploaded by

knk333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views2 pages

Custom Moving Average Strategy Code

The document contains two Pine Script codes for TradingView. The first code implements a Simple Moving Average (SMA) indicator, while the second code establishes a crossover strategy using 20 and 50 period SMAs for trading signals. The strategy includes conditions for entering buy and sell trades based on the crossover of the fast and slow moving averages.

Uploaded by

knk333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Paste following codes here: https://cutt.

ly/9fikso

Custom Moving Average Indicator

//@version=5
indicator('Simple Moving Average', shorttitle='SMA', overlay=true)
length = input.int(14, minval=1)
src = close
sma = ta.sma(src, length)
plot(sma, color=color.new(color.blue, 0), linewidth=2)

Custom Moving Average Crossover Strategy

//@version=5
strategy("20 and 50 SMA Crossover Strategy", overlay=true)

// Define input parameters


fast_length = input.int(20, title="Fast MA Length")
slow_length = input.int(50, title="Slow MA Length")

// Calculate simple moving averages


fast_ma = ta.sma(close, fast_length)
slow_ma = ta.sma(close, slow_length)

// Plot moving averages on chart


plot(fast_ma, color=color.green, title="Fast MA")
plot(slow_ma, color=color.red, title="Slow MA")

// Define trading conditions


buy_signal = ta.crossover(fast_ma, slow_ma)
sell_signal = ta.crossunder(fast_ma, slow_ma)
// Execute trades based on trading conditions
if (buy_signal)
strategy.entry("Buy", strategy.long)

if (sell_signal)
strategy.entry("Sell", strategy.

You might also like