create account

python-bitshares 边学边记 (五) / Market类 by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$122.83
python-bitshares 边学边记 (五) / Market类
在之前的几篇文章中,我们简单介绍了如何安装python-bitshares 、python-bitshares的钱包相关操作、BitShares类以及Account类。

详情可以参考文末的参考链接。

![](https://steemitimages.com/DQmab7QPFcG6Q4U9Z1FMThkdVcDneSY5xR2bPxVWryEZfeX/image.png)
(图源 :pixabay)

这节我们来继续学习python-bitshares 。

# Market类

bitshares中最激动人心的特点是啥,我个人认为无疑就是去中心化交易所了,在这个市场你可以交易很多资产,和中心化交易所相比,交易都是由bitshares自动撮合完成的,公开透明。无需担心交易所搞鬼,侵吞你的资产。其实还有很多特性和有点,但是我水平有限,理解还不够深入,就不多加评论了,大家自己去慢慢发掘。

好啦,既然bitshares的交易所这么牛叉,我们来看看用python-bitshares咋玩。

#### 创建实例

我们可以使用以下代码创建Market实例
`from bitshares.market import Market`
`market = Market("BTS:CNY")`

其中***`"BTS:CNY"`***为我们指定的市场。前边***`BASE`***, 后边***`QUOTE`***,亦即以BTS为基础的CNY报价。

其中市场交易对可以已如下方式指定(三者效果相同):
* ``base:quote`` 以``:``分隔
* ``base/quote`` 以``/``分隔
* ``base-quote`` 以``-``分隔


#### 查看报价

创建Market类实例之后,我们就可以随时查看市场报价了
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`pprint(market.ticker())`
![](https://steemitimages.com/DQmbTqkbYrbUfitoKcJY4ckSeNH3sXAXLHM8C6cfD5iawYP/image.png)

有没有发现,其实获取的信息和我们公众号获取的一样
![](https://steemitimages.com/DQmcHiYyCLLPvBmNkniv9zkynLb6Y58pYdcMN3HqgXy8kY8/image.png)
所以你要仅仅想查个行情啥的,用我们公众号就好啦:)


#### 获取24小时内的交易量

除了报价以外,还有个功能是获取24小时内的交易量
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`pprint(market.volume24h())`
![](https://steemitimages.com/DQmZF3z8SVWWxRV5bxFpHWjPGAL7DpHKR4XBst9Ddc46Wxi/image.png)
其实ticker信息中已经包含了交易量信息,但是bitshares还设计了这个API,估计是为了访问快速吧?我瞎猜的。😀

#### 获取市场订单信息/orderbook

以下代码获取市场当前订单信息
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`pprint(market.orderbook(5))`
![](https://steemitimages.com/DQmThDa1d5uLouTiA2uFCHvnS7Bn8L4TqmMWZLQ98BXLR1H/image.png)
我们可以通过指定limit参数,来返回指定数量的订单信息,示例中我指定为5。

#### 获取历史成交信息

可以用history获取账户信息,参数定义如下:
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`pprint(market.trades(5))`
![](https://steemitimages.com/DQmdww9nft8PDzKA4gzasEvwvdSWf8bgnrtyn7w3HssHENy/image.png)
我们同样可以通过指定limit参数来限制返回记录的个数。

除了获取整个市场的历史交易信息以外,我们还可以获得指定账户的历史交易信息,方法定义如下:
`def accounttrades(self, account=None, limit=25):`
![](https://steemitimages.com/DQmSYqi1spEKRofQtV771Ai4554D4JPbWwCdtbimpngRGQJ/image.png)
我测试了一下,返回为空,看了一下代码,它是使用***`get_fill_order_history`***API 获取所有的撮合订单信息,再按用户筛选出属于对应用户的订单。***此处有很明显的BUG!***

另外还有一个
`accountopenorders`方法,个人认为和Account类的openorders属性重复。


# 如何买卖对应资产

上述内容,我们介绍了如何使用Market类获取市场的各种信息。接下来则是激动人心的时刻了,亦即如何使用Market类下单。

#### buy方法

我们可以使用***`buy`***方法来挂买单:
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`market.buy(1.00001, 2, account='xxxxx')`
![](https://steemitimages.com/DQmUvFXcdHARhgDFmfYYn1C7GFRxK5hoXyPiP3imEUqTM1x/image.png)
挂单成功了,不过现在以这个价位挂单,想买到BTS,那无异于痴心妄想。


#### sell方法

用法和buy方法大同小异啦
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`market.sell(2.00001, 2, account='xxxx')`

![](https://steemitimages.com/DQmehVfJAT7atQbQ3bh7BsdffcVrhBtDW9dgz8zG2hxBEPz/image.png)

#### cancel方法

除了能挂买单、卖单以外,取消订单功能也很重要。
cancel方法 用于取消订单。
`from pprint import pprint`
`from bitshares.market import Market`
`market = Market("BTS:CNY")`
`market.cancel(['1.7.42839275'], 'xxxx')`
![](https://steemitimages.com/DQmWYpyoqbsTbExax7y4g1eagNHUr1ajPiefpnoJUNDYskU/image.png)
查看账户活动记录,可以发现订单取消成功。


# 总结

Market类可以用于获取bitshares交易所指定交易对的市场信息,也可以用来挂买单卖单以及取消订单。强大的不要不要的。

感兴趣的,自己去体验一下吧,写不动了,本文就此打住啦。

# 参考信息

* https://github.com/xeroc/python-bitshares
* [python-bitshares 边学边记 (一) : 简介与安装](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares)
* [python-bitshares 边学边记 (二) : 钱包操作](https://steemit.com/python-bitshares/@oflyhigh/3ab1oc-python-bitshares)
* [python-bitshares 边学边记 (三) / BitShares类](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-bitshares)
* [python-bitshares 边学边记 (四) / Account类](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-account)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 122 others
👎  
properties (23)
authoroflyhigh
permlinkpython-bitshares-market
categorypython-bitshares
json_metadata{"tags":["python-bitshares","bitshares","python","cn","cn-programming"],"image":["https://steemitimages.com/DQmab7QPFcG6Q4U9Z1FMThkdVcDneSY5xR2bPxVWryEZfeX/image.png","https://steemitimages.com/DQmbTqkbYrbUfitoKcJY4ckSeNH3sXAXLHM8C6cfD5iawYP/image.png","https://steemitimages.com/DQmcHiYyCLLPvBmNkniv9zkynLb6Y58pYdcMN3HqgXy8kY8/image.png","https://steemitimages.com/DQmZF3z8SVWWxRV5bxFpHWjPGAL7DpHKR4XBst9Ddc46Wxi/image.png","https://steemitimages.com/DQmThDa1d5uLouTiA2uFCHvnS7Bn8L4TqmMWZLQ98BXLR1H/image.png","https://steemitimages.com/DQmdww9nft8PDzKA4gzasEvwvdSWf8bgnrtyn7w3HssHENy/image.png","https://steemitimages.com/DQmSYqi1spEKRofQtV771Ai4554D4JPbWwCdtbimpngRGQJ/image.png","https://steemitimages.com/DQmUvFXcdHARhgDFmfYYn1C7GFRxK5hoXyPiP3imEUqTM1x/image.png","https://steemitimages.com/DQmehVfJAT7atQbQ3bh7BsdffcVrhBtDW9dgz8zG2hxBEPz/image.png","https://steemitimages.com/DQmWYpyoqbsTbExax7y4g1eagNHUr1ajPiefpnoJUNDYskU/image.png"],"links":["https://github.com/xeroc/python-bitshares","https://steemit.com/python-bitshares/@oflyhigh/python-bitshares","https://steemit.com/python-bitshares/@oflyhigh/3ab1oc-python-bitshares","https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-bitshares","https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-account"],"app":"steemit/0.1","format":"markdown"}
created2017-12-13 13:42:39
last_update2017-12-13 13:42:39
depth0
children15
last_payout2017-12-20 13:42:39
cashout_time1969-12-31 23:59:59
total_payout_value99.272 HBD
curator_payout_value23.558 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,902
author_reputation6,311,297,148,094,855
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,365,817
net_rshares23,523,357,220,847
author_curate_reward""
vote details (187)
@bitw ·
我用这个测试时候,不值得为什么,提示钱包锁定。
from pprint import pprint
from bitshares.market import Market
market = Market("BTS:CNY")
market.buy(0.1, 1, account="bitw002")

错误信息如下:
![](https://steemitimages.com/DQmbPTawsWWS8EWDcaXKaCy9mGrA3N99FPqxe2BaXmZDXUN/%E5%9B%BE%E7%89%87.png)
properties (22)
authorbitw
permlinkre-oflyhigh-python-bitshares-market-20180316t082342602z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"image":["https://steemitimages.com/DQmbPTawsWWS8EWDcaXKaCy9mGrA3N99FPqxe2BaXmZDXUN/%E5%9B%BE%E7%89%87.png"],"app":"steemit/0.1"}
created2018-03-16 08:23:39
last_update2018-03-16 08:23:39
depth1
children2
last_payout2018-03-23 08:23:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length261
author_reputation88,258,865
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,751,987
net_rshares0
@oflyhigh ·
参考这个:
https://steemit.com/python-bitshares/@oflyhigh/3ab1oc-python-bitshares
properties (22)
authoroflyhigh
permlinkre-bitw-re-oflyhigh-python-bitshares-market-20180316t122423044z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"links":["https://steemit.com/python-bitshares/@oflyhigh/3ab1oc-python-bitshares"],"app":"steemit/0.1"}
created2018-03-16 12:24:24
last_update2018-03-16 12:24:24
depth2
children1
last_payout2018-03-23 12:24:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length76
author_reputation6,311,297,148,094,855
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,766,026
net_rshares0
@bitw · (edited)
非常感谢!
问题解决了!
同时我还找到了这个链接,希望给后来查阅的人提供帮助
http://python.usyiyi.cn/sources/pythonbitshares/latest/index.html#document-market
properties (22)
authorbitw
permlinkre-oflyhigh-re-bitw-re-oflyhigh-python-bitshares-market-20180316t181341812z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1","links":["http://python.usyiyi.cn/sources/pythonbitshares/latest/index.html#document-market"]}
created2018-03-16 18:13:39
last_update2018-03-16 18:15:06
depth3
children0
last_payout2018-03-23 18:13:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length120
author_reputation88,258,865
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,808,926
net_rshares0
@dianclasher ·
Good job
properties (22)
authordianclasher
permlinkre-oflyhigh-python-bitshares-market-20171213t134326251z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"community":"busy","app":"busy/2.1.0"}
created2017-12-13 13:43:30
last_update2017-12-13 13:43:30
depth1
children0
last_payout2017-12-20 13:43:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8
author_reputation7,293,061,268,372
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,365,888
net_rshares0
@joendegz ·
awesome tutorial , on python , will try and see if it works
properties (22)
authorjoendegz
permlinkre-oflyhigh-python-bitshares-market-20171213t135236814z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-13 13:45:48
last_update2017-12-13 13:45:48
depth1
children0
last_payout2017-12-20 13:45:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length59
author_reputation12,403,121,899,997
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,366,136
net_rshares0
@mohsin01 ·
$0.12
非常感谢您的更新,共享bitshare程序,祝你好运 @oflyhigh
👍  
properties (23)
authormohsin01
permlinkre-oflyhigh-python-bitshares-market-20171213t144923590z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"users":["oflyhigh"],"app":"steemit/0.1"}
created2017-12-13 14:49:24
last_update2017-12-13 14:49:24
depth1
children0
last_payout2017-12-20 14:49:24
cashout_time1969-12-31 23:59:59
total_payout_value0.087 HBD
curator_payout_value0.029 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length36
author_reputation8,329,306,629,966
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,372,933
net_rshares22,161,426,244
author_curate_reward""
vote details (1)
@mr-j ·
技术大牛
properties (22)
authormr-j
permlinkre-oflyhigh-python-bitshares-market-20171214t010707511z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-14 01:07:06
last_update2017-12-14 01:07:06
depth1
children0
last_payout2017-12-21 01:07:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4
author_reputation46,002,694,057
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,432,523
net_rshares0
@nazarwills ·
$0.10
My translation is less supportive of your writing .. But I think your writing is very interesting friend. I upvote 100%
👍  
properties (23)
authornazarwills
permlinkre-oflyhigh-python-bitshares-market-20171213t141711610z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-13 14:17:15
last_update2017-12-13 14:17:15
depth1
children0
last_payout2017-12-20 14:17:15
cashout_time1969-12-31 23:59:59
total_payout_value0.103 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length119
author_reputation2,137,881,175,942
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,369,326
net_rshares19,594,909,772
author_curate_reward""
vote details (1)
@neurallearner ·
Come and learn [how AI processes images](https://steemit.com/ai/@neurallearner/ai-introduction-part-2-images-and-and-convolutional-neural-network) :)
properties (22)
authorneurallearner
permlinkre-oflyhigh-python-bitshares-market-20171216t224219216z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"links":["https://steemit.com/ai/@neurallearner/ai-introduction-part-2-images-and-and-convolutional-neural-network"],"app":"steemit/0.1"}
created2017-12-16 22:42:21
last_update2017-12-16 22:42:21
depth1
children0
last_payout2017-12-23 22:42:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length149
author_reputation52,688,262,389
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,805,289
net_rshares0
@quike ·
I loved your tutorial more than me that I am learning Python
properties (22)
authorquike
permlinkre-oflyhigh-python-bitshares-market-20171213t144417120z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-13 14:20:27
last_update2017-12-13 14:20:27
depth1
children0
last_payout2017-12-20 14:20:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation20,240,876,626
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,369,697
net_rshares0
@raull ·
感谢您的信息
properties (22)
authorraull
permlinkre-oflyhigh-python-bitshares-market-20171213t221158616z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-13 22:12:03
last_update2017-12-13 22:12:03
depth1
children0
last_payout2017-12-20 22:12:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation605,954,227,256
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,418,223
net_rshares0
@watch0ut ·
请问:取消订单,程序怎么获取到挂单的单号?
properties (22)
authorwatch0ut
permlinkre-oflyhigh-python-bitshares-market-20180118t143528973z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2018-01-18 14:35:27
last_update2018-01-18 14:35:27
depth1
children0
last_payout2018-01-25 14:35:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation0
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,407,961
net_rshares0
@williamjose ·
非常感谢您分享信息。成功和祝福
👍  , ,
properties (23)
authorwilliamjose
permlinkre-oflyhigh-python-bitshares-market-20171215t152321575z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-15 14:56:09
last_update2017-12-15 14:56:09
depth1
children0
last_payout2017-12-22 14:56:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15
author_reputation695,089,030,775
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,631,801
net_rshares1,475,070,139
author_curate_reward""
vote details (3)
@xhenhow ·
bitshares中是不是没办法提交市价单啊
properties (22)
authorxhenhow
permlinkre-oflyhigh-python-bitshares-market-20180512t082142890z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2018-05-12 08:22:15
last_update2018-05-12 08:22:15
depth1
children0
last_payout2018-05-19 08:22:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation0
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id55,260,040
net_rshares0
@zalandir ·
$0.18
sorry i can't relate....
👍  
properties (23)
authorzalandir
permlinkre-oflyhigh-python-bitshares-market-20171213t152617565z
categorypython-bitshares
json_metadata{"tags":["python-bitshares"],"app":"steemit/0.1"}
created2017-12-13 15:26:12
last_update2017-12-13 15:26:12
depth1
children0
last_payout2017-12-20 15:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.134 HBD
curator_payout_value0.042 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length24
author_reputation4,340,222,057,580
root_title"python-bitshares 边学边记 (五) / Market类"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,377,031
net_rshares32,723,823,329
author_curate_reward""
vote details (1)