EMA-Volatility Trading Algorithm (For Bots)


EMA-Volatility Trading is a speculative strategy that uses market volatility and average price support to its advantage. It aims to sell to people who are pushing the price very high, buy from people who are pushing the price very low, and reset when it returns to a 10-period EMA (Exponential Moving Average). At all other times, it does nothing. It does not predict long term price movements, but instead simply tries to profit in both fiat and cryptocoin as the market moves. It also leaves some coins in the account should the price trend upwards without volatility, and some cash in the account should it trend down without volatility, which can be traded or adjusted manually. The algorithm can be executed manually or applied to a trading bot, which the author has already implemented with successful results using the python programming language. Should you be interested in obtaining a trading bot, when it is available it will be linked here, or you may apply the rules contained in this algorithm to any other bot you may have written or purchased already.

Basis of observation

When a large number of people start buying cryptocoins (or any commodity, for that matter), emotional urgency to not miss the boat tends to take over and cause excessive buying, and when the price stops rising or even falling, they begin to feel the risk of their position and sell, driving the price back down. Generally, the more people caught up in this action, the more powerfully the price moves. Since the EMA follows price, when volume dies down, the price and EMA once again become harmonious, but while volume is high, the price can move very quickly, then return to touch the EMA multiple times, before continuing upwards or downwards depending on the trend. Each time the price is away from the EMA a large amount (10-30%), there is a high chance of overpurchase or oversale, and a price retraction. Going against the market at this time can lead to a profit as the price naturally returns to the EMA. More profit is made the quicker the price returns to the EMA, since the EMA continues to follow price over time, and a slow return might be an indicator of a strong market trend.

Method of interaction

At some point in time, an initial position must be set. The initial position will place half of the total cash value of the account (cash + cash value of coins) into actual cash, and the other into coins, and an initial EMA is created from market data. This is the “RESET” stance of the trading algorithm, when initiating the algorithm it is best to start when the price is naturally close to the current EMA, but it is not necessary, as the algorithm will equalize it after the first trade→reset sequence. Every time a reset is called, it will evaluate the total cash value at current market price of the account, and buy or sell coins to make half the value up in cash, and half up in cryptocurrency. I use 4-hour intervals in this guide.

There are 3 types of trades that the algorithm / bot will execute: Trigger trades, EMA Trades, and Resets

  • Trigger Trades - short term trades that only trade once per interval, to catch a fast move in the market. These are generally optional and need not be implemented for a lower risk strategy.
  • EMA trades - interval trades which trade based on the closing price of the interval candlestick. This is the bread and butter trade of the algorithm.
  • Reset trades - Once a trade has been made the algorithm is allowed to reset once the price meets the EMA.

