def process_deposite_history(self, history, gui=False, app=None):
        ## GUI only work in main thread
        assets = self.assets
        for (accountname, depositList) in history:
            for deposit in sorted(depositList, key=lambda x: x['insertTime']):
                query_asset = deposit['asset']
                if query_asset not in assets:
                    assets[query_asset] = AssetInfo(query_asset,
                                                    self.trade_pair_list)
                asset_obj = assets[query_asset]
                if deposit['asset'] != "USDT":
                    question = "帳戶:%s\n輸入本%s紀錄的USDT成本. \n(數量:%d, 時間:%s):" % \
                        (accountname, deposit['asset'], deposit['amount'],tu.datetime_to_utc_time(deposit['insertTime']))
                    if gui:
                        if app == None: return

                        dialog = inputDialog(app, question)
                        app.wait_window(dialog)
                        cost = float(dialog.output)
                    else:
                        cost = float(input(question))
                    asset_obj.upgrade_by_deposit(deposit['amount'],
                                                 cost * deposit['amount'])
                    asset_obj.print_info()
                else:
                    self.info.deposit_USDT = self.info.deposit_USDT + deposit[
                        'amount']
                if not self.info.last_deposit_timestamp or self.info.last_deposit_timestamp < deposit[
                        'insertTime']:
                    self.info.last_deposit_timestamp = deposit['insertTime'] + 1
    def summary_deposite_history(self, gui=False):
        INIT_EPOCH = 1483228800000  # 2017/01/01
        EPOCH_INTERVAL = 5000000000
        epoch = INIT_EPOCH if not self.info.last_deposit_timestamp else self.info.last_deposit_timestamp
        assets = self.assets
        data = []
        print(self.accountname)
        while epoch < tu.get_current_timestamp():
            res = self.get_deposite_history(epoch, epoch + EPOCH_INTERVAL)
            print("Processing " + (tu.datetime_to_utc_time(epoch)) + " to " +
                  tu.datetime_to_utc_time(epoch + EPOCH_INTERVAL))
            if res.json()["depositList"]:
                if gui:
                    data.append((self.accountname, res.json()["depositList"]))
                else:
                    self.process_deposite_history(res.json()["depositList"])
                    #print (tu.datetime_to_utc_time(self.info.last_deposit_timestamp))

            epoch += EPOCH_INTERVAL

        self.save_assets()
        return (data, 0)
def generate_summary_report_to_excel(clients_dict,
                                     meta_file_path='./',
                                     truncate_sheet=False):

    for client in clients_dict.values():
        print(client)
    source = {0: '資產更新時間'}
    source_columns = ['資產更新時間']
    profit_data = []
    source_data = [tu.datetime_to_utc_time(tu.get_current_timestamp())]
    for client_name, client in sorted(clients_dict.items(),
                                      key=lambda d: d[1].info.index):
        profit_data.append(client_name + '_利潤')
        source_columns.append(client_name + '_利潤')
        source_columns.append(client_name + '_資產數量')
        source[client.info.index * 2 + 1] = client_name + '_利潤'
        source[client.info.index * 2 + 2] = client_name + '_資產數量'
        source_data.append(client.info.profit)
        source_data.append(client.info.asset_num)

    df = DataFrame([source_data], columns=source_columns)
    filename = meta_file_path + sep + '電子貨幣實時盈虧計算.xlsx'
    sheet_name = 'Summary'
    header = False
    (startrow, writer) = load_excel(filename, sheet_name, truncate_sheet)
    if startrow is None:
        startrow = 0
        header = True
    # write out the new sheet

    print(df, file=sys.stderr)
    df.style.applymap(color_negative_red,
                      subset=profit_data).to_excel(writer,
                                                   sheet_name=sheet_name,
                                                   startrow=startrow,
                                                   float_format="%.4f",
                                                   index=False,
                                                   header=header)
    ws = writer.sheets[sheet_name]
    thin = Side(border_style="thin", color="000000")
    for index, column_cells in enumerate(ws.columns):
        cell = ws.cell(row=1, column=index + 1)
        cell.value = source[index] if index in source else ''
        cell.font = Font(bold=True)
        cell.border = Border(top=thin, left=thin, right=thin, bottom=thin)
        ws.column_dimensions[column_cells[0].column_letter].width = 24

    writer.save()
    writer.close()
    print("Output summary to " + filename)
def generate_single_report_to_excel(client, truncate_sheet=True):
    assets = client.assets
    accountname = client.accountname
    file_path = client.info.save_path
    if 'USDT' in assets: del assets['USDT']
    if 'USD' in assets: del assets['USD']
    iter_list = list(assets.keys())
    for asset in iter_list:
        if asset not in client.info.query_list:
            #print (asset)
            del assets[asset]
    client.info.asset_num = len(assets)

    source = [
        v.__dict__ for k, v in sorted(assets.items(), key=lambda d: d[0])
    ]

    df = DataFrame(source)
    df['last_query_timestamp'].fillna(value=tu.DEFAULT_EPOCH_TIME,
                                      inplace=True)
    most_recent_date = df['last_query_timestamp'].max()
    df['last_query_timestamp'] = [
        tu.datetime_to_utc_time(x) for x in df['last_query_timestamp']
    ]
    df['利潤'] = df['current_price'] * df['qty'] - df['quoteQty']
    df['USDT 價值'] = df['current_price'] * df['qty']

    df.rename(columns = {'name':'貨幣種類', 'cost': '價格成本', 'qty': '持有數量', 'quoteQty': '買進總成本', \
                         'current_price': '現價', 'transection_count': '交易次數', 'last_query_timestamp' : '最後交易時間'}, inplace = True)
    df = df[[
        '貨幣種類', '利潤', '價格成本', '現價', '買進總成本', 'USDT 價值', '持有數量', '交易次數',
        '最後交易時間'
    ]]
    df = df.sort_values('利潤', ascending=False)
    summary = DataFrame.from_dict({
        "貨幣種類": ['TOTAL'],
        "利潤": [df['利潤'].sum()],
        "價格成本": [None],
        "現價": [None],
        "買進總成本": [None],
        'USDT 價值': [None],
        '持有數量': [None],
        '交易次數': [df['交易次數'].sum()],
        '最後交易時間': [tu.datetime_to_utc_time(most_recent_date)]
    })
    df = df.append(summary, ignore_index=True)

    filename = file_path + sep + '電子貨幣實時盈虧計算.xlsx'
    sheet_name = accountname
    (startrow, writer) = load_excel(filename, sheet_name, truncate_sheet)
    df.style.applymap(color_negative_red,
                      subset=['利潤']).to_excel(writer,
                                              sheet_name=sheet_name,
                                              float_format="%.4f")
    worksheet = writer.sheets[sheet_name]
    for column_cells in worksheet.columns:
        worksheet.column_dimensions[column_cells[0].column_letter].width = 18

    writer.save()
    writer.close()

    print("Output report to " + filename)
 def __str__(self):
     return "利潤: %s\n 資產數量: %s\n 存入 USDT: %s \n 資產更新時間: %s\n" \
 % (self.profit, self.asset_num, self.deposit_USDT, tu.datetime_to_utc_time(self.last_asset_upgrade_time))