Let’s write a cryptocurrency bot. (part 3)

beep bop beep

Want to make your own Bitcoin bot? This is part three of a three part series on creating a cryptocurrency trading bot using the boilerplate and framework library called Bowhead for general use in the cryptocurrency space.

— This is a three part article: Part 1, Part 2 and Part 3 (you are here)

In Part 1 we walked through the install and set up of the Bowhead library, we registered accounts on various services and set up our data collection. We looked around for strategies and built one from an article we found online, we discussed indicators and libraries provided with Bowhead and then we built a very basic cryptocurrency bot.

In Part 2 we expanded our indicators to create a full signals responder with 12 signals that can be used to confirm or deny entry and exit positions. We added a full strategies library with twenty real strategies and showed how they work, then we set up a test to combine these strategies with the signals and ran it live and logged the results to the bowhead database. Then we built a real-time GDAX bot that we can expand on, and we got a start on setting up your Bowhead install as a full REST API so you can code up bots or web interfaces in Python, Go, Node or whatever language you prefer which can handle consuming a json REST API.

For Part 3 our goals are:

  1. We are still missing a piece of many traders strategies and that is the the candle patterns. Chart candles are typically eyeballed but can be programmatically identified. Combined with strategies and signals these can be great indicators of trends or for spotting reversals. Bowhead has 61 candle patterns it can identify, I have carefully organized these by function and use and will walk you through them.
  2. We would not want to try to use a candle strategy unless we know if we are in a trend or not. We add in new trend identification indicators and talk about how they can be used.
  3. We introduce pivots to bowhead and discuss how they can be used as entry and exit confirmation.
  4. We need more strategies that combine signals, trends and the candles in the first part of the article, we provide a new library which will put all of these together and will walk through the new methods and explain them.
  5. We need to be able to test these new methods, so we put together a testing script that we can use to show how the previous parts are used.
  6. We set up a testing script to try all combinations and see if we can find new strategies.

As always, you will want to snag the most recent copy of Bowhead.


I would like to point out before getting into part 3 that there are good number of people who believe that much, if not all, of quantitative technical analysis with it’s fanciful named algorithms and exotic sounding candle patterns is nothing more than mystical souped-up numerological ballyhoo. They believe that humans as pattern finding creatures see faces in clouds and yearn to find meaning in the squiggly lines on our computers, so there are books and videos which talk about the ‘efficiency of markets’ and how technicals will just slow you down and stunt your growth as a trader. And, to some extent I can see the view — you cannot fully rely on technicals, it will bite you if you do.

However, I personally believe there is a ton of usefulness in technical analysis for the domains that computers are good at, which is rigid classification, organization, and pattern identification. And with those you can take your ‘naked’ strategy and translate it into a computerized system that will trade while you are driving, or sleeping. I would never suggest using a system you don’t understand, or suggest using a computerized system that is only using one indicator, the idea here is to use many different indicators combined in many different ways to have a consensus for when to enter and when to exit a trade.

My goal with this tutorial is to set the chess board up and teach you how to move the pieces — basically just how to build your own trading system, not just run a strategy that is not well explained. If you go through these articles you should be able to set up a bot with ‘your’ rules to trade ‘your’ way. This entire tutorial is a lead-in to the next planned set of tutorials which will be a three-part series, again with bowhead, on trading using machine learning, both local and cloud-based.

I use OOP PHP for the main library because of the extant market of PHP programmers, there are at least 5,000,000 PHP programmers in the world, and PHP has become a very different language than it used to be, more like Java now than a ‘scripting’ language. Additionally I chose Laravel as it is the best framework I have encountered in any language (if you can find something actually better, let me know) and it makes a lot of things like setting up caching, queues and workers absolutely trivial. PHP, and to an extent Laravel, have a huge install base, it runs much, if not most of the web (including sites like Facebook). Additionally PHP is excellent as a ‘bridge’ language to other languages as it has a wealth of libraries available for virtually everything via composer and packagist.

It is key that Bowhead be simple and easy to work with, so I have taken care to write it in easy to understand OOP syntax and broken it up into logical chunks using traits and helper classes.

That said, this is the last major part in this series and will cover a lot of ground. So, let’s get started.

Chart candle identification

Bowhead comes with the Candles class whose main method, instead of running just one candle identification, runs all 61 candle identifications via a method named allCandles(). The candles will be returned in an array organized with bearish as -100, bullish as 100 or 0 (zero) if not found. The return shows if they are in the current part of the data stream, the recent data (last 15 or so candles) or anywhere in the entire data set passed to allCandles() including ‘where’ with the section of the data that contains the candle. This is all so you can determine if one candle was found at a position and then another was found shortly after — we may want to do some action.

