Exemplo n.º 1
0
def test_MacdTransformer():
    df = get_kdata(entity_id='stock_sz_000338',
                   start_timestamp='2019-01-01',
                   provider='joinquant')

    t = MacdTransformer()

    result_df = t.transform(df)

    print(result_df)
Exemplo n.º 2
0
def test_MacdTransformer():
    df = get_kdata(region=Region.CHN, entity_id='stock_sz_000338',
                   start_timestamp='2019-01-01',
                   provider=Provider.JoinQuant,
                   index=['entity_id', 'timestamp'])

    t = MacdTransformer()

    result_df = t.transform(df)

    print(result_df)
Exemplo n.º 3
0
def test_MacdTransformer():
    df = get_kdata(
        entity_id="stock_sz_000338",
        start_timestamp="2019-01-01",
        provider="joinquant",
        index=["entity_id", "timestamp"],
    )

    t = MacdTransformer()

    result_df = t.transform(df)

    print(result_df)
Exemplo n.º 4
0
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
Exemplo n.º 5
0
 def __init__(self,
              entity_schema: Type[EntityMixin] = Stock,
              provider: str = None,
              entity_provider: str = None,
              entity_ids: List[str] = None,
              exchanges: List[str] = None,
              codes: List[str] = None,
              the_timestamp: Union[str, pd.Timestamp] = None,
              start_timestamp: Union[str, pd.Timestamp] = None,
              end_timestamp: Union[str, pd.Timestamp] = None,
              columns: List = None,
              filters: List = None,
              order: object = None,
              limit: int = None,
              level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
              category_field: str = 'entity_id',
              time_field: str = 'timestamp',
              computing_window: int = None,
              keep_all_timestamp: bool = False,
              fill_method: str = 'ffill',
              effective_number: int = None,
              need_persist: bool = False,
              dry_run: bool = False,
              factor_name: str = None,
              clear_state: bool = False,
              adjust_type: Union[AdjustType, str] = None) -> None:
     super().__init__(entity_schema, provider, entity_provider, entity_ids,
                      exchanges, codes, the_timestamp, start_timestamp,
                      end_timestamp, columns, filters, order, limit, level,
                      category_field, time_field, computing_window,
                      keep_all_timestamp, fill_method, effective_number,
                      MacdTransformer(), None, need_persist, dry_run,
                      factor_name, clear_state, adjust_type)
Exemplo n.º 6
0
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
Exemplo n.º 7
0
class MacdFactor(TechnicalFactor):
    transformer = MacdTransformer()

    def drawer_factor_df_list(self) -> Optional[List[pd.DataFrame]]:
        return None

    def drawer_sub_df_list(self) -> Optional[List[pd.DataFrame]]:
        return [self.factor_df]
Exemplo n.º 8
0
class MacdFactor(TechnicalFactor):
    transformer = MacdTransformer(count_live_dead=True)

    def drawer_factor_df_list(self) -> Optional[List[pd.DataFrame]]:
        return None

    def drawer_sub_df_list(self) -> Optional[List[pd.DataFrame]]:
        return [self.factor_df[["diff", "dea", "macd"]]]

    def drawer_sub_col_chart(self) -> Optional[dict]:
        return {"diff": "line", "dea": "line", "macd": "bar"}
Exemplo n.º 9
0
class MacdFactor(TechnicalFactor):
    transformer = MacdTransformer(count_live_dead=True)

    def drawer_factor_df_list(self) -> Optional[List[pd.DataFrame]]:
        return None

    def drawer_sub_df_list(self) -> Optional[List[pd.DataFrame]]:
        return [self.factor_df[['diff', 'dea', 'macd']]]

    def drawer_sub_col_chart(self) -> Optional[dict]:
        return {'diff': 'line', 'dea': 'line', 'macd': 'bar'}
