Building a cryptocurrency trading bot with Moneywagon and Shapeshift!
Aug. 4, 2016, 4:37 p.m., by chris
This very simple trading bot script is only 30 lines of code. There are roughly three sections: the first section defines private keys and currencies that you wish to trade. The second section make the determination if a trade should be made or not. The third section actually executes the trade.
The script as it is written below is probably not profitable. I wrote it to demonstrate the abilities of Moneywagon. Hopefully somebody can improve upon my code and make it so it can be profitable. Personally, I don't think such a thing is possible. If it was possible, everyone would be doing it, which would probably make it no longer profitable.
The entire body of the script is below:
First, make sure you have python installed. Then install Moneywagon:
pip install moneywagon
First section: Definitions
from moneywagon import get_current_price, get_address_balance
from moneywagon.tx import Transaction
from bitcoin import privkey_to_address
data = [
This section is where you tell the script the private keys to funds you want to trade with. If you wanted to use this script for yourself, you would have to generate your own private keys. The command line code for doing this is:
openssl rand 10000 | moneywagon generate-keypair btc - | python -mjson.tool
This command generates a new private key. It outputs this key in a multitude of formats. The format this trading bot script uses is WIF.
You can also use this script to trade between other currencies other than BTC and LTC. As long as the currency is supported by moneywagon and shapeshift, it will work. The way to find out if it works is to try it, if it fails, then you know it doesn't work.
Second Section: Determining if the trade should be made, and if so, how much should be traded
current_rate = get_current_price(data, data)
if current_rate > 150:
to_sell = data # price is high, sell BTC
to_buy = data
elif current_rate < 100:
to_sell = data # price is low, buy BTC
to_buy = data
raise SystemExit() # no trade
amount_to_sell = get_address_balance(to_sell, privkey_to_address(to_sell)) / 4
Lets go line by line. The first line calls a moneywagon function to get the current exchange rate between the two currencies (which in this case is BTC and LTC).
The second line starts off an 8 line "if" clause. The purpose of this 8 line block of code is to set two variables: to_sell and to_buy. These variables are used in the third section when we execute the trade. The way it's written now, the script will sell BTC if the exchange rate is higher than 150. Conversely, it will sell LTC if the exchange rate is lower than 100. If the exchange rate is between 100 and 150, it makes no trade.
The last line we calculate the amount that we want to trade. First a call is made through moneywagon to get the balance of the sell currency. The amount to sell is 1/4 the total balance.
Third Section: Executing the trade through Shapeshift
tx = Transaction(to_sell, verbose=True)
tx.onchain_exchange(to_buy, privkey_to_address(to_buy)), amount_to_sell)
This code generates a transaction, and then sends that transaction to the network. The output address of this transaction is a shapeshift.io deposit addresses, which will be withdrawn to the address of the "buy" currency. Note the call to the "onchain_exchange" method of the transaction object. That method finds the best exchange rate for the given pair across all defined onchain exchange service (which currently is just shapeshift.io, but when other onchain exchanges come about, they will be added as well).
Running the script
Thi script can be ran manually, or it can be set to execute along an interval. The utility cron can be used to execute the script on a hourly basis, daily basis, or even weekly basis. Because of the way section two works, it makes sense to execute this script on a longer time frame, say daily or weekly instead of every minute or hour.
Making this script better
There re a few things you can do to make the script better. For one, you can use something other than the price to make the determination of whether to make the trade or not. I used price because thats the most simple to demonstrate. A better way would be to use a technical indicator such as RSI or MACD. I am not a stock trader, so I don't know what those number means, but I'm sure the pros do. I don't even know of a cryptocurrency price API that has technical indicators... If anyone knows of one please let me know in the comments.