def update_optimize_params(self, is_continue: bool): logger.info('action=update_optimize_params status=run') df = DataFrameCandle(self.product_code, self.duration) df.set_all_candles(self.past_period) if df.candles: self.optimized_trade_params = df.optimize_params() if self.optimized_trade_params is not None: logger.info(f'action=update_optimize_params params={self.optimized_trade_params.__dict__}') if is_continue and self.optimized_trade_params is None: time.sleep(10 * duration_seconds(self.duration)) self.update_optimize_params(is_continue)
def api_make_handler(): product_code = request.args.get('product_code') if not product_code: return jsonify({'error': 'No product_code params'}), 400 limit_str = request.args.get('limit') limit = 1000 if limit_str: limit = int(limit_str) if limit < 0 or limit > 1000: limit = 1000 duration = request.args.get('duration') if not duration: duration = constants.DURATION_1M duration_time = constants.TRADE_MAP[duration]['duration'] df = DataFrameCandle(product_code, duration_time) df.set_all_candles(limit) sma = request.args.get('sma') if sma: str_sma_period_1 = request.args.get('smaPeriod1') str_sma_period_2 = request.args.get('smaPeriod2') str_sma_period_3 = request.args.get('smaPeriod3') if str_sma_period_1: period_1 = int(str_sma_period_1) if str_sma_period_2: period_2 = int(str_sma_period_2) if str_sma_period_3: period_3 = int(str_sma_period_3) if not str_sma_period_1 or period_1 < 0: period_1 = 7 if not str_sma_period_2 or period_2 < 0: period_2 = 7 if not str_sma_period_3 or period_3 < 0: period_3 = 7 df.add_sma(period_1) df.add_sma(period_2) df.add_sma(period_3) return jsonify(df.value), 200
def api_make_handler(): product_code = request.args.get('product_code') if not product_code: return jsonify({'error': 'No product_code params'}), 400 limit_str = request.args.get('limit') limit = 1000 if limit_str: limit = int(limit_str) if limit < 0 or limit > 1000: limit = 1000 duration = request.args.get('duration') if not duration: duration = constants.DURATION_1M duration_time = constants.TRADE_MAP[duration]['duration'] df = DataFrameCandle(product_code, duration_time) df.set_all_candles(limit) return jsonify(df.value), 200
import settings logging.basicConfig(level=logging.INFO, stream=sys.stdout) if __name__ == "__main__": from app.models.events import SignalEvent import datetime import settings import constants now = datetime.datetime.utcnow() from app.models.dfcandle import DataFrameCandle df = DataFrameCandle() df.set_all_candles(limit=100) candle1 = df.candles[1] candle2 = df.candles[10] # s = SignalEvent(time=candle1.time, product_code=settings.product_code, side=constants.BUY, price=candle1.close, units=10) # s.save() # s = SignalEvent(time=candle2.time, product_code=settings.product_code, side=constants.SELL, price=candle2.close, units=10) # s.save() # streamThread = Thread(target=stream.stream_ingestion_data) serverThread = Thread(target=start) # # streamThread.start() serverThread.start() #
def api_make_handler(): product_code = request.args.get('product_code') if not product_code: return jsonify({'error': 'No product_code params'}), 400 limit_str = request.args.get('limit') limit = 1000 if limit_str: limit = int(limit_str) if limit < 0 or limit > 1000: limit = 1000 duration = request.args.get('duration') if not duration: duration = constants.DURATION_1M duration_time = constants.TRADE_MAP[duration]['duration'] df = DataFrameCandle(product_code, duration_time) df.set_all_candles(limit) sma = request.args.get('sma') if sma: str_sma_period_1 = request.args.get('smaPeriod1') str_sma_period_2 = request.args.get('smaPeriod2') str_sma_period_3 = request.args.get('smaPeriod3') if str_sma_period_1: period_1 = int(str_sma_period_1) if str_sma_period_2: period_2 = int(str_sma_period_2) if str_sma_period_3: period_3 = int(str_sma_period_3) if not str_sma_period_1 or period_1 < 0: period_1 = 7 if not str_sma_period_2 or period_2 < 0: period_2 = 14 if not str_sma_period_3 or period_3 < 0: period_3 = 50 df.add_sma(period_1) df.add_sma(period_2) df.add_sma(period_3) ema = request.args.get('ema') if ema: str_ema_period_1 = request.args.get('emaPeriod1') str_ema_period_2 = request.args.get('emaPeriod2') str_ema_period_3 = request.args.get('emaPeriod3') if str_ema_period_1: period_1 = int(str_ema_period_1) if str_ema_period_2: period_2 = int(str_ema_period_2) if str_ema_period_3: period_3 = int(str_ema_period_3) if not str_ema_period_1 or period_1 < 0: period_1 = 7 if not str_ema_period_2 or period_2 < 0: period_2 = 14 if not str_ema_period_3 or period_3 < 0: period_3 = 50 df.add_ema(period_1) df.add_ema(period_2) df.add_ema(period_3) bbands = request.args.get('bbands') if bbands: str_n = request.args.get('bbandsN') str_k = request.args.get('bbandsK') if str_n: n = int(str_n) if str_k: k = float(str_k) if not str_n or n < 0 or n is None: n = 20 if not str_k or k < 0 or k is None: k = 2.0 df.add_bbands(n, k) ichimoku = request.args.get('ichimoku') if ichimoku: df.add_ichimoku() rsi = request.args.get('rsi') if rsi: str_period = request.args.get('rsiPeriod') if str_period: period = int(str_period) else: period = 14 df.add_rsi(period) macd = request.args.get('macd') if macd: str_macd_period_1 = request.args.get('macdPeriod1') str_macd_period_2 = request.args.get('macdPeriod2') str_macd_period_3 = request.args.get('macdPeriod3') if str_macd_period_1: period_1 = int(str_macd_period_1) if str_macd_period_2: period_2 = int(str_macd_period_2) if str_macd_period_3: period_3 = int(str_macd_period_3) if not str_macd_period_1 or period_1 < 0: period_1 = 12 if not str_macd_period_2 or period_2 < 0: period_2 = 26 if not str_macd_period_3 or period_3 < 0: period_3 = 9 df.add_macd(period_1, period_2, period_3) events = request.args.get('events') if events: if settings.back_test: from app.controllers.streamdata import stream df.events = stream.ai.signal_events else: df.add_events(df.candles[0].time) return jsonify(df.value), 200
from app.controllers.streamdata import stream from app.controllers.webserver import start import app.models import settings logging.basicConfig(level=logging.INFO, stream=sys.stdout) if __name__ == "__main__": from app.models.dfcandle import DataFrameCandle import talib import numpy as np df = DataFrameCandle(settings.product_code, settings.trade_duration) df.set_all_candles(100) df.add_sma(7) print(df.value) ''' # streamThread = Thread(target=stream.stream_ingestion_data) serverThread = Thread(target=start) # streamThread.start() serverThread.start() # streamThread.join() serverThread.join() '''
def api_make_handler(): product_code = request.args.get('product_code') if not product_code: return jsonify({'error': 'No product_code params'}), 400 limit_str = request.args.get('limit') limit = 1000 if limit_str: limit = int(limit_str) if limit < 0 or limit > 1000: limit = 1000 duration = request.args.get('duration') if not duration: duration = constants.DURATION_1M duration_time = constants.TRADE_MAP[duration]['duration'] df = DataFrameCandle(product_code, duration_time) df.set_all_candles(limit) sma = request.args.get('sma') if sma: str_sma_period_1 = request.args.get('smaPeriod1') str_sma_period_2 = request.args.get('smaPeriod2') str_sma_period_3 = request.args.get('smaPeriod3') if str_sma_period_1: period_1 = int(str_sma_period_1) if str_sma_period_2: period_2 = int(str_sma_period_2) if str_sma_period_3: period_3 = int(str_sma_period_3) if not str_sma_period_1 or period_1 < 0: period_1 = 7 if not str_sma_period_2 or period_2 < 0: period_2 = 14 if not str_sma_period_3 or period_3 < 0: period_3 = 50 df.add_sma(period_1) df.add_sma(period_2) df.add_sma(period_3) ema = request.args.get('ema') if ema: str_ema_period_1 = request.args.get('emaPeriod1') str_ema_period_2 = request.args.get('emaPeriod2') str_ema_period_3 = request.args.get('emaPeriod3') if str_ema_period_1: period_1 = int(str_ema_period_1) if str_ema_period_2: period_2 = int(str_ema_period_2) if str_ema_period_3: period_3 = int(str_ema_period_3) if not str_ema_period_1 or period_1 < 0: period_1 = 7 if not str_ema_period_2 or period_2 < 0: period_2 = 14 if not str_ema_period_3 or period_3 < 0: period_3 = 50 df.add_ema(period_1) df.add_ema(period_2) df.add_ema(period_3) bbands = request.args.get('bbands') if bbands: str_n = request.args.get('bbandsN') str_k = request.args.get('bbandsK') if str_n: n = int(str_n) if str_k: k = float(str_k) if not str_n or n < 0 or n is None: n = 20 if not str_k or k < 0 or k is None: k = 2.0 df.add_bbands(n, k) ichimoku = request.args.get('ichimoku') if ichimoku: df.add_ichimoku() return jsonify(df.value), 200
def trade(self): logger.info('action=trade status=run') params = self.optimized_trade_params if params is None: return df = DataFrameCandle(self.product_code, self.duration) df.set_all_candles(self.past_period) if params.ema_enable: ema_values_1 = talib.EMA(np.array(df.closes), params.ema_period_1) ema_values_2 = talib.EMA(np.array(df.closes), params.ema_period_2) if params.bb_enable: bb_up, _, bb_down = talib.BBANDS(np.array(df.closes), params.bb_n, params.bb_k, params.bb_k, 0) if params.ichimoku_enable: tenkan, kijun, senkou_a, senkou_b, chikou = ichimoku_cloud(df.closes) if params.rsi_enable: rsi_values = talib.RSI(np.array(df.closes), params.rsi_period) if params.macd_enable: macd, macd_signal, _ = talib.MACD(np.array(df.closes), params.macd_fast_period, params.macd_slow_period, params.macd_signal_period) for i in range(1, len(df.candles)): buy_point, sell_point = 0, 0 if params.ema_enable and params.ema_period_1 <= i and params.ema_period_2 <= i: if ema_values_1[i - 1] < ema_values_2[i - 1] and ema_values_1[i] >= ema_values_2[i]: buy_point += 1 if ema_values_1[i - 1] > ema_values_2[i - 1] and ema_values_1[i] <= ema_values_2[i]: sell_point += 1 if params.bb_enable and params.bb_n <= i: if bb_down[i - 1] > df.candles[i - 1].close and bb_down[i] <= df.candles[i].close: buy_point += 1 if bb_up[i - 1] < df.candles[i - 1].close and bb_up[i] >= df.candles[i].close: sell_point += 1 if params.ichimoku_enable: if (chikou[i-1] < df.candles[i-1].high and chikou[i] >= df.candles[i].high and senkou_a[i] < df.candles[i].low and senkou_b[i] < df.candles[i].low and tenkan[i] > kijun[i]): buy_point += 1 if (chikou[i - 1] > df.candles[i - 1].low and chikou[i] <= df.candles[i].low and senkou_a[i] > df.candles[i].high and senkou_b[i] > df.candles[i].high and tenkan[i] < kijun[i]): sell_point += 1 if params.macd_enable: if macd[i] < 0 and macd_signal[i] < 0 and macd[i - 1] < macd_signal[i - 1] and macd[i] >= macd_signal[i]: buy_point += 1 if macd[i] > 0 and macd_signal[i] > 0 and macd[i-1] > macd_signal[i - 1] and macd[i] <= macd_signal[i]: sell_point += 1 if params.rsi_enable and rsi_values[i-1] != 0 and rsi_values[i-1] != 100: if rsi_values[i-1] < params.rsi_buy_thread and rsi_values[i] >= params.rsi_buy_thread: buy_point += 1 if rsi_values[i-1] > params.rsi_sell_thread and rsi_values[i] <= params.rsi_sell_thread: sell_point += 1 if buy_point > 0: if not self.buy(df.candles[i]): continue self.stop_limit = df.candles[i].close * self.stop_limit_percent if sell_point > 0 or self.stop_limit > df.candles[i].close: if not self.sell(df.candles[i]): continue self.stop_limit = 0.0 self.update_optimize_params(is_continue=True)
def api_make_handler(): product_code = request.args.get('product_code') if not product_code: return jsonify({'error': 'No product_code params'}), 400 limit_str = request.args.get('limit') limit = 1000 if limit_str: limit = int(limit_str) if limit < 0 or limit > 1000: limit = 1000 duration = request.args.get('duration') if not duration: duration = constants.DURATION_1M duration_time = constants.TRADE_MAP[duration]['duration'] df = DataFrameCandle(product_code, duration_time) df.set_all_candles(limit) sma = request.args.get('sma') if sma: str_sma_period_1 = request.args.get('smaPeriod1') str_sma_period_2 = request.args.get('smaPeriod2') str_sma_period_3 = request.args.get('smaPeriod3') if str_sma_period_1: period_1 = int(str_sma_period_1) if str_sma_period_2: period_2 = int(str_sma_period_2) if str_sma_period_3: period_3 = int(str_sma_period_3) if not str_sma_period_1 or period_1 < 0: period_1 = 7 if not str_sma_period_2 or period_2 < 0: period_2 = 14 if not str_sma_period_3 or period_3 < 0: period_3 = 50 df.add_sma(period_1) df.add_sma(period_2) df.add_sma(period_3) ema = request.args.get('ema') if ema: str_ema_period_1 = request.args.get('emaPeriod1') str_ema_period_2 = request.args.get('emaPeriod2') str_ema_period_3 = request.args.get('emaPeriod3') if str_ema_period_1: period_1 = int(str_ema_period_1) if str_ema_period_2: period_2 = int(str_ema_period_2) if str_ema_period_3: period_3 = int(str_ema_period_3) if not str_ema_period_1 or period_1 < 0: period_1 = 7 if not str_ema_period_2 or period_2 < 0: period_2 = 14 if not str_ema_period_3 or period_3 < 0: period_3 = 50 df.add_ema(period_1) df.add_ema(period_2) df.add_ema(period_3) bbands = request.args.get('bbands') if bbands: str_n = request.args.get('bbandsN') str_k = request.args.get('bbandsK') if str_n: n = int(str_n) if str_k: k = float(str_k) if not str_n or n < 0 or n is None: n = 20 if not str_k or k < 0 or k is None: k = 2.0 df.add_bbands(n, k) atr = request.args.get('atr') if atr: str_n = request.args.get('atrN') str_k = request.args.get('atrK') if str_n: n = int(str_n) if str_k: k = float(str_k) if not str_n or n < 0 or n is None: n = 20 if not str_k or k < 0 or k is None: k = 2.0 df.add_atr(n, k) di = request.args.get('di') if di: str_n = request.args.get('diN') if str_n: n = int(str_n) if not str_n or n < 0 or n is None: n = 14 df.add_di(n) adx = request.args.get('adx') if adx: str_n = request.args.get('adxN') if str_n: n = int(str_n) if not str_n or n < 0 or n is None: n = 14 df.add_adx(n) ichimoku = request.args.get('ichimoku') if ichimoku: df.add_ichimoku() rsi = request.args.get('rsi') if rsi: str_period = request.args.get('rsiPeriod') if str_period: period = int(str_period) else: period = 14 df.add_rsi(period) macd = request.args.get('macd') if macd: str_macd_period_1 = request.args.get('macdPeriod1') str_macd_period_2 = request.args.get('macdPeriod2') str_macd_period_3 = request.args.get('macdPeriod3') if str_macd_period_1: period_1 = int(str_macd_period_1) if str_macd_period_2: period_2 = int(str_macd_period_2) if str_macd_period_3: period_3 = int(str_macd_period_3) if not str_macd_period_1 or period_1 < 0: period_1 = 12 if not str_macd_period_2 or period_2 < 0: period_2 = 26 if not str_macd_period_3 or period_3 < 0: period_3 = 9 df.add_macd(period_1, period_2, period_3) force_idx = request.args.get('force_idx') if force_idx: str_force_idx_period_1 = request.args.get('forceIdxPeriod1') str_force_idx_period_2 = request.args.get('forceIdxPeriod2') str_force_idx_period_3 = request.args.get('forceIdxPeriod3') if str_force_idx_period_1: period_1 = int(str_force_idx_period_1) if str_force_idx_period_2: period_2 = int(str_force_idx_period_2) if str_force_idx_period_3: period_3 = int(str_force_idx_period_3) if not str_force_idx_period_1 or period_1 < 0: period_1 = 1 if not str_force_idx_period_2 or period_2 < 0: period_2 = 2 if not str_force_idx_period_3 or period_3 < 0: period_3 = 13 df.add_force_index(period_1) df.add_force_index(period_2) df.add_force_index(period_3) events = request.args.get('events') if events: df.add_events(df.candles[0].time) return jsonify(df.value), 200
def index(): df = DataFrameCandle(settings.product_code, settings.trade_duration) df.set_all_candles(settings.past_period) candles = df.candles return render_template('./google.html', candles=candles)