Exemplo n.º 10
0
    def __init__(self,
                 region: Region,
                 entity_schema: EntityMixin = Stock,
                 provider: Provider = Provider.Default,
                 entity_provider: Provider = Provider.Default,
                 entity_ids: List[str] = None,
                 exchanges: List[str] = None,
                 codes: List[str] = None,
                 the_timestamp: Union[str, pd.Timestamp] = None,
                 start_timestamp: Union[str, pd.Timestamp] = None,
                 end_timestamp: Union[str, pd.Timestamp] = None,
                 columns: List = [
                     'id', 'entity_id', 'timestamp', 'level', 'open', 'close',
                     'high', 'low'
                 ],
                 filters: List = None,
                 order: object = None,
                 limit: int = None,
                 level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
                 category_field: str = 'entity_id',
                 time_field: str = 'timestamp',
                 computing_window: int = None,
                 keep_all_timestamp: bool = False,
                 fill_method: str = 'ffill',
                 effective_number: int = None,
                 transformer: Transformer = MacdTransformer(),
                 accumulator: Accumulator = None,
                 need_persist: bool = False,
                 dry_run: bool = False,
                 adjust_type: Union[AdjustType, str] = None) -> None:
        self.adjust_type = adjust_type
        self.data_schema = get_kdata_schema(EntityType(
            entity_schema.__name__.lower()),
                                            level=level,
                                            adjust_type=adjust_type)

        if transformer:
            self.indicator_cols = transformer.indicators

        super().__init__(self.data_schema, region, entity_schema, provider,
                         entity_provider, entity_ids, exchanges, codes,
                         the_timestamp, start_timestamp, end_timestamp,
                         columns, filters, order, limit, level, category_field,
                         time_field, computing_window, keep_all_timestamp,
                         fill_method, effective_number, transformer,
                         accumulator, need_persist, dry_run)
Exemplo n.º 11
0
    def __init__(self,
                 entity_schema: Type[EntityMixin] = Stock,
                 provider: str = None,
                 entity_provider: str = None,
                 entity_ids: List[str] = None,
                 exchanges: List[str] = None,
                 codes: List[str] = None,
                 the_timestamp: Union[str, pd.Timestamp] = None,
                 start_timestamp: Union[str, pd.Timestamp] = None,
                 end_timestamp: Union[str, pd.Timestamp] = None,
                 columns: List = None,
                 filters: List = None,
                 order: object = None,
                 limit: int = None,
                 level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
                 category_field: str = 'entity_id',
                 time_field: str = 'timestamp',
                 computing_window: int = None,
                 keep_all_timestamp: bool = False,
                 fill_method: str = 'ffill',
                 effective_number: int = None,
                 transformer: Transformer = MacdTransformer(),
                 accumulator: Accumulator = None,
                 need_persist: bool = False,
                 dry_run: bool = False, factor_name: str = None, clear_state: bool = False,
                 adjust_type: Union[AdjustType, str] = None) -> None:
        if columns is None:
            columns = ['id', 'entity_id', 'timestamp', 'level', 'open', 'close', 'high', 'low']
        self.adjust_type = adjust_type
        self.data_schema = get_kdata_schema(entity_schema.__name__, level=level, adjust_type=adjust_type)

        if transformer:
            self.indicator_cols = transformer.indicators

        if not factor_name:
            if type(level) == str:
                factor_name = f'{type(self).__name__.lower()}_{level}'
            else:
                factor_name = f'{type(self).__name__.lower()}_{level.value}'

        super().__init__(self.data_schema, entity_schema, provider, entity_provider, entity_ids, exchanges, codes,
                         the_timestamp, start_timestamp, end_timestamp, columns, filters, order, limit, level,
                         category_field, time_field, computing_window, keep_all_timestamp, fill_method,
                         effective_number, transformer, accumulator, need_persist, dry_run, factor_name, clear_state)
Exemplo n.º 12
0
    def __init__(self,
                 entity_ids: List[str] = None,
                 entity_type: str = 'stock',
                 exchanges: List[str] = ['sh', 'sz'],
                 codes: List[str] = None,
                 the_timestamp: Union[str, pd.Timestamp] = None,
                 start_timestamp: Union[str, pd.Timestamp] = None,
                 end_timestamp: Union[str, pd.Timestamp] = None,
                 columns: List = None,
                 filters: List = None,
                 order: object = None,
                 limit: int = None,
                 provider: str = 'joinquant',
                 level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
                 category_field: str = 'entity_id',
                 time_field: str = 'timestamp',
                 persist_factor: bool = False, dry_run: bool = False) -> None:
        transformer = MacdTransformer()

        super().__init__(entity_ids, entity_type, exchanges, codes, the_timestamp, start_timestamp, end_timestamp,
                         columns, filters, order, limit, provider, level, category_field, time_field, 26,
                         False, None, None, transformer, None, persist_factor, dry_run)
