Ejemplo n.º 1
0
#for ticker in tickers:    
#    fed = DataReader(ticker,data_source,start,end)
#    print(fed.tail(10))
#    fed.plot(title = ticker,figsize = (15,5))
#    plt.show()

sp = DataReader(tickers[7],data_source,start,end)
print(sp.tail(10))
sp.plot(title = tickers[7],figsize = (15,5))
plt.show()

num = 233
risk_num = 60


sp1 = sp.copy()
sample = sp1.dropna()
ret_cum = sample / sample.iloc[0,:]
ret_per = sample / sample.shift(1) - 1
print(ret_cum.tail())
print(ret_per.tail())
ret_cum.plot(title = "cumulative return")

mn  = ret_per.rolling(window = num).mean()
#(rolling_mean(ret_per,window = num)
stdev  = ret_per.rolling(window = num).std()
mn.plot(title = "rolling mean")
stdev.plot(title = "rolling std")
sharp_roll = 252 ** 0.5 * (mn / stdev)
sharp_roll.plot(title = "rolling sharp")
plt.show() 
Ejemplo n.º 2
0
                df['maPos'].iloc[row] = -1

            if (df['OBV'].iloc[row] > 0):
                df['obvPos'].iloc[row] = 1
            else:
                df['obvPos'].iloc[row] = -1

            if (df['CCI'].iloc[row] > 0):
                df['cciPos'].iloc[row] = 1
            else:
                df['cciPos'].iloc[row] = -1

        # Clean up dataframe
        frame = pd.DataFrame()
        frame['PC'] = df['Adj Close'].pct_change()
        db = df.copy()
        df = df.drop(columns=[
            'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume',
            'upper_band', 'lower_band', 'middle_band', 'lower_band', 'macd',
            'macdsignal', 'macdhist', 'RSI', 'Momentum', 'Z-Score', 'SMA',
            'EMA', 'OBV', 'CCI'
        ])
        df['pos'] = df.mean(axis=1)
        df['PC'] = frame['PC']

        db = db.reset_index()
        db['Buy'] = np.where((df['pos'] < 0.7), 1, 0)
        db['Sell'] = np.where((df['pos'] < -0.3), 1, 0)

        db['Buy_ind'] = np.where((db['Buy'] > db['Buy'].shift(1)), 1, 0)
        db['Sell_ind'] = np.where((db['Sell'] > db['Sell'].shift(1)), 1, 0)