Skip to content

Instantly share code, notes, and snippets.

View ugik's full-sized avatar

GK ugik

View GitHub Profile
COINMETRO = "https://api.coinmetro.com"
coin = 'ETH'
price, orderAmount = getAmountETH(amount, 0)
pSize = str(orderAmount) # coin position size
headers={"Authorization":CMclient.bearerToken, 'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'orderType':'market', 'buyingCurrency':coin, 'sellingCurrency':'USD',
'sellingQty':pSize}
@ugik
ugik / gist:196ffa7b4699b463bd381fdd08675f5a
Created August 26, 2021 00:32
get_tick_and_step_size
def get_tick_and_step_size( symbol):
tick_size = None
step_size = None
symbol_info = client.get_symbol_info(symbol)
for filt in symbol_info['filters']:
if filt['filterType'] == 'PRICE_FILTER':
tick_size = float(filt['tickSize'])
elif filt['filterType'] == 'LOT_SIZE':
step_size = float(filt['stepSize'])
return tick_size, step_size
def float_precision(f, n):
n = int(math.log10(1 / float(n)))
f = math.floor(float(f) * 10 ** n) / 10 ** n
f = "{:0.0{}f}".format(float(f), n)
return str(int(f)) if int(n) == 0 else f
def order_market_buy(symbol, quantity):
tick_size, step_size = get_tick_and_step_size(symbol)
order = client.order_market_buy(symbol=symbol, quantity=float_precision(quantity, step_size))
print(symbol, 'order placed...', end="", flush=True)
return wait_confirmation(symbol, order)
# deal with possible delay in getting order confirmation
# per: https://dev.binance.vision/t/faq-error-message-order-does-not-exist/46
def wait_confirmation(symbol, order, status=ORDER_STATUS_FILLED, try_times=20):
for t in range(1,try_times): # try a few times
try:
currentOrder = client.get_order(symbol=symbol,orderId=order["orderId"])
if currentOrder['status'] == status:
return currentOrder
except Exception as e:
pass
@ugik
ugik / Supertrend code
Created July 2, 2021 17:34
Supertrend backtest
verbose = True
if True:
if True:
p = 20
arm = 6
df = pd.DataFrame(candlesticks, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df['open'] = df['open'].astype(float)
@ugik
ugik / Supertrend code
Created July 2, 2021 17:32
Supertrend backtest
verbose = True
if True:
if True:
p = 20
arm = 6
df = pd.DataFrame(candlesticks, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df['open'] = df['open'].astype(float)
def tr(data):
data['previous_close'] = data['close'].shift(1)
data['high-low'] = abs(data['high'] - data['low'])
data['high-pc'] = abs(data['high'] - data['previous_close'])
data['low-pc'] = abs(data['low'] - data['previous_close'])
tr = data[['high-low', 'high-pc', 'low-pc']].max(axis=1)
return tr
trades = []
long_trades = []
for key in results:
for trade in results[key]:
if trade[0] > 4:
if long_trades:
long_trades.append(float(trade[2]))
else:
long_trades = [float(trade[2])]
results = {}
sequences = []
sequence = 0
prev_time = None
for key in norm:
for t in norm[key]:
if not prev_time:
prev_time = t[0]
continue