Exemplo n.º 13
0
    def __init__(self,
                 entity_ids: List[str] = None,
                 entity_type: str = 'stock',
                 exchanges: List[str] = ['sh', 'sz'],
                 codes: List[str] = None,
                 the_timestamp: Union[str, pd.Timestamp] = None,
                 start_timestamp: Union[str, pd.Timestamp] = None,
                 end_timestamp: Union[str, pd.Timestamp] = None,
                 columns: List = None,
                 filters: List = None,
                 order: object = None,
                 limit: int = None,
                 provider: str = 'joinquant',
                 level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
                 category_field: str = 'entity_id',
                 time_field: str = 'timestamp',
                 computing_window: int = None,
                 keep_all_timestamp: bool = False,
                 fill_method: str = 'ffill',
                 effective_number: int = 10,
                 transformer: Transformer = MacdTransformer(),
                 accumulator: Accumulator = None,
                 persist_factor: bool = False,
                 dry_run: bool = True) -> None:
        self.data_schema = get_kdata_schema(entity_type, level=level)

        if transformer:
            self.indicator_cols = transformer.indicator_cols

        if not columns:
            columns = ['id', 'entity_id', 'timestamp', 'level', 'open', 'close', 'high', 'low']

        super().__init__(self.data_schema, entity_ids, entity_type, exchanges, codes, the_timestamp, start_timestamp,
                         end_timestamp, columns, filters, order, limit, provider, level, category_field, time_field,
                         computing_window, keep_all_timestamp, fill_method, effective_number,
                         transformer, accumulator, persist_factor, dry_run)
Exemplo n.º 14
0
        consecutive_count(self.factor_df, 'live', pattern=self.pattern)
        self.result_df = self.factor_df[['score']]


class GoldCrossFactor(LiveOrDeadFactor):
    def do_compute(self):
        super().do_compute()
        s = self.factor_df['live'] == 1
        self.result_df = s.to_frame(name='score')


if __name__ == '__main__':
    factor = TechnicalFactor(codes=['000338', '000778'],
                             start_timestamp='2019-01-01',
                             end_timestamp='2019-06-10',
                             transformer=MacdTransformer(normal=True))

    print(factor.factor_df.tail())

    factor.move_on(to_timestamp='2019-06-17')
    diff = factor.factor_df['diff']
    dea = factor.factor_df['dea']
    macd = factor.factor_df['macd']

    print(factor.factor_df.loc[('stock_sz_000338', )])
    print(factor.factor_df.loc[('stock_sz_000778', )])
# the __all__ is generated
__all__ = [
    'TechnicalFactor', 'BullFactor', 'MacdFactor', 'KeepBullFactor',
    'LiveOrDeadFactor', 'GoldCrossFactor'
]
Exemplo n.º 15
0
        super().__init__(entity_ids, entity_type, exchanges, codes, the_timestamp, start_timestamp, end_timestamp,
                         columns, filters, order, limit, provider, level, category_field, time_field, 26,
                         False, None, None, transformer, None, persist_factor, dry_run)

    def do_compute(self):
        super().do_compute()
        s = (self.factor_df['diff'] > 0) & (self.factor_df['dea'] > 0)
        self.result_df = s.to_frame(name='score')


if __name__ == '__main__':
    factor = TechnicalFactor(entity_type='stock',
                             codes=['000338'],
                             start_timestamp='2019-01-01',
                             end_timestamp='2019-06-10',
                             level=IntervalLevel.LEVEL_1DAY,
                             provider='joinquant',
                             computing_window=26,
                             transformer=MacdTransformer())

    print(factor.get_factor_df().tail())

    factor.move_on(to_timestamp='2019-06-17')
    diff = factor.get_factor_df()['diff']
    dea = factor.get_factor_df()['dea']
    macd = factor.get_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