def createThreading(Cases, num, *args): ''' 多线程触发请求的入口 :param Cases: :param num:生成线程数 :param args: :return: ''' if type(Cases) == dict: Server = numpy.where(Cases['Server'] == 'default', Config.Default['Server'], Cases['Server']) for Case in Cases['Cases']: if args: if Case['CaseName'] in args: Log.print_info(1, '已匹配到指定测试用例:{0}'.format(Case['CaseName'])) dealCasesMulThreading(Server, Case, Cases, num) else: dealCasesMulThreading(Server, Case, Cases, num) elif type(Cases) == list: for cl in Cases: Server = numpy.where(cl['Server'] == 'default', Config.Default['Server'], cl['Server']) for Case in cl['Cases']: if args: if Case['CaseName'] in args: Log.print_info( 1, '已匹配到指定测试用例:{0}'.format(Case['CaseName'])) dealCasesMulThreading(Server, Case, cl, num) else: dealCasesMulThreading(Server, Case, cl, num)
def Buy(price): """ 购买:根据价格和购买单位计算出需要花费多少 quote,把 quote 从原始资金拿到冻结资金中 注意:这里的价格是已经加了0.3 usdt 后的价格 """ global __totalQuote, __tradePart, __quoteBalance, __frozeQuoteBalance # 计算每次买入多少 usdt 的 btc costQuote = MathUtil.GetPrecision(__totalQuote / __tradePart, 2) if __quoteBalance > costQuote: # 资金足够,可以购买 __LogBalance("Before Buy Action") __quoteBalance -= costQuote __frozeQuoteBalance += costQuote # 计算要花费的 usdt 按现在的价格能买多少 btc buyAmount = costQuote / price buyAmount = MathUtil.GetPrecision(buyAmount, 4) Log.Print("BM - Buy Info: price:{} costQuote:{} buyAmount:{}".format( price, costQuote, buyAmount)) Log.Info( Const.logFile, "BM - Buy Info: price:{} costQuote:{} buyAmount:{}".format( price, costQuote, buyAmount)) __SaveBalance() __LogBalance("After Buy Action") return costQuote, buyAmount # 返回以 costQuote usdt 买入 buyAmount 的 btc return -1, -1
def run_queries(discs, result_list, language, fingerprint, mixed, do_quick_match): # Try a text-based match first. (match1, albums1, arts1) = run_query_on_discs(discs, result_list, language, fingerprint, mixed, do_quick_match) final_match = match1 # If the result looks shoddy, try with fingerprinting. if albums1 > len(discs) or match1 < 75 or arts1 == 0: Log('Not impressed, trying the other way (fingerprinting: %s)' % (not fingerprint)) other_result_list = [] (match2, albums2, arts2) = run_query_on_discs(discs, other_result_list, language, not fingerprint, mixed, do_quick_match) if match2 > match1 or (match2 == match1 and (albums2 < albums1 or arts2 > arts1)): Log('This way gave a better match, keeping.') result_list[:] = other_result_list final_match = match2 return final_match
def postMul(url, header, body): ''' post请求,多线程场景 :param url: :param header: :param body: 直接传字典数据即可 :return: ''' Log.print_info( 1, 'INSTRUMENTATION_STATUS: thread start={0}'.format( threading.currentThread().getName())) start_ = int(round(time.time() * 1000)) res = requests.post(url, headers=header, json=body) end_ = int(round(time.time() * 1000)) Log.print_info( 1, 'INSTRUMENTATION_STATUS: thread={0},time={1}'.format( threading.currentThread().getName(), end_ - start_)) if res.status_code == 200: Log.print_info( 1, 'INSTRUMENTATION_STATUS: thread={0}, res={1}'.format( threading.currentThread().getName(), json.loads(res.text))) else: res = {} res['code'] = -1 res['msg'] = '接口404' Log.print_info( 1, 'INSTRUMENTATION_STATUS: thread={0},res={1}'.format( threading.currentThread().getName(), res)) Log.print_info( 1, 'INSTRUMENTATION_STATUS: thread={0},result=Pass'.format( threading.currentThread().getName()))
def InitSystem(): """ 加载密钥,初始化交易所服务 获取Account id,赋值到交易所服务 """ configFile = "Config.json" try: configJsonStr = IOUtil.ReadTextFromFile(configFile) configData = json.loads(configJsonStr) access_key = configData['access_key'] secret_key = configData['secret_key'] HuobiServices.init_key(access_key, secret_key) accounts = HuobiServices.get_accounts() account_id = accounts['data'][0]['id'] HuobiServices.init_account(account_id) HuobiServices.init_symbols() except Exception as e: logStr = "Fatal Error: Init System Faild!\n Exception:{}".format(e) Log.Print(logStr) Log.Info(Const.logFile, logStr) sys.exit()
def Start(): t = threading.Thread(target=__WorkThread) t.setDaemon(True) t.start() logStr = "DataDownloader Started!" Log.Print(logStr) Log.Info(Const.logFile,logStr)
def on_modified(self, event): Log.i('监听到文件: yaml 发生了变化') try: gen_page_py() except Exception as e: Log.e('\n!!!!!!!---pages.yaml---!!!!!!\n解析文件 pages.yaml 错误\n' '请到{}路径下检查修改后重新保存.'.format(self.watch_path))
def create(Cases, *args): ''' 校验出入参数,并走执行case的步骤 :param Cases: :param args: :return: ''' if type(Cases) == dict: Server = numpy.where(Cases['Server'] == 'default', Config.Default['Server'], Cases['Server']) for Case in Cases['Cases']: if args: if Case['CaseName'] in args: Log.print_info(1, '已匹配到指定测试用例:{0}'.format(Case['CaseName'])) dealCases(Server, Case, Cases) else: dealCases(Server, Case, Cases) elif type(Cases) == list: for cl in Cases: Server = numpy.where(cl['Server'] == 'default', Config.Default['Server'], cl['Server']) for Case in cl['Cases']: if args: if Case['CaseName'] in args: Log.print_info( 1, '已匹配到指定测试用例:{0}'.format(Case['CaseName'])) dealCases(Server, Case, cl) else: dealCases(Server, Case, cl)
def UnLockHold(self): """ 因为一些原因下单失败,需要回滚持有 """ logStr = "HM - UnLock Hold: " + self.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) self.state = 'hold'
def HoldOnSelling(self,sellOrderId): """ 当卖单下单成功后,调用此方法设置状态 """ if self.state != 'lock': logStr = "HM - ##### ERROR! hold state error for Sell! " + self.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) self.state = 'selling' self.sellOrderId = sellOrderId
def __LogBalance(actionName): global __baseBalance, __quoteBalance, __frozeQuoteBalance, __totalProfit, __totalQuote Log.Print( "BM - {}: baseBalance:{} quoteBalance:{} frozeQuote:{} totalProfit:{} totalQuote:{}" .format(actionName, __baseBalance, __quoteBalance, __frozeQuoteBalance, __totalProfit, __totalQuote)) Log.Info( Const.logFile, "BM - {}: baseBalance:{} quoteBalance:{} frozeQuote:{} totalProfit:{} totalQuote:{}" .format(actionName, __baseBalance, __quoteBalance, __frozeQuoteBalance, __totalProfit, __totalQuote))
def FallbackHold(operationId): """ 卖出因为一些原因下单失败,回滚持有 """ for hold in holds: if hold.operationId == operationId: hold.UnLockHold() logStr = "HM - ##### Fallback Hold: " + hold.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) break SaveHoldsData()
def HoldOnSelling(operationId,sellOrderId): """ 一个持有,卖出下单成功 """ for hold in holds: if hold.operationId == operationId: hold.HoldOnSelling(sellOrderId) logStr = "HM - Hold On Selling: " + hold.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) break SaveHoldsData()
def BuyFallback(costQuote): """ 购买回滚,把当时要花费的 quote 从冻结资金拿到原始资金 """ global __quoteBalance, __frozeQuoteBalance __LogBalance("Before Buy Fallback") Log.Print("BM - Buy Fallback Info: costQuote:{}".format(costQuote)) Log.Info(Const.logFile, "BM - Buy Fallback Info: costQuote:{}".format(costQuote)) __frozeQuoteBalance -= costQuote __quoteBalance += costQuote __SaveBalance() __LogBalance("After Buy Fallback")
def LoadTradeOperations(): global __tradeOperations if os.path.exists(Const.dataFile_orderManager): try: jsonStr = IOUtil.ReadTextFromFile(Const.dataFile_orderManager) __tradeOperations = json.loads( jsonStr, object_hook=__Json2TradeOperationObj) except Exception as e: logStr = "OM - ##### EXCEPTION! Load Trade Operations Faild! EXCEPTION:{}".format( e) Log.Print(logStr) Log.Info(Const.logFile, logStr) sys.exit()
def LockHold(self): """ 要卖的时候,本地调用此方法锁定一个持有 """ if self.state != 'hold': logStr = "HM - ##### FAILD! hold lock faild! " + self.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) return False logStr = "HM - SUCCESS! hold lock successful! " + self.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) self.state = 'lock' return True
def __LoadHoldsData(): """ 加载所有的持有数据 """ global holds if os.path.exists(__dataFile): try: jsonStr = IOUtil.ReadTextFromFile(__dataFile) holds = json.loads(jsonStr,object_hook=__HoldJson2Obj) except Exception as e: logStr = "HM - ##### EXCEPTION! Load Hold Buy data exception: {}".format(e) Log.Print(logStr) Log.Info(Const.logFile,logStr) sys.exit()
def SellFilled(operationId, sellOrderId, filledPrice, filledCash): """ 一个卖出成交了 """ for x in range(len(holds)): hold = holds[x] if hold.operationId == operationId: hold.SellFilled(filledPrice,filledCash) del holds[x] ArchiveHold(hold) logStr = "HM - Sell Filled: " + hold.__str__() Log.Print(logStr) Log.Info(Const.logFile,logStr) break SaveHoldsData()
def group_tracks_by_disc(query_list): tracks_by_disc = defaultdict(list) # See if we have multiple disks, first checking tags. discs = set([t.disc for t in query_list if t.disc is not None]) if len(discs) > 1: for t in query_list: tracks_by_disc[t.disc].append(t) return tracks_by_disc.values() # Otherwise, let's sort by filename, and see if we have clusters of tracks. sorted_tracks = sorted(query_list, key=lambda track: track.parts[0]) disc = 1 last_index = 0 for t in sorted_tracks: if t.index < last_index: disc += 1 if t.index != 1: Log('Disc %d didn\'t start with first track, we won\'t use this method.' % disc) tracks_by_disc = defaultdict(list) break tracks_by_disc[disc].append(t) last_index = t.index if len(tracks_by_disc) > 1: return tracks_by_disc.values() # Otherwise, let's consider it a single disc. return [query_list]
def __init__(self): ''' 初始化日志对象,获取登录token ''' token = Token.Token().get_token() self.log = Log.MyLog() self.headers = {"X-SITE-ID": "127", "Authonrization": token}
def __init__(self): config = Config.Config() self.log = Log.MyLog() self.host = config.sql_host self.port = int(config.sql_port) self.user = config.sql_user self.password = config.sql_password
def BuyFilled(costQuote, baseAmount): """ 买入成交,从冻结资金中减去当时的花费,然后把购买的 base 数量加到原始资金中 注意:传到这里的 baseAmount 是已经去掉手续费的了 """ global __frozeQuoteBalance, __baseBalance __LogBalance("Before Buy Filled") Log.Print("BM - Buy Filled Info: costQuote:{} filledAmount:{}".format( costQuote, baseAmount)) Log.Info( Const.logFile, "BM - Buy Filled Info: costQuote:{} filledAmount:{}".format( costQuote, baseAmount)) __frozeQuoteBalance -= costQuote __baseBalance += baseAmount __SaveBalance() __LogBalance("After Buy Filled")
def SellFilled(self,filledPrice,filledCash): """ 当卖单成交时,调用此方法进行最终的结算 """ self.sellFilledPrice = filledPrice gainedQuote = filledCash profit = gainedQuote - self.buyCost profit = MathUtil.GetPrecision(profit,4) if profit <= 0: logStr = "HM - ##### FATAL ERROR! You are lose Money: !!!! " + self.__str__ + " filledPrice:{} filledCash:{}".format(filledPrice,filledCash) Log.Print(logStr) Log.Info(Const.logFile,logStr) sys.exit() self.state = 'selled' self.profit = profit logStr = "HM - Cool! Sell Filled: buyPrice:{} buyCost:{} holdAmount:{} sellPrice:{} filledCash:{} profit:{}".format(self.buyPrice,self.buyCost,self.holdAmount,self.sellFilledPrice,filledCash,profit) Log.Print(logStr) Log.Info(Const.logFile,logStr)
def post(url, header, body): ''' Post请求封装 :param url: :param header: :param body: :return: ''' Log.print_info(2, 'header:{0}'.format(header)) Log.print_info(2, 'body:{0}'.format(body)) res = requests.post(url=url, headers=header, json=body) if res.status_code == 200: return (json.loads(res.text)) else: res = {} res['code'] = -1 res['msg'] = '接口404' return res
def SellFilled(filledQuote, profit, selledAmount): """ 如果卖出成交了,则把获得的 quote 资金加到原始资金中 """ global __baseBalance, __quoteBalance, __totalQuote, __totalProfit __LogBalance("Before Sell Filled") Log.Print( "BM - Sell Filled Info: filledQuote:{} profit:{} selledAmount:{}". format(filledQuote, profit, selledAmount)) Log.Info( Const.logFile, "BM - Sell Filled Info: filledQuote:{} profit:{} selledAmount:{}". format(filledQuote, profit, selledAmount)) __baseBalance -= selledAmount __quoteBalance += filledQuote __totalProfit += profit __totalQuote += profit __SaveBalance() __LogBalance("After Sell Filled")
def OnTradeFilled(tradeOperation, fieldCash): """ 当一个订单成交时,从操作列表中找到对应的交易索引,然后从交易列表中删除,保存数据 """ global __tradeOperations index = -1 for x in range(len(__tradeOperations)): trade = __tradeOperations[x] if trade.operationId == tradeOperation.operationId: index = x break if index >= 0: del __tradeOperations[index] SaveTradeOperations() if tradeOperation.tradeType == 1: # 处理买入订单成交 gainedBase = tradeOperation.tradeAmount * 0.997 gainedBase = MathUtil.GetPrecision(gainedBase, 4) logStr = "OM - Buy Order Filled: operationId:{} gainedBase:{}".format( tradeOperation.operationId, gainedBase) Log.Print(logStr) Log.Info(Const.logFile, logStr) HoldManager.BuyFilled(tradeOperation.operationId, tradeOperation.tradePrice, gainedBase, tradeOperation.cost, tradeOperation.exchangerOrderId) BalanceManager.BuyFilled(tradeOperation.cost, gainedBase) elif tradeOperation.tradeType == 0: # 处理卖出订单成交 # TODO: 这里要实际成交一单,查看一下最终收益和自己计算的收益是否一样 fieldCash = float(fieldCash) * 0.997 # 这里为避免float交易不精确,所以往少里算一点 profit = fieldCash - tradeOperation.cost logStr = "OM - Sell Order Filled: operationId:{} fieldCash:{} profit:{}".format( tradeOperation.operationId, fieldCash, profit) Log.Print(logStr) Log.Info(Const.logFile, logStr) HoldManager.SellFilled(tradeOperation.operationId, tradeOperation.exchangerOrderId, tradeOperation.tradePrice, fieldCash) BalanceManager.SellFilled(fieldCash, profit, tradeOperation.tradeAmount)
def improve_from_tag(existing, file, tag): tags = None try: tags = mutagen.File(file, easy=True) except: Log('There was an exception thrown reading tags.') if tags and tag in tags: existing = tags[tag][0] return toBytes(existing)
def check_values(dfs, threshold) -> bool: # column names and indices are assumed to be the same assert len(dfs) == 2 cols = dfs[0].columns for c in cols: # if the column in the 2 dataframes do not equal if not dfs[0][c].equals(dfs[1][c]): # get indices of diff elements (True is different) # this returns T/F vs numeric indices {0, ..., n-1} diff = dfs[0][c] != dfs[1][c] # get rows that have different values for this column df1 = dfs[0][diff][c] df2 = dfs[1][diff][c] # check how different the values are for i in df1.index: elems = [df1.loc[i], df2.loc[i]] try: # get values by index vals = [float(e) for e in elems] if abs(vals[0] - vals[1]) < threshold: Log.warn("abs(%f - %f) = %f < %f" % (vals[0], vals[1], abs(vals[0] - vals[1]), threshold)) pass else: Log.err("abs(%f - %f) = %f >= %f" % (vals[0], vals[1], abs(vals[0] - vals[1]), threshold)) except: Log.err("%s != %s" % (elems[0], elems[1])) return False return True
def SendBuy(operationId, price, amount, cost): """ 直接在主线程中下买单,最多尝试2次 买入下单 operationId: 操作Id price: 买入价格 amount: 买入数量 cost: 买入总花费 """ global __tradeOperations for x in range(2): orderSended = False try: #orderData = HuobiServices.send_order(amount,'api','btcusdt','buy-limit',price) orderData = __DEBUG_ConstructTradeResult() status = orderData['status'] orderId = orderData['data'] if status == 'ok': orderSended = True buyOperation = TradeOperation(1, price, amount, operationId, orderId, cost) __tradeOperations.append(buyOperation) SaveTradeOperations() logStr = "OM - SUCCESS! Send Buy Order OK! operationId:{} price:{} amount:{} cost:{} orderId:{}".format( operationId, price, amount, cost, orderId) Log.Print(logStr) Log.Info(Const.logFile, logStr) return else: logStr = "OM - ##### FAILD! Send Buy Order FAILD! operationId:{} price:{} amount:{} rawJson:{}".format( operationId, price, amount, orderData) Log.Print(logStr) Log.Info(Const.logFile, logStr) except Exception as e: logStr = "OM - ##### EXCEPTION! Send Buy Order Exception! operationId:{} price:{} amount:{} Exception:{}".format( operationId, price, amount, e) Log.Print(logStr) Log.Info(Const.logFile, logStr) if orderSended == True: logStr = "OM - #### Buy FATAL ERROR!!!!!" Log.Print(logStr) Log.Info(Const.logFile, logStr) time.sleep(1) #走到这里,说明下买单失败,资金回滚 BalanceManage.BuyFallback(cost)
def SendSell(operationId, price, amount, buyCost): """ 卖出持有 operationId: 操作Id price: 卖出价格 amount: 卖出数量 buyCost: 购买的时候花了多少钱,用于计算收益 """ global __tradeOperations for x in range(2): orderSended = False try: #orderData = HuobiServices.send_order(amount,'api','btcusdt','sell-limit',price) orderData = __DEBUG_ConstructTradeResult() status = orderData['status'] orderId = orderData['data'] if status == 'ok': orderSended = True sellOperation = TradeOperation(0, price, amount, operationId, orderId, buyCost) __tradeOperations.append(sellOperation) SaveTradeOperations() logStr = "OM - SUCCESS! Send Sell Order OK! operationId:{} price:{} amount:{} cost:{} orderId:{}".format( operationId, price, amount, buyCost, orderId) Log.Print(logStr) Log.Info(Const.logFile, logStr) return else: logStr = "OM - ##### FAILD! Send Sell Order FAILD! operationId:{} price:{} amount:{} rawJson:{}".format( operationId, price, amount, orderData) Log.Print(logStr) Log.Info(Const.logFile, logStr) except Exception as e: logStr = "OM - ##### EXCEPTION! Send Sell Order Exception! operationId:{} price:{} amount:{} Exception:{}".format( operationId, price, amount, e) Log.Print(logStr) Log.Info(Const.logFile, logStr) if orderSended == True: logStr = "OM - #### Sell FATAL ERROR!!!!!" Log.Print(logStr) Log.Info(Const.logFile, logStr) sys.exit() time.sleep(1) # 因为一些原因,卖出失败了,所以要回滚持有 print("Ready Fallback ", operationId) HoldManager.FallbackHold(operationId)