0bps

Strategy vault / Machine learning

Pure Stochastic Long Only Strategy

This is a pure Stochastic strategy that uses the indicator for entry and exit signals, going long only. It enters long when K line crosses above D line in oversold zone with close above previous high, and exits on…

Author
ChaoZhang
Complexity
Starter · 42 lines
Parameters
6
Backtest bar
5m · intraday
Tested on
BTC/USDT · futures

Overview

This is a pure Stochastic strategy that uses the indicator for entry and exit signals, going long only. It enters long when K line crosses above D line in oversold zone with close above previous high, and exits on profit taking or stop loss triggers. Simple and easy to implement.

Strategy Logic

The main logic is:

  1. Calculate Stochastic K and D values
  2. Enter long when K crosses above D in oversold zone and close breaks previous high
  3. Set moving stop loss below fast EMA on close
  4. Take profit when K crosses below D or K enters overbought zone

K crossing D in oversold suggests potential upside reversal. Close breaking high adds confidence.

EMA stop locks in profits. K crossing D in overbought acts as profit take signal.

Going long only, it suits instruments like equities with one-sided trends. Simple to implement.

Advantages

  • Uses Stochastic to identify oversold regions
  • K and D lines avoid false signals
  • Close breakout adds confidence
  • Stop loss and take profit manages risks
  • Simple logic makes it easy to implement

Risks and Mitigation

  • Potential for Stochastic false signals
  • Has some loss risks
  • Unable to take profit at trend tops

Mitigations:

  1. Optimize parameters for greater accuracy
  2. Use moving stops to control loss risks
  3. Add indicators to predict trend reversal

Enhancement Opportunities

The strategy can be enhanced by:

  1. Adding short side opportunities for full market coverage
  2. Adaptive stops based on volatility
  3. Machine learning for parameter optimization
  4. Incorporate trailing take profit strategy
  5. Portfolio combinations to build multifactor system

Conclusion

This is a pure Stochastic long strategy using the indicator for oversold entries and managed exits. Simple and practical, it fits instruments like equities well. Expanding to the short side, parameter optimization can make it a more robust system.

Parameters

NameDefaultWhat it controls
v_input_110length
v_input_280OverBought
v_input_320OverSold
v_input_47smoothK
v_input_54smoothD
v_input_65emaperiodf

Source Pine Script

/*backtest
start: 2023-09-11 00:00:00
end: 2023-09-12 14:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version= 4
// see for original idea:  http://www.enricomalverti.com/2016/12/stocastico/
// https://sauciusfinance.altervista.org
strategy(title="Pure Stochastic long only", overlay = false, max_bars_back=500)

// INPUTS & calculations
length = input(10, minval=1)
OverBought = input(80, minval = 50, step = 10)
OverSold = input(20, minval = 10, step = 5)
smoothK = input(7, minval=1)
smoothD = input(4, minval=1)
k = sma(stoch(close, high, low, length), smoothK)
d = sma(k, smoothD)
// We keep EMA 7 (n period of stochastic /2) as target price
emaperiodf = input(5, minval = 1)
emaf = ema(close,emaperiodf)
entryl = k > d and k <= OverSold and close >= high[1]
/// Entry
strategy.entry("Long", true, when = entryl)

middle = (OverBought+OverSold)/2
close1= crossunder(close,emaf)// **close under EMA fast**
close2= k < d and k > middle
close3 = (k >= OverBought)
// exits.
strategy.close("Long", when = close1, comment="stop Ema Fast")
strategy.close("Long", when = close2, comment ="cross k&d")
strategy.close("Long", when = close3, comment = "high value of K")


plot(k, color=#0000FF,  linewidth= 2, title="k Stoch")
plot(d, color=#787B86, linewidth= 1, title="d stoch signal")
plot(OverBought)
plot(OverSold)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-19. 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/Pure-Stochastic-Long-Only-Strategy.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