duanli0119 2018-11-25 03:00
浏览 46

使用Cakephp 3.x ORM或SQL查询计算体积加权平均值

I am trying to create a price tracker for learning purposes, very similar to what coinmarketcap does. I make an update call which gathers the info from each of the saved exchanges api's and store the results in the database every 5 minutes. I was wondering if there was a way to setup a find query that would calculate a volume weighted average price based on the database structure below (omitted obvious columns such as id and created/modified timestamps)

I currently obtain vwap by calculating the BTC/USD vwap price and store that. Than I convert any marktes that are quoted in BTC to USD using the price from the BTC/USD calculation. I then repeat for ETH/USD and so on. I'm wondering if I should rethink my database structure is there a way to build a query that would calculate vwaps in different quote currencies that I can than use to convert each market into other quote currencies.

Note: volume has 2 columns quote_volume and base_volume but has been condensed to volume for brevity.

Associations:

  • Exchanges hasMany Markets
  • Markets hasMany Prices
  • Markets hasMany Quotes
  • Prices hasMany PriceHistories

ExchangesTable

|------------|-------------|
|    name    |   website   |
|------------|-------------|
|    Simex   |     www     |
|------------|-------------|

MarketsTable

|---------------|------------|-------------|-------------|
|  exchange_id  |     name   |    quote    |    base     |
|---------------|------------|-------------|-------------|
|       12      |    BTCUSD  |     USD     |     BTC     |
|---------------|------------|-------------|-------------|

PricesTable

|---------------|-----------|------------|------------|-------------|
|    market_id  |    last   |    high    |    low     |    volume   |       
|---------------|-----------|------------|------------|-------------|
|        5      |  3989.36  |  4012.78   |   3942.87  | 125,000,000 |         
|---------------|-----------|------------|------------|-------------|

PriceHistoriesTable

|---------------|-----------|------------|------------|-------------|
|    prices_id  |    last   |    high    |    low     |    volume   |       
|---------------|-----------|------------|------------|-------------|
|        8      |  3989.36  |  4012.78   |   3942.87  | 125,000,000 |         
|---------------|-----------|------------|------------|-------------|
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线