Skip to content

Instantly share code, notes, and snippets.

View ugik's full-sized avatar

GK ugik

View GitHub Profile
# setup EMA indicator values
def setup_ema():
ticksAgo = datetime.datetime.now() - timedelta(hours= 24)
candlesticks = client.get_historical_klines(EXCHANGE, Client.KLINE_INTERVAL_3MINUTE, str(ticksAgo))
for candle in candlesticks:
del candle[-6:]
ema = pd.DataFrame(candlesticks, columns=['date', EXCHANGE+'_O', EXCHANGE+'_H', \
def telegram_EMA(sma, back=50, show=False):
file = 'EMAs.jpg'
ax = sma[-back:].plot(y="BTCUSDT_C", kind="line", color="k", \
linewidth=3, use_index=True, x_compat=True, \
rot=25, linestyle='-', figsize=(6, 5))
sma[-back:].plot(y="EMA20", kind="line", ax=ax, color="y", \
x_compat=True, rot=25, linestyle=':', figsize=(6, 5))
sma[-back:].plot(y="EMA50", kind="line", ax=ax, color="r", \
file = "stats.jpg"
# create your own numerical lists here...
days = [30, 21, 14, 5, 2]
data = [10, 19, 6, 34, 12]
labels = days
plt.xticks(range(len(data)), labels)
plt.xlabel('Days')
plt.ylabel('% Position Gain')
dateFrom = '2022-08-05 12:00:00'
positionData = getPositionInfo(startDate=timezoneGMT(parser.parse(dateFrom)), exchange='BTCUSDT')
file = coin +'.jpg'
ax = plt.gca()
dateFrom = str(timezoneGMT(parser.parse(dateFrom)))
positionData.plot(kind='line',x='date',y=coin[:3],ax=ax, marker='.', \
title= coin[:3] +" since "+dateFrom)
plt.savefig(file, bbox_inches='tight')
client = Client(config.BINANCE_API_KEY, config.BINANCE_API_SECRET, tld='us')
def getPositionInfo(startDate=None, exchange='BTCUSDT'):
if not startDate:
return
candlesticks = client.get_historical_klines(exchange, Client.KLINE_INTERVAL_1HOUR, str(startDate))
# trim each candle
for candle in candlesticks:
del candle[-6:]
buyingQty, sellingQty = getBuyingDataETH(side='SELL')
# SHORT SELL order
headers={"Authorization":CMclient.bearerToken, 'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'orderType':'limit', 'buyingCurrency':'USD', 'sellingCurrency':'ETH',
'buyingQty':buyingQty, 'sellingQty':sellingQty, 'margin':'true', 'userData':'foo'}
response = requests.request("POST", f'{COINMETRO}/exchange/orders/create', headers=headers, data=payload)
responseJson = json.loads(response._content)
orderID = responseJson['orderID']
print(responseJson)
payload = {'orderType':'market', 'buyingCurrency':'USD', 'sellingCurrency':coin,
'sellingQty':pSize, 'margin':'true'}
def getBuyingDataETH(side, usd=None, eth=None):
book = CMclient.get_full_book('ETHUSD')
topBid = float(list(book['book']['bid'].items())[0][0])
topAsk = float(list(book['book']['ask'].items())[0][0])
spread = topAsk-topBid
print('bid', topBid, 'ask', topAsk, 'spread', spread, '%', spread/topAsk*100)
if side=='SELL':
sellingQty = eth
buyingQty = eth*topAsk
payload = {'orderType':'limit', 'buyingCurrency':'ETH', 'sellingCurrency':'USD',
'buyingQty':buyingQty, 'sellingQty':sellingQty}
def getAmountETH(positionSize, timeFrom):
checkToken()
latestPrice = CMclient.get_latest_trades(pair='ETHUSD', From=timeFrom)['tickHistory'][-1]['price']
buyAmount = float("{:.4f}".format(positionSize/latestPrice))
return latestPrice, buyAmount