0

I made a new pandas DataFrame by adding the last 15 items, minus the last item, from the binance historical. And every minute I add the new data to the newly made DataFrame. This works great and gives the following output after a few minutes:

              open           close  ...            high                 time
0   61095.98000000  61099.44000000  ...  61115.93000000  2021-11-05 15:34:00
1   61099.45000000  61038.01000000  ...  61104.11000000  2021-11-05 15:35:00
2   61038.00000000  61010.96000000  ...  61080.05000000  2021-11-05 15:36:00
3   61010.97000000  60978.94000000  ...  61017.93000000  2021-11-05 15:37:00
4   60977.80000000  60945.65000000  ...  60990.54000000  2021-11-05 15:38:00
5   60945.65000000  60900.00000000  ...  60958.00000000  2021-11-05 15:39:00
6   60900.00000000  60991.44000000  ...  60993.71000000  2021-11-05 15:40:00
7   60991.45000000  60888.04000000  ...  60996.14000000  2021-11-05 15:41:00
8   60888.04000000  60880.94000000  ...  60906.98000000  2021-11-05 15:42:00
9   60880.95000000  60810.99000000  ...  60880.95000000  2021-11-05 15:43:00
10  60810.99000000  60763.94000000  ...  60864.74000000  2021-11-05 15:44:00
11  60763.95000000  60872.14000000  ...  60896.86000000  2021-11-05 15:45:00
12  60873.17000000  60908.84000000  ...  60934.22000000  2021-11-05 15:46:00
13  60908.84000000  60844.19000000  ...  60914.94000000  2021-11-05 15:47:00
14  60844.18000000  60870.40000000  ...  60879.34000000  2021-11-05 15:48:00
15  60870.39000000  60845.01000000  ...  60877.53000000  2021-11-05 15:49:00
16  60845.02000000  60900.62000000  ...  60920.64000000  2021-11-05 15:50:00
17  60900.63000000  60873.40000000  ...  60901.29000000  2021-11-05 15:51:00

[18 rows x 5 columns]

Now I want to add the stochastic oscillator to this DataFrame. I tried this by adding the following line to my code:

df.ta.stoch(high='high', low='low', k=14, d=3, append=True)

However after adding this code, my DataFrame won't show anymore by the print statement and neither does the stochastic when I try to print it. How do I make the stochastic work with my self made DataFrame? If you have any questions, ask away! Thanks in advance!

My full code below:

#import threading
import pandas_ta as ta
import websocket
import json
import pandas as pd
from binance import Client

client = Client('5Z5VtpcArDgm525AC6sUoy8TJer4tlel4Twt1ENf7OIzeLB2qx6oaLICHL9jVKoa', 'e641miivoiguEU3gOKjuYEvVKdFNazehtovZtJKdQXb2NVxwTeo1AQ2TBDArocWU')
historical = client.get_historical_klines('BTCUSDT', Client.KLINE_INTERVAL_1MINUTE, '11/05/2021', limit=1000)
SOCKET = "wss://stream.binance.com:9443/ws/btcusdt@kline_1m"

hist_df = pd.DataFrame(historical)
hist_df.columns = ['Open time','Open', 'High', 'Low', 'Close', 'Volume', 'Close time', 'Quote Asset Volume', 'Number of trades', 'TB Base volume','Tb Quote volume','Ignore']



def on_open(ws):
    print('opened connection')

def on_close(ws):
    print('closed connection')

dic = {'open': [], 'close': [], 'low': [], 'high': [], 'time': []}

history_open_price = hist_df['Open'][-15:-1]
history_close_price = hist_df['Close'][-15:-1]
history_highest_price = hist_df['High'][-15:-1]
history_lowest_price = hist_df['Low'][-15:-1]
history_time = pd.to_datetime(hist_df['Open time']/1000, unit='s')[-15:-1]

for a, b in history_open_price.items():
    dic['open'].append(b)
for a, b in history_close_price.items():
    dic['close'].append(b)
for a, b in history_highest_price.items():
    dic['high'].append(b)
for a, b in history_lowest_price.items():
    dic['low'].append(b)
for a, b in history_time.items():
    dic['time'].append(str(b))

def on_message(ws, message):
    json_message = json.loads(message)
    candle = json_message['k']

    open_price = candle['o']
    close_price = candle['c']
    highest_price = candle['h']
    lowest_price = candle['l']
    status = candle['x']
    open_time = pd.to_datetime(candle['t']/1000, unit='s')
    if status:
        dic['open'].append(open_price)
        dic['close'].append(close_price)
        dic['high'].append(highest_price)
        dic['low'].append(lowest_price)
        dic['time'].append(str(open_time))
        df = pd.DataFrame(dic)
        # df.ta.stoch(high='high', low='low', k=14, d=3, append=True)

        print(df)

ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
Jellyfish
  • 67
  • 9

1 Answers1

0

You can add it just like this:

stoch=ta.stoch(df["high"],df["low"],df["close"],14,3,1)
df["stoch_K"] = stoch['STOCHk_14_3_1']
df["stoch_D"] = stoch['STOCHd_14_3_1']
Zea
  • 145
  • 1
  • 6