Skip to content

Instantly share code, notes, and snippets.

@aido
Created May 28, 2013 22:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aido/5666513 to your computer and use it in GitHub Desktop.
Save aido/5666513 to your computer and use it in GitHub Desktop.
A simple TA bot for goxtool Mt.Gox trading bot framework making use of the TA-Lib library (http://ta-lib.org) and the Cython based wrapper ta-lib (https://github.com/mrjbq7/ta-lib).
"""
_talib.py
"""
import numpy as np
import talib
import datetime
import strategy
import goxapi
from goxapi import OHLCV
class Strategy(strategy.Strategy):
"""a TA 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(
c.tim,
# 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)
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)
# Current price in relation to EMA
self.debug("Simple Moving Average (SMA) = %f" % a_sma[-1])
self.debug("Weighted Moving Average (WMA) = %f" % a_wma[-1])
self.debug("Chaikin A/D Line (AD) = %f" % a_chaikin[-1])
self.debug("Counterattack (CDLCOUNTERATTACK) = %f" % a_cdlCounterAttack[-1])
self.debug("CLS = %f" % a_cls[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment