areed1192 / python-trading-robot

A trading robot, that can submit basic orders in an automated fashion using the TD API.
MIT License
527 stars 311 forks source link

RSI Calculation #19

Open J-Addy opened 3 years ago

J-Addy commented 3 years ago

I'm a newer graduate (so I am not 100% confident in this), but I am pretty sure that the method used in this calculation is a standard weighted average instead of "Wilder's" weighted average. In order to get Wilder's we need to recursively calculate the average based on the previous averages. This can be fixed by changing the ewm method for up and down days to :

    # Calculate the EWMA for the Up days.
    self._frame['ewma_up'] = self._price_groups['up_day'].transform(
        lambda x: x.ewm(alpha=1.0 / period,adjust=False).mean()
    )

Also, when putting the Relative Strength index into the dataframe, the given method only returned values over 95. So I modified it to directly put the Relative strength index variable into the dataframe. Although, when I look at the given method, I am not sure why it doesn't work. Essentially no RSI values should ever be 0 or 100, so wouldn't the np.where statement parameters 0, 100 be essentially ignored?

    # Calculate the Relative Strength Index
    self._frame['rsi'] = 100.0 - (100.0 / (1.0 + relative_strength))