Strategy · Pine Script v4 · Own pane

IK 60-15 Stochastic Strategy

by hasnocool· code credited to userIan

This strategy enters long trades based on standard Stochastic oscillator conditions combined with a 60-minute higher-timeframe trend filter.

View on GitHub

Repaints

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code52
Settings8inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

52 lines of Pine Script v4 from (IK) Stoch-60-15.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

(IK) Stoch-60-15.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © userIan
//@version=4
strategy(title="(IK) 60-15", precision=2, default_qty_type=strategy.cash, default_qty_value=100.0, initial_capital=100.0, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1)
// get input 
i_length        = input(group="Stochastic", title="Stochastic Length", minval=1, defval=18)                                     
i_k             = input(group="Stochastic", title="Stochastic %K", minval=1, defval=2)
i_d             = input(group="Stochastic", title="Stochastic %D", minval=1, defval=10)
i_trailingStop  = input(group="Trade Settings", title="Trailing Stop?", type=input.bool, defval=true)                           // should stop loss be dynamic, based on most recent high, or static, based on entry price?
i_stopPercent   = input(group="Trade Settings", title="Stop Percent", type=input.float, defval=0.05, maxval=1.0, minval=0.001)  // how much (percentage) can price fall before exiting the trade
i_takeProfit    = input(group="Trade Settings", title="Take Profit", type=input.float, defval=0.05, maxval=1.0, minval=0.001)   // how much (percentage) can price rise before exiting the trade
i_sellThreshold = input(group="Trade Settings", title="Sell Threshold", type=input.float, defval=75.0, maxval=100, minval=0.0)  // when stochastic %D crosses under this value, exit the trade
i_buyThreshold  = input(group="Trade Settings", title="Buy Threshold", type=input.float, defval=40.0, maxval=100, minval=0.0)   // stochastic %D must be below this value to enter a trade
// declare order variables
var recentHigh  = 0.0   // the highest high while in a trade
var stop        = 0.0   // the price that will trigger as stop loss
var entryPrice  = 0.0   // the price when the trade was entered
// build stochastic
sto = stoch(close, high, low, i_length)
K   = sma(sto, i_k)
D   = sma(K, i_d)
// get stochastic trend
stoch_upTrend = D > D[1]
// get stochastic trend for 60 minute
stoch_60         = security(syminfo.tickerid, "60", D)
stoch_60_upTrend = (stoch_60[4] >= stoch_60[8])
// entry
if (D < i_buyThreshold) and stoch_upTrend and stoch_60_upTrend and barstate.isconfirmed
    recentHigh := close
    entryPrice := close
    stop       := (close * (1-i_stopPercent)) 
    strategy.entry(id="60-15-Long", long=strategy.long, comment='buy')
// update recent high, trailing stop
if close > recentHigh  
    recentHigh := close
    stop       := i_trailingStop ? (recentHigh * (1-i_stopPercent)) : stop
// exit
strategy.exit(id="60-15-Long", stop=stop, comment='sell-stop')
if close > (entryPrice * (1+i_takeProfit)) and barstate.isconfirmed
    strategy.close(id="60-15-Long", comment='sell-profit')
if crossunder(D, i_sellThreshold) and barstate.isconfirmed
    strategy.close(id="60-15-Long", comment='sell-trend')
    
// plot
plot(K, title="%K", color=color.blue, linewidth=1)
plot(D, title="%D", color=color.orange, linewidth=1)
plot(stoch_60, title="Hourly Stochastic (D)", color= stoch_60_upTrend ? color.green : color.red, style=plot.style_stepline)
upperBand  = hline(i_sellThreshold, title="Upper Limit", linestyle=hline.style_dashed)
middleBand = hline(50, title="Midline", linestyle=hline.style_dotted)
lowerBand  = hline(i_buyThreshold, title="Lower Limit", linestyle=hline.style_dashed)
fill(lowerBand, upperBand, color=color.purple, transp=75)
Expand (61 lines)

Overview

IK 60-15 Stochastic Strategy opens positions when lower-timeframe Stochastic %D is below a buy threshold and both lower and higher-timeframe %D lines are trending upwards. Exits are triggered by a take-profit target, a %D crossunder of the sell threshold, or a custom trailing stop loss. The script plots %K, %D, and the 60-minute %D stepline in a separate indicator pane. Note that the unshifted higher-timeframe security call introduces lookahead bias on historical bars.

Details

Published by

hasnocool on GitHub

Code credit

userIan, as written in the script's header

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Crypto

Timeframes

M1-M5, M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Stochastic Lengthint18i_length
Stochastic %Kint2i_k
Stochastic %Dint10i_d
Trailing Stop?booltruei_trailingStop
Stop Percentfloat0.05i_stopPercent
Take Profitfloat0.05i_takeProfit
Sell Thresholdfloat75.0i_sellThreshold
Buy Thresholdfloat40.0i_buyThreshold
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

Q-Trend + KVO Strategy

by TradersPost
218 stars

This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und

Strategy v6 TrendVolume
Updated 3 months ago · None stated GitHub repository of Q-Trend + KVO Strategy

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)
114 stars

This strategy estimates price behavior using ordinary least squares regression between higher-timeframe data and a generated dataset, smoothed by a Karobein oscillator. It enters long positions when the oscillator turns upward and short positions when it turns

Strategy v4 Oscillators & momentumAI / machine learning Repaints
65 stars

This strategy calculates a 20-period Relative Strength Index (RSI) and generates entry orders based on crossovers and crossunders of the 50 level. When RSI crosses above 50, it places a long stop entry at the current high price with a custom alert message. Whe

Strategy v4 Oscillators & momentum
Updated 4 days ago · GPL-3.0 GitHub repository of Strategy Using Alert Message

BTC Futures MACD

by iamrichardD
57 stars

This strategy plots a MACD oscillator in a separate pane and trades Bitcoin futures from zero-line and signal-line crosses. Optional early momentum signals, a volume filter, and a trend moving-average filter can confirm entries. It sizes positions as fixed con

Strategy v6 Oscillators & momentumMoving averages Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of BTC Futures MACD

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers