def test_add_candle_puts_two_candles_in_list(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) cat.add_candle(candle2) assert cat.candles_list == [candle1, candle2]
def test_delete_candle_removes_the_candle_from_list(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) cat.add_candle(candle2) cat.delete_candle(54) assert cat.candles_list == [candle2]
def test_delete_both_candles_gives_empty_list(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) cat.add_candle(candle2) cat.delete_candle(54) cat.delete_candle(2) assert cat.candles_list == []
def test_delete_candle_gives_error_if_candle_not_in_list(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100") candle3 = Candle("Beach", 3, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) cat.add_candle(candle2) with pytest.raises(ValueError): cat.delete_candle(candle3) assert cat.candles_list == [candle1, candle2] with pytest.raises(ValueError) as error: cat.delete_candle(candle3) assert str(error.value) == "Item not found"
def build_candles(ticker: str, trader: shift.Trader, state: Dict[str, Any], end_time): queue_size = 30 while trader.get_last_trade_time() < end_time: s = 0 price = trader.get_last_price(ticker) candle = Candle(price, price, price, price) while s < candle_size: price = trader.get_last_price(ticker) if price < candle.low: candle.setLow(price) if price > candle.high: candle.setHigh(price) sleep(1) s += 1 price = trader.get_last_price(ticker) candle.setClose(price) try: state[candles_key][ticker] += [candle] except KeyError: state[candles_key][ticker] = deque(maxlen=queue_size) sleep(0.5) state[candles_key][ticker] += [candle]
def candles_from_file(filename): with open(filename) as plik: linie = plik.read().split("\n")[1:-1] values = [l.split(";") for l in linie] dates = [v[0] for v in values] candles = [Candle([to_float(s) for s in v[1:5]]) for v in values] return dates, candles
def update_candles(self): self.candles.append( Candle(self.bar, self.ichimoku.Tenkan.Current.Value, self.ichimoku.Kijun.Current.Value, self.ichimoku.SenkouA.Current.Value, self.ichimoku.SenkouB.Current.Value, self.pattern_long, self.pattern_short))
def update_tables_imperative(tns): ##CandleFetcher.update_tables(tns) ##perform normal update of any legit candles(replacing fake ones) for i, tn in enumerate(tns): sec_now = time.time() last_candle_date = CandleTable.get_last_date(tn) target_curr = CandleTable.get_target_currency(tn) period = int(CandleTable.get_period(tn)) curr_pair = "USDT_" + target_curr last_candle_date += period while (last_candle_date < sec_now): ##(top_bid, bottom_ask) = OrderMaker.get_spread(curr_pair) ##if curr_avail[target_curr]: ##means it is true, means it is available to be sold ##close = bottom_ask ##else: ##close = top_bid close = OrderMaker.get_last_trade_rate(curr_pair, last_candle_date) c = Candle(tn, last_candle_date, 0, 0, 0, close, 0, 0, 0) c.save() last_candle_date += period dbm = DBManager.get_instance() dbm.save_and_close()
def cut_table(orig_table_name, date_start, date_end=9999999999): ##print "Cutting table: ", orig_table_name, " candle data between: ", timestamp_to_date(date_start), " ---- ", timestamp_to_date(date_end) ##create new table curr_ref = CandleTable.get_ref_currency(orig_table_name) curr_target = CandleTable.get_target_currency(orig_table_name) period = CandleTable.get_period(orig_table_name) new_table = CandleTable(curr_ref, curr_target, date_start, date_end, period) new_table_name = new_table.table_name if DBManager.exists_table(new_table_name): DBManager.drop_table(new_table_name) new_table.save() ##populate new table with candles from orig_table that lie between the 2 dates candle_array = CandleTable.get_candle_array_by_date( orig_table_name, date_start, date_end) for c in candle_array: new_c = Candle(new_table_name, c.date, c.high, c.low, c.open, c.close, c.volume, c.quoteVolume, c.weightedAverage) new_c.save() dbm = DBManager.get_instance() return new_table_name
def test_update_price_on_id_not_found_gives_error(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) with pytest.raises(ValueError) as e: cat.update_candle_price(2, "£80") assert str(e.value) == "Item not found"
def handle_message(msg): if msg['e'] == 'error': print(str(msg)) elif msg['e'] == 'kline': kline = msg['k'] handle_new_candle( msg['s'], Candle(kline['t'], float(kline['o']), float(kline['c']), float(kline['h']), float(kline['l'])))
def getCandleHistoricalData(self): self.CandleData = self.conn.api_query( "returnChartData", { "currencyPair": self.pair, "start": self.startTime, "end": self.endTime, "period": self.period }) self.candles = Candle(self.CandleData)
def test_new_candle_correct(): candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") assert candle1.name == "Beach" assert candle1.id == 54 assert candle1.fragrance == "lavender" assert candle1.candle_type == "square" assert candle1.burn_time == "4 hours" assert candle1.dimensions == "10cm by 10cm" assert candle1.price == "£100"
def normalize_candle(self, candle, minVal, maxVal): ratio = (self.IMG_HEIGHT - self.CHART_MARGIN_BOTTOM - self.CHART_MARGIN_TOP - self.CHART_PADDING * 2.0) / (maxVal - minVal) # (320 - 30 -10) / 55-24 = 9 bottom = self.IMG_HEIGHT - self.CHART_MARGIN_BOTTOM - self.CHART_PADDING return Candle(bottom - (candle.open - minVal) * ratio, bottom - (candle.high - minVal) * ratio, bottom - (candle.low - minVal) * ratio, bottom - (candle.close - minVal) * ratio, candle.open_time, candle.close_time, candle.volume)
def tick(self, tick): # find candle to tick to for candle in reversed(self._candles): if candle.time <= tick['time'] and candle.close > tick['time']: candle.tick(tick) return if candle.time > tick['time']: break candle = Candle(self, tick=tick) self.candle_builder(candle)
def insert(self): for c in self.data['candleStick']: try: ct = Candle(self.table_name, c['date'], c['high'], c['low'], c['open'], c['close'], c['volume'], c['quoteVolume'], c['weightedAverage']) try: ct.save() except: "Duplicate candle, cannot insert" except: print("Candle cannot be parsed") dbm = DBManager.get_instance() dbm.save_and_close()
def detect_bullish_patterns(df: DataFrame): """ Finds all bullish candlestick patterns in a DataFrame with daily candlesticks. :param df: DataFrame with 3 days of candlestick data """ assert type(df) == DataFrame assert len(df) >= 3 symbol = df['symbol'][0] recent_candle1 = Candle(df.iloc[len(df) - 3]) recent_candle2 = Candle(df.iloc[len(df) - 2]) recent_candle3 = Candle(df.iloc[len(df) - 1]) pattern = '' # Search for each type of pattern if is_bullish_engulfing(recent_candle1, recent_candle2, recent_candle3): pattern = 'bullish_engulfing' elif is_three_white_knights(recent_candle1, recent_candle2, recent_candle3): pattern = 'three_white_knights' elif is_morning_star(recent_candle1, recent_candle2, recent_candle3): pattern = 'morning_star' # Confirm a bullish candlestick pattern with a bullish OBV # To prove it's not a false-positive if pattern != '': obv_df = obv(df).df obv_df['obv_ema'] = obv_df['obv'].ewm(span=20).mean() # Check to see if the OBV crossed above the EMA signal recent_obv_df = pd.DataFrame(obv_df.tail(2)) if recent_obv_df['obv'][0] <= recent_obv_df['obv_ema'][0] \ and recent_obv_df['obv'][1] > recent_obv_df['obv_ema'][1]: # print(f'{symbol} crossed the OBV EMA today') return pattern return ''
def load_chart(self, start): con = self.instrument.account.con pair = self.instrument.pair candles = con.get_instrument_history(pair, granularity=self.gran, count=5000, start=start) for candle in candles['candles']: candle['time'] = time_to_datetime(candle['time']) c = Candle(self, candle=candle) self.candle_builder(c) if (len(candles['candles']) == 5000): self.load_chart(candle['time'].isoformat('T') + 'Z')
def main(): app.connect('127.0.0.1', 7496, 123) api_thread = threading.Thread(target=run_loop, daemon=True) api_thread.start() time.sleep(1) usdcad_contract = Contract() usdcad_contract.symbol = 'USD' usdcad_contract.secType = 'CASH' usdcad_contract.exchange = 'IDEALPRO' usdcad_contract.currency = 'CAD' requestID = 100 app.reqHistoricalData(requestID, usdcad_contract, '', '10 D', '1 hour', 'BID', 0, 2, True, []) try: while True: time.sleep(3) if app.isDirty(): candles = [] ts = datetime.fromtimestamp(int(app.data[1][0])) print('checking for big shadow for candle @', ts.strftime('%b %d %Y %H:%M:%S')) for i in range(1, config.BS_NUMCANDLES + 1): candles.append( Candle(app.data[i][1], app.data[i][2], app.data[i][3], app.data[i][4])) cg = CandleGroup(candles) if cg.bigShadow(maxBodyRatio=config.BS_BODYRATIO, wickPercent=config.BS_WICKPERCENTAGE): print('big shadow found for candle @', ts.strftime('%b %d %Y %H:%M:%S')) sendtext(('found big shadow on USDCAD 1H @', ts.strftime('%b %d %Y %H:%M:%S'))) except KeyboardInterrupt: pass app.disconnect()
def candle_sticks(ticker): """ return daily time series candle sticks and volumes for a certain ticker """ candles_json = load_json(ticker) time_series = json.loads(candles_json)["Time Series (Daily)"] candles = [] for key in time_series.keys(): candle_date = key candle_value = time_series[candle_date] volume = candle_value["5. volume"] open_ = candle_value["1. open"] close = candle_value["4. close"] high = candle_value["2. high"] low = candle_value["3. low"] candle_ = Candle(candle_date, volume, open_, close, high, low) candles.append(candle_) return sorted(candles)
def test_pearson(self): """ case: empty chart """ c = Chart( Instrument(4) ) self.assertRaises(Exception, c.pearson, 0, c.get_size() ) """ case: one candle """ # TODO """ case: straight line, positive slope """ c = Chart(in_instrument=Instrument(4), count=0) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=i), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson > 0.99999 and pearson <= 1 ) """ case: straight line, negative slope """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp - timedelta(seconds=i), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson < -0.99999 and pearson >= -1 ) """ case: V shape """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100.1234 seconds_elapsed = 0 for i in range(9,-1,-1): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) seconds_elapsed += 1 for i in range(0,10): fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + i, low_bid=(fake_price + i / 2) ) ) seconds_elapsed += 1 c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float arithmetic self.assertTrue( pearson > -0.000001 and pearson < 0.000001 ) """ case: random """ c = Chart( in_instrument=Instrument(4), count=0 ) fake_candles = [] fake_timestamp = datetime.utcnow() fake_price = 100 seconds_elapsed = 0 for i in range(0,10000): offset = random.randrange(1, fake_price) fake_candles.append( Candle( timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed), high_ask=fake_price + offset, low_bid=(fake_price + offset / 2) ) ) seconds_elapsed += 1 c._candles = fake_candles pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' ) # Allow some play for float rounding print('pearson of random chart: {}'.format(pearson) ) self.assertTrue( pearson > -0.02 and pearson < 0.02 )
def test_add_candle_puts_candle_in_list(): cat = Catalogue("2021 Catalogue") candle1 = Candle("beach", "name", 54, "Fragrance", "Candle_Type", "Burn_Time", "height", "side", "price") cat.add_candle(candle1) assert cat.candle_list == [candle1]
def __init__( self, in_instrument, # <Instrument> granularity='S5', # string - See Oanda's documentation count=None, # int - number of candles start=None, # datetime - UTC end=None, # datetime - UTC price='MBA', # string include_first=None, # bool daily_alignment=None, # int alignment_timezone=None, # string - timezone weekly_alignment=None ): self._candles = [] # verify instance of <Instrument> by accessing a member. if in_instrument.get_id() == 0: pass if not count in [0]: # do send None # get candles from broker instrument_history = Broker.get_instrument_history( instrument=in_instrument, granularity=granularity, count=count, from_time=start, to=end, price=price, include_first=include_first, daily_alignment=daily_alignment, alignment_timezone=alignment_timezone, weekly_alignment=weekly_alignment ) if instrument_history == None: Log.write('chart.py __init__(): Failed to get instrument history.') raise Exception else: candles_raw = instrument_history['candles'] for c_r in candles_raw: new_candle = Candle( timestamp=util_date.string_to_date(c_r['time']), volume=float(c_r['volume']), complete=bool(c_r['complete']), open_bid=float(c_r['bid']['o']), high_bid=float(c_r['bid']['h']), low_bid=float(c_r['bid']['l']), close_bid=float(c_r['bid']['c']), open_ask=float(c_r['ask']['o']), high_ask=float(c_r['ask']['h']), low_ask=float(c_r['ask']['l']), close_ask=float(c_r['ask']['c']) ) self._candles.append(new_candle) self._instrument = in_instrument self._granularity = granularity self._start_index = 0 # start self._price = price self.include_first = include_first self.daily_alignment = daily_alignment self._alignment_timezone = alignment_timezone self.weekly_alignment = weekly_alignment
def test_str_returns_all_details(): candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") result = candle1.__str__() assert result == "Candle name: Beach, ID: 54, fragrance: lavender, Candle type: square, Burn time: 4 hours, Dimensions: 10cm by 10cm, Price: £100"
# if len(sys.argv) != 2: # print('Usage: python {} filename.csv'.format(sys.argv[0])) # sys.exit(1) datafile = '/Users/renero/Documents/SideProjects/sailboatsfactory/data/100.csv' # A sliding window of 'k', that will contain the # k-n elements of a sequence, and the expected 'n' values', as predictions. k = 4 n = 1 raw = read_ticks(datafile) training, test = train_test_split(raw, test_size=0.2) # This class allows me to encode the candlesticks, so I do it for every row. # I must pass all raw values to compute max and min values. candle = Candle(raw) # Encode every row, using a lambda function. # The last k elements of the sliding window contain NaNs, as the sliding window # doesn't work for them. training_candles = training.apply(lambda row: candle.encode(row), axis=1).to_frame() training_sliding = candle.make_sliding(training_candles, k).iloc[0:-k, :] # And now, repeat it for the test set test_candles = test.apply(lambda row: candle.encode(row), axis=1).to_frame() test_sliding = candle.make_sliding(test_candles, k - n).iloc[0:-k, :] # Save everything to CSV files. training_sliding.to_csv('training.csv', header=False, index=False, sep='|') test_sliding.to_csv('test.csv', header=False, index=False, sep='|')
REQUEST_URL = 'https://min-api.cryptocompare.com/data/histoday?fsym=' + SYMBOL + '&tsym=USD&limit=3650&aggregate=1&e=CCCAGG' DATA_KEY = 'Data' df = pd.DataFrame(columns=['time', 'open', 'high', 'low', 'close', 'volumefrom', 'volumeto']) pd.options.display.max_rows = 999 pd.set_option('expand_frame_repr', False) pd.options.display.max_columns = 999 # load history from api with urllib.request.urlopen(REQUEST_URL) as response: json_object = json.loads(response.read().decode('utf-8')) # convert coin history data to dataframe count = 0 for candle_dict in json_object[DATA_KEY]: candle = Candle(candle_dict) import datetime df = df.append(candle.to_dataframe(), ignore_index=True) count += 1 if count % 100 == 0: print('Loading candles... ' + str(count)) print(df) # write to timestamp-named file import os HISTORY_PATH = '/Users/jschmidt/dev/crypto/cryptodata/cryptocompare/' if not os.path.exists(HISTORY_PATH): os.mkdir(HISTORY_PATH) date_time_file_path = os.path.join(HISTORY_PATH, 'coin_history_daily_' + SYMBOL + '_' + datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S%f') + '.csv') df.to_csv(date_time_file_path)
from ichimoku import Ichimoku from candle import Candle symbols = ['BTCUSDT'] candles = {} ichimoku = {} client = Client('', '') bsm = BinanceSocketManager(client) for symbol in symbols: klines = client.get_klines(symbol=symbol, interval=KLINE_INTERVAL_1MINUTE) candles[symbol] = list( map( lambda candle: Candle(candle[0], float(candle[1]), float(candle[ 4]), float(candle[2]), float(candle[3])), klines[len(klines) - 53:])) # print(candles[symbol]) ichimoku[symbol] = Ichimoku(candles[symbol][:-1]) print('RESULT') print(ichimoku[symbol].result) def handle_message(msg): if msg['e'] == 'error': print(str(msg)) elif msg['e'] == 'kline': kline = msg['k'] handle_new_candle( msg['s'],
def get_candles(self): candles: list = self.api.get_realtime_candles(self.active.name, self.candle_size) return list(map(lambda key: Candle(candles[key]), candles))
def main(): app.connect('127.0.0.1', 7496, 123) api_thread = threading.Thread(target=run_loop, daemon=True) api_thread.start() time.sleep(1) for fc in pairs: app.reqHistoricalData(fc.requestId(), fc.contract(), '', '2 D', '1 hour', 'BID', 0, 2, True, []) try: lastTS = {} for cp in pairs: lastTS[cp.pair()] = -1 while True: time.sleep(10) for cp in pairs: data = app.data[cp.pair()] print( 'last close price for ', cp.pair(), 'is', data[0][4], '@', datetime.fromtimestamp(int( data[0][0])).strftime('%b %d %Y %H:%M:%S')) if lastTS[cp.pair()] == data[0][0]: continue lastTS[cp.pair()] = data[0][0] candles = [] cnt = 1 while len(candles) != config.BS_NUMCANDLES: candles.append( Candle(data[cnt][1], data[cnt][2], data[cnt][3], data[cnt][4])) if len(candles) == 1: ts = datetime.fromtimestamp(int(data[cnt][0])) print(cp.pair(), ': checking candle patterns @', ts.strftime('%b %d %Y %H:%M:%S')) cnt += 1 cg = CandleGroup(candles) found = False msgString = '' if candles[0].isHammer(): msgString += cp.pair( ) + ' : hammer found for candle @ ' + ts.strftime( '%b %d %Y %H:%M:%S\n') found = True if candles[0].isHangingMan(): msgString += cp.pair( ) + ' : hanging man found for candle @ ' + ts.strftime( '%b %d %Y %H:%M:%S\n') found = True if cg.bigShadow(maxBodyRatio=config.BS_BODYRATIO, wickPercent=config.BS_WICKPERCENTAGE): msgString += cp.pair( ) + ' : big shadow found for candle @ ' + ts.strftime( '%b %d %Y %H:%M:%S\n') found = True if found: print(msgString) sendtext(msgString) except KeyboardInterrupt: pass app.disconnect()
def test_update_price_changes_to_new_price(): cat = Catalogue("2021 catalogue") candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100") cat.add_candle(candle1) cat.update_candle_price(54, "£80") assert candle1.price == "£80"