Strategy vault / Machine learning
Quantitative Trading Strategy Based on SMA and EMA
This strategy is named "Quantitative Trading Strategy Based on SMA and EMA". Its main idea is to combine SMA lines and EMA lines with different parameters to construct trading signals. 1. Calculate the SMA9, SMA50…

I. Strategy Overview
This strategy is named "Quantitative Trading Strategy Based on SMA and EMA". Its main idea is to combine SMA lines and EMA lines with different parameters to construct trading signals.
II. Strategy Principle
Calculate the SMA9, SMA50, SMA180 of close price and EMA20.
Determine buy and sell signals based on the relationship between close price and support sup and resistance res. Generate buy signal BuySignal when close breaks through sup, and generate sell signal SellSignal when close breaks through res.
When buy signal triggers, execute long position strategy; when sell signal triggers, close long position.
When sell signal triggers, execute short position strategy; when buy signal triggers, close short position.
III. Advantage Analysis
Combining multiple moving averages to form trading signals improves accuracy and stability.
Calculating dynamic support and resistance makes trading signals more reliable.
Adopting high, medium and low volatility moving averages considers both long-term trend and short-term breakthroughs, improving the strategy's profitability.
Supporting both long and short positions can obtain profits in trending and sideways markets.
IV. Risk Analysis
SMA has lagging effect, which may delay buy and sell signals and affect strategy performance.
No stop loss mechanism, losses may expand.
Insufficient backtesting data, parameters need to be adjusted according to the market.
Relying on technical indicators, unable to cope with black swan events.
Solutions:
- Adjust SMA periods properly.
- Set reasonable stop loss.
- Increase sample size for backtesting, adjust parameters.
- Improve risk control mechanisms.
V. Optimization
Add volatility based stop loss to control single loss.
Add machine learning models to assist with trend judgment and signal generation.
Add key price analysis to improve support and resistance accuracy.
Test different parameter combinations to find better parameters.
VI. Summary
This strategy combines the technical indicators of SMA and EMA to construct trading signals, and calculates dynamic support and resistance to form a complete buy and sell logic. The advantages are flexible parameters, two-way trading, adaptable to different markets, but it also faces issues like lagging and inadequate stop loss. Future optimizations can be made in aspects like stop loss, trend judgment, key price analysis to improve stability and profitability. ]
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 9 | SMA1 |
| v_input_2 | 50 | SMA2 |
| v_input_3 | 180 | SMA3 |
| v_input_4 | 20 | EMA1 |
| v_input_5 | 3 | BUY/SELL Swing |
| v_input_6 | false | BUY/SELL Bar Color |
| v_input_7 | false | BUY/SELL Background Color |
Source Pine Script
/*backtest
start: 2023-12-10 00:00:00
end: 2023-12-11 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy(title="StrategySMA 9/50/180 | EMA 20 | BUY/SELL", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
//SMA and EMA code
smaInput1 = input(9, title="SMA1")
smaInput2 = input(50, title="SMA2")
smaInput3 = input(180, title="SMA3")
emaInput1 = input(20, title="EMA1")
sma1 = sma(close, smaInput1)
sma2 = sma(close, smaInput2)
sma3 = sma(close, smaInput3)
EMA1 = ema(close, emaInput1)
plot(sma1, color= color.red , title="SMA1")
plot(sma2, color = color.blue, title="SMA2")
plot(sma3, color= color.white, title="SMA3")
plot(EMA1, color = color.yellow, title="EMA1")
no=input(3,title="BUY/SELL Swing")
Barcolor=input(false,title="BUY/SELL Bar Color")
Bgcolor=input(false,title="BUY/SELL Background Color")
res=highest(high,no)
sup=lowest(low,no)
avd=iff(close>res[1],1,iff(close<sup[1],-1,0))
avn=valuewhen(avd!=0,avd,0)
tsl=iff(avn==1,sup,res)
// Buy/sell signals
BuySignal = crossover(close, tsl)
SellSignal = crossunder(close, tsl)
// Enter long position
strategy.entry("Buy", strategy.long, when=BuySignal)
// Exit long position
strategy.exit("Sell", "Buy", when=SellSignal)
// Enter short position
strategy.entry("Sell", strategy.short, when=SellSignal)
// Exit short position
strategy.exit("Buy", "Sell", when=BuySignal)
colr = close>=tsl ? color.green : close<=tsl ? color.red : na
plot(tsl, color=colr)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-12. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Quantitative-Trading-Strategy-Based-on-SMA-and-EMA.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Out of the box Machine Learning Trading StrategyPine Script · Machine learning · 1d
- Hilo Activator Buy Sell Signals StrategyPine Script · Machine learning · 10m
- Multi Indicator Decision Tree Strategy IMACD EMA and IchimokuPine Script · Machine learning · 30m
- Multi indicator Assisted StrategyPine Script · Machine learning · 1d
- Moving Average Rebound StrategyPine Script · Machine learning · 1d
- Dynamic Regression Channel StrategyPine Script · Machine learning · 2h