0bps

Strategy vault / Mean reversion

Simple EMA20 Strategy Stochastic

The strategy you have built uses an EMA20 (an exponential moving average indicator with a period of 20) and a stochastic oscillator. 1. At the beginning, you have set up the parameters for the stochastic oscillator…

Author
ChaoZhang
Complexity
Starter · 26 lines
Parameters
3
Backtest bar
4h · intraday
Tested on
BTC/USDT · futures

The strategy you have built uses an EMA20 (an exponential moving average indicator with a period of 20) and a stochastic oscillator.

  1. At the beginning, you have set up the parameters for the stochastic oscillator, which consists of %K and %D parameters. %K measures the current market rate for an asset, and %D is a moving average of %K.

  2. Then you calculate the values of %K and %D based on the historical prices of the asset (close, high, low).

  3. Next, the 20-period EMA is calculated.

  4. After this, you plot the EMA20 on the chart.

  5. Then you define the conditions for entering a long position (buying) and exiting the position (selling).

You will enter a position when:

  • The lowest price (low) is greater than the EMA20.
  • And the %K is greater than %D (this generally indicates an oversold market).
  • And the current EMA is greater than the EMA of 20 periods ago (indicating that the market is trending upwards).

You will exit the position when:

  • The closing price falls below the EMA.

According to this strategy, you might invest when the market has been oversold and is now starting a trend upwards. And you would sell off your investment when the trend is about to return downwards again.

Please remember that all trading strategies come with risks and should be used wisely.

Parameters

NameDefaultWhat it controls
v_input_int_114%K Length
v_input_int_2true%K Smoothing
v_input_int_33%D Smoothing

Source Pine Script

/*backtest
start: 2022-09-01 00:00:00
end: 2023-09-07 00:00:00
period: 4h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// © dragolite95
//@version=5
strategy("Simple EMA20 Strat", overlay=true, margin_long=100, margin_short=100)

periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)

ema = ta.ema(close, 20)

plot(series=ema, title="ema 20", color=color.blue)

if(low > ema and k > d and ema > ema[20])
    strategy.entry("long", strategy.long)
if(close < ema)
    strategy.close("long")

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-08. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Simple-EMA20-Strategy-Stochastic.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open