def screen(): client = Client("api-key", "api-secret", {"verify": False, "timeout": 20}) positiveCoin = get_positive_coin.positiveCoin bullishKScoin = [] for m in positiveCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_30MINUTE) if candles[-2][1] < candles[-2][4] <= candles[-1][1] < candles[-1][4]: bullishKScoin.append(m) macdCoin = [] for m in bullishKScoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_1HOUR) close = [] for n in candles: close.append(float(n[4])) mac = macd(close, 12, 26) macs = macds(mac) mach = macdh(mac, macs) # if mach[-1] > 0 and (macs[-1] - macs[-2]) > 0 and (mac[-1] - mac[-2]) > 0: # macdCoin.append(m) if mach[-1] > 0 and (macs[-1] - macs[-2]) > 0 and (mac[-1] - mac[-2]) > 0: macdCoin.append(m) bollCoin = [] for m in macdCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_1HOUR) close = [] for n in candles: close.append(float(n[4])) lb = lbb(close, 20, 2) mb = mbb(close, 20, 2) ub = ubb(close, 20, 2) if lb[-1] < close[-1] < ((0.8 * ub[-1]) + (0.2 * mb[-1])): bollCoin.append(m) maxDemandRatio = 0 buyingCoin = '' for m in bollCoin: depth = client.get_order_book(symbol=m) buyingVol = 0 sellingVol = 0 for n in depth['bids'][0:20]: buyingVol = buyingVol + float(n[1]) for n in depth['asks'][0:20]: sellingVol = sellingVol + float(n[1]) demandRatio = buyingVol / sellingVol print(demandRatio) print(maxDemandRatio) if demandRatio > maxDemandRatio: maxDemandRatio = demandRatio buyingCoin = m print(bullishKScoin) print(macdCoin) print(bollCoin) print(buyingCoin) return buyingCoin
def mbb(self, df): period = 20 if len(df) < period: period = len(df) // 2 data = mbb(df, period) return (data)
# In[5]: data = socket.get_candles(instrument = 'GBP/USD', period = 'D1', start = dt.datetime(2016,1,1), end = dt.datetime(2018, 6, 10)) # In[6]: #Define useful variables data['upper_band'] = ubb(data['askclose'], period = 20) data['mid_band'] = mbb(data['askclose'], period = 20 ) data['lower_band'] = lbb(data['askclose'], period = 20 ) data['percent_b'] = percent_b(data['askclose'], period =20) data # In[7]: fig = plt.figure(figsize=(12,8)) ax1 = fig.add_subplot(111, xlabel = 'Date',ylabel='Close') data['askclose'].plot(ax=ax1, color='r', lw=1) data['upper_band'].plot(ax=ax1, color = 'b', lw= 1)
def middle_bollinger_band(data, period): return mbb(data, period)