I have a script for buy/sell strategy with data output. This is done for one MA. It is working fine and gives the desired data.
However since Im on the basic plan, I cannot have many indicators/strategies open at the same time. Right now, I have to go to options and change the MA input from eg. 10 to 9 and see the output. This is very tedious to do for 6 different MA’s, for the D, W and M chart for each stock.
I would like to add a second/other MA and compare which one gives a better result.
Since strategies are void and cannot be assigned to variables it creates issues for me. And currently, I want to use
strategy.netprofit_percent
which is mutable. Therefore I cannot use it for more than one MA or buy/sell.
Essentially what Im trying to achieve is comparing many sma strategies and outputting their annual performance in a table so that later I can analyze individual stocks and see which MA performs best for each stock.
The output table will look something like:
- 7 sma: %
- 9 sma: %
- 10 sma: %
- 7 ema: %
- 9 ema: %
- 10 ema: %
I would really like something like the following – generated by chatgpt:
indicator("Compare Moving Averages Strategies", overlay=true)
// Input for SMA and EMA lengths
sma_length = input.int(20, title="SMA Length")
ema_length = input.int(20, title="EMA Length")
// Calculate SMA and EMA
sma = ta.sma(close, sma_length)
ema = ta.ema(close, ema_length)
// Plot SMA and EMA
plot(sma, color=color.blue, title="SMA")
plot(ema, color=color.red, title="EMA")
// Create two separate strategies <-- This part does not work
var strategy_1 = strategy("SMA Strategy", overlay=true, initial_capital=100000)
var strategy_2 = strategy("EMA Strategy", overlay=true, initial_capital=100000)
// Define conditions for SMA strategy
sma_long = ta.crossover(close, sma)
sma_short = ta.crossunder(close, sma)
// Define conditions for EMA strategy
ema_long = ta.crossover(close, ema)
ema_short = ta.crossunder(close, ema)
// Implement SMA strategy
if (sma_long)
strategy_1.entry("SMA Long", strategy.long)
if (sma_short)
strategy_1.close("SMA Long")
// Implement EMA strategy
if (ema_long)
strategy_2.entry("EMA Long", strategy.long)
if (ema_short)
strategy_2.close("EMA Long")
// Plot buy and sell signals
plotshape(series=sma_long, location=location.belowbar, color=color.green, style=shape.labelup, title="SMA Buy Signal", text="BUY")
plotshape(series=sma_short, location=location.abovebar, color=color.red, style=shape.labeldown, title="SMA Sell Signal", text="SELL")
plotshape(series=ema_long, location=location.belowbar, color=color.green, style=shape.labelup, title="EMA Buy Signal", text="BUY")
plotshape(series=ema_short, location=location.abovebar, color=color.red, style=shape.labeldown, title="EMA Sell Signal", text="SELL")
// Calculate strategy performance
sma_performance = strategy_1.equity(strategy_1.initial_capital) / strategy_1.initial_capital - 1
ema_performance = strategy_2.equity(strategy_2.initial_capital) / strategy_2.initial_capital - 1
// Plot strategy performance on the chart
plot(series=sma_performance, color=color.blue, linewidth=2, title="SMA Strategy Performance")
plot(series=ema_performance, color=color.red, linewidth=2, title="EMA Strategy Performance")
// Display strategy performance in the title
indicator(title="Compare Moving Averages Strategies - SMA: " + str.tostring(sma_performance) + " - EMA: " + str.tostring(ema_performance))```