-
So I've hacked together something that I thought should work, I could use some help finding where I went wrong. currently the solution is to write a temporary csv file with history, then add new lines, read csv into a dataframe, then plot all. The program runs , drops a csv, the candles update , but are messed up and the opens and closes don't line up. play with my code, tell me all the things I did wrong. Thanks :) import pandas as pd
import finplot as fplt
import requests
import json
def get_bars(symbol, interval, limit):
url = 'https://api.binance.com/api/v1/klines?symbol=' + symbol + '&interval=' + interval + '&limit=' + limit
data = json.loads(requests.get(url).text)
cur_data = pd.DataFrame(data)
cur_data.columns = ['time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_time',
'qav', 'num_trades', 'taker_base_vol', 'taker_quote_vol', 'ignore']
cur_data['time'] = pd.to_datetime(cur_data['time'], unit='ms', origin='unix')
cur_data['Open'] = pd.to_numeric(cur_data["Open"])
cur_data["Close"] = pd.to_numeric(cur_data["Close"])
cur_data['High'] = pd.to_numeric(cur_data["High"])
cur_data['Low'] = pd.to_numeric(cur_data["Low"])
cur_data['Volume'] = pd.to_numeric(cur_data["Volume"])
cur_data['Close_time'] = pd.to_datetime(cur_data['Close_time'], unit='ms', origin='unix')
cur_data['qav'] = pd.to_numeric(cur_data["qav"])
cur_data['num_trades'] = pd.to_numeric(cur_data["num_trades"])
cur_data['taker_base_vol'] = pd.to_numeric(cur_data["taker_base_vol"])
cur_data['taker_quote_vol'] = pd.to_numeric(cur_data["taker_quote_vol"])
cur_data['ignore'] = pd.to_numeric(cur_data["ignore"])
cur_data.to_csv('bars.csv', mode='a', header=False, index=False)
return cur_data
def get_bars_historic(symbol, interval, limit):
url = 'https://api.binance.com/api/v1/klines?symbol=' + symbol + '&interval=' + interval + '&limit=' + limit
data = json.loads(requests.get(url).text)
histdata = pd.DataFrame(data)
histdata.columns = ['time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_time', 'qav', 'num_trades',
'taker_base_vol', 'taker_quote_vol', 'ignore']
histdata['time'] = pd.to_datetime(histdata['time'], unit='ms', origin='unix')
histdata['Open'] = pd.to_numeric(histdata["Open"])
histdata["Close"] = pd.to_numeric(histdata["Close"])
histdata['High'] = pd.to_numeric(histdata["High"])
histdata['Low'] = pd.to_numeric(histdata["Low"])
histdata['Volume'] = pd.to_numeric(histdata["Volume"])
histdata['Close_time'] = pd.to_datetime(histdata['Close_time'], unit='ms', origin='unix')
histdata['qav'] = pd.to_numeric(histdata["qav"])
histdata['num_trades'] = pd.to_numeric(histdata["num_trades"])
histdata['taker_base_vol'] = pd.to_numeric(histdata["taker_base_vol"])
histdata['taker_quote_vol'] = pd.to_numeric(histdata["taker_quote_vol"])
histdata['ignore'] = pd.to_numeric(histdata["ignore"])
histdata.to_csv('bars.csv', header=True, index=False)
return histdata
def calc_bollinger_bands(df):
r = df['Close'].rolling(20)
df['bbh'] = r.mean() + 2*r.std()
df['bbl'] = r.mean() - 2*r.std()
def update():
get_cur_data = get_bars('ETHUSDT', '1m', '1')
current_data = get_cur_data[['time', 'Open', 'Close', 'High', 'Low', 'Volume', 'Close_time']]
df_total = pd.read_csv('bars.csv').drop_duplicates('time').reset_index(drop=True)
df_total['time'] = pd.to_datetime(df_total['time'])
df_total['Close_time'] = pd.to_datetime(df_total['Close_time'])
print('current data #########################################')
print(current_data.to_string())
print('######################################################')
print('Historic data ########################################')
print(historic_data.tail().to_string())
print('######################################################')
print('Total data ########################################')
print(df_total.tail().to_string())
print('######################################################')
candlesticks = df_total['time Open Close High Low'.split()]
calc_bollinger_bands(df_total)
#volumes = DataStruct['time Open Close Volume'.split()
bollband_hi = df_total['time bbh'.split()]
bollband_lo = df_total['time bbl'.split()]
if not plots:
global ax, ax2
fplt.plot(df_total['time'], df_total['Close'].rolling(9).mean(), ax=ax, color='#ff0000', legend='ma-9')
fplt.plot(df_total['time'], df_total['Close'].rolling(24).mean(), ax=ax, color='#00ff00', legend='ma-24')
fplt.plot(df_total['time'], df_total['Close'].rolling(100).mean(), ax=ax, color='#0000ff', legend='ma-100')
fplt.plot(df_total['time'], df_total['Close'].rolling(200).mean(), ax=ax, color='#005555', legend='ma-200')
plots.append(fplt.candlestick_ochl(candlesticks))
plots.append(fplt.plot(bollband_hi, color='#4e4ef1'))
plots.append(fplt.plot(bollband_lo, color='#4e4ef1'))
#plots.append(fplt.volume_ocv(volumes, ax=ax.overlay()))
macd = df_total.Close.ewm(span=12).mean() - df_total.Close.ewm(span=26).mean()
signal = macd.ewm(span=9).mean()
df_total['macd_diff'] = macd - signal
fplt.volume_ocv(df_total[['time', 'Open', 'Close', 'macd_diff']], ax=ax2, colorfunc=fplt.strength_colorfilter)
fplt.plot(macd, ax=ax2, legend='MACD')
fplt.plot(signal, ax=ax2, legend='Signal')
else:
# every time after we just update the data sources on each plot
plots[0].update_data(df_total)
plots[1].update_data(bollband_hi)
plots[2].update_data(bollband_lo)
#plots[3].update_data(volumes)
fplt.background = '#222222'
fplt.foreground = '#ffffff'
fplt.odd_plot_background = '#222222'
fplt.cross_hair_color = '#00ff00'
fplt.candle_bull_color = '#00ff00'
fplt.candle_bull_body_color = '#007700'
fplt.candle_bear_color = '#ff0000'
fplt.volume_bull_color = fplt.candle_bull_color
fplt.volume_bull_body_color = fplt.candle_bull_body_color
fplt.volume_bear_color = fplt.candle_bear_color
plots = []
# clear the bars.csv
f = open("bars.csv", "w")
f.truncate()
f.close()
ax, ax2 = fplt.create_plot('Realtime ETHUSDT 1m (Binance)', init_zoom_periods=100, maximize=True, rows=2)
ax.set_visible(crosshair=True, xaxis=True, yaxis=True, xgrid=True, ygrid=True)
get_hist_data = get_bars_historic('ETHUSDT', '1m', '200')
historic_data = get_hist_data[['time', 'Open', 'Close', 'High', 'Low', 'Volume', 'Close_time']]
fplt.timer_callback(update, 5.0)
fplt.show() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
You can get rid of that wall of numeric conversions in your functions with a one liner,
And it seems that when you call get_bars() in the update function you aren't doing anything with the returned data? Then you have to open the csv again to get the data you just return with the same function. And in that save to csv you are saving the same data multiple times? |
Beta Was this translation helpful? Give feedback.
-
I've done pretty good with ETH & ADA this year, up all in all, not by crazy
amounts. Definitely losing some trades. I don't have the balls to hold for
10x returns. Sometimes the devil gets to me and I play with leverage and
lose lol. Did 300% a couple weeks ago on ETH with 20x, woo big win then two
days later lose all but 20% by missing the chart for a bit. I play for
accumulation these days. Really regret selling my 5 coins a handful of
years ago, I mean I made good profit but got 100% out of crypto at $3.5k
thinking it needed some downtime before going again.... then it went
straight for 10k , facepalm moment. Always hold, this shits going places!
…On Wed., Mar. 3, 2021, 1:08 a.m. Jonas Byström, ***@***.***> wrote:
I use a bot which is very trivial, but was able to 13x in a month, which I
thought was really good. But it lost it equally quickly. I think in the
long run I prefer it doing something stupid from time to time, rather than
me making stupid, emotional, irrational decisions all the time. :) I guess
most people trading are much better at that, so I'm probably not catering
to the bot peoples. What profits are you able to make on what instruments?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#132 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTE57Y4IB62W6NAM66CEXDTBXVAZANCNFSM4YJOQHNQ>
.
|
Beta Was this translation helpful? Give feedback.
You can get rid of that wall of numeric conversions in your functions with a one liner,
data = [pd.to_numeric(i) for i in data]
And it seems that when you call get_bars() in the update function you aren't doing anything with the returned data? Then you have to open the csv again to get the data you just return with the same function.
And in that save to csv you are saving the same data multiple times?