MtQuid...
Would you be willing to share a basic demo of the Ta-lib integration? I do not live in python so it would be very helpful for me, and I am sure others.
Ok.
We will use TA-Lib for bython from these lovely chaps
http://mrjbq7.github.io/ta-lib/index.htmlAssuming you are working on a debian system you will need to do the following to install ta-lib for python
If on windows or mac then check
http://mrjbq7.github.io/ta-lib/install.htmlsudo apt-get install python-dev
sudo easy_install cython
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xvf ta-lib-0.4.0-src.tar.gz
cd ta-lib
./configure --prefix=/usr
make
#can use make install here if you want
sudo checkinstall
sudo easy_install TA-Lib
Once you have TA-Lib installed this is your strategy
"""
_talib.py
This example strategy will produce some HighStock.js compatible JSON files
"""
import numpy as np
import talib
import datetime
import json
import strategy
import goxapi
from goxapi import OHLCV
class OHLCV_Encoder(json.JSONEncoder):
"""JSONEncoder for class OHLCV()"""
def default(self, obj):
if isinstance(obj, OHLCV):
return [obj.tim, obj.opn, obj.hig, obj.low, obj.cls, obj.vol]
else:
return json.JSONEncoder.default(self, obj)
class Strategy(strategy.Strategy):
"""a protfolio rebalancing bot"""
def __init__(self, gox):
strategy.Strategy.__init__(self, gox)
def slot_history_changed(self, history, _dummy):
"""History has changed so recalculate EMAs"""
candles = []
# read them all - don't wory about the history parameter
for c in reversed(self.gox.history.candles):
candles.append(
OHLCV(
# fix time for HighStock.js JSON import
c.tim * 1000,
# adjust values to be human readable
goxapi.int2float(c.opn, self.gox.currency),
goxapi.int2float(c.hig, self.gox.currency),
goxapi.int2float(c.low, self.gox.currency),
goxapi.int2float(c.cls, self.gox.currency),
goxapi.int2float(c.vol, "BTC")
)
)
self.debug("New EMAs from history with %d candles" % len(candles))
rng = range(len(candles))
iterable = (candles[i].opn for i in rng)
a_opn = np.fromiter(iterable, np.float)
iterable = (candles[i].hig for i in rng)
a_hig = np.fromiter(iterable, np.float)
iterable = (candles[i].low for i in rng)
a_low = np.fromiter(iterable, np.float)
iterable = (candles[i].cls for i in rng)
a_cls = np.fromiter(iterable, np.float)
iterable = (candles[i].vol for i in rng)
a_vol = np.fromiter(iterable, np.float)
iterable = (candles[i].tim for i in rng)
a_tim = np.fromiter(iterable, np.int64)
a_sma = talib.SMA(a_cls, 10)
a_wma = talib.WMA(a_cls, 25)
a_chaikin = talib.AD(a_hig, a_low, a_cls, a_vol)
a_cdlCounterAttack = talib.CDLCOUNTERATTACK(a_opn, a_hig, a_low, a_cls)
# create json compatible with HighStock.js
with open("talib_ohlcv.json", 'w') as outfile:
json.dump(candles, outfile, cls = OHLCV_Encoder)
with open("talib_sma.json", 'w') as outfile:
# first 10 elements contain Nan
json.dump(np.dstack((a_tim[10:], a_sma[10:])).tolist()[0], outfile)
with open("talib_wma.json", 'w') as outfile:
# first 25 elements contain Nan
json.dump(np.dstack((a_tim[25:], a_wma[25:])).tolist()[0], outfile)
with open("talib_chaikin.json", 'w') as outfile:
json.dump(np.dstack((a_tim, a_chaikin)).tolist()[0], outfile)
with open("talib_cdlCounterAttack.json", 'w') as outfile:
json.dump(np.dstack((a_tim, a_cdlCounterAttack)).tolist()[0], outfile)
# Current price in relation to EMA
self.debug("SMA = %f" % a_sma[-1])
self.debug("WMA = %f" % a_wma[-1])
self.debug("CLS = %f" % a_cls[-1])
Run goxtool like so
python ./goxtool.py --protocol=websocket --strategy=_talib.py
Create index.html like this
BTC Highstock - Goxtool/TA-Lib And run a simple web server like this
python -m SimpleHTTPServer 8080
Then go to
http://localhost:8080/index.html and see sunshine
You could have the index.html auto reload with simple script or refresh with your mouse
EDIT: Also, can anyone give me a hint on how to create a separate debug file for my bot? The regular debug goxtool file gets too big and its very cumbersome to check.
To reduce debug clutter for writing strategies I use a crafty tail. I did have a cut on the end but it lagged
tail -f goxtool.log| grep -E 'Strat|Trace|^[^0-9]'
And how are the results?.
I just loaded the chart along with current data from bitcoin charts and they are pretty similar.
http://bitcoincharts.com/charts/chart.json?m=mtgoxUSD&r=1&i=15-minI don't know what weighted moving average function they are using but the data is close enough for my use though I probably should check it out on a few more days before I invest the yacht
If you use this data you will need to alter the timestamp for HighStock.js add on 000 (three zeros)
EDIT: I haven't checked for the 1 hour EMAs but will do some more testing and experimenting later. Using the TA-Lib you can specify the timeperiod and then ignore those first timeperiod output values as the results settle down. I've used the default values from bitcoincharts. Simple at 10 and Weighted at 25.