def test_macd(): factor = TechnicalFactor(codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider=Provider.JoinQuant, computing_window=None, transformer=MacdTransformer(), adjust_type='qfq') print(factor.factor_df.tail()) # compare with east money manually diff = factor.factor_df['diff'] dea = factor.factor_df['dea'] macd = factor.factor_df['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.14 assert round(dea.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.15 assert round(macd.loc[('stock_sz_000338', '2019-06-10')], 2) == 0.02 factor.move_on(to_timestamp='2019-06-17') diff = factor.factor_df['diff'] dea = factor.factor_df['dea'] macd = factor.factor_df['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-17')], 2) == 0.06 assert round(dea.loc[('stock_sz_000338', '2019-06-17')], 2) == -0.03 assert round(macd.loc[('stock_sz_000338', '2019-06-17')], 2) <= 0.19
def get_trading_signals_figure(order_reader: OrderReader, security_id: str, provider: Union[str, Provider], level): security_type, _, _ = decode_security_id(security_id) security_factor = TechnicalFactor(security_type=security_type, security_list=[security_id], level=level, provider=provider) if df_is_not_null(security_factor.get_data_df()): print(security_factor.get_data_df().tail()) # generate the annotation df order_reader.move_on(timeout=0) df = order_reader.get_data_df().copy() if df_is_not_null(df): df['value'] = df['order_price'] df['flag'] = df['order_type'].apply(lambda x: order_type_flag(x)) df['color'] = df['order_type'].apply(lambda x: order_type_color(x)) print(df.tail()) data, layout = security_factor.draw(render=None, figures=go.Candlestick, annotation_df=df) return go.Figure(data=data, layout=layout)
def test_macd(): factor = TechnicalFactor(entity_type='stock', codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider='joinquant', indicators=['macd'], indicators_param=[{ 'slow': 26, 'fast': 12, 'n': 9 }], auto_load=True) print(factor.get_depth_df().tail()) # compare with east money manually diff = factor.get_depth_df()['diff'] dea = factor.get_depth_df()['dea'] macd = factor.get_depth_df()['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.14 assert round(dea.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.15 assert round(macd.loc[('stock_sz_000338', '2019-06-10')], 2) == 0.02 factor.move_on(to_timestamp='2019-06-17') diff = factor.get_depth_df()['diff'] dea = factor.get_depth_df()['dea'] macd = factor.get_depth_df()['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-17')], 2) == 0.06 assert round(dea.loc[('stock_sz_000338', '2019-06-17')], 2) == -0.03 assert round(macd.loc[('stock_sz_000338', '2019-06-17')], 2) == 0.19
def test_ma(): factor = TechnicalFactor(codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider=Provider.JoinQuant, computing_window=30, transformer=MaTransformer(windows=[5, 10, 30]), adjust_type='qfq') print(factor.factor_df.tail()) # compare with east money manually ma5 = factor.factor_df['ma5'] ma10 = factor.factor_df['ma10'] ma30 = factor.factor_df['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-10')], 2) <= 11.23 assert round(ma10.loc[('stock_sz_000338', '2019-06-10')], 2) <= 11.43 assert round(ma30.loc[('stock_sz_000338', '2019-06-10')], 2) <= 11.52 factor.move_on(to_timestamp='2019-06-17') ma5 = factor.factor_df['ma5'] ma10 = factor.factor_df['ma10'] ma30 = factor.factor_df['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-17')], 2) <= 12.06 assert round(ma10.loc[('stock_sz_000338', '2019-06-17')], 2) <= 11.64 assert round(ma30.loc[('stock_sz_000338', '2019-06-17')], 2) <= 11.50
def test_ma(): factor = TechnicalFactor(security_type=SecurityType.stock, codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=TradingLevel.LEVEL_1DAY, provider=Provider.JOINQUANT, indicators=['ma', 'ma', 'ma'], indicators_param=[{ 'window': 5 }, { 'window': 10 }, { 'window': 30 }]) print(factor.get_depth_df().tail()) # compare with east money manually ma5 = factor.get_depth_df()['ma5'] ma10 = factor.get_depth_df()['ma10'] ma30 = factor.get_depth_df()['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.48 assert round(ma10.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.69 assert round(ma30.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.79 factor.move_on(to_timestamp='2019-06-17') ma5 = factor.get_depth_df()['ma5'] ma10 = factor.get_depth_df()['ma10'] ma30 = factor.get_depth_df()['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-17')], 2) == 12.33 assert round(ma10.loc[('stock_sz_000338', '2019-06-17')], 2) == 11.91 assert round(ma30.loc[('stock_sz_000338', '2019-06-17')], 2) == 11.76
def test_macd(): factor = TechnicalFactor(security_type=SecurityType.stock, codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=TradingLevel.LEVEL_1DAY, provider=Provider.JOINQUANT, indicators=['macd'], indicators_param=[{ 'slow': 26, 'fast': 12, 'n': 9 }]) print(factor.get_depth_df().tail()) # compare with east money manually diff = factor.get_depth_df()['diff'] dea = factor.get_depth_df()['dea'] macd = factor.get_depth_df()['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.15 assert round(dea.loc[('stock_sz_000338', '2019-06-10')], 2) == -0.16 assert round(macd.loc[('stock_sz_000338', '2019-06-10')], 2) == 0.02 factor.move_on(to_timestamp='2019-06-17') diff = factor.get_depth_df()['diff'] dea = factor.get_depth_df()['dea'] macd = factor.get_depth_df()['macd'] assert round(diff.loc[('stock_sz_000338', '2019-06-17')], 2) == 0.07 assert round(dea.loc[('stock_sz_000338', '2019-06-17')], 2) == -0.03 assert round(macd.loc[('stock_sz_000338', '2019-06-17')], 2) == 0.19
def test_macd(): factor = TechnicalFactor( provider="joinquant", codes=["000338"], start_timestamp="2019-01-01", end_timestamp="2019-06-10", level=IntervalLevel.LEVEL_1DAY, computing_window=None, transformer=MacdTransformer(), adjust_type="qfq", ) print(factor.factor_df.tail()) # compare with east money manually diff = factor.factor_df["diff"] dea = factor.factor_df["dea"] macd = factor.factor_df["macd"] assert round(diff.loc[("stock_sz_000338", "2019-06-10")], 2) == -0.14 assert round(dea.loc[("stock_sz_000338", "2019-06-10")], 2) == -0.15 assert round(macd.loc[("stock_sz_000338", "2019-06-10")], 2) == 0.02 factor.move_on(to_timestamp="2019-06-17") diff = factor.factor_df["diff"] dea = factor.factor_df["dea"] macd = factor.factor_df["macd"] assert round(diff.loc[("stock_sz_000338", "2019-06-17")], 2) == 0.06 assert round(dea.loc[("stock_sz_000338", "2019-06-17")], 2) == -0.03 assert round(macd.loc[("stock_sz_000338", "2019-06-17")], 2) <= 0.19
def test_ma(): factor = TechnicalFactor( provider="joinquant", codes=["000338"], start_timestamp="2019-01-01", end_timestamp="2019-06-10", level=IntervalLevel.LEVEL_1DAY, computing_window=30, transformer=MaTransformer(windows=[5, 10, 30]), adjust_type="qfq", ) print(factor.factor_df.tail()) # compare with east money manually ma5 = factor.factor_df["ma5"] ma10 = factor.factor_df["ma10"] ma30 = factor.factor_df["ma30"] assert round(ma5.loc[("stock_sz_000338", "2019-06-10")], 2) <= 11.23 assert round(ma10.loc[("stock_sz_000338", "2019-06-10")], 2) <= 11.43 assert round(ma30.loc[("stock_sz_000338", "2019-06-10")], 2) <= 11.52 factor.move_on(to_timestamp="2019-06-17") ma5 = factor.factor_df["ma5"] ma10 = factor.factor_df["ma10"] ma30 = factor.factor_df["ma30"] assert round(ma5.loc[("stock_sz_000338", "2019-06-17")], 2) <= 12.06 assert round(ma10.loc[("stock_sz_000338", "2019-06-17")], 2) <= 11.64 assert round(ma30.loc[("stock_sz_000338", "2019-06-17")], 2) <= 11.50
def get_trader_details(account_reader: AccountReader, order_reader: OrderReader, provider: Union[str, Provider]): graph_list = [] account_data, account_layout = account_reader.draw(render=None, value_field='all_value', keep_ui_state=False) for trader_name in account_reader.trader_names: graph_list.append( dcc.Graph(id='{}_account'.format(trader_name), figure={ 'data': account_data, 'layout': account_layout })) df_account = account_reader.get_data_df() if df_is_not_null(df_account): df_orders = order_reader.get_data_df() if df_is_not_null(df_orders): grouped = df_orders.groupby('security_id') for security_id, order_df in grouped: security_type, _, _ = decode_security_id(security_id) security_factor = TechnicalFactor(security_type=security_type, security_list=[security_id], level=account_reader.level, provider=provider) if df_is_not_null(security_factor.get_data_df()): print(security_factor.get_data_df().tail()) data, layout = security_factor.draw(figure=go.Candlestick, render=None, keep_ui_state=False) graph_list.append( dcc.Graph(id='{}_signals'.format(security_id), figure={ 'data': data, 'layout': layout })) return graph_list
def test_ma(): factor = TechnicalFactor(entity_type='stock', codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider='joinquant', indicators=['ma', 'ma', 'ma'], indicators_param=[{ 'window': 5 }, { 'window': 10 }, { 'window': 30 }], auto_load=True) print(factor.get_depth_df().tail()) # compare with east money manually ma5 = factor.get_depth_df()['ma5'] ma10 = factor.get_depth_df()['ma10'] ma30 = factor.get_depth_df()['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.23 assert round(ma10.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.43 assert round(ma30.loc[('stock_sz_000338', '2019-06-10')], 2) == 11.52 factor.move_on(to_timestamp='2019-06-17') ma5 = factor.get_depth_df()['ma5'] ma10 = factor.get_depth_df()['ma10'] ma30 = factor.get_depth_df()['ma30'] assert round(ma5.loc[('stock_sz_000338', '2019-06-17')], 2) == 12.06 assert round(ma10.loc[('stock_sz_000338', '2019-06-17')], 2) == 11.64 assert round(ma30.loc[('stock_sz_000338', '2019-06-17')], 2) == 11.50
def get_trader_detail_figures(trader_domain: business.Trader, account_reader: AccountReader, order_reader: OrderReader): graph_list = [] if account_reader: account_data, account_layout = account_reader.draw( render=None, value_fields=['all_value'], keep_ui_state=False) for trader_name in account_reader.trader_names: graph_list.append( dcc.Graph(id='{}-account'.format(trader_name), figure={ 'data': account_data, 'layout': account_layout })) order_reader.move_on(timeout=0) df_orders = order_reader.get_data_df().copy() if df_is_not_null(df_orders): grouped = df_orders.groupby('security_id') for security_id, order_df in grouped: security_type, _, _ = decode_security_id(security_id) indicators = [] indicators_param = [] indicator_cols = [] if trader_domain.technical_factors: tech_factors = simplejson.loads( trader_domain.technical_factors) for factor in tech_factors: indicators += factor['indicators'] indicators_param += factor['indicators_param'] indicator_cols += factor['indicator_cols'] security_factor = TechnicalFactor( security_type=security_type, security_list=[security_id], start_timestamp=trader_domain.start_timestamp, end_timestamp=trader_domain.end_timestamp, level=trader_domain.level, provider=trader_domain.provider, indicators=indicators, indicators_param=indicators_param) # generate the annotation df df = order_df.copy() if df_is_not_null(df): df['value'] = df['order_price'] df['flag'] = df['order_type'].apply( lambda x: order_type_flag(x)) df['color'] = df['order_type'].apply( lambda x: order_type_color(x)) print(df.tail()) data, layout = security_factor.draw_with_indicators( render=None, annotation_df=df, indicators=indicator_cols, height=620) if trader_domain.real_time: result = get_current_price(security_list=[security_id]) bid_ask = result.get(security_id) if bid_ask: graph_list.append( daq.LEDDisplay(id='ask', label=f'ask price', value=bid_ask[0], color="#00da3c")) graph_list.append( daq.LEDDisplay(id='bid', label=f'bid price', value=bid_ask[1], color="#FF5E5E")) graph_list.append( dcc.Graph(id='{}-{}-signals'.format(trader_domain.trader_name, security_id), figure={ 'data': data, 'layout': layout })) return graph_list
def get_trader_detail_figures(trader_domain: business.Trader, account_reader: AccountReader, order_reader: OrderReader): graph_list = [] account_data, account_layout = account_reader.draw( render=None, value_fields=['all_value'], keep_ui_state=False) for trader_name in account_reader.trader_names: graph_list.append( dcc.Graph(id='{}-account'.format(trader_name), figure={ 'data': account_data, 'layout': account_layout })) df_orders = order_reader.get_data_df() if df_is_not_null(df_orders): grouped = df_orders.groupby('security_id') for security_id, order_df in grouped: security_type, _, _ = decode_security_id(security_id) # TODO:just show the indicators used by the trader security_factor = TechnicalFactor( security_type=security_type, security_list=[security_id], start_timestamp=trader_domain.start_timestamp, end_timestamp=trader_domain.end_timestamp, level=trader_domain.level, provider=trader_domain.provider, indicators=['ma', 'ma'], indicators_param=[{ 'window': 5 }, { 'window': 10 }]) # if df_is_not_null(security_factor.get_data_df()): # print(security_factor.get_data_df().tail()) # generate the annotation df order_reader.move_on(timeout=0) df = order_reader.get_data_df().copy() if df_is_not_null(df): df['value'] = df['order_price'] df['flag'] = df['order_type'].apply( lambda x: order_type_flag(x)) df['color'] = df['order_type'].apply( lambda x: order_type_color(x)) print(df.tail()) data, layout = security_factor.draw_with_indicators( render=None, annotation_df=df) graph_list.append( dcc.Graph(id='{}-{}-signals'.format(trader_domain.trader_name, security_id), figure={ 'data': data, 'layout': layout })) return graph_list