tensortrade-org / tensortrade

An open source reinforcement learning framework for training, evaluating, and deploying robust trading agents.
https://discord.gg/ZZ7BGWh
Apache License 2.0
4.45k stars 1.01k forks source link

rsi function incorrect calculation in train_and_evaluate.ipynb #463

Closed max0x7ba closed 1 month ago

max0x7ba commented 1 year ago

Describe the current behavior rsi function in train_and_evaluate.ipynb calculation is incorrect by construction:

def rsi(price: 'pd.Series[pd.Float64Dtype]', period: float) -> 'pd.Series[pd.Float64Dtype]':
    r = price.diff()
    upside = np.minimum(r, 0).abs()
    downside = np.maximum(r, 0).abs()
    rs = upside.ewm(alpha=1 / period).mean() / downside.ewm(alpha=1 / period).mean()
    return 100*(1 - (1 + rs) ** -1)

In the code upside is actually the downside and vice versa. abs() applied without discrimination or good reason only masks the error in the calculation.

Describe the expected behavior

One fix is:

    upside = np.maximum(r, 0)
    downside = -np.minimum(r, 0) 

or, simpler:

    upside = r.clip(lower=0)
    downside = -r.clip(upper=0)
rhamnett commented 1 month ago

Fixed