def SaveHoldsData(): """ 将持有数据保存到文件 """ global holds jsonStr = json.dumps(holds,default=__HoldObj2Json) IOUtil.WriteTextToFile(__dataFile,jsonStr)
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 Info(logFilePath, logText): """ 日志写入 """ date = TimeUtil.GetShanghaiTime() pathArray = os.path.splitext(logFilePath) realLogFile = "{}_{}_{}_{}{}".format(pathArray[0], date.year, date.month, date.day, pathArray[1]) logText = date.strftime("%Y-%m-%d %H:%M:%S") + " - [" + logText + "]" IOUtil.AppendTextToFile(realLogFile, logText)
def __SaveBalance(): """ 保存资产数据 """ global __baseBalance, __quoteBalance, __frozeQuoteBalance, __totalProfit, __totalQuote dictData = {} dictData['baseBalance'] = __baseBalance dictData['quoteBalance'] = __quoteBalance dictData['frozeQuoteBalance'] = __frozeQuoteBalance dictData['totalProfit'] = __totalProfit dictData['totalQuote'] = __totalQuote balanceData = json.dumps(dictData) IOUtil.WriteTextToFile(__balanceFile, balanceData)
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 __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 __LoadBalance(): """ 加载资产数据,如果数据文件不存在,则初始化资产数据 """ global __baseBalance, __quoteBalance, __frozeQuoteBalance, __totalProfit, __totalQuote if os.path.exists(__balanceFile): try: jsonStr = IOUtil.ReadTextFromFile(__balanceFile) jsonData = json.loads(jsonStr) __baseBalance = jsonData['baseBalance'] __quoteBalance = jsonData['quoteBalance'] __frozeQuoteBalance = jsonData['frozeQuoteBalance'] __totalProfit = jsonData['totalProfit'] __totalQuote = jsonData['totalQuote'] except Exception as e: Log.Print("BM - ##### Fatal Error , can not load balance file! ", e) Log.Info( Const.logFile, "BM - ##### Fatal Error, Can not load balance file! " + str(e)) sys.exit() else: __SaveBalance()
def SaveTradeOperations(): global __tradeOperations jsonStr = json.dumps(__tradeOperations, default=__TradeOperation2Json) IOUtil.WriteTextToFile(Const.dataFile_orderManager, jsonStr)
def ArchiveHold(hold): """ 将已经卖出成交的持有归档 """ archiveStr = GetArchiveHoldStr(hold) IOUtil.AppendTextToFile(__filledDataFile,archiveStr)