def update_density(symbol, account=None): symbol = get_mt4_symbol(symbol) last_time = price_redis.get(symbol + TIME_SUFFIX) last_time = parse(last_time) if last_time else None now = datetime.utcnow() data = price_redis.get('%s_H1' % symbol) data = json.loads(data) if data else {} fxcm = account or SingletonFXCM(AccountType.DEMO, settings.FXCM_ACCOUNT_ID, settings.FXCM_ACCESS_TOKEN) if last_time: df = fxcm.fxcmpy.get_candles(get_fxcm_symbol(symbol), period='m1', start=last_time, end=now, columns=['askhigh', 'bidlow', 'tickqty']) else: df = fxcm.fxcmpy.get_candles(get_fxcm_symbol(symbol), period='m1', number=FXCM.MAX_CANDLES, end=now, columns=['askhigh', 'bidlow', 'tickqty']) _process_df(df, data, symbol) print('Data length = %s' % len(df)) _save_redis(symbol, data) now = df.iloc[-1].name.to_pydatetime() + relativedelta(seconds=30) price_redis.set(symbol + TIME_SUFFIX, str(now)) return now
def check_trade_exist(self, instrument, side): instrument = get_fxcm_symbol(instrument) is_buy = side == OrderSide.BUY trades = self.account.get_trades() for id, trade in trades.items(): trade_instrument = get_fxcm_symbol(trade.get_currency()) if trade_instrument == instrument and is_buy == trade.get_isBuy(): return True return False
def draw_recent(symbol, days=30, account=None, to_plotly=False): symbol = get_mt4_symbol(symbol) now = datetime.utcnow() fxcm = account or SingletonFXCM(AccountType.DEMO, settings.FXCM_ACCOUNT_ID, settings.FXCM_ACCESS_TOKEN) df = fxcm.fxcmpy.get_candles( get_fxcm_symbol(symbol), period='m1', number=FXCM.MAX_CANDLES, end=now, columns=['askclose', 'askhigh', 'bidlow', 'tickqty']) price = df.iloc[-1].askclose if days: start = now - relativedelta(days=days) end = df.iloc[0].name.to_pydatetime() - relativedelta(seconds=30) print(end) while end - start > timedelta(minutes=1): if (end - start).days > 6: df2 = fxcm.fxcmpy.get_candles( get_fxcm_symbol(symbol), period='m1', number=FXCM.MAX_CANDLES, end=end, columns=['askclose', 'askhigh', 'bidlow', 'tickqty']) else: df2 = fxcm.fxcmpy.get_candles( get_fxcm_symbol(symbol), period='m1', start=start, end=end, columns=['askclose', 'askhigh', 'bidlow', 'tickqty']) df = df.append(df2, sort=True) end = df2.iloc[0].name.to_pydatetime() - relativedelta(seconds=30) print(end) result = {} _process_df(df, result, symbol) keylist = result.keys() keys = sorted(keylist) output = '' for k in keys: output += ' %s: %s,\n' % (k, result[k]) output = '''{ %s }''' % output sorted_result = eval(output) filename = '%s_%s days' % (symbol, days) _draw(sorted_result, symbol, price, filename, to_plotly)
def get_dataframe(self, instrument, granularity, fromTime, toTime): instrument = get_fxcm_symbol(instrument) end = get_candle_time(toTime, granularity) fromTime = get_candle_time(fromTime, granularity) granularity = get_fxcm_timeframe(granularity) result = None while end > fromTime: data = self.fxcmpy.get_candles(instrument, period=granularity, number=self.MAX_CANDLES, end=end, columns=[ 'askhigh', 'askopen', 'bidclose', 'bidlow', 'tickqty' ]) if result is not None: result = pd.concat([result, data], sort=True).sort_index() else: result = data end = result.iloc[0].name.to_pydatetime() - relativedelta( seconds=10) result.columns = ['high', 'open', 'close', 'low', 'volume'] return result[result.index >= fromTime]
def get_lots(self, instrument, stop_loss_pips=None, risk_ratio=Decimal('0.05')): max_trade = 5 if len(self.get_trades()) >= max_trade: return 0 equity = self.get_balance() if not stop_loss_pips: return equity / 1000 * Decimal('0.1') instrument = get_fxcm_symbol(instrument) pip_unit = pip(instrument) risk = equity * risk_ratio value = risk / stop_loss_pips / pip_unit if instrument.upper().endswith('USD'): price = self.get_price(instrument) value = value * price elif instrument.upper().startswith('USD'): lots = equity / 1000 * Decimal('0.1') return lots.quantize(Decimal("0.01")) else: # cross pair raise NotImplementedError units = int(value / 100) * 100 return units_to_lots(units).quantize(Decimal("0.01"))
def run(self): logger.info('%s statup.' % self.__class__.__name__) logger.info('Registered handler: %s' % ', '.join([x.__class__.__name__ for x in self.handlers])) self.pairs = [get_fxcm_symbol(pair) for pair in self.pairs] pair_list = ",".join(self.pairs) logger.info('Pairs: %s' % pair_list) if not self.is_market_open: logger.info('Market is closed now.') logger.info('####################################') while self.running: while self.is_market_open: event = self.get(False) if event: if event.type == ConnectEvent.type: self.process_connect_event(event) else: self.handle_event(event) else: break time.sleep(settings.LOOP_SLEEP) self.loop_counter += 1 self.check_market_status() if self.is_market_open: self.generate_heartbeat() self.check_connection()
def market_order(self, instrument, side, lots, take_profit=None, stop_loss=None, trailing_pip=None, **kwargs): timeInForce = kwargs.get('timeInForce', TimeInForce.FOK) symbol = get_fxcm_symbol(instrument) is_buy = side == OrderSide.BUY amount = lots_to_units(lots) / 1000 is_in_pips = kwargs.get('is_in_pips', True) if is_in_pips and stop_loss and stop_loss > 0: stop_loss = -1 * stop_loss # change to negtive if using pips return self.fxcmpy.open_trade(symbol, is_buy, amount, timeInForce, order_type='AtMarket', rate=0, is_in_pips=is_in_pips, limit=take_profit, at_market=0, stop=stop_loss, trailing_step=trailing_pip, account_id=self.account_id)
def get_candle(self, instrument, granularity, count=120, fromTime=None, toTime=None, price_type='M', smooth=False): instrument = get_fxcm_symbol(instrument) granularity = get_fxcm_timeframe(granularity) return self.fxcmpy.get_candles(instrument, period=granularity, number=count, start=fromTime, end=toTime)
def close_symbol(self, instrument, side, percent=None): instrument = get_fxcm_symbol(instrument) data = [] for trade_id, trade in self.fxcmpy.open_pos.items(): if instrument == trade.get_currency(): trade_side = OrderSide.BUY if trade.get_isBuy() else OrderSide.SELL if trade_side == side: units = trade.get_amount() if percent and 1 >= percent > 0: units = int(units * percent) try: trade.close(amount=units) data.append({'trade_id': trade_id, 'side': side, 'instrument': instrument, 'units': units}) except Exception as ex: logger.error('Cant close trade = %s, %s' % (trade_id, ex)) return data
def init_density(symbol, start=datetime(2019, 1, 18, 18, 1), account=None): symbol = get_mt4_symbol(symbol) fxcm = account or SingletonFXCM(AccountType.DEMO, settings.FXCM_ACCOUNT_ID, settings.FXCM_ACCESS_TOKEN) now = datetime.utcnow() - relativedelta(minutes=1) # shift 1 minute end = datetime.utcnow() result = {} count = 0 while end > start: df = fxcm.fxcmpy.get_candles(get_fxcm_symbol(symbol), period='m1', number=FXCM.MAX_CANDLES, end=end, columns=['askhigh', 'bidlow', 'tickqty']) _process_df(df, result, symbol) count += 1 print(count, end) end = df.iloc[0].name.to_pydatetime() - relativedelta(seconds=30) _save_redis(symbol, result) price_redis.set(symbol + TIME_SUFFIX, str(now))
def _make_limit_or_stop_order(self, instrument, side, price, lots, take_profit=None, stop_loss=None, trailing_pip=None, **kwargs): symbol = get_fxcm_symbol(instrument) is_buy = side == OrderSide.BUY amount = lots_to_units(lots) / 1000 is_in_pips = kwargs.get('is_in_pips', True) order_id = kwargs.get('order_id', None) if order_id: order = self.get_order(order_id) if trailing_pip: order.set_trailing_step(trailing_pip) kw = {} if stop_loss: kw['stop'] = stop_loss kw['is_stop_in_pips'] = is_in_pips if take_profit: kw['limit'] = take_profit kw['is_limit_in_pips'] = is_in_pips if kw: self.fxcmpy.change_order_stop_limit(order_id, **kw) else: return self.create_entry_order(symbol, is_buy, amount, time_in_force=TimeInForce.GTC, price=price, take_profit=take_profit, stop_loss=stop_loss, trailing_pip=trailing_pip, **kwargs)
def close_position(self, instrument, longUnits='ALL', shortUnits='ALL'): instrument = get_fxcm_symbol(instrument) self.fxcmpy.close_all_for_symbol(instrument)