Programming with python on the steem blockchain-Lesson 4- Convert STEEM and SBD in the internal market
INTRO
Following the logic I explained in the previous lesson, and in order to be fully in control over your finances here in the STEEM blockchain, it is important in my opinion to have always an alternative if something, for any reason, goes south.
With this simple lesson, I'm going to show you how to write a simple python code in order to change your assets SBD into STEEM or vice versa.
FROM PREVIOUS LESSON
In my mind this is the logical continuation of the previous lesson so, in order to have a complete picture, I invite you to start from that one here
In particular, these lines we explained already
from beem import Steem
from beem.nodelist import NodeList
nodelist = NodeList()
nodelist.update_nodes()
stm = Steem(node=nodelist.get_steem_nodes(),keys=''YOUR PRIV ACTIVE KEY")
account = Account("YOUR ACCOUNT", blockchain_instance=stm)
print(account.balances)
try:
account.claim_reward_balance()
except Exception as e:
print ('probably nothing to claim', e)
IMPORT ALL THE LIBRARIES AND CONNECT TO THE BLOCKCHAIN
Now let's start to adapt the previous code with this new purpose, that is being able to trade in the internal and decentralized market.
from beem.account import Account
from beem import Steem
from beem.nodelist import NodeList
from beem.market import Market
In particular, we added the last line.
Now let's connect with the STEEM blockchain, and with its own decentralized market.
nodelist = NodeList()
nodelist.update_nodes()
stm=Steem(node=nodelist.get_steem_nodes(),keys='[YOUR PRIV ACTIVE KEY]')
market=Market(blockchain_instance=stm)
Let's be sure that we are going to trade STEEM, and SBD and not other assets.
print (market)
The output must be like this:
{'base': <Asset STEEM>, 'quote': <Asset SBD>, 'blockchain_instance': <Steem node=https://api.steemit.com, nobroadcast=False>}
CHECK THE ORDER BOOK
To check the order book, we need to use the order book command.
The asks will give us the 25 entries from the SELL column.
It means the order of people that are selling their STEEM to buy SBD
This is the command:
book=market.orderbook(limit=25, raw_data=False)
for i in book['asks']:
print(i)
This is the output:
2040.910 STEEM 150.653 SBD @ 0.073816 SBD/STEEM
500.000 STEEM 36.910 SBD @ 0.073820 SBD/STEEM
62.029 STEEM 4.579 SBD @ 0.073820 SBD/STEEM
30.246 STEEM 2.234 SBD @ 0.073861 SBD/STEEM
498.541 STEEM 36.842 SBD @ 0.073899 SBD/STEEM
405.406 STEEM 30.000 SBD @ 0.073999 SBD/STEEM
200.000 STEEM 14.804 SBD @ 0.074020 SBD/STEEM
41.000 STEEM 3.038 SBD @ 0.074097 SBD/STEEM
200.000 STEEM 14.822 SBD @ 0.074110 SBD/STEEM
75.000 STEEM 5.565 SBD @ 0.074200 SBD/STEEM
100.000 STEEM 7.422 SBD @ 0.074220 SBD/STEEM
100.000 STEEM 7.427 SBD @ 0.074270 SBD/STEEM
75.000 STEEM 5.572 SBD @ 0.074293 SBD/STEEM
100.000 STEEM 7.430 SBD @ 0.074300 SBD/STEEM
79.147 STEEM 5.885 SBD @ 0.074355 SBD/STEEM
117.886 STEEM 8.768 SBD @ 0.074376 SBD/STEEM
45.819 STEEM 3.408 SBD @ 0.074379 SBD/STEEM
73.820 STEEM 5.492 SBD @ 0.074397 SBD/STEEM
70.000 STEEM 5.208 SBD @ 0.074400 SBD/STEEM
265.775 STEEM 19.798 SBD @ 0.074491 SBD/STEEM
384.423 STEEM 28.637 SBD @ 0.074493 SBD/STEEM
70.000 STEEM 5.215 SBD @ 0.074500 SBD/STEEM
42.285 STEEM 3.151 SBD @ 0.074518 SBD/STEEM
165.629 STEEM 12.346 SBD @ 0.074540 SBD/STEEM
57.000 STEEM 4.252 SBD @ 0.074596 SBD/STEEM
To get the BUY orders, so the orders from people that wish to sell their SBD to buy STEEM.
This is the command:
for i in book['bids']:
print(i)
This, similar to the previous one, the output:
56.619 STEEM 4.178 SBD @ 0.073791 SBD/STEEM
1.274 STEEM 0.094 SBD @ 0.073783 SBD/STEEM
68.256 STEEM 5.000 SBD @ 0.073253 SBD/STEEM
41.296 STEEM 3.025 SBD @ 0.073251 SBD/STEEM
113.347 STEEM 8.299 SBD @ 0.073217 SBD/STEEM
56.005 STEEM 4.098 SBD @ 0.073172 SBD/STEEM
12.424 STEEM 0.909 SBD @ 0.073164 SBD/STEEM
70.000 STEEM 5.117 SBD @ 0.073100 SBD/STEEM
0.178 STEEM 0.013 SBD @ 0.073033 SBD/STEEM
150.406 STEEM 10.980 SBD @ 0.073002 SBD/STEEM
70.000 STEEM 5.110 SBD @ 0.073000 SBD/STEEM
182.507 STEEM 13.323 SBD @ 0.072999 SBD/STEEM
7.425 STEEM 0.542 SBD @ 0.072996 SBD/STEEM
70.000 STEEM 5.103 SBD @ 0.072900 SBD/STEEM
70.000 STEEM 5.096 SBD @ 0.072800 SBD/STEEM
35.976 STEEM 2.619 SBD @ 0.072798 SBD/STEEM
70.000 STEEM 5.089 SBD @ 0.072700 SBD/STEEM
70.000 STEEM 5.082 SBD @ 0.072600 SBD/STEEM
338.147 STEEM 24.517 SBD @ 0.072503 SBD/STEEM
70.000 STEEM 5.075 SBD @ 0.072500 SBD/STEEM
353.338 STEEM 25.617 SBD @ 0.072499 SBD/STEEM
74.430 STEEM 5.396 SBD @ 0.072497 SBD/STEEM
43.519 STEEM 3.155 SBD @ 0.072497 SBD/STEEM
13.794 STEEM 1.000 SBD @ 0.072495 SBD/STEEM
70.000 STEEM 5.068 SBD @ 0.072400 SBD/STEEM
PLACE AN ORDER
Now let's place a BUY order, so we are going to sell some SBD to get STEEM, this choice is done just for explanation purposes.
Let's pick the best price from the book orders to sell our SBD, decide the quantity, and place the order.
At the time of this article, I had 4.153 SBD available in my account, so I'm going to sell 0.153 SBD in the following example.
book=market.orderbook(limit=1, raw_data=False)
for i in book['bids']:
price=i['price']
market.sell(price=price,amount=0.153,account=account)
The order will be placed automatically, and you can check it in your wallet, after a week if not liquidated will be deleted automatically.
If you wish to sell your STEEM to buy SBD it is a bit more "complicated, because the amount will remain in SBD, so you need to calculate by yourself the quantity you wish to sell to get the desired amount of STEEM.
book=market.orderbook(limit=1, raw_data=False)
for i in book['asks']:
price=i['price']
market.buy(price=price,amount=0.153,account=account)
As you can see the command changed from market.sell
to market.buy
CONCLUSION
I think these concepts are quite useful, not just for what I've written at the beginning of this article, but also because, from these few lines is possible to write a TRADE BOT that can works 24/7 in the decentralized market, without buying or selling fees, and this means, that with the right trade strategy is possible to create a profitable program.
I'm not a trader, but if some someone among the traders outside there wish to contact me, I think there is the possibility to develop something quite interesting.
Furthermore, I really hope you found this simple lesson useful, of course I'm available to help you out via the comment section below, or if you wish by booking a private lesson with me.
LIST OF ALL MY LESSONS:
- https://steemit.com/hive-197809/@the.journal/program-with-python-on-the-steem-blockchain-lesson1-retrieve-data-from-a-community
- https://steemit.com/hive-197809/@the.journal/programming-with-python-on-the-steem-blockchain-lesson2-set-up-a-curation-trail
- https://steemit.com/hive-197809/@the.journal/programming-with-python-on-the-steem-blockchain-lesson-3-send-steem-or-sdb
Do you think the volumes in the internal market are high enough for the bot?