def __init__(self, df: pd.DataFrame, table: str, date_range: str, column_sort: str): self._df = df self._table_name = table self._table = StockDatabase().get_table_by_name(self._table_name) self._date_range = date_range self._columns = self._df.columns self._sort_column = column_sort self._rows = [] self.__fill_rows_for_selected_table__()
def __init__(self, std_dev_range=30, len_learning_range=30, train_model=False, number_symbols=1, manipulation_type=MT.ONE_ROW_PER_VALUE, breakout_level_threshold=0): self._std_dev_range = std_dev_range self._len_learning_range = len_learning_range self._db_stock = StockDatabase() self._manipulation_type = manipulation_type self._breakout_level_threshold = breakout_level_threshold self._list = [ 'MMM', 'MCD', 'MRK', 'MSFT', 'MAR', 'MXIM', 'MCHP', 'MU', 'MNST', 'MYL', 'MDLZ', 'MELI' ] self._symbols = self._list[:number_symbols] self._col_value_dict = { DC.OPEN: 1, DC.LOW: 2, DC.HIGH: 3, DC.CLOSE: 4, DC.VOLUME: 5 } self._col_value_dict = {DC.OPEN: 1, DC.CLOSE: 4, DC.VOLUME: 5} if train_model: self.__train_model__()
def add_wave_tick_lists(self): access_layer_stock = AccessLayer4Stock(StockDatabase()) trade_entity = self.get_first_element() while trade_entity is not None: symbol = trade_entity.symbol ts_list = [ trade_entity.ts_before_pattern, trade_entity.ts_pattern_start, trade_entity.ts_pattern_end, trade_entity.ts_trade_max ] wave_tick_lists = access_layer_stock.get_wave_tick_lists_for_time_stamp_intervals( symbol, ts_list) trade_entity.wave_tick_list_before_pattern = wave_tick_lists[0] trade_entity.wave_tick_list_pattern = wave_tick_lists[1] trade_entity.wave_tick_list_after_breakout = wave_tick_lists[2] trade_entity = self.get_next_element()
class DBTable: def __init__(self, df: pd.DataFrame, table: str, date_range: str, column_sort: str): self._df = df self._table_name = table self._table = StockDatabase().get_table_by_name(self._table_name) self._date_range = date_range self._columns = self._df.columns self._sort_column = column_sort self._rows = [] self.__fill_rows_for_selected_table__() @property def columns(self): return self._columns @property def height_for_display(self): height = max(100, 50 + len(self._rows) * 40) if height > 400: return 400 return max(100, height) def get_rows_for_selected_items(self): if len(self._rows) == 0: return [DBRow(self._columns).get_row_as_dict()] DBRow.sort_column = self._sort_column sort_reverse = True sorted_list = sorted(self._rows, reverse=sort_reverse) return [row.get_row_as_dict() for row in sorted_list] def get_table_style_cell_conditional(self) -> list: cols_left = self._table.get_string_column_names() return [{ 'if': { 'column_id': c }, 'textAlign': 'left' } for c in cols_left] def __fill_rows_for_selected_table__(self): self._rows = [] if self._table_name != '': for index, row in self._df.iterrows(): db_row = DBRow(self._columns) for column in self._columns: db_row.add_value(column, row[column]) self._rows.append(db_row)
def __init__(self, stock_db: StockDatabase = None): self._base_value = 1000.0 self._stock_db = StockDatabase() if stock_db is None else stock_db self._access_layer_stocks = AccessLayer4Stock(self._stock_db) self._columns = [ 'Symbol', 'Timestamp', 'Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume' ] self._index = self.__get_index__() self._period = PRD.DAILY self._aggregation = 1 self._total_volume_average = 0 self._index_elements = 0 self._index_element_dict = {} self.__fill_ticker_element_dict__() self.__adjust_to_total_average_volume__()
def __init__(self, pattern_type=FT.CHANNEL, period=PRD.DAILY, mean_aggregation=4, number_trades=math.inf, pattern_id='', compare_with_orig=True): self._pattern_type = pattern_type self._period = period self._mean_aggregation = mean_aggregation self._number_trades = number_trades self._pattern_id = pattern_id self._compare_with_orig = compare_with_orig self._trade_access_layer = AccessLayer4Trade(StockDatabase()) self._trade_entity_collection = None self._trade_policy = None self.__fill_trade_entity_collection__() self._print_details_per_trade = False
def __init__(self, for_semi_deep_copy=False, with_predictor=True, run_on_server=False): # print('SystemConfiguration.__init__: for_semi_deep_copy={}'.format(for_semi_deep_copy)) self.run_on_server = run_on_server self.file_log = PatternLog() self.runtime_config = RuntimeConfiguration() self.crypto_config = BitfinexConfiguration() self.exchange_config = self.crypto_config self.exchange_config.small_profit_parameter_dict = self.__get_small_profit_parameter_dict__( ) self.shares_config = IBKRConfiguration() self.sound_machine = PatternSoundMachine() self.process_manager = PatternProcessManager() if for_semi_deep_copy: return self.config = PatternConfiguration() self.db_stock = StockDatabase() self.index_config = IndexConfiguration( self.db_stock, INDICES.get_index_list_for_index_configuration()) self.pattern_table = PatternTable() self.trade_table = TradeTable() self.wave_table = WaveTable() self.asset_table = AssetTable() self.df_cache = MyDataFrameCache() self.graph_cache = MyGraphCache() self.data_provider = PatternDataProvider(self.config, self.index_config, self.db_stock, self.df_cache, self.run_on_server) if with_predictor: self.predictor_optimizer = PatternPredictorOptimizer(self.db_stock) self.fibonacci_predictor = FibonacciPredictor(self.db_stock, 7) self.fibonacci_wave_data_handler = FibonacciWaveDataHandler( self.db_stock) self.master_predictor_handler = PatternMasterPredictorHandler( self.__get_pattern_predictor_api__(self.config)) self.trade_strategy_optimizer = TradeOptimizer( self.db_stock, self.expected_win_pct)
Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2018-05-14 """ from pattern_database.stock_database import StockDatabase from sertl_analytics.constants.pattern_constants import PRD, WAVEST, INDICES from fibonacci.fibonacci_wave_data import FibonacciWaveDataHandler class FWDHTC: # FibonacciWaveDataHandler TestCase RELOAD = 'Reload' HEAT_MAP = 'Heatmap' fibonacci_wave_data_handler = FibonacciWaveDataHandler(StockDatabase()) fibonacci_wave_data_handler.load_data(period=PRD.DAILY, aggregation=1) test_case = FWDHTC.HEAT_MAP if test_case == FWDHTC.RELOAD: fibonacci_wave_data_handler.reload_data_when_outdated() elif test_case == FWDHTC.HEAT_MAP: period = PRD.DAILY aggregation = 1 index = INDICES.Q_FSE fibonacci_wave_data_handler.init_tick_key_list_for_retrospection( 100, period=period, aggregation=aggregation) x_data = fibonacci_wave_data_handler.tick_key_list_for_retrospection y_data = WAVEST.get_waves_types_for_processing( [fibonacci_wave_data_handler.period_for_retrospection])
""" Description: This module contains tests for both classes IndexConfiguration and IndicesComponentFetcher Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2019-01-15 """ from sertl_analytics.constants.pattern_constants import INDICES, EQUITY_TYPE from sertl_analytics.datafetcher.web_data_fetcher import IndicesComponentFetcher from pattern_index_configuration import IndexConfiguration from pattern_database.stock_database import StockDatabase # indices_component_list = IndicesComponentFetcher.get_ticker_name_dic(INDICES.CRYPTO_CCY) # print(indices_component_list.items()) index_configuration = IndexConfiguration( StockDatabase(), INDICES.get_index_list_for_index_configuration()) symbol = 'MMM' index = index_configuration.get_index_for_symbol(symbol) print('Index for symbol {}={}'.format(symbol, index))
def __perform_task__(self): db_stock = StockDatabase() db_stock.update_stock_data_by_index(INDICES.FOREX, PRD.DAILY)
def __perform_task__(self): db_stock = StockDatabase() db_stock.update_stock_data_by_index(INDICES.INDICES, PRD.DAILY) db_stock.update_stock_data_by_index(INDICES.DOW_JONES, PRD.DAILY) db_stock.update_stock_data_for_symbol('MRAM') db_stock.update_stock_data_for_symbol('FCEL') db_stock.update_stock_data_for_symbol('TSLA') db_stock.update_stock_data_for_symbol('GE') db_stock.update_stock_data_by_index(INDICES.NASDAQ100, PRD.DAILY) db_stock.update_stock_data_by_index(INDICES.Q_FSE, PRD.DAILY) self._db_updater.calculate_index_list( [INDICES.NASDAQ100, INDICES.Q_FSE])
def __perform_task__(self): db_stock = StockDatabase() exchange_config = BitfinexConfiguration() db_stock.update_crypto_currencies( PRD.DAILY, symbol_list=exchange_config.ticker_id_list)
def __perform_task__(self): db_stock = StockDatabase() db_stock.delete_duplicate_records(db_stock.trade_table) db_stock.delete_duplicate_records(db_stock.pattern_table) db_stock.delete_duplicate_records(db_stock.wave_table)
def __get_db__(self): return StockDatabase()
""" Description: This module contains test cases for the Fibonacci wave predictions Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2019-02-20 """ from pattern_database.stock_database import StockDatabase from fibonacci.fibonacci_predictor import FibonacciPredictor from sertl_analytics.constants.pattern_constants import PRED, STBL, FT, DC, MT, MDC, WPC label = DC.WAVE_END_FLAG # label = DC.WAVE_MAX_RETR_PCT # label = DC.WAVE_MAX_RETR_TS_PCT fib_prediction = FibonacciPredictor(StockDatabase(), 5) # fib_prediction.train_models_for_label(label) # fib_prediction.perform_cross_validation(label)
def __get_df_secondary__(): # ToDo - this should only be available at the deferred class... return StockDatabase().get_trade_records_for_statistics_as_dataframe()
def __init__(self, stock_db: StockDatabase = None): self._stock_db = StockDatabase() if stock_db is None else stock_db self._table = self.__get_table__()
class AccessLayer: def __init__(self, stock_db: StockDatabase = None): self._stock_db = StockDatabase() if stock_db is None else stock_db self._table = self.__get_table__() @property def table_name(self): return self._table.name def get_all_as_data_frame(self, index_col='', columns=None, where_clause=''): selected_columns = '*' if columns is None else ','.join(columns) where_clause_new = '' if where_clause == '' else ' WHERE {}'.format( where_clause) return self.select_data_by_query( "SELECT {} from {}{}".format(selected_columns, self._table.name, where_clause_new), index_col) def get_all_as_data_dict(self, index_col: str): df = self.select_data_by_query( "SELECT * from {}".format(self._table.name), index_col) return df.to_dict(orient='index') def get_as_data_dict_by_data_dict(self, data_dict: dict, sort_columns=None, index_col=''): df = self.select_data_by_data_dict(data_dict, target_columns=None, sort_columns=sort_columns, index_col=index_col) return df.to_dict(orient='index') def select_data_by_data_dict(self, data_dict: dict, target_columns=None, sort_columns=None, index_col='') -> pd.DataFrame: query = self._table.get_query_select_by_data_dict( data_dict, target_columns, sort_columns) db_df = DatabaseDataFrame(self._stock_db, query) return self.__get_dataframe_with_index_column__(db_df.df, index_col) def select_data_by_query(self, query: str, index_col='') -> pd.DataFrame: db_df = DatabaseDataFrame(self._stock_db, query) return self.__get_dataframe_with_index_column__(db_df.df, index_col) @staticmethod def __get_dataframe_with_index_column__(df: pd.DataFrame, index_col: str): if not df.empty and index_col != '': df.set_index(index_col, drop=False, inplace=True) return df def insert_data(self, input_dict_list: list): self._stock_db.insert_data_into_table(self._table.name, input_dict_list) def update_record(self, record_id: str, data_dict: dict): # Syntax: UPDATE users SET field1='value1', field2='value2' pass def delete_record_by_id(self, record_id: str): pass def delete_record_by_rowid(self, rowid: int): query = self._table.get_query_delete_by_row_id(rowid) print(query) self._stock_db.delete_records(query) def delete_existing_record(self, data_dict: dict): df = self.__get_data_frame_with_row_id__(data_dict) if df.shape[0] > 0: self.delete_record_by_rowid(df.iloc[0][DC.ROWID]) else: print('Nothing to delete for table {}: {}'.format( self._table.name, data_dict)) def is_record_available(self, data_dict: dict): df = self.__get_data_frame_with_row_id__(data_dict) return df.shape[0] > 0 def are_any_records_available(self, data_dict: dict): df = self.__get_data_frame_with_row_id__(data_dict) return df.shape[0] > 0 def __get_data_frame_with_row_id__(self, data_dict: dict) -> pd.DataFrame: query = self._table.get_query_select_by_data_dict(data_dict) return self.__get_data_frame_with_row_id_by_query__(query) def __get_data_frame_with_row_id_by_query__(self, query: str) -> pd.DataFrame: db_df = DatabaseDataFrame(self._stock_db, query) return db_df.df def __get_table__(self) -> MyTable: pass def get_update_set_clause_from_data_dict(self, data_dict: dict): return self._table.get_update_set_clause_from_data_dict(data_dict)
""" Description: This module starts the stock database processes (create tables, update data, ...) CAUTION: This script is NOT saved on git hub. Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2018-06-10 """ from pattern_database.stock_database import StockDatabase from pattern_database.stock_index_handler import IndexHandler from sertl_analytics.constants.pattern_constants import INDICES stock_db = StockDatabase() index_handler = IndexHandler(index=INDICES.NASDAQ100, db_stock=stock_db, save_to_database=False) index_handler.calculate_index_data_frame()
""" Description: This module contains test cases for access layer methods Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2018-05-14 """ from pattern_database.stock_index_data_frame import IndexDataFrame from pattern_database.stock_database import StockDatabase db_stock = StockDatabase() index_data_frame = IndexDataFrame(db_stock) index_data_frame.print_details() # index_data_frame.get_index_data_frame_for_date_range('2019-01-01', '2020-01-01')
""" Description: This module starts the stock database processes (create tables, update data, ...) CAUTION: This script is NOT saved on git hub. Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2018-06-10 """ from pattern_database.stock_database import StockDatabase stock_db = StockDatabase() # stock_db.create_pattern_feature_table() # stock_db.create_pattern_table() stock_db.create_pattern_view() # stock_db.create_wave_view() # symbol_list = ['MMM', 'NVDA', 'EURUSD', 'BTCUSD'] # for symbol in symbol_list: # print('Index for {}: {}'.format(symbol, stock_db.get_index_for_symbol(symbol)))
def __init__(self): self._trade_access_layer = AccessLayer4Trade(StockDatabase()) self.__init_parameters__()