In the following list I’ve organized the TALib candles in Bowhead, so that the name of the candle is on the left which I have attempted to link to Investopedia. Then I have added if it is a bear or a bull (or the color of the candle for indecision) and all of those I link to Bulkowski’s pattern site. If there is a star (*) next to a ‘bear’ or a ‘bull’ that indicates that that candle pattern actually does the opposite of what it is supposed to do in theory. Then, if a candle is a good performer, I have add (gp) and for poor performers I add (pp). These notes are from Bulkowski’s site, he did a massive survey of 4.5M candles for his book and I really like his data-first approach.

Two Crows— (bear*) reversal (really a weak continuation)
Three Black Crows — (bear) reliable reversal (gp)
Three Inside Up/Down — (bull/bear) common semi-reliable reversal
Three-Line Strike — (bear*/bull*) rare continuation (really reversal)
Three Outside Up/Down — (bull/bear) common reliable reversal
Three Stars In The South — (bull) rare very reliable reversal
Three Advancing White Soldiers — (bull), rare very reliable reversal
Abandoned Baby — (bull/bear) rare reliable reversal
Advance Block— (bear*), unreliable reversal (really a continuation)
Belt-hold— (bull/bear), common reversal (bull/bear) (gp)
Breakaway — (bull/bear) rare reversal (pp) as bull, (gp) as bear
Closing Marubozu — (white/black) unreliable continuation pattern
Concealing Baby Swallow — (bull*) rare reversal, (really a continuation)
Counterattack — (bull/bear) common reversals (bull reliable, bear not)
Dark Cloud Cover — (bear), semi-reliable reversal (gp)
Doji — (bull/bear) common and unreliable reversal
Doji Star
 — (bull*/bear*) reversal (continuation)
Dragonfly Doji— (bull) unreliable reversal
Engulfing Pattern — (bull/bear) reversal candle
Evening Doji Star — (bear) reliable reversal candle
Evening Star — (bear) very reliable reversal candle
Up/Down-gap side-by-side white lines — (bull) reliable rare continuation
Gravestone Doji— (bear) reversal or indecision, common in bull market
Hammer — (bull) reliable reversal, but price does not travel far back up
Hanging Man — (bear*) unreliable reversal (really a bullish continuation)
Harami Pattern — (bull/bear) unreliable reversal
Harami Cross Pattern— (bull/bear) unreliable reversal
High-Wave Candle — common indecision pattern, see more
Hikkake Pattern — (bull/bear) common unreliable continuation or reversal
Modified Hikkake Pattern — (bull/bear) same as regular but only reversal
Homing Pigeon— (bull*) common reversal or continuation
Identical Three Crows— (bear) very reliable bearish reversal
In-Neck Pattern — (bear) rare unreliable continuation (gp)
Inverted Hammer — (bull) reversal (gp)
Kicking — (bull/bear) unreliable reversal (pp)
Kicking, by the longer marubozu — same as Kicking
Ladder Bottom — (bull) very rare bullish reversal (gp)
Long Legged Doji— common indecision, see more
Long Line Candle— (white/black) very common unreliable continuation
Marubozu — (white/black) very common unreliable continuation
Matching Low — (bull*) reversal (really a bearish continuation)
Mat Hold— (bull) rare reliable continuation (pp)
Morning Doji Star— (bull) rare reliable reversal (gp)
Morning Star— (bull) reliable reversal (gp)
On-Neck Pattern— (bear) unreliable continuation (gp)
Piercing Pattern — (bull) reliable reversal (gp)
Rickshaw Man— unreliable indecision (pp)
Rising/Falling Three Methods — (bull/bear) rare reliable continuation (gp)
Separating Lines — (bull/bear) reliable continuation
Shooting Star — (bear) common reliable reversal (2-line=continuation)*
Short Line Candle — (white/black) common indecision
Spinning Top — (white/black) indecision
Stalled Pattern — (bear*) reversal, (really a bull continuation) (pp)
Stick Sandwich — (bull*) reversal (really a bear continuation) (gp)
Takuri line— (bull) reliable reversal
Tasuki Gap — (bull/bear*) rare — bull reliable, bear unreliable
Thrusting Pattern — (bear*) unreliable continuation (really bullish reversal)
Tristar Pattern— (bull/bear) unreliable reversal
Unique 3 River — (bull*) rare reversal (really a bearish continuation)
Upside Gap Two Crows — (bear*) reversal (really bullish continuation) (pp)
Upside/Downside Gap Three Methods — (bull*/bear*) reversals, (both really are continuations)

Keep this list handy for reference.

As always, I have created a test script so you can see the current candles on Forex pairs. Green is bullish, red is bearish. You can find all the test commands in the app/Console/Commands/ directory.

php artisan bowhead:test_candles
php artisan bowhead:test_candles