Every 4 hours (in this example I use 4-hr candlesticks, but the implementer can make this whatever they wish), a new EMA is calculated based on the closing price of the last candlestick, and it is decided whether to execute a trade, or do nothing. If a trade is attractive, 10% of the value of available coins is sent to market if the trend is upwards (ie 10% of coins are sold), and 10% of the value of available cash is sent to market if the trend is down (we want to start buying when it's cheaper).

The price is polled every 10 seconds to see if a Trigger trade can be made, or whether the account should be reset. Upon reset, the total value of the account is calculated based on market value, and a trade is executed which sets the account to 50% cash and 50% cryptocurrency.

Potential return

This trading bot will only profit in volatile markets, but can be left to trade without having to watch the price. It is a long term strategy for holders of coins that wish to hedge their risk against market movement either way, since most of the time half of the total starting account will be stored in cash. The downside to this, is that if the price continues a long term increase with no volatility, an investor who wholly held their money in cryptocurrency would have a greater account value in terms of fiat than those who followed this strategy, and the upside is that if the price continues a long term decrease with no volatility, the investor with 100% of their portfolio in coins would lose much more value. This strategy is most suited to people who wish to invest in cryptocurrencies but with reduced risk, foregoing long term price potential but gaining interest potential. This strategy allows the cash and coins in an exchange account to perform work at market, both reducing and profiting from general volatility by providing market liquidity when it is most needed.

The algorithm is estimated to achieve 3-5% average growth of capital per month, on the 4hr interval, based on an average trade profit of 6% per volatile move, in the currency losing value in the move. If the price is going up, the algorithm will sell 10% of its coin holdings as long as the price is 10% greater than the EMA. If the price is going down, the algorithm will buy 10% of its USD holdings worth of cryptocurrency, as long as the price is 10% less than the EMA. The algorithm has been implemented into a bot which has successfully profited according to design in two months of running it, but has not had enough time to fully verify the long term return rate.

Fig 1.1 is an example of the expected trades for a trading bot following this strategy, using a 4hr MtGox bitcoin/USD chart from November 10, 2013 to December 27, 2013. As you can see if you study the graph, most trades are profitable with a few losses, where the EMA dropped below the price before the price returned to the EMA. This should be a relatively common occurrence, but as you can see, the profitable trades far outweighed the loss trades during this period. A trade is considered profitable when the price bought or sold is advantageous compared to the next reset price.


The Algorithm

This is intended to be run by a bot, but it can be run manually if you wish.

  Calculate initial EMA
    SMA = sum (last 10 candle closing prices) / 10
    EMA = (market price - SMA) * 0.1818181818 + SMA

Main Loop
(loop indefinitely every 10 seconds or so to avoid spamming the exchange servers - they'll ban you if you don't throttle)
	# calculate the current price / EMA ratio
	ratio = (100 * market price / EMA) - 100
	# if we need to reset, do it
	if reset_flag is true
		if (ratio < 0 and last_trade was 'high') or (ratio > 0 and last_trade was 'low')
			# set flags so we don't reset again
			set reset_flag to false
			set last_trade to none
			# calculate required btc for a proper 50/50 balance
			requiredBTC = (balanceUSD + balanceBTC * market_price) / (2 * market_price)
			# buy or sell btc as appropriate
			if balance_BTC < requiredBTC
				buy (requiredBTC - balanceBTC) btc @ market price
				sell (balanceBTC - requiredBTC) btc @ market price
			# if we're using trigger trades, set those flags to true so we can do them again
			set trigger_flag to true 
			set trigger30_flag to true 	
	# check triggers if we're using them
	if trigger_flag is true
		# this trigger fires if price is 10% higher than EMA, and sells 10% of BTC balance
		if market_price > EMA * 1.1  
			set trigger_flag to false
			set reset_flag to true
			sell (balanceBTC * 0.1) btc @ market_price
			set last_trade to 'high'
		# this trigger fires if price is 10% lower than EMA, and buys 10% of USD balance worth
		else if market_price < EMA / 1.1 
			set trigger_flag to false
			set reset_flag to true
			buy (balanceUSD * 0.1 / market_price) btc @ market_price
			set last_trade to 'low'

	# check 30% trigger if we're using it
	if trigger30_flag is true
		# this trigger fires if price is 30% higher than EMA, and sells 20% of BTC balance
		if market_price > EMA * 1.3
			set trigger30_flag to false
			set reset_flag to true
			sell (balanceBTC * 0.3) btc @ market_price
			set last_trade to 'high'
		# this trigger fires if price is 30% lower than EMA, and buys 20% of USD balance worth
		else if market_price < EMA / 1.3
			set trigger_flag to false
			set reset_flag to true
			buy (balanceUSD * 0.2 / market_price) btc @ market_price
			set last_trade to 'low'

	# check time to see if we need to update EMA (14400 = 4 hours in seconds)
	if current time - last time I checked > 14400
		EMA = (market_price - EMA) * 0.1818181818 + EMA # this can be read as "(new)EMA = (market_price - (old)EMA) * 0.1818181818 + (old)EMA"
		# the trigger here is the same as the short trigger, except it only checks once every 4 hours, not every loop
		if market_price > EMA * 1.1
			set reset_flag to true
			sell (balanceBTC * 0.1) btc @ market_price
			set trade_type to 'high'
		else if market_price < EMA / 1.1
			set reset_flag to true
			buy (balanceUSD * 0.1 / market_price) btc @ market_price
			set trade_type to 'low'


You can adjust the percentages as you like in the algorithm.

The parts where it compares market_price to EMA * 1.X is the percentage required to execute a trade. If you want more trades, make this lower, if you want fewer trades, make this higher. You can also change the percentage of balance used per trade. Where you see a buy or sell (balanceBTC * 0.X), the X can be changed to any percentage you like.

If you'd like to trade more of your balance per trade, make this higher, if you'd like to trade less, make this lower. Keep in mind, though, that if the price starts trending and you set it too high, you will have fewer and fewer coins to sell at higher prices, and have a greater chance of losing money, so use caution. The values I use (10% to trigger a trade, 10% of balance value bought or sold) is a relatively conservative figure.

Investing | Cryptocurrency | Currency

QR Code
QR Code ema-volatility_trading_algorithm_for_bots (generated for current page)