-1

I already calculated the RSI for BTC which worked fine. I want to add a new column [Position] to my dataframe, which indicates if my current position is long or short. Now I am trying to do a Loop, which should give me the output, if I am Longing or Shorting BTC. The idea is the following: If RSI is in overbought conditions (>70) and it falls below 70 again, the output position should be Short. If Rsi is in oversold conditions (<30) and it starts to rise above 30, the signal should be Long. IF RSI is rising and the last signal was long, i want that all values in between are long too until a short signal arises (and vice versa).

Furthermore, the first RSI calculated on 2016-03-13 (beginning of time series) is 40.41 and rising, which is why it would be great if the first values until a short signal is generated are set to Long, which I did not integrate in my code yet, does someone know how to implement this?

My code looks like this:

import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt

from pandas_datareader import data as pdr

btc = pdr.get_data_yahoo('BTC-USD', 
                          start=datetime.datetime(2016, 2, 28), 
                          end=datetime.datetime(2020, 2, 27))

btc.tail()

btc = btc.reset_index()

def computeRSI (data, time_window):
    diff = data.diff(1).dropna()        # diff in one field(one day)

    #this preservers dimensions off diff values
    up_chg = 0 * diff
    down_chg = 0 * diff

    # up change is equal to the positive difference, otherwise equal to zero
    up_chg[diff > 0] = diff[ diff>0 ]

    # down change is equal to negative deifference, otherwise equal to zero
    down_chg[diff < 0] = diff[ diff < 0 ]

    # calculate EMAs
    up_chg_avg   = up_chg.ewm(com=time_window-1 , min_periods=time_window).mean()
    down_chg_avg = down_chg.ewm(com=time_window-1 , min_periods=time_window).mean()

    rs = abs(up_chg_avg/down_chg_avg)
    rsi = 100 - 100/(1+rs)
    return rsi

btc['RSI'] = computeRSI(btc['Close'], 14)

# get rid of the first 14 NA values
rsibtc = btc[-1448:]

rsibtc.head()

Position = []

for i in range(0,rsibtc.shape[0]):
    if rsibtc['RSI'].iloc[i] < 70 and rsibtc['RSI'].iloc[i-1] >70
        Position.append('Short')
    elif rsibtc['RSI'].iloc[i] >30 and rsibtc['RSI'].iloc[i-1]<30
        Position.append('Long')
    else Position.append(Position.iloc[i-1])

rsibtc['Position'] = Position
rsibtc.tail()

The Loop gives me an Syntax error... Could someone please advise me how to get this loop running?

Thank you very much for your support! I appreciate every idea from you guys!

All the best & stay save!

Matthias
  • 19
  • 5

1 Answers1

-1

Matthias,

It looks like the if statements in your for loop are the ones causing the syntax error. They are not properly formatted. You should have a colon after the if predicate. So I've rewritten it for you:

for i in range(0,rsibtc.shape[0]):
    if rsibtc['RSI'].iloc[i] < 70 and rsibtc['RSI'].iloc[i-1] > 70:
        Position.append('Short')
    elif rsibtc['RSI'].iloc[i] > 30 and rsibtc['RSI'].iloc[i-1] < 30:
        Position.append('Long')
    else:
        Position.append(Position.iloc[i-1])
takinbo
  • 105
  • 2