---
# System prepended metadata

title: Data Processing for Matreial UI Chart

---

# Data Processing for Matreial UI Chart
## 藉由FTX api 取得資料並在前端做圖
1. 取得所持有幣種歷史價格
2. 數據清理
3. 提供api接口給前端使用

## 數據清理
一開始沒有想太多，覺得用幾個function就可以將每日資料彙整加總，
惡夢就開始了，一直處理不同 api 資料取得之格式
```python=
 def get_my_crypto_candle_list(markets : list, period : int = period):
                ## calculate how many days data showing at chart
        every_market_price_list = []
        for index in range(len(markets)):
            name_after_market_dict = {}
            name_after_market_dict[markets[index]] = ftx.get_market_price_history(market=markets[index], 
                    resolution=86400, 
                    start_time=datetime.datetime.now().timestamp()-(86400*int(period)),
                    end_time=datetime.datetime.now().timestamp())
            every_market_price_list.append(name_after_market_dict)
        return every_market_price_list

    #every crypto quantity   
    def get_my_crypto_quantity():
        account_info = ftx.get_balances()
        name_and_quant = {}
        for quant in account_info:
            name_and_quant[quant['coin']] = quant['total']       
        return name_and_quant

    #calculate net value USD 
    def calculate_net_value(period = period):
        quant = get_my_crypto_quantity()
        crypto_names = list(quant.keys())
        markets_prices = get_my_crypto_candle_list(['FTT/USDT', 'ETH/USDT'] ,period)
        for market_price in markets_prices:
            for name in crypto_names:
                if name+"/USDT" in list(market_price.keys()):
                    for price in market_price[name+"/USDT"] :
                        print(price['close'] * quant[name] )
                

        
    end_time = datetime.datetime.now().timestamp()
    markets = ftx.get_market_price_history(market='BTC/USDT', 
                    resolution=86400, 
                    start_time=end_time-(86400*int(period)), 
                    end_time=end_time)
    close_price = []
    for market in markets:
        d = {}
        d['close'] = market['close']
        close_price.append(d)
```

* 費盡千辛萬苦才將兩個幣種加總
![](https://i.imgur.com/BqE1QJa.png)

## 改用 pandas DataFrame 很快就將不同日期資料彙整完成
![](https://i.imgur.com/B8p3ozf.png)

![](https://i.imgur.com/ldGot27.png)

* api 接口完成後取得資料
![](https://i.imgur.com/J3yQBrZ.png)



## my code
https://github.com/bruce-ycyang/my_code/blob/master/data_processing.ipynb