You may be curious why we would want to include unreliable candles here, this is not to say they are not valuable, they are, they just carry much less weight than other similar candles and they can still be used, just not by themselves. Indecision candles are valuable in that if you spot them, that means that your bot may want to hold off entering a position. Additionally ones like the “In Neck Pattern” (and others) have excellent performance if they don’t reverse which is something worth knowing, so it is well worth your time to read the links here and understand these candle patterns.

I recognize that the list is pretty daunting and there is a lot of data to process there, so I have organized the candles to be easier to manage in Bowhead. They are organized into groups of reversals, continuations, counters (ones which work opposite as expected) and indecision, all with their percentage of expected behavior (anything under 60% is considered unreliable). Also a new method, called candle_value() located in the CandleMap trait, we will use which can return a candle score depending on what candles recently have been spotted and which we currently have. However, this is not foolproof and you would still be well served to read up on the candles in order to develop your own strategies.

Candles by themselves are not really all that useful, they can be, yes — but in general they are used with other indicators and typically within a trend.

Which leads us to…

Spotting trends

We need to be able to spot trends, for this we will need some more indicators, ones which we can use to tell us if we are in a trend or if we are cycling/ranging. We can also use what are call pivots to signal us if we are in a trend.

While reading a paper on Gold and Silver HFT with Hilbert Transforms I decided to add in the HT functions to Bowhead for trend spotting as they would be perfect indicators for the system. Hilbert Transforms are way out of the scope of this article and are covered in an entire book (you can find online). I have worked up the HT functions into the Bowhead library and I will briefly introduce them here.

Hilbert Transform - Trend vs Cycle Mode — Simply tell us if the market is either trending or cycling, with an additional parameter the method returns the number of days we have been in a trend or a cycle.

Hilbert Transform - Instantaneous Trendline — smoothed trendline, if the price moves 1.5% away from the trendline we can declare a trend.

Hilbert Transform - Sinewave (MESA indicator)— We are actually using DSP on the prices to attempt to get a lag-free/low-lag indicator. This indicator can be passed an extra parameter and it will tell you in we are in a trend or not. (when used as an indicator do not use in a trending market)

Market Meanness Index (link) — This indicator is not a measure of how grumpy the market is, it shows if we are currently in or out of a trend based on price reverting to the mean.

To test our trend identification we have the following command, where green named function means we are in a bull trend (or simply a trend) and red is a bear trend. Indecision can happen between these trend identifiers, as different methods are used, but if more than two agree the trend is real.

php artisan bowhead:test_trends
php artisan bowhead:test_trends

Pivot points

Fibonacci retracement

Bowhead provides several types of Pivots which can be used for determining trend and are very useful for setting take profit and stop loss levels as they identify support and resistance levels.

Fibonacci Retracement — The most common method for picking stop losses and take profit points, uses prior grouping of data for current pivots. So commonly used it has been suggested that it is self fulfilling support and resistance because everyone expects them to be at those locations.

Demark levels — Named after it’s inventor, it is forward weighted version standard pivot points, so is more useful on lower timeframe charts. (1m, 5m)

Standard pivot points — There are many ways to compute these, this is done via the method outlined here.

All three return an array with P, S1, S2, R1 and R2 (fib has S3 and R3), P=Pivot, S=Support and R=Resistance. If we are above the resistance levels we are generally considered in an uptrend and alternately if we are below the support levels we are considered in a downtrend. You can use momentum-based indicators to determine which resistance to use for a take profit — R1 for low, R2 for medium, R3 for high etc, and set your stop losses to your preferred support/risk level.

Strategies with candles:

Lets try out two quick strategies with indicators and candlesticks which I place in a trait called CandleStrategies

  1. Moving averages and candlesticks — strategy bowhead_ema_candle() using an EMA20 crossover with any reversal candle with over 60% success.
  2. RSI and candlesticks — strategy bowhead_rsi_candleusing() an RSI > 70 or < 30 with any reversal candle with over 60% success.

Take a look at the methods in the trait to see how this is done, it is pretty simple — here is the moving averages strategy with comments.

