This strategy combines the Stochastic Oscillator with a Moving Average, generating trading signals by observing the overbought and oversold conditions of the stochastic indicator and the trend of the moving average. It produces a short signal when the stochastic indicator is in the overbought zone and the moving average is downward, and a long signal when in the oversold zone and the moving average is upward. Additionally, the strategy introduces a stochastic indicator filter, which can also generate corresponding trading signals when the stochastic K line crosses the D line after staying below 50 for a certain number of K lines. The strategy also sets a Stop Loss to control risk.
Calculate the Stochastic Oscillator to obtain the K line and D line. Parameters are adjustable, including the stochastic period, K smoothing, D smoothing, overbought zone, and oversold zone.
Calculate the Moving Average, using the closing price by default, with an adjustable period.
Calculate the Stochastic Indicator Filter. When the K line stays below 50 for a certain number of K lines, it generates a filter signal.
Conditions for generating a long signal: Stochastic indicator crosses upward in the oversold zone OR Stochastic indicator filter signal AND Moving average is upward.
Conditions for generating a short signal: Stochastic indicator crosses downward in the overbought zone OR Stochastic indicator filter signal AND Moving average is downward.
Long position closing condition: Stochastic K line crosses above the Moving Average AND the Average turns downward.
Short position closing condition: Stochastic K line crosses below the Moving Average AND the Average turns upward.
Position management uses a fixed percentage of funds, 10% by default. It also sets a Stop Loss, 2% by default.
By combining overbought/oversold and trend characteristics, it can chase up and kill down in a trend.
The Stochastic Indicator Filter avoids frequent trading in oscillating markets.
The Stop Loss setting helps control drawdowns.
The code structure is clear, parameters are adjustable, and it is suitable for further optimization.
The Stochastic Oscillator has a certain lag, which may miss the best buying and selling points.
The accuracy of capturing orders at trend turning points is poor, and the frequency of stop-loss may be high.
Fixed-ratio fund management has a large drawdown in the case of consecutive losses.
Introduce more filtering conditions, such as price behavior, other auxiliary indicators, etc., to improve signal accuracy.
Divide signals into strong and weak, and increase positions when strong signals appear.
Optimize the judgment of trend turning points to capture more market movements.
Optimize position management, consider adjusting positions based on floating profit and loss ratios, etc.
Try different parameter combinations to find the optimal parameters.
Based on the Stochastic Oscillator, this strategy combines Moving Averages to judge trends, while also utilizing the filtering function of the Stochastic Indicator itself, generating relatively reliable trading signals. The overall idea of the strategy is clear and suitable for use in trending markets. However, due to the lag of the Stochastic Oscillator, its performance at market turning points may be poor, and its overall adaptability and robustness need further examination. In the future, the strategy can be improved from aspects such as filtering conditions, position management, and parameter optimization.
/*backtest start: 2024-03-01 00:00:00 end: 2024-03-31 23:59:59 period: 1h basePeriod: 15m exchanges: [{"eid":"Binance","currency":"BTC_USDT"}] */ // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Pablo_2uc //@version=5 strategy("Estrategia Estocástico + MA con Stop Loss y Filtro Estocástico", overlay=true) // Parámetros del Estocástico length = input.int(14, title="Longitud Estocástico") smoothK = input.int(3, title="Suavizado K") smoothD = input.int(3, title="Suavizado D") oversold = input.int(20, title="Sobreventa") overbought = input.int(80, title="Sobrecompra") // Parámetros de la Media Móvil maLength = input.int(9, title="Longitud MA") maSource = input(close, title="Fuente MA") // Capital inicial capital = 5000 // Tamaño de posición (10% del capital) positionSize = capital * 0.10 // Stop Loss (2% del precio de entrada) stopLossPercent = input.int(2, title="Stop Loss (%)") / 100 // Número de ruedas para el filtro estocástico filterPeriods = input.int(12, title="Ruedas de Filtro Estocástico") // Cálculo del Estocástico k = ta.sma(ta.stoch(close, high, low, length), smoothK) d = ta.sma(k, smoothD) // Cálculo de la Media Móvil ma = ta.sma(maSource, maLength) // Filtro estocástico stochasticFilter = ta.sma(k > 50 ? 1 : 0, filterPeriods) // Condiciones de entrada en largo y corto longCondition = (ta.crossunder(k, oversold) or ta.crossover(stochasticFilter, 1)) and ma > ma[1] shortCondition = (ta.crossover(k, overbought) or ta.crossover(stochasticFilter, 1)) and ma < ma[1] // Condiciones de salida exitLongCondition = ta.crossover(k, ma) and ma < ma[1] exitShortCondition = ta.crossunder(k, ma) and ma > ma[1] // Estrategia if (longCondition) strategy.entry("Long", strategy.long, qty=positionSize) strategy.exit("Exit Long", "Long", stop=close * (1 - stopLossPercent)) if (shortCondition) strategy.entry("Short", strategy.short, qty=positionSize) strategy.exit("Exit Short", "Short", stop=close * (1 + stopLossPercent)) // Cierre de posiciones if (exitLongCondition) strategy.close("Long") if (exitShortCondition) strategy.close("Short")template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6