visit
Pumps, dumps, and liquidation. Welcome to Bitcoin.
Trading Bitcoin has once again become the newest, hottest thing all the investors are trading. The possibility of actually profiting off that $1,000 candlestick that shot out of nowhere is very alluring.
However, it's not easy to predict these fluctuations, and getting lost in the market is something all-too-familiar for many of us.
For these reasons, today I will be showing you how to program a Bitcoin trading bot in less than 10 minutes. Prepare to cut out the emotion, and bring in the algorithms. Trade at your own risk.
Before any programming, we must first find a viable strategy to use to play the market and model the bot after. For this article, I chose to use a simple volume-based trading strategy.
The strategy is simple: when volume peaks, there is more interest in the market, and this means the trend with the volume peak will probably continue in the future.
In the image below, three distinct peaks in volume are circled. The arrows represent that even if the peak in volume is followed by a dump, it will usually rise higher than current market value in the near future, as the trend is still upwards.
Now that we have a basic strategy in mind for this bot, we can begin to program!
Perhaps the most basic language to write our bot in will be PineScript, TradingView's language for writing indicators and scripts.
To begin programming, navigate to the PineScript editor in your TradingView account (shown below).
Now, open New > Blank strategy script.
At this point, your editor should look something similar to this://@version=4
strategy("My Strategy", overlay=true)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
You can now begin to program.
Firstly, we must derive an algorithmic method to find a "peak" in Bitcoin's volume based on the volumes of surrounding candlesticks. To pull values for volume, insert these lines into your editor.
volumeOne = volume[1]
volumeTwo = volume[2]
volumeThree = volume[3]
lastVolume = volume[0]
averageVolume = (volumeOne+volumeTwo+volumeThree)/3
Essentially, for every candlestick on our chart, these lines will pull the 3 volumes of the 3 candlesticks before it and then calculate their average. These values will be necessary for our next step. To determine whether volume has peaked at a given candlestick, I will be using a simple method of determining whether the current bar's volume is 5 times greater than that of the average of the 3 bars before it. Note: in a more advanced bot, you could calculate the standard deviation or IQR of the data to determine peaks.
This calculation will be entered as two boolean values as shown below, either true to long, true to short, or false to not long, false to not short.
longCondition = lastVolume > averageVolume * 5 and close[0] > open[0]
shortCondition = lastVolume > averageVolume * 5 and close[0] < open[0]
Having written our long and short conditions, the buy and sell orders may finally be added to our script. This can be done as shown below. Note: limit orders are being used because most exchanges charge high taker fees, and our bot aims to avoid those, as it trades at a high frequency.
strategy.order("long", true, limit = close - 1, when = longCondition)
strategy.order("short", false, limit = close + 1, when = shortCondition)
Finally, our Bitcoin bot has been finished, and is technically functional as is. However, there is one more thing we must add to it: stop-loss and take-profit orders. Stop-loss orders prevent bad trades from losing too much money and take-profit orders allow us to get out with out profit before the price could take a hit.
To implement this, we will add two values that the user may input: percentage change to cause a stop-loss, and percentage change to cause a take-profit. The orders will be written as shown below, and on the next image, the two user-entered variables are programmed at the top.
strategy.exit("exit", "long", profit = abs(close*profit - close), loss = abs(close*stopLoss - close))
strategy.exit("exit", "short", profit = abs(close*profit - close), loss = abs(close*stopLoss - close))
And with that, you've finished programming your Bitcoin trading bot! Here is what you're script should look like now:
//@version=4
strategy("Volume Momentum")
stopLoss = input(title="Stop loss", defval=0.9)
profit = input(title="Profit gain", defval=1.2)
volumeOne = volume[3]
volumeTwo = volume[1]
volumeThree = volume[2]
volumeFour = volume[4]
volumeFive = volume[5]
lastVolume = volume[0]
averageVolume = (volumeOne+volumeTwo+volumeThree)/3
plot(lastVolume/10000, color=#00ffaa)
plot(averageVolume/10000, color = #000000)
longCondition = lastVolume > averageVolume * 5 and close[0] > open[0]
shortCondition = lastVolume > averageVolume * 5 and close[0] < open[0]
strategy.order("long", true, limit = close - 1, when = longCondition)
strategy.exit("exit", "long", profit = abs(close*profit - close), loss = abs(close*stopLoss - close))
strategy.order("short", false, limit = close + 1, when = shortCondition)
strategy.exit("exit", "short", profit = abs(close*profit - close), loss = abs(close*stopLoss - close))
In an account with $100, trading $10 at a time, the bot profited near 10% in a week! In the future, we can add filters to make the bot pickier in its trades and thus more accurate.
And that's it! Congrats!