每天进步一点点:再聊HiveSQL查询内部市场历史成交

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@oflyhigh·
0.000 HBD
每天进步一点点:再聊HiveSQL查询内部市场历史成交
之前写过一篇帖子,[每天进步一点点:使用HiveSQL查询历史挂单以及成交](https://hive.blog/hive-105017/@oflyhigh/6ycw3x-hivesql),介绍了通过HiveSQL中的TxLimitOrdersCreates表以及VOFillOrders表来查询挂单以及历史成交。

![image.png](https://images.hive.blog/DQmPQVkwTyjuqZgb9w6QZ7MiGRFwLS73NiNcZZiujsfsSDj/image.png)
(图源 :[pixabay](https://pixabay.com/photos/lightbulb-idea-creativity-base-3104355/))

# 简单查询

很长一段时间内,我内部市场中的角色比较单一,就是作为Taker,买买买,所以查询起来比较简单。

比如查询我最近100条订单成交记录,并计算出每笔的价格,查询语句如下:
```
select top 100 current_pays, current_pays_symbol, open_owner, open_pays, open_pays_symbol, current_pays/open_pays as price, timestamp
    from VOFillOrders 
    where current_owner='oflyhigh' 
    order by timestamp desc
```

其中`current_owner='oflyhigh' `指明我是作为Taker的。

# 区分买单卖单

但是昨天HIVE大涨,我没忍住出售了一些,那么再用上边这个查询就有问题了。比如其中会有部分类似如下的结果:
![1675572359188.png](https://images.hive.blog/DQmcM1G7Av3gFt9BmUT6jKabr2C3QdTzdB9dZFX5JPdn62x/1675572359188.png)

不说别的,很明显,`1.8x`的价格信息就肯定是错误的。虽然我坚信HIVE的价格应该可以回到1.8x甚至更高,但是至少这两天还没有这样的价格。

那么问题出在哪里呢?答案就是上边的查询中,***没有区分出售以及购买***。其实,出售与购买只是我们习惯性的概念,在内部市场的机制看来,是不存在出售与购买的区别的。比如***我们习惯上的购买HIVE,其实就是售出HBD***。

但是计算价格时,我们是要用`HBD/HIVE`,而不是`HIVE/HBD`,所以查询时还是要区分的。所以我们写查询时,还是要用逻辑上的买单(买入HIVE)和卖单(卖出HIVE)来区分一下。

#### 查询买单成交记录

所以如果查询我最近100条买单成交记录,并计算每笔价格,查询语句如下:
```
select top 100 current_pays, current_pays_symbol, open_owner, open_pays, open_pays_symbol, current_pays/open_pays as price, timestamp
    from VOFillOrders 
    where current_owner='oflyhigh' and current_pays_symbol='HBD'
    order by timestamp desc
```
不难发现,区别就是条件语句中增加了`and  current_pays_symbol='HBD'`,当然也可以用`and  open_pays_symbol='HIVE'`,无论用哪个,都是买入HIVE(卖出HBD)的意思。

这样结果就是正确地显示我了最近100条买入HIVE的成交信息。

#### 查询卖单成交记录

同理,我们可以写出如下查询卖单成交记录的查询:
```
select top 100 current_pays, current_pays_symbol, open_owner, open_pays, open_pays_symbol, open_pays/current_pays as price, timestamp
    from VOFillOrders 
    where current_owner='oflyhigh' and current_pays_symbol='HIVE'
    order by timestamp desc
```
这里需要注意的是,除了将`current_pays_symbol='HBD'`换成`current_pays_symbol='HIVE'`外,价格计算也要做相应的调整,确保是`HBD/HIVE`。

其中部分返回结果如下:
![image.png](https://images.hive.blog/DQmYdD844uB2PWuMvwyMfaKxn3yfq5BbuX9wgUFZaf2NFto/image.png)
哎,昨天卖的有点急,均价0.53,卖亏啦。

无意中发现一些早期的成交记录,吐血呀:
![image.png](https://images.hive.blog/DQmQxUxyCVNkdx8zrbUY4YrGBVy81p7ZaBZsCHbT9tbAYxC/image.png)

# 时间段限制

好了,现在我们能够区分买单、卖单了,也通过纠正谁除以谁的方式解决了价格计算的错误。

但是新问题来了,我想查询某一时间段的信息,比如说只查询2022的买单成交信息,该如何查询呢?

其实这个也很简单,查询语句如下:
```
select top 200 current_pays, current_pays_symbol, open_owner, open_pays, open_pays_symbol, 
	open_pays/current_pays as price, timestamp
	from VOFillOrders 
	where current_owner='oflyhigh' and current_pays_symbol ='HBD'
		and timestamp > CAST('1/1/2022 0:00:00 AM' as DateTime)  and timestamp < CAST('1/1/2023 0:00:00 AM' as DateTime)
	order by timestamp desc
```
也就是说,将时间字符串转换成DateTime类型,并与timestamp进行比较即可。

而实际操作中,我发现
>`and timestamp > CAST('1/1/2022 0:00:00 AM' as DateTime)  and timestamp < CAST('1/1/2023 0:00:00 AM' as DateTime)`

替换成:
`and timestamp > 2022 and timestamp < 2023`

同样是可以工作的,不得不感慨一下,MS SQL SERVER的强大呢。

# 结论

通过指定一些条件,我们可以灵活地查询买单或者卖单,或者查询某个时间段内的成交信息。

了解历史成交信息,对于我们做出操作决策十分重要,至少不至于把高抛低吸操作成高吸低抛,哈哈哈哈。

# 相关链接

* [每天进步一点点:使用HiveSQL查询历史挂单以及成交](https://hive.blog/hive-105017/@oflyhigh/6ycw3x-hivesql)
* [每天进步一点点:聊聊HIVE市场的fill_order](https://hive.blog/hive-105017/@oflyhigh/hive-fillorder)
* [每天进步一点点:MS-SQL保留指定位数小数](https://hive.blog/hive-105017/@oflyhigh/ms-sql)
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,