$ema  = trader_ema($data, 20); // get the ema
$ema = @array_pop($ema) ?? 0; // get the current ema value
$cand = $this->candle_value() // get the candle_values
$current_price  = array_pop($data['close']); // get the current 
$previous_price = array_pop($data['close']); // get prior price
if ($cand['current']['indecision'] > 0) {
return 0; // don't trade on indecision
// if current reverse_bear > 60% and ema cross-over
($cand['current']['reverse_bear'] > 60 && $current_price > $ema && $previous_price < $ema) {
return 1; // buy
// if current reverse_bull > 60% and ema cross-under
($cand['current']['reverse_bull'] > 60 && $current_price < $ema && $previous_price > $ema) {
return -1; // sell
return 0; // default

Obviously in a full trading system we will use pivots to set stop loss and take profit.

Finding advanced strategies with random walks

There are a lot of candles and I would assume they work differently in different markets and even on different currency pairs. We are going to use our signals library, our strategies library and our candles library all together and will try out every single combination of strategies with signals and candles on one pair, the BTC/USD pair. Next, we are going to use our signals library along with our strategies library and test those.

php artisan bowhead:random_walk

This command will run a through all the indicators, and signals and then current candles and do demo orders on them each minute it will also check up on orders that are currently active to see if they need to be updated in the database.

Using the same SQL from part2, you can check up on the status of these to see how they fare.

More strategies

FAMA/MAMA in action

I felt that Bowhead could use a few more strategies to help fill things out. All the strategies are designed to be used with trend indicators, signals and candles.

FAMA/MAMA — 5m/1h — The mother of all moving averages, one slow, one fast, when the fast one crosses the slow one you buy or sell.

Powerranger —1h — Stochastic strategy set up to work in a ranging/cycling market.

5th Element —1h — MACD strategy with confirmation.

Trend bouncer — 1h — two sets of Bollinger bands, trade on touch and retracement.

EMA/ADX — 5m — two EMA’s (12/36) and ADX for confirmation.

SAR/SMA —1m — Scalping strategy with SMA60 and SAR for confirmation.

Putting it all together into a trading system

Signals, strategies, trend-spotting, pivots and candles, where to begin?

Typically for a trading system you want to decide what you want to trade, to start just pick one thing and get to know what pair, BTC/USD or one of the forex pairs (I am very comfortable with USD/JPY) and start small. How you do this is to decide if you want to do a trend following system, a breakout system or a counter-trend system. You also need to determine if you want to play both sides and go long and short in the same system.

To start, I would suggest the following basic system:

  1. BTC/USD — five minute
  2. ‘Uptrends only’ — using +5 signals and trend spotting
  3. have pre-set take-profit and stop loss points set with pivots. (note: with BTC you will need to play with these. Sometimes during busy days movement BTC can swing $30 on either side of a pivot middle line so add the current spread to the stop loss and the take profit pivot points. If you are unsure, add $15 to either side)
  4. Select one of the 5-minute time-series strategies that you understand, bowhead_famamama or bowhead_stoch_adx would both be good to start.
  5. Keep a tally of your money, not just in that database but make sure your strategy is mindful of how much you have.
  6. Run it in test mode for a while.

Once you see how this is functioning, you could branch out and add multiple strategies with this and fine-tune your signals and trend spotting and add in some candles based on percentages for confirmation.

I would plan out my strategy on paper first and then run tests, and more tests, and then just let it sit ‘testing’ live for an entire month before I put real money on it. You will also want to make sure not to over-engineer it, making it so restrictive that it seldom does a trade. Your system should be able to find at least a couple trades per day — depending on your time series, on a 1 minute series it should find a good number, on a 1 hour series, much less.

Really, the sky is the limit here for what you can do, there is an unlimited variety of potential here for you to work with so — now, it is in your hands, you have the toolset in Bowhead and it is organized and should be a simple matter for you to create your system in a single evening.

Final words

Thanks for reading through this tutorial series. I will be updating here with the video tutorials I hope to create to accompany this series. I don’t have a lot of experience with creating video tutorials, so I need to do some research.

I didn’t get to everything I wanted to get to on this series, and Bowhead is far from being finished. The live GDAX bot I had planned for the second article didn’t get finished because GDAX was down and/or returning errors during parts of the time I was trying to work on it. The REST API is loosely fleshed out but needs a ton of work still. And I still have not even added in a full backtesting system with charts. (The console component can do charts, in a terminal, but the big question I get is about charts and graphs). Backtesting is of course a major component and I this is going to be added. I have done some work on this, but want to have it done right.

I have a planned series of articles that I would like to do on machine learning and tying it into Bowhead. ML is a much larger topic and much of that will require some different software installed on servers, much of which can be notoriously difficult to install, and utilizing cloud systems such as Amazon’s machine learning systems or the Google cloud ML offerings.

As of July 2017 AWS has the least expensive offering. I want to focus on a few key areas that are typically not covered, which would be more forecasting trends instead of individual price movements and attempting to find volatility patterns. If you are interested, Bowhead has prediction scripts written in Python already included.

A note about risk

As always, PLEASE NOTE: Cryptocurrency and Forex trading constitute extreme risk, and automating that risk could mean that you compound your losses. Do not use money you cannot afford to lose. ALWAYS trade in DEMO mode with any strategy you build for a long time before you turn it loose with your money.

Get social:

If you enjoyed this article, recommend by clicking on that heart icon on the left of this text, and feel free to share it, I work hard on these and feel they are useful

Show your support

Clapping shows how much you appreciated joel degan’s story.