每天进步一点点:再聊HiveSQL查询内部市场历史成交
hive-105017·@oflyhigh·
0.000 HBD每天进步一点点:再聊HiveSQL查询内部市场历史成交
之前写过一篇帖子,[每天进步一点点:使用HiveSQL查询历史挂单以及成交](https://hive.blog/hive-105017/@oflyhigh/6ycw3x-hivesql),介绍了通过HiveSQL中的TxLimitOrdersCreates表以及VOFillOrders表来查询挂单以及历史成交。  (图源 :[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大涨,我没忍住出售了一些,那么再用上边这个查询就有问题了。比如其中会有部分类似如下的结果:  不说别的,很明显,`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`。 其中部分返回结果如下:  哎,昨天卖的有点急,均价0.53,卖亏啦。 无意中发现一些早期的成交记录,吐血呀:  # 时间段限制 好了,现在我们能够区分买单、卖单了,也通过纠正谁除以谁的方式解决了价格计算的错误。 但是新问题来了,我想查询某一时间段的信息,比如说只查询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)
👍 joeyarnoldvn, julesquirin, netaterra, warmstill, deanliu, therealyme, nextgen622, cnfund, quochuy, teamvn, smartvote, pocketjs, drexlord, gmlgang, hungryharish, icon123456, ffcrossculture, btshuang, mygod, liangfengyouren, ethanlee, steemindian, bilpcoinbot, atma-yoga, hungryanu, pishio, oldman28, lnakuma, gerber, mice-k, dcityrewards, steem.leo, ezzy, exyle, daan, someguy123, shitsignals, jeanlucsr, felander, accelerator, unconditionalove, bestboom, themightyvolcano, dlike, newsposter, determine, dpend.active, hivechat, dcrops, followjohngalt, cakemonster, kggymlife, triplea.bot, ribary, sketching, bengy, a-bot, bigmoneyman, poliwalt10, blurtyield, lestrange, curly-q, janaveda, wolf-dawg, yogacoach, mermaidvampire, shebe, jamzmie, floatinglin, besheda, julian2013, soufianechakrouf, catwomanteresa, davidke20, itchyfeetdonica, archisteem, kimzwarch, namchau, yumisee, ryenneleow, bichen, mia-cc, fintian, travelgirl, japanguide, voxmortis, quiltedduckfarm, digital.mine, pet.society, ioioioioi, aafeng, cnstm, vickyli, newhope, likuang007, lianjingmedia, rj-photo, jywahaha, rasalom, joythewanderer, geekgirl, minloulou, etherpunk, magicmonk, minigame, roberto58, andyjim, htliao, nanosesame, otom, minminlou, kgsupport, sunsan, bgmoha, moleah, hmayak, dailyke20, jimhawkins, korver, meanbees, patronpass, mirteg, ambiguity, freedomring, lovelemon, passion-fruit, theluvbug, zerofive, lordbutterfly, chenlocus, belemo, zwhammer, joshman, belemo.leo, iproto, skiptvads, lovelingling, hivefolks, pappyelblanco, leomolina, cconn, fortune-master, tryskele, hyborian-strain, tvb, yestermorrow, winly, adm, penguinpablo, cryptonized, funnyman, alphacore, hungrybear, hive.friends, jacuzzi, starrouge, lovequeen, tcpaikano, dapeng, zuun.net, suhunter, hivewatchers, steemcleaners, logic, seikatsumkt, tomiscurious, votehero, msp-makeaminnow, eturnerx, investegg, voter000, eturnerx-honey, teachblogger, fredo77200, sasaadrian, diehardknocks, beco132, ardina, azazqwe, xiaoyaodidi, atyh, marygong77777, cuervoh, alpha-omega, idx, abit, laoyao, midnightoil, xiaohui, helene, bxt, exec, hive-117638, atmama, trafalgar, raindrop, traf, xtrafalgar, artefactoestudio, kattycrochet, evahe, love5200, abundancelife, ace108, queercoin, henrietta27, theargirova, ssebasv, el-dee-are-es, annepink, yanhan, svm038, uruiamme, semennemes, tina1219, rauti, lauti, olaunlimited, my451r, ratnaayub, haoeeicc, mao317, ying82, itsabandoned, drag33, tingjie, ericaliu, ikrahch, winniex, lazy001, tzae, jychbetter, philipmak, socram, alenox, elikast, victory622, tarotbyfergus, manncpt, payger, globalschool, opt2o, schmidi, thehealthylife, mammasitta, massivevibration, onartbali, susanli3769, collinz, pexpresiones, isabelpena, oizaguirres, leninbracho50, petronila, rodyservi, nacarid, karolines, axeltheartist, alcidescadiz, celi130, nildasalazar, ernick, karlin, hojaraskita, syllem, yolmare, jadams2k18, junior182, marinmex, obandoduarte, sevalo13, josemoises, yosmandiaz, hermaryrc, emaxisonline, andreseloy581, yiobri, gubbahomestead, rivalhw, cn-reader, dengyanping888, zhangyan-123, santigs, softworld, bagpack, lsdmercyy, aellly, juanperaza46, thewealthunit, jaybone, jayrent,