def __init__(self, account_currency, account_id, symbol, timeframe, position_entry_time_limit, time_to_change_stop_loss_before_news, atr_period_for_stop_loss, atr_multiply_for_stop_loss, watch_for_high_impact_news, watch_for_medium_impact_news, watch_for_low_impact_news): self.account_id = account_id self.account_currency = account_currency self.symbol = symbol self.timeframe = timeframe self.position_entry_time_limit = datetime.timedelta( minutes=position_entry_time_limit) self.time_to_change_stop_loss_before_news = datetime.timedelta( minutes=time_to_change_stop_loss_before_news) self.atr_period = int(atr_period_for_stop_loss) self.stop_loss_atr_multiply = atr_multiply_for_stop_loss self.impact_list_to_watch = [] if watch_for_high_impact_news == 'True': self.impact_list_to_watch.append('High Impact Expected') if watch_for_medium_impact_news == 'True': self.impact_list_to_watch.append('Medium Impact Expected') if watch_for_low_impact_news == 'True': self.impact_list_to_watch.append('Low Impact Expected') self.db = Db_Controller() self.economic_calendar = pd.DataFrame()
def __init__(self, account_currency, account_id, symbol, timeframe, atr_period, stop_loss_atr_multiply, limit_atr_multiply, risk_percent): self.account_currency=account_currency self.account_id=account_id self.symbol=symbol self.timeframe=timeframe self.atr_period=int(atr_period) self.stop_loss_atr_multiply=stop_loss_atr_multiply self.limit_atr_multiply=limit_atr_multiply self.risk_percent=risk_percent self.db=Db_Controller()
def __init__(self): with open('./data/account_info.cfg', 'rb' ) as f: # Reading user's fxcm information from a stored file account_info_dict = pickle.load(f) self.token = account_info_dict[ 'token'] # FXCM token or API key " a46718dbcf04edf1b8135816d96d38a7703f2d65 " use this in gui self.account_type = account_info_dict[ 'account_type'] #Can be demo or real self.connection = None self.connection_status = 'Disconnected' self.db = Db_Controller() self.account_id = account_info_dict['account_id'] self.account_name = account_info_dict['account_name'] self.fxcm_account_currency_list = ['AUD', 'USD', 'EUR', 'GBP'] self.account_currency = account_info_dict['account_currency'] self.available_symbols_list = [ 'AUDUSD', 'EURUSD', 'USDJPY', 'GBPUSD', 'USDCHF', 'NZDUSD' ] self.available_timeframe_list = [ 'm1', 'm5', 'm15', 'm30', 'H1', 'H2', 'H3', 'H4', 'H6', 'H8', 'D1', 'W1', 'M1' ]
def __init__(self, account_currency, account_id, symbol, timeframe, atr_period_for_stop_loss, atr_multiply_for_stop_loss, atr_multiply_for_limit, watch_for_high_impact_news, watch_for_medium_impact_news, watch_for_low_impact_news, risk_percent): self.account_id = account_id self.account_currency = account_currency self.symbol = symbol self.timeframe = timeframe self.atr_period = int(atr_period_for_stop_loss) self.stop_loss_atr_multiply = atr_multiply_for_stop_loss self.limit_atr_multiply = atr_multiply_for_limit self.risk_percent = risk_percent self.news_dict = newsDict self.low_impact_weight = 1 self.medium_impact_weight = 2 self.high_impact_weight = 3 self.impact_weight_dict = { 'High Impact Expected': 3, 'Medium Impact Expected': 2, 'Low Impact Expected': 1, } self.risk_management = equity_atr_based_risk_management( self.account_currency, self.account_id, symbol, timeframe, atr_period_for_stop_loss, atr_multiply_for_stop_loss, atr_multiply_for_limit, risk_percent) self.base_currency = self.symbol[:3] self.impact_list_to_watch = [] if watch_for_high_impact_news == 'True': self.impact_list_to_watch.append('High Impact Expected') if watch_for_medium_impact_news == 'True': self.impact_list_to_watch.append('Medium Impact Expected') if watch_for_low_impact_news == 'True': self.impact_list_to_watch.append('Low Impact Expected') self.db = Db_Controller() self.economic_calendar = pd.DataFrame()
class margin_atr_based_risk_management: def __init__(self, account_currency, account_id, symbol, timeframe, atr_period, stop_loss_atr_multiply, limit_atr_multiply, risk_percent): self.account_currency=account_currency self.account_id=account_id self.symbol=symbol self.timeframe=timeframe self.atr_period=int(atr_period) self.stop_loss_atr_multiply=stop_loss_atr_multiply self.limit_atr_multiply=limit_atr_multiply self.risk_percent=risk_percent self.db=Db_Controller() def get_account_info(self): fxcm_info=self.db.query_table('Fxcm_Info', ('*',), fields=("accountId",), values=(self.account_id,)) return fxcm_info def stop_loss_limit(self, price, last_atr, position_type): try: ''' stop loss is placed stop_loss_atr_multiply time of atr ''' if position_type=='buy': stop_loss_pip=last_atr*self.stop_loss_atr_multiply stop_loss=price-stop_loss_pip limit_pip=last_atr*self.limit_atr_multiply limit=price+limit_pip else: stop_loss_pip=last_atr*self.stop_loss_atr_multiply stop_loss=price+stop_loss_pip limit_pip=last_atr*self.limit_atr_multiply limit=price-limit_pip if self.symbol[3:]=='JPY': stop_loss_pip=stop_loss_pip*100 limit_pip=limit_pip*100 else: stop_loss_pip=stop_loss_pip*10000 limit_pip=limit_pip*10000 return stop_loss, limit, stop_loss_pip, limit_pip except Exception as e: print(e, 'stop_loss_limit') def position_size_stop_loss(self, position_type): try: ''' Lot Number of unit Standard 100,000 Mini 10,000 Micro 1,000 Nano 100 Position size = ((account value x risk per trade) / pips risked)/ pip value per standard lot Margin Requirement = Current Price × Units Traded × Margin ''' data=self.db.query_price_data(self.symbol, self.timeframe, self.atr_period*2) data['atr']=atr(list(data.bidclose), self.atr_period) last_atr=data.atr.iloc[-1] price=data.bidclose.iloc[-1] fxcm_info=self.get_account_info()[0] margin=fxcm_info[11] stop_loss, limit, stop_loss_pip, limit_pip=self.stop_loss_limit(price, last_atr, position_type) leverage=leverage_cal(self.symbol, margin) standard_lot_pip_value=pip_value_cal(self.symbol, self.account_currency, price, 100000) position_size=int(((((margin*self.risk_percent/100)/stop_loss_pip)/standard_lot_pip_value)*100)*1000) required_margin=int(price*position_size/leverage) c = CurrencyConverter() required_margin=int(c.convert(required_margin, self.symbol[:3], self.account_currency)) if self.symbol[3:]=='JPY': required_margin=required_margin/100 return position_size/1000, required_margin, stop_loss, limit, stop_loss_pip, limit_pip except Exception as e: print(e, 'position_size_stop_loss') def backtest(self, position_type, data, margin): try: ''' Lot Number of unit Standard 100,000 Mini 10,000 Micro 1,000 Nano 100 Position size = ((account value x risk per trade) / pips risked)/ pip value per standard lot Margin Requirement = Current Price × Units Traded × Margin ''' data['atr']=atr(list(data.bidclose), self.atr_period) last_atr=data.atr.iloc[-1] price=data.bidclose.iloc[-1] stop_loss, limit, stop_loss_pip, limit_pip=self.stop_loss_limit(price, last_atr, position_type) leverage=leverage_cal(self.symbol, margin) standard_lot_pip_value=pip_value_cal(self.symbol, self.account_currency, price, 100000) position_size=int(((((margin*self.risk_percent/100)/stop_loss_pip)/standard_lot_pip_value)*100)*1000) required_margin=int(price*position_size/leverage) c = CurrencyConverter() required_margin=int(c.convert(required_margin, self.symbol[:3], self.account_currency)) pip_value=pip_value_cal(self.symbol, self.account_currency, price, position_size) if self.symbol[3:]=='JPY': required_margin=required_margin/100 return position_size, required_margin, stop_loss, limit, stop_loss_pip, limit_pip, pip_value except Exception as e: print(e, 'backtest')
class check_economic_calendar_entry_atr_based_stop: def __init__(self, account_currency, account_id, symbol, timeframe, position_entry_time_limit, time_to_change_stop_loss_before_news, atr_period_for_stop_loss, atr_multiply_for_stop_loss, watch_for_high_impact_news, watch_for_medium_impact_news, watch_for_low_impact_news): self.account_id = account_id self.account_currency = account_currency self.symbol = symbol self.timeframe = timeframe self.position_entry_time_limit = datetime.timedelta( minutes=position_entry_time_limit) self.time_to_change_stop_loss_before_news = datetime.timedelta( minutes=time_to_change_stop_loss_before_news) self.atr_period = int(atr_period_for_stop_loss) self.stop_loss_atr_multiply = atr_multiply_for_stop_loss self.impact_list_to_watch = [] if watch_for_high_impact_news == 'True': self.impact_list_to_watch.append('High Impact Expected') if watch_for_medium_impact_news == 'True': self.impact_list_to_watch.append('Medium Impact Expected') if watch_for_low_impact_news == 'True': self.impact_list_to_watch.append('Low Impact Expected') self.db = Db_Controller() self.economic_calendar = pd.DataFrame() def update_economic_calendar(self): self.last_update_time = datetime.datetime.utcnow() self.economic_calendar = get_economic_calendar() def check_condition_entry(self): if self.economic_calendar.empty: self.update_economic_calendar() if self.economic_calendar.empty: return False if utc_to_central_time( self.last_update_time).day != utc_to_central_time( datetime.datetime.utcnow()).day: self.update_economic_calendar() now_utc = datetime.datetime.utcnow() check_time_ahead = now_utc + self.position_entry_time_limit if self.economic_calendar.loc[ (self.economic_calendar['date'] > now_utc) & (self.economic_calendar['date'] < check_time_ahead) & (self.economic_calendar['impact'].isin(self.impact_list_to_watch)) & (self.economic_calendar['currency'].str.contains( self.symbol))].empty: return False #Trade allowed else: return True #Trade not allowed def check_condition_stop(self, position_type): if self.economic_calendar.empty: self.update_economic_calendar() if self.economic_calendar.empty: return False if utc_to_central_time( self.last_update_time).day != utc_to_central_time( datetime.datetime.utcnow()).day: self.update_economic_calendar() now_utc = datetime.datetime.utcnow() check_time_ahead = now_utc + self.time_to_change_stop_loss_before_news if self.economic_calendar.loc[ (self.economic_calendar['date'] > now_utc) & (self.economic_calendar['date'] < check_time_ahead) & (self.economic_calendar['impact'].isin(self.impact_list_to_watch)) & (self.economic_calendar['currency'].str.contains( self.symbol))].empty: data = self.db.query_price_data(self.symbol, self.timeframe, self.atr_period * 2) data['atr'] = atr(list(data.bidclose), self.atr_period) last_atr = data.atr.iloc[-1] price = data.bidclose.iloc[-1] if position_type == 'buy': stop_loss = price - last_atr * self.stop_loss_atr_multiply return stop_loss else: stop_loss = price + last_atr * self.stop_loss_atr_multiply return stop_loss else: return False #No need to change stop loss
def get_data(self): quantity = 2 * max(self.ema_1_period, self.ema_2_period) self.db = Db_Controller() self.data = self.db.query_price_data(self.symbol, self.timeframe, quantity)
class ema_cross: def __init__(self, symbol, timeframe, ema_1_period, ema_2_period): self.symbol = symbol self.timeframe = timeframe self.ema_1_period = int(ema_1_period) self.ema_2_period = int(ema_2_period) self.data = pd.DataFrame() self.required_score = 10 self.conditions_weights = {'ema': 10} self.condictions_state = { 'buy': { 'ema': False, }, 'sell': { 'ema': False } } def get_data(self): quantity = 2 * max(self.ema_1_period, self.ema_2_period) self.db = Db_Controller() self.data = self.db.query_price_data(self.symbol, self.timeframe, quantity) def check_exit(self, current_position): try: self.get_data() self.data['ema_1'] = ema(list(self.data.bidclose), self.ema_1_period) self.data['ema_2'] = ema(list(self.data.bidclose), self.ema_2_period) if current_position == 'buy': if ((self.data.ema_1.iloc[-1] > self.data.ema_2.iloc[-1])): self.condictions_state['sell']['ema'] = True self.condictions_state['buy']['ema'] = False return 'exit' elif current_position == 'sell': if ((self.data.ema_1.iloc[-1] < self.data.ema_2.iloc[-1])): self.condictions_state['buy']['ema'] = True self.condictions_state['sell']['ema'] = False return 'exit' else: self.condictions_state['buy']['ema'] = False self.condictions_state['sell']['ema'] = False return None except Exception as e: print(e, 454545) def check_entry(self): try: self.get_data() self.data['ema_1'] = ema(list(self.data.bidclose), self.ema_1_period) self.data['ema_2'] = ema(list(self.data.bidclose), self.ema_2_period) if ((self.data.ema_1.iloc[-1] > self.data.ema_2.iloc[-1])): self.condictions_state['sell']['ema'] = True self.condictions_state['buy']['ema'] = False elif ((self.data.ema_1.iloc[-1] < self.data.ema_2.iloc[-1])): self.condictions_state['buy']['ema'] = True self.condictions_state['sell']['ema'] = False else: self.condictions_state['buy']['ema'] = False self.condictions_state['sell']['ema'] = False collected_score_buy = 0 collected_score_sell = 0 for k, v in self.conditions_weights.items(): if self.condictions_state['buy'][k] == True: collected_score_buy += v elif self.condictions_state['sell'][k] == True: collected_score_sell += v if collected_score_buy >= self.required_score: return 'buy' elif collected_score_sell >= self.required_score: return 'sell' else: return None except Exception as e: print(e, 98555555986668) return None def backtest_exit(self, current_position, data): try: self.condictions_state_backtest = { 'buy': { 'ema': False, }, 'sell': { 'ema': False, } } if len(data.date) > max(self.ema_1_period, self.ema_2_period): data['ema_1'] = ema(list(data.bidclose), self.ema_1_period) data['ema_2'] = ema(list(data.bidclose), self.ema_2_period) if current_position == 'buy': if ((data.ema_1.iloc[-1] > data.ema_2.iloc[-1])): self.condictions_state_backtest['sell']['ema'] = True self.condictions_state_backtest['buy']['ema'] = False return 'exit' elif current_position == 'sell': if ((data.ema_1.iloc[-1] < data.ema_2.iloc[-1])): self.condictions_state_backtest['buy']['ema'] = True self.condictions_state_backtest['sell']['ema'] = False return 'exit' else: self.condictions_state_backtest['buy']['ema'] = False self.condictions_state_backtest['sell']['ema'] = False return None else: return None except: return None def backtest_entry(self, data): try: self.condictions_state_backtest = { 'buy': { 'ema': False, }, 'sell': { 'ema': False, } } if len(data.date) > max(self.ema_1_period, self.ema_2_period): data['ema_1'] = ema(list(data.bidclose), self.ema_1_period) data['ema_2'] = ema(list(data.bidclose), self.ema_2_period) if ((data.ema_1.iloc[-1] > data.ema_2.iloc[-1])): self.condictions_state_backtest['sell']['ema'] = True self.condictions_state_backtest['buy']['ema'] = False elif ((data.ema_1.iloc[-1] < data.ema_2.iloc[-1])): self.condictions_state_backtest['buy']['ema'] = True self.condictions_state_backtest['sell']['ema'] = False else: self.condictions_state_backtest['buy']['ema'] = False self.condictions_state_backtest['sell']['ema'] = False self.required_score collected_score_buy = 0 collected_score_sell = 0 for k, v in self.conditions_weights.items(): if self.condictions_state_backtest['buy'][k] == True: collected_score_buy += v elif self.condictions_state_backtest['sell'][k] == True: collected_score_sell += v if collected_score_buy >= self.required_score: return 'buy' elif collected_score_sell >= self.required_score: return 'sell' else: return None else: return None except Exception as e: print(e, 11111111111111) return None
class strategy_linear_regression_channel: def __init__(self, symbol, timeframe, lrc_std_1, lrc_period_1, lrc_std_2, lrc_period_2, lrc_std_3, lrc_period_3, lrc_std_exit, lrc_period_exit, cmf_period, cmf_ma_period, r_percent_period): self.symbol = symbol self.timeframe = timeframe self.lrc_std_1 = lrc_std_1 self.lrc_period_1 = int(lrc_period_1) self.lrc_std_2 = lrc_std_2 self.lrc_period_2 = int(lrc_period_2) self.lrc_std_3 = lrc_std_3 self.lrc_period_3 = int(lrc_period_3) self.cmf_period = int(cmf_period) self.cmf_ma_period = int(cmf_ma_period) self.r_percent_period = int(r_percent_period) self.data = pd.DataFrame() self.required_score = 30 self.conditions_weights = { 'linear_regression_channel': 10, 'r_percent': 10, 'cmf': 10 } self.condictions_state = { 'buy': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False }, 'sell': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False } } def get_data(self): quantity = 2 * max(self.lrc_period_1, self.lrc_period_2, self.lrc_period_3) self.db = Db_Controller() self.data = self.db.query_price_data(self.symbol, self.timeframe, quantity) def check_exit(self, current_position): try: self.get_data() linear_regression_exit, upper_line_exit, lower_line_exit, slope_exit, channel_width_exit = linear_regression_channel( self.data, self.lrc_period_exit, self.lrc_std_exit) self.data['cmf'] = cmf(list(self.data.bidclose), list(self.data.bidhigh), list(self.data.bidlow), list(self.data.tickqty), self.cmf_period) self.data['cmf_ma'] = ema(list(self.data.cmf), self.cmf_ma_period) self.data['r_percent'] = r_percent(self.data, self.r_percent_period) if current_position == 'buy': if ((self.data.bidclose.iloc[-1] > upper_line_exit[-1])): self.condictions_state['sell'][ 'linear_regression_channel'] = True self.condictions_state['buy'][ 'linear_regression_channel'] = False return 'exit' elif current_position == 'sell': if ((self.data.bidclose.iloc[-1] < lower_line_exit[-1])): self.condictions_state['buy'][ 'linear_regression_channel'] = True self.condictions_state['sell'][ 'linear_regression_channel'] = False return 'exit' else: self.condictions_state['buy'][ 'linear_regression_channel'] = False self.condictions_state['sell'][ 'linear_regression_channel'] = False return None except Exception as e: print(e) return None def check_entry(self): try: self.get_data() linear_regression_1, upper_line_1, lower_line_1, slope_1, channel_width_1 = linear_regression_channel( self.data, self.lrc_period_1, self.lrc_std_1) linear_regression_2, upper_line_2, lower_line_2, slope_2, channel_width_2 = linear_regression_channel( self.data, self.lrc_period_2, self.lrc_std_2) linear_regression_3, upper_line_3, lower_line_3, slope_3, channel_width_3 = linear_regression_channel( self.data, self.lrc_period_3, self.lrc_std_3) self.data['cmf'] = cmf(list(self.data.bidclose), list(self.data.bidhigh), list(self.data.bidlow), list(self.data.tickqty), self.cmf_period) self.data['cmf_ma'] = ema(list(self.data.cmf), self.cmf_ma_period) self.data['r_percent'] = r_percent(self.data, self.r_percent_period) if ((self.data.bidclose.iloc[-1] > upper_line_1[-1]) or (self.data.bidclose.iloc[-1] > upper_line_2[-1]) or (self.data.bidclose.iloc[-1] > upper_line_3[-1])): self.condictions_state['sell'][ 'linear_regression_channel'] = True self.condictions_state['buy'][ 'linear_regression_channel'] = False elif ((self.data.bidclose.iloc[-1] < lower_line_1[-1]) or (self.data.bidclose.iloc[-1] < lower_line_2[-1]) or (self.data.bidclose.iloc[-1] < lower_line_3[-1])): self.condictions_state['buy'][ 'linear_regression_channel'] = True self.condictions_state['sell'][ 'linear_regression_channel'] = False else: self.condictions_state['buy'][ 'linear_regression_channel'] = False self.condictions_state['sell'][ 'linear_regression_channel'] = False if self.data.r_percent.iloc[-2] > -20 and self.data.r_percent.iloc[ -1] < -20: self.condictions_state['sell']['r_percent'] = True self.condictions_state['buy']['r_percent'] = False elif self.data.r_percent.iloc[ -2] < -80 and self.data.r_percent.iloc[-1] > -80: self.condictions_state['buy']['r_percent'] = True self.condictions_state['sell']['r_percent'] = False else: self.condictions_state['buy']['r_percent'] = False self.condictions_state['sell']['r_percent'] = False if self.data.cmf.iloc[-1] > self.data.cmf_ma.iloc[-1]: self.condictions_state['sell']['cmf'] = False self.condictions_state['buy']['cmf'] = True else: self.condictions_state['sell']['cmf'] = True self.condictions_state['buy']['cmf'] = False collected_score_buy = 0 collected_score_sell = 0 for k, v in self.conditions_weights.items(): if self.condictions_state['buy'][k] == True: collected_score_buy += v elif self.condictions_state['sell'][k] == True: collected_score_sell += v if collected_score_buy >= self.required_score: return 'buy' elif collected_score_sell >= self.required_score: return 'sell' else: return None except Exception as e: print(e, 9898989898) return None def backtest_exit(self, current_position, data): try: self.condictions_state_backtest = { 'buy': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False }, 'sell': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False } } if len(data.date) > exit * 2: data = data.iloc[(len(data.date) - (2 * max(self.lrc_period_1, self. lrc_period_2, self.lrc_period_3))):] linear_regression_exit, upper_line_exit, lower_line_exit, slope_exit, channel_width_exit = linear_regression_channel( data, self.lrc_period_exit, self.lrc_std_exit) data['cmf'] = cmf(list(data.bidclose), list(data.bidhigh), list(data.bidlow), list(data.tickqty), self.cmf_period) data['cmf_ma'] = ema(list(data.cmf), self.cmf_ma_period) data['r_percent'] = r_percent(data, self.r_percent_period) if current_position == 'buy': if ((data.bidclose.iloc[-1] > upper_line_exit[-1])): self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = True self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = False return 'exit' elif current_position == 'sell': if ((data.bidclose.iloc[-1] < lower_line_exit[-1])): self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = True self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = False return 'exit' else: self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = False self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = False return None else: return None except: return None def backtest_entry(self, data): try: self.condictions_state_backtest = { 'buy': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False }, 'sell': { 'linear_regression_channel': False, 'r_percent': False, 'cmf': False } } if len(data.date) > max(self.lrc_period_1, self.lrc_period_2, self.lrc_period_3): data = data.iloc[(len(data.date) - (2 * max(self.lrc_period_1, self. lrc_period_2, self.lrc_period_3))):] linear_regression_1, upper_line_1, lower_line_1, slope_1, channel_width_1 = linear_regression_channel( data, self.lrc_period_1, self.lrc_std_1) linear_regression_2, upper_line_2, lower_line_2, slope_2, channel_width_2 = linear_regression_channel( data, self.lrc_period_2, self.lrc_std_2) linear_regression_3, upper_line_3, lower_line_3, slope_3, channel_width_3 = linear_regression_channel( data, self.lrc_period_3, self.lrc_std_3) data['cmf'] = cmf(list(data.bidclose), list(data.bidhigh), list(data.bidlow), list(data.tickqty), self.cmf_period) data['cmf_ma'] = ema(list(data.cmf), self.cmf_ma_period) data['r_percent'] = r_percent(data, self.r_percent_period) if ((data.bidclose.iloc[-1] > upper_line_1[-1]) or (data.bidclose.iloc[-1] > upper_line_2[-1]) or (data.bidclose.iloc[-1] > upper_line_3[-1])): self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = True self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = False elif ((data.bidclose.iloc[-1] < lower_line_1[-1]) or (data.bidclose.iloc[-1] < lower_line_2[-1]) or (data.bidclose.iloc[-1] < lower_line_3[-1])): self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = True self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = False else: self.condictions_state_backtest['buy'][ 'linear_regression_channel'] = False self.condictions_state_backtest['sell'][ 'linear_regression_channel'] = False if data.r_percent.iloc[-2] > -10 and data.r_percent.iloc[ -1] < -20: self.condictions_state_backtest['sell']['r_percent'] = True self.condictions_state_backtest['buy']['r_percent'] = False elif data.r_percent.iloc[-2] < -90 and data.r_percent.iloc[ -1] > -80: self.condictions_state_backtest['buy']['r_percent'] = True self.condictions_state_backtest['sell'][ 'r_percent'] = False else: self.condictions_state_backtest['buy']['r_percent'] = False self.condictions_state_backtest['sell'][ 'r_percent'] = False if data.cmf.iloc[-1] > data.cmf_ma.iloc[-1]: self.condictions_state_backtest['sell']['cmf'] = False self.condictions_state_backtest['buy']['cmf'] = True else: self.condictions_state_backtest['sell']['cmf'] = True self.condictions_state_backtest['buy']['cmf'] = False collected_score_buy = 0 collected_score_sell = 0 for k, v in self.conditions_weights.items(): if self.condictions_state_backtest['buy'][k] == True: collected_score_buy += v elif self.condictions_state_backtest['sell'][k] == True: collected_score_sell += v if collected_score_buy >= self.required_score: return 'buy' elif collected_score_sell >= self.required_score: return 'sell' else: return None else: return None except Exception as e: print(e, 11111111111111) return None
def get_data(self): quantity = 2 * max(self.lrc_period_1, self.lrc_period_2, self.lrc_period_3) self.db = Db_Controller() self.data = self.db.query_price_data(self.symbol, self.timeframe, quantity)
class Fxcm(): """ FXCM controller. This class contains methods acting as wrapers for FXCM API class with additional functionality. All the methods are direct implementation of FXCM API Full documentation can be retrived from: https://fxcm.github.io/rest-api-docs/#section/Overview """ def __init__(self): with open('./data/account_info.cfg', 'rb' ) as f: # Reading user's fxcm information from a stored file account_info_dict = pickle.load(f) self.token = account_info_dict[ 'token'] # FXCM token or API key " a46718dbcf04edf1b8135816d96d38a7703f2d65 " use this in gui self.account_type = account_info_dict[ 'account_type'] #Can be demo or real self.connection = None self.connection_status = 'Disconnected' self.db = Db_Controller() self.account_id = account_info_dict['account_id'] self.account_name = account_info_dict['account_name'] self.fxcm_account_currency_list = ['AUD', 'USD', 'EUR', 'GBP'] self.account_currency = account_info_dict['account_currency'] self.available_symbols_list = [ 'AUDUSD', 'EURUSD', 'USDJPY', 'GBPUSD', 'USDCHF', 'NZDUSD' ] self.available_timeframe_list = [ 'm1', 'm5', 'm15', 'm30', 'H1', 'H2', 'H3', 'H4', 'H6', 'H8', 'D1', 'W1', 'M1' ] def timestamp_to_datetime(self, given_time): """ This function converts timestamp to datetime """ given_time = str(given_time) if len(given_time) == 13: given_time = str(0) + given_time new_time = datetime.datetime.strptime(given_time, '%m%d%Y%H%M%S') new_time = new_time.strftime("%Y-%m-%d %H:%M") return new_time elif len(given_time) == 14: new_time = datetime.datetime.strptime(given_time, '%m%d%Y%H%M%S') new_time = new_time.strftime("%Y-%m-%d %H:%M") return new_time elif len(given_time) == 16: given_time = str(0) + given_time new_time = datetime.datetime.strptime(given_time, '%m%d%Y%H%M%S%f') new_time = new_time.strftime("%Y-%m-%d %H:%M") return new_time elif len(given_time) == 17: new_time = datetime.datetime.strptime(given_time, '%m%d%Y%H%M%S%f') new_time = new_time.strftime("%Y-%m-%d %H:%M") return new_time def connect(self, startegy_name): """ This function makes an instance of fxcmpy.fxcmpy to connect to server, and is used by auto trading system for creating connection Once the fxcmpy instance is created, two new threads will be created as fxcmpy does that automatically. the name of its thread can be gotten from 'socket_thread._name' on the connection object. when closing the connection this name is used to stop its thread """ if self.connection == None or self.connection.is_connected() != True: try: self.disconnect() self.connection = fxcmpy(access_token=self.token, log_level='error', server=self.account_type) if self.connection.is_connected(): self.connection_status = 'Connected' else: self.disconnect() self.connection_status = 'Disconnected' except: self.connection_status = 'Disconnected' self.disconnect() def connect_gui(self): """ This function makes an instance of fxcmpy.fxcmpy to connect to server, and is used by gui for creating connection and starting required threads Once the fxcmpy instance is created, two new threads will be created as fxcmpy does that automatically. the name of its thread can be gotten from 'socket_thread._name' on the connection object. when closing the connection this name is used to stop its thread """ try: if self.connection == None or self.connection.is_connected( ) != True: self.disconnect_gui() self.connection = fxcmpy(access_token=self.token, log_level='error', server=self.account_type) connection_status = self.connection.is_connected() if connection_status != True: self.connection_status = 'Disconnected' self.disconnect_gui() return 'Problem in connection' else: self.connection_status = 'Connected' account_info = self.connection.get_accounts( ) #Getting account info data # Inserting account info data if not exits into db if len( self.db.query_table( 'Fxcm_Info', ('accountId', ), fields=('accountId', ), values=(self.account_id, ))) == 0: self.db.insert_into_account_info_table(account_info) return True else: 'Connection is already established' except Exception as e: self.disconnect_gui() return str(e) def start_connection_thread_gui(self): """ This function is called from login page when connect button is clicked. It calls connect_gui ethod to establish connection and then calls gui_threads to create and start threads for updating fxcm data """ result = self.connect_gui() if result == True: self.gui_threads() return True else: return result def gui_threads(self): """ This method initializes threads for updating fxcm data """ class update_all_info_thread(threading.Thread): """ This class inherits from threading.Thread class. Created to handle updating account data, open positions, closed positions and price data for chart """ def __init__(self, fxcm_instance, symbol, timeframe): threading.Thread.__init__(self) self.event = threading.Event() self.fxcm_instance = fxcm_instance self.symbol = symbol self.timeframe = timeframe self.name = 'Update_info_thread' self.change_symbol_timeframe_signal = False self.change_symbol_timeframe(self.symbol, self.timeframe) def change_symbol_timeframe(self, symbol, timeframe): """ This method changes symbol and timeframe for getting price data """ self.fxcm_instance.db.create_price_data_table( symbol, timeframe ) #Creating price data table for new symbol and timeframe self.symbol = symbol self.timeframe = timeframe self.change_symbol_timeframe_signal = True def stop(self): self.event.set() def run(self): while True: try: if self.event.is_set() == True: break else: connection_status = self.fxcm_instance.connection.is_connected( ) if connection_status == True: self.fxcm_instance.connection_status = 'Connected' self.fxcm_instance.get_acc_info() time.sleep(1) self.fxcm_instance.get_open_positions() time.sleep(1) self.fxcm_instance.get_closed_positions() time.sleep(1) if self.change_symbol_timeframe_signal == True: candle_result = self.fxcm_instance.get_price_data( self.symbol, self.timeframe) if candle_result == True: self.change_symbol_timeframe_signal = False else: self.fxcm_instance.disconnect_gui() time.sleep(30) self.fxcm_instance.connect_gui() else: candle_result = self.fxcm_instance.get_price_data( self.symbol, self.timeframe, 100) if candle_result == False: self.fxcm_instance.disconnect_gui() time.sleep(30) self.fxcm_instance.connect_gui() else: self.fxcm_instance.connection_status = 'Disconnected' self.fxcm_instance.disconnect_gui() time.sleep(30) self.fxcm_instance.connect_gui() self.event.clear() time.sleep(10) except: self.fxcm_instance.connection_status = 'Disconnected' self.fxcm_instance.disconnect_gui() time.sleep(30) self.fxcm_instance.connect_gui() self.event.clear() #Creating an instance of update_all_info_thread and starting it to start updating required data self.all_info_thread = update_all_info_thread( self, self.available_symbols_list[0], self.available_timeframe_list[0]) self.all_info_thread.start() def disconnect(self): """ This method disconnect autotrading fxcm connection objects """ try: self.connection.socket._heartbeat_thread._halt.set() self.connection = None self.connection_status = 'Disconnected' except: self.connection = None self.connection_status = 'Disconnected' def disconnect_gui(self): """ This method disconnect gui fxcm connection objects and stop update related threads """ try: try: self.all_info_thread.stop( ) #Stoping the thread of updating information except: pass try: self.connection.socket._heartbeat_thread._halt.set() self.connection = None self.connection_status = 'Disconnected' except: self.connection = None self.connection_status = 'Disconnected' except: pass def change_symbol_timeframe_chart(self, symbol, timeframe): """ This method is called from gui to change symbol and timeframe used in update_all_info_thread to get new data """ try: self.all_info_thread.change_symbol_timeframe(symbol, timeframe) except: pass def get_price_data(self, trading_symbol, timeframe, quantity=10000): """ This method gets price data for given symbol and timeframe from fxcm and sends it to be stored in db """ try: symbol = list(trading_symbol) symbol.insert(3, '/') symbol = ''.join(symbol) data = pd.DataFrame() data = self.connection.get_candles(symbol, period=timeframe, number=quantity) if data.empty != True: self.db.insert_into_price_data_table(data, trading_symbol, timeframe) return True else: return False except: return False def get_acc_info(self): """ This method gets account info from fxcm and sends it to be stored in db """ acc_info = self.connection.get_accounts() self.db.update_account_info_table(self.account_id, acc_info) def update_token(self, new_token, account_currency, account_type): """ This method updates token and account currency. It does not only changes the token value, it validates it by connecting to server using given token and account type and if the connection is established, the it get account info from fxcm to store accountId and accountName and finally it disconnects and store update token, accountId and accountName, account type and account currency in a file named account_info.cfg. """ try: self.token = new_token self.account_type = account_type self.account_currency = account_currency self.connection = fxcmpy(access_token=self.token, log_level='error', server=self.account_type) account_info = self.connection.get_accounts() self.disconnect() self.account_id = account_info.accountId.iloc[0] self.account_name = account_info.accountName.iloc[0] if len( self.db.query_table('Fxcm_Info', ('accountId', ), fields=('accountId', ), values=(self.account_id, ))) == 0: self.db.insert_into_account_info_table(account_info) with open('./data/account_info.cfg', 'rb') as f: account_info_dict = pickle.load(f) account_info_dict['token'] = self.token account_info_dict['account_type'] = self.account_type account_info_dict['account_id'] = self.account_id account_info_dict['account_name'] = self.account_name account_info_dict['account_currency'] = self.account_currency with open('./data/account_info.cfg', 'wb') as f: pickle.dump(account_info_dict, f) self.connection = None return True except Exception as e: try: self.disconnect() except: pass self.connection = None self.account_id = None self.account_name = None self.account_type = '' self.token = '' self.account_currency = None return str(e) def get_open_positions(self): """ This method gets open positions from fxcm and sends it to be stored in db """ open_positions = self.connection.get_open_positions() self.db.update_open_positions_table(open_positions, self.account_id) def get_open_trade_ids(self): return self.connection.get_open_trade_ids() def get_closed_positions(self): """ This method gets closed positions from fxcm and sends it to be stored in db """ closed_positions = self.connection.get_closed_positions() self.db.update_closed_positions_table(closed_positions) def open_position(self, **position_parameters): try: print('open_position...............') """ Function to add a position to FXCM server Inputs: **position_parameters->dictionary List of different variables to open a position Output: Opened position and created db row and tradeId """ position_maker = position_parameters['maker'] symbol = position_parameters['symbol'] symbol = list(symbol) symbol.insert(3, '/') symbol = ''.join(symbol) del position_parameters['maker'] position_parameters['symbol'] = symbol order = self.connection.open_trade(**position_parameters) trade_id = int(order.get_tradeId()) while True: open_positions = self.connection.get_open_positions() data = open_positions.loc[open_positions['tradeId'] == str( trade_id)] if data.empty == False: break time.sleep(10) data['positionMaker'] = [ position_maker, ] self.db.insert_into_open_positions(data) return trade_id except: return None def close_position(self, **position_parameters): try: print('close_position...............') """ Function to close a position from FXCM server Inputs: **position_parameters->dictionary List of different variables to close a position Output: Closed FXCM position, deleted OpenPosition row and created ClosedPosition row in db """ position_maker = position_parameters['maker'] del position_parameters['maker'] self.connection.close_trade(**position_parameters) self.db.delete_from_table('OpenPosition', position_parameters['trade_id']) closed_positions = self.connection.get_closed_positions() closed_position = closed_positions.loc[ closed_positions['tradeId'] == str( position_parameters['trade_id'])] closed_position['positionMaker'] = [ position_maker, ] self.db.insert_into_closed_positions(closed_position) return True except Exception as e: print(e) return False def edit_position_stop_limit(self, **position_parameters): """ This method edits an open position """ try: self.connection.change_trade_stop_limit(**position_parameters) except: pass def close_all_positions(self): """ This method closess all open positions """ try: data = self.connection.get_open_positions() for i, j in enumerate(data.tradeId): self.db.delete_from_table('OpenPosition', j) self.connection.close_all() data = self.connection.get_closed_positions() makers_list = [] for i in range(len(data.tadeId)): makers_list.append('Manual') data['positionMaker'] = makers_list self.db.insert_into_closed_positions(data) except: pass def get_open_positions_ids(self): try: return self.connection.get_open_trade_ids() except: return [] def get_default_acc_id(self): try: return self.connection.get_default_account() except: return []