Created
May 28, 2013 22:01
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
_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