def sell(self): """ Executes a market sell order liquidating the entire position. :return: (GdaxOrder) The posted GdaxOrder object reflecting the sell order. """ if self.sell_order is not None: raise Exception("Existing sell order - " "cannot re-sell: " "{}".format(self.sell_order)) o = GdaxOrder(self.gdax, self.pair, order_type='market', side='sell', size=self.size) o.post() # Wait a few moments # and update the order with fill info. sleep(5) o.update() msg = '{} Trailing stop order @ price {} ' \ 'executed - {}'.format(now(), self.price, o) self.notify_user(msg) self.sell_order = o return o
def update(self, data=None): """ Updates GdaxOrder class properties using data provided when calling the function or called automatically by the API. :param data: (dict) Should contain fields such as id, price, size, side, order_type, time_in_force, etc. :return: (None) """ if data is None: if self.id is None: raise Exception("Order has no ID and " "no data was provided to update.") data = self.gdax.get_orders(self.id) self.id = data.get('id') self.price = float(data.get('price', 0)) self.size = float(data.get('size', 0)) self.side = data.get('side') self.order_type = data.get('type') self.time_in_force = data.get('time_in_force') self.post_only = data.get('post_only') self.created_at = timestamp_from_utc(data.get('created_at')) self.funds = float(data.get('funds', 0)) self.fill_fees = float(data.get('fill_fees', 0)) self.filled_size = float(data.get('filled_size', 0)) self.executed_value = float(data.get('executed_value', 0)) self.status = data.get('status') self.settled = data.get('settled') self._update_time = now()
def sync_account_info(self, force=True): u = self.times.get('a') if not force and u: t = now_minus(seconds=self.sync_interval) if u > t: return False self.gdax.sync_accounts() self.times['a'] = now()
def export_chart(): d = GdaxChartData( g, 'ETH-USD', now_minus(days=1), now(), granularity=60 * 5, ) d.df.to_csv(chart_path, index=False)
def get_next_sync_times(self): times = dict() for ctype, interval in self.sync_intervals.items(): synced = self.times.get(ctype, None) if synced is None: times[ctype] = now() else: due = synced + DateOffset(seconds=interval) times[ctype] = due return times
def sync_ticker_info(self, force=True): u = self.times.get('t') if not force and u: t = now_minus(seconds=self.sync_interval) if u > t: return False t = self.gdax.get_ticker(self.name) self._price = float(t['price']) self._volume24hr = float(t['volume']) self.times['t'] = now()
def sync_24hr_stats(self, force=True): u = self.times.get('s') if not force and u: t = now_minus(seconds=self.sync_interval) if u > t: return False s = self.gdax.get_24hr_stats(self.name) self._volume24hr = float(s['volume']) self._high24hr = float(s['high']) self._low24hr = float(s['low']) self.times['s'] = now()
def generate_candles(product, gdax=None, out_path=None, start=None, end=None, granularity=60 * 60 * 24): """ Generates a .csv file containing open, high, low, close & volume information for a given product. :param product: :param gdax: :param out_path: :param start: :param end: :param granularity: :return: (GdaxChartData, str) Returns the generated ChartData object along with the out_path. """ if gdax is None: gdax = Gdax() if out_path is None: import os from stocklook.config import config d = config['DATA_DIRECTORY'] c = product.split('-')[0] n = '{}_candles.csv'.format(c) out_path = os.path.join(d, n) if start is None: from stocklook.utils.timetools import now_minus start = now_minus(weeks=4) if end is None: from stocklook.utils.timetools import now end = now() data = GdaxChartData(gdax, product, start, end, granularity) data.df.to_csv(out_path, index=False) get_buypoint(data) return data, out_path
def get_chart(self, time_frame='5M'): """ Access to GdaxChartData objects that are automatically created, cached, and/or refreshed on an interval. :param time_frame (str, default '5M') The timeframe interval of chart data to get. 5M: 5 minutes 15M: 15 minutes 1H: 1 hour 4H: 4 hours 1D: daily :return: (stocklook.crypto.gdax.chartdata.GdaxChartData) """ assert time_frame in self.TIME_FRAMES key = 'chart_data_{}'.format(time_frame) chart = self._charts.get(key, None) granularity, hours_back, seconds = self.TIMEFRAME_MAP[time_frame] timed_out = timeout_check(key, t_data=self._t_data, seconds=seconds) start = now_minus(hours=hours_back) end = now() if chart is None: from stocklook.crypto.gdax.chartdata import GdaxChartData chart = GdaxChartData(self.gdax, self.product_id, start, end, granularity=granularity) chart.get_candles() self._charts[key] = chart elif timed_out: chart.start = start chart.end = end chart.get_candles() return chart
from stocklook.crypto.gdax import Gdax, GdaxProducts, GdaxOrderBook, GdaxChartData, get_buypoint, GdaxDatabase from stocklook.utils.timetools import now_minus, now if __name__ == '__main__': g = Gdax() #for a in g.accounts.values(): # print(a) # print("\n") # print("Total account value: ${}".format(g.get_total_value())) pair = GdaxProducts.LTC_USD book = GdaxOrderBook(g, pair) product = g.get_product(pair) start = now_minus(days=5) end = now() granularity = 60 * 60 * 24 chart = GdaxChartData(g, pair, start, end, granularity) price = product.price res_price, res_qty = book.get_next_resistance() su_price, su_qty = book.get_next_support() avg_vol = int(chart.avg_vol) avg_range = round(chart.avg_range, 2) avg_close = round(chart.avg_close, 2) avg_rsi = round(chart.avg_rsi, 2) cdf = chart.df bdf = book.get_data_frame() bdf = bdf[bdf['qty'] >= 5]
def sync_chart_data(self): """ Ensures GdaxChartData objects are created and kept up-to-date. These objects are assigned to GdaxAnalyzer objects so they'll all be analyzing the same set(s) of data refreshed on a timely basis. ChartData objects are assigned as follows: GdaxTrader.h: 4 hours of data, 5 minutes timeframes GdaxTrader.d: 1 day of data, 15 minutes timeframes GdaxTrader.w: 1 week of data, 4 hours timeframes GdaxTrader.m 1 month of data, 4 hours timeframes Default Timeframes are as follows: GdaxTrader.h: 3 minute refreshes - most-up-to-date GdaxTrader.d: 15 minute refreshes GdaxTrader.w: 2 hour refreshes GdaxTrader.m: 6 hour refreshes :return: """ times = self.get_next_sync_times() t = now() h_due = times['h'] <= t d_due = times['d'] <= t w_due = times['w'] <= t m_due = times['m'] <= t # Hour Chart if self.h is None or h_due: start, end = now_minus(hours=4), now() granularity = 60 * 5 if self.h is None: self.h = GdaxChartData(self.gdax, self.product, start, end, granularity) else: self.h.refresh(start=start, end=end) self.times['h'] = end # Day chart if self.d is None or d_due: start, end = now_minus(hours=24), now() granularity = 60 * 15 if self.d is None: self.d = GdaxChartData(self.gdax, self.product, start, end, granularity) else: self.d.refresh(start=start, end=end) self.times['d'] = end # Week chart if self.w is None or w_due: start = now_minus(days=7) end = now() granularity = 60 * 60 * 4 if self.w is None: self.w = GdaxChartData(self.gdax, self.product, start, end, granularity) else: self.w.refresh(start=start, end=end) self.times['w'] = end # Month chart if self.m is None or m_due: start = now_minus(months=1) end = now() granularity = 60 * 60 * 24 if self.m is None: self.m = GdaxChartData(self.gdax, self.product, start, end, granularity) else: self.m.refresh(start=start, end=end) self.times['m'] = end book = self.gdax.get_book(self.product) # Make sure analyzers aren't missing charts. # if h chart is missing all are probably missing. for a in self.analyzers.values(): if a.h is None: a.register_data(self.h, self.d, self.w, self.m) return self.h, self.d, self.w, self.m
trade_df.to_csv(tout_path, index=False) print("Done: {}".format(out_path)) try: top_id = int(strat_df.iloc[0]['maker_id']) print("Top decision maker: {}".format(strat.makers[top_id])) except: pass return tout_path, trade_df if __name__ == '__main__': data_dir = config['DATA_DIRECTORY'] product = 'LTC-USD' day_range = 60 start = now_minus(days=60) end = now() granularity = 60 * 60 * 4 grans = [(60 * 60, 4), (60 * 15, 3)] results = list() for i in range(30): fp, df = run_macd_rsi_decisions(data_dir, product, start, end, granularity, overwrite=False) results.append((fp, df)) start -= pd.DateOffset(day_range) end -= pd.DateOffset(day_range) print("Start: {}, End: {}".format(start, end))