Skip to content
All library documents

KDJ Long Entries with ATR Stops and Momentum-Based Exits

Article Strategy library · Author: Jesse community

Summary

This daily-chart strategy uses the KDJ oscillator to open long positions when its J component is above both K and D, and it does not initiate short trades. At entry, it calculates position quantity from a stated risk allocation and places a stop at a multiple of the 14-period average true range (ATR) below the entry price.

Exit logic has two branches: a profitable long position is liquidated if the latest J reading is lower than its prior reading, while a position that is not profitable is closed when J falls below K and D. The prose characterizes the profitable-trade rule as responding to a decline in J, and the code compares current and previous oscillator readings. No market, testing period, or backtest results are supplied, so the strategy's performance and robustness are unknown. The implementation also uses a logical expression for the K/D comparison that may not behave like a direct comparison against both values, an important detail to verify before relying on its signals.

Key ideas

  • The strategy opens long positions when KDJ's J component exceeds both K and D.
  • It sizes positions using a stated percentage of available capital and an ATR-based stop distance.
  • The example uses a 14-period ATR and sets the stop two ATR units below entry.
  • A profitable trade exits when J declines from its previous value, while a losing trade exits on a downside KDJ condition.
  • The document reports no tested market, backtest period, or performance results.

Tags

Full text
# KDJstrategy


# KDJstrategy









Simple KDJ Indicator Strategy
1D Timeframe
Repo: https://github.com/matty5690/example-strategies
Opens position when J value is above K and D values and closes when J value is below K and D values

## Source (MIT)

```python
"""
Simple KDJ Indicator Strategy
1D Timeframe
Repo: https://github.com/matty5690/example-strategies
Opens position when J value is above K and D values and closes when J value is below K and D values



"""

from jesse.strategies import Strategy
import jesse.indicators as ta
from jesse import utils


class KDJstrategy(Strategy):

    @property
    def KDJIndicator(self): #KDJ Indicator
        return ta.kdj(self.candles) #returns namedtuple KDJ(k,d,j) with default parameters

    @property
    def LastKDJ(self):
        return ta.kdj(self.candles[:-1]) #returns the previous candles KDJ values

    @property
    def atr(self):
        return ta.atr(self.candles, period = 14) #14 period ATR used for calculating stop loss

    def should_long(self) -> bool:
        #long signal if J value crosses above K and D values
        return self.KDJIndicator[2] > (self.KDJIndicator[0] and self.KDJIndicator[1])

    def should_short(self) -> bool:
        return False

    def should_cancel(self) -> bool:
        return True

    def go_long(self):
        # Open long position and risk 5% of  available capital
        risk_perc = 5
        entry = self.price
        stop = entry - 2 * self.atr #trailing stop loss of 2x ATR
        qty = utils.risk_to_qty(self.capital, risk_perc, entry, stop)
        self.buy = qty, self.price
        self.stop_loss = qty, stop

    def go_short(self):
        return False

    def update_position(self):
        #if position is in profit it will close if the J value at close is less than the previous J value
        if self.is_long and self.position.pnl_percentage > 0 and self.LastKDJ[2] > self.KDJIndicator[2]:
            self.liquidate()
        #if position is not in profit and the J value crosses under the K and D values then the position will close
        elif self.is_long and self.KDJIndicator[2] < (self.KDJIndicator[0] and self.KDJIndicator[1]):
            self.liquidate()

```

Shown in full with attribution under the source's licence. Licence: MIT

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.