Friday, August 2, 2019

BTC/USDT Probability Plot

BTC/USD::Pr(close:n+1>close:n)
I have been working on developing a technical indicator based upon the binomial model. Essentially estimating the 95% CI of the probability of the next close being higher than the current close.
In the above, I have linked directly to the CoinGecko API and so it should be live - up to the minute data for the last 31 days
  For x observed nonconforming units in a sample of size n,
      a two-sided conservative 100(1 − α)% confidence interval for π is
      [π, π􏰆] = [qbeta(α/2;x,n−x+1), qbeta(1−α/2;x+1,n−x)]
      Without making any assumptions about the form of the distribution of the closes,
      it is possible to estimate Pr(close+1>close): the probability of exceeding the current close price.
      We assume naively that both the past and the future close prices are independently and randomly chosen
      from the same stationary process.  An estimate of this probability is the count of previous close prices
      that are greater than the current close price divided by the number of closes up until now.
      The binomial distribution can then be used to compute a one-sided lower confidence bound on this probability.
      The conservative method below gives a one-sided lower 95% confidence bound on the probability of exceeding
      the current close  on a randomly selected close.

NB it would be better to test for stationarity first (eg with an ADF test) and be sure we arent in a trend (i.e. kendall)

Thursday, February 14, 2019

Developing a basic cryptocurrency automatic trading bot

Hi All Today I am going to show you how to make a basic automatic cryptocurrency trading bot.


 What is a bot
A bot is a program that analyses some incoming data and performs trades according to some algorithm automatically

Level of programming background required: some experience required
 - if you don't know what a loop is or what an if-then block is, then this post might be a bit too complex to follow.

Ok, lets get to it then. The first thing to do is to decide a few things.
What language will the bot be in, what exchanges will it trade, etc.

Because Im only making a very simple bot, I am going to use Node.js and I'm going to trade on Kucoin.

So, prerequisites: 

  • Node installed
  • Kucoin account
  • Some kind of text editor (I like Atom)

The core of any algorithm we chose will be to get data from the exchange and analyse it in some way. 

So the first thing we need to do is get data from the exchange.

Luckily, there is a handy node package to assist us called CCXT
 npm install ccxt

while we are installing things, 
 npm install minimist
 npm install jStat
 npm install nodemailer

minimist will be used to handle arguments we want to pass to our node app
jstat will be used for the beta distribution function 
nodemailer will be used to send emails

the pseudocode for our bot is like this:

  1.  every so many minutes:
  2.    download(kucoin.data)
  3.    analyse(data):
  4.      signal = EMA(close, 12) //12 period EMA on close price
  5.      line = EMA(close, 26)
  6.      histogram = line - signal
  7.      rsi = 100-100/(1+EMA(gains,14)/EMA(losses,14))
  8.      resistance = 1-pr(next close > close)  (i.e. see here)
  9.      divergence =sign(Kendall(line))-sign(Kendall(close)) //here Kendall returns the S statistic
  10.      if(RSI>70 & macd<signal & histogram <0 & resistance >0.85 & divergence ==2) then analysis = SELL
  11. if(RSI<30 & macd>signal & histogram >0 & resistance <0.15 & divergence ==-2) then analysis = BUY
  12.      if analysis = sell then set sell limit order to the upper body end of the last candle (i.e. the median + 1.5IQR)
  13.      if analysis = buy then set buy limit order to the lower body end of the last candle (i.e. the median - 1.5IQR)
  14.      otherwise do nothing
  15.    if any trade made then send email with advice of trade

There are a few fundamental functions we will need to implement. 

  1. The EMA (exponential moving average)
  2. a Quantile function
  3. the resistance function
  4. the Kendall function

The pseudocode:
 EMA(data, period)  {  m = 2/(period +1)  ema[1] = data[1] //starting EMA from the first point in the data  for ( x = 2 to x < data.length) {       ema[x] = data[x]-ema[x-1])*m+ema[x-1]  }
  return ema}

Quantile(data, p) {
 rx= get the rank of p
 sort data ascending
 rd= interpolate the position r in the data
 return rd
}

Resistance(x,n) {
 return 1-beta.inv( 0.05, x, n-x+1 )
}


Kendall(x) {
 for each point in x
 let s =sum the sign of the difference from xi to xi+1
 return s
}



The full implementation of the algorithm is here: https://github.com/phraudsta/BasicBot

Thanks for reading
Týr

Monday, January 28, 2019

MTC-BTC Probability Graph

MTC-BTC::Pr(close:n+1>close:n)
I have been working on developing a technical indicator based upon the binomial model. Essentially estimating the 95% CI of the probability of the next close being higher than the current close.
In the above, I have linked directly to the CoinGecko API and so it should be live - up to the minute data for the last 31 days
It is looking at the DOC.COM token (MTC), but could be used on anything, really.
  For x observed nonconforming units in a sample of size n,
      a two-sided conservative 100(1 − α)% confidence interval for π is
      [π, π􏰆] = [qbeta(α/2;x,n−x+1), qbeta(1−α/2;x+1,n−x)]
      Without making any assumptions about the form of the distribution of the closes,
      it is possible to estimate Pr(close+1>close): the probability of exceeding the current close price.
      We assume naively that both the past and the future close prices are independently and randomly chosen
      from the same stationary process.  An estimate of this probability is the count of previous close prices
      that are greater than the current close price divided by the number of closes up until now.
      The binomial distribution can then be used to compute a one-sided lower confidence bound on this probability.
      The conservative method below gives a one-sided lower 95% confidence bound on the probability of exceeding
      the current close  on a randomly selected close.

NB it would be better to test for stationarity first (eg with an ADF test) and be sure we arent in a trend (i.e. kendall)