def buy_limit_msg(self,market, quantity, rate): try: res = BinanceAPI(api_key,api_secret).buy_limit(market, quantity, rate) if res['orderId']: buy_info = "报警:币种为:{cointype}。买单价为:{price}。买单量为:{num}".format(cointype=market,price=rate,num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},买单失败.api返回内容为:{reject}".format(cointype=market,reject=res['msg']) self.dingding_warn(error_info)
def buy_market_msg(self, market, quantity): try: res = BinanceAPI(api_key, api_secret).buy_market(market, quantity) if res['orderId']: buy_info = "报警:币种为:{cointype}。买单量为:{num}".format( cointype=market, num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},买单失败.".format(cointype=market) self.dingding_warn(error_info)
def sell_limit_msg(self, market, quantity, rate): try: res = BinanceAPI(api_key, api_secret).sell_limit(market, quantity, rate) if res['orderId']: buy_info = "报警:币种为:{cointype}。卖单价为:{price}。卖单量为:{num}".format( cointype=market, price=rate, num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},卖单失败".format(cointype=market) self.dingding_warn(error_info)
class Binance: def __init__(self): self.client = BinanceAPI(config.api_key, config.api_secret) def balances(self): balances = self.client.get_account() for balance in balances['balances']: if float(balance['locked']) > 0 or float(balance['free']) > 0: print('%s: %s' % (balance['asset'], balance['free'])) def balance(self, asset="BTC"): balances = self.client.get_account() balances['balances'] = { item['asset']: item for item in balances['balances'] } print(balances['balances'][asset]['free']) def orders(self, symbol, limit): orders = self.client.get_open_orders(symbol, limit) print(orders) def tickers(self): return self.client.get_all_tickers() def server_time(self): return self.client.get_server_time() def openorders(self): return self.client.get_open_orders() def profits(self, asset='BTC'): coins = self.client.get_products() for coin in coins['data']: if coin['quoteAsset'] == asset: orders = self.client.get_order_books(coin['symbol'], 5) lastBid = float(orders['bids'][0][0]) # last buy price (bid) lastAsk = float(orders['asks'][0][0]) # last sell price (ask) profit = (lastAsk - lastBid) / lastBid * 100 print('%.2f%% profit : %s (bid:%.8f-ask%.8f)' % (profit, coin['symbol'], lastBid, lastAsk))
def sell_limit_msg(self,market, quantity, rate): ''' :param market: :param quantity: 数量 :param rate: 价格 :return: ''' try: res = BinanceAPI(api_key,api_secret).sell_limit(market, quantity, rate) if res['orderId']: buy_info = "报警:币种为:{cointype}。卖单价为:{price}。卖单量为:{num}".format(cointype=market,price=rate,num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},卖单失败.api返回内容为:{reject}".format(cointype=market,reject=res['msg']) self.dingding_warn(error_info+str(res)) return res
def sell_market_msg(self, market, quantity): ''' :param market: :param quantity: 数量 :param rate: 价格 :return: ''' try: res = BinanceAPI(api_key, api_secret).sell_market(market, quantity) if res['orderId']: buy_info = "报警:币种为:{cointype}。卖单量为:{num}".format( cointype=market, num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},卖单失败".format(cointype=market) self.dingding_warn(error_info + str(res)) return res
def buy_limit_future_msg(self,market, quantity, price,profit_usdt=None): ''' 合约做多单,带有钉钉消息 :param market: 交易对 :param quantity: 数量 :param price: 价格 :return: ''' try: res = BinanceAPI(api_key,api_secret).limit_future_order('BUY', market, quantity, price) if res['orderId']: buy_info = "报警:币种为:{cointype}。买入做多价格为:{price}。数量为:{num}。盈利USDT数为:{profit_usdt}".format(cointype=market,price=price,num=quantity,profit_usdt=abs(profit_usdt)) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},卖出空单失败.api返回内容为:{reject}".format(cointype=market,reject=res['msg']) self.dingding_warn(error_info+str(res)) return res
def sell_limit_future_msg(self,market, quantity, price): ''' 合约做空单,带有钉钉消息 :param market: 交易对 :param quantity: 数量 :param price: 价格 :return: ''' try: res = BinanceAPI(api_key,api_secret).limit_future_order('SELL', market, quantity, price) if res['orderId']: buy_info = "报警:币种为:{cointype}。卖出做空价格为:{price}。数量为:{num}".format(cointype=market,price=price,num=quantity) self.dingding_warn(buy_info) return res except BaseException as e: error_info = "报警:币种为:{cointype},卖出做空空单失败.api返回内容为:{reject}".format(cointype=market,reject=res['msg']) self.dingding_warn(error_info+str(res)) return res
# -*- coding: utf-8 -*- from app.BinanceAPI import BinanceAPI from app.authorization import api_key, api_secret from data.runBetData import RunBetData from app.dingding import Message import time binan = BinanceAPI(api_key, api_secret) runbet = RunBetData() msg = Message() class Run_Main(): def __init__(self): self.coinType = runbet.get_cointype() # 交易币种 pass def loop_run(self): while True: cur_market_price = binan.get_ticker_price( runbet.get_cointype()) # 当前交易对市价 grid_buy_price = runbet.get_buy_price() # 当前网格买入价格 grid_sell_price = runbet.get_sell_price() # 当前网格卖出价格 quantity = runbet.get_quantity() # 买入量 step = runbet.get_step() # 当前步数 if grid_buy_price >= cur_market_price: # 是否满足买入价 res = msg.buy_limit_msg(self.coinType, quantity, grid_buy_price) if res['orderId']: # 挂单成功 runbet.modify_price(grid_buy_price,
# wechat : findpanpan from data.runBetData import RunBetData from app.BinanceAPI import BinanceAPI from app.authorization import api_key,api_secret from data.calcIndex import CalcIndex binan = BinanceAPI(api_key,api_secret) index = CalcIndex() runbet = RunBetData() tmp_data = { "runBet": { "next_buy_price": 0, "grid_sell_price": 0, "step": 0 }, "config": { "profit_ratio": 0, "double_throw_ratio": 0, "cointype": "", "quantity": [] } } class CreateData(): def __init__(self): pass if __name__ == "__main__":
def __init__(self): self.client = BinanceAPI(config.api_key, config.api_secret)
# -*- coding: utf-8 -* from app.BinanceAPI import BinanceAPI from app.authorization import api_key, api_secret from data.runBetData import RunBetData import time binan = BinanceAPI(api_key, api_secret) runbet = RunBetData() # def loop_fun(): # while True: # if runbet.get_buy_price() >= binan.get_ticker_price(runbet.get_cointype()): # order_id = binan.buy_limit(runbet.get_cointype(),runbet.get_quantity(),runbet.get_buy_price()) # runbet.modify_price(runbet.get_buy_price(),runbet.get_step()+1) # time.sleep(60*3) # 挂单后,停止运行3分钟 # elif runbet.get_sell_price() < binan.get_ticker_price(runbet.get_cointype()): # order_id = binan.sell_limit(runbet.get_cointype(),runbet.get_quantity(), runbet.get_sell_price()) # runbet.modify_price(runbet.get_sell_price(),runbet.get_step()-1) # time.sleep(60*3) # 吃单后,停止运行3分钟 # else: # print("当前 {cointype} 市价:{market_price}。未能满足交易,继续运行".format(cointype=runbet.get_cointype()[:-4],market_price = binan.get_ticker_price(runbet.get_cointype()))) # time.sleep(1) if __name__ == "__main__": # try: # loop_fun() # except Exception as e: # print(str(e)) print(binan.get_klines("WINGUSDT"))