AaronAnima / sharpe

sharpe is a unified, interactive, general-purpose environment for backtesting or applying machine learning(supervised learning and reinforcement learning) in the context of quantitative trading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CI status

sharpe

sharpe is a unified, interactive, general-purpose environment for backtesting or applying machine learning(supervised learning and reinforcement learning) in the context of quantitative trading.

it's designed to allow maximum flexibility and simplicity in the practical research process: raw financial data -> feature engineering -> model training -> trading strategy -> backtesting, come in the reasonable level of abstraction.

core features:

  • unified: unify the supervised learning and reinforcement learning in a framework.
  • interactive: state(feature) -> action(portfolio-weight) -> reward(profit-and-loss/returns) bar-by-bar, allow maximum workflow control.
  • general-purpose: market-independent, instrument-independent, trading-frequency-independent.
sharpe is considered in early alpha release. Stuff might break.

outline

1 Additonal Features

  • Support rule-based and factor-based trading strategy backtesting
  • Helper functions for data/order management and rl algorithms.
  • Various environment wrappers(e.g. data type wrapper, support pandas, numpy, pytorch tensor)
  • Logging, visualization, and experiments management
  • Unit tested, continuously integrated

2 Install

$ git clone https://github.com/StateOfTheArt-quant/sharpe
$ cd sharpe
$ python setup.py install

3 Quick Start

The following snippet showcases the whole workflow of trading strategy development in sharpe.

 from sharpe.utils.mock_data import create_toy_feature
 from sharpe.data.data_source import DataSource
 from sharpe.environment import TradingEnv
 from sharpe.mod.sys_account.api.api import order_target_portfolio
 import random
 random.seed(111)

 feature_df, price_s = create_toy_feature(order_book_ids_number=2, feature_number=3, start="2020-01-01", end="2020-01-11", random_seed=111)
 data_source = DataSource(feature_df=feature_df, price_s=price_s)

 env= TradingEnv(data_source=data_source, look_backward_window=4, mode="rl")
 print('----------------------------------------------------------------------')

 company_id = "000001.XSHE"


 def your_strategy(state):
      """
      here is a random strategy, only trade the first stock with a random target percent
      """

      target_percent_of_postion =  round(random.random(),2)
      target_pososition_dict = {company_id : target_percent_of_postion}
      print("the target portfolio is to be: {}".format(target_pososition_dict))
      # call trade API
      action = order_target_portfolio(target_pososition_dict)
      return action


 state = env.reset()

 while True:
      print("the current trading_dt is: {}".format(env.trading_dt))
      action = your_strategy(state)

      next_state, reward, done, info = env.step(action)
      print("the reward of this action: {}".format(reward))
      print("the next state is \n {}".format(next_state))
      if done:
           break
      else:
           state = next_state
env.render()

assets/images/unit_net_value.png

4 Documentation

5 Examples

6 Communication & Contributing

Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub

7 Acknowledgements

sharpe derived from our initial project trading_gym, which now is a event-driven(or observer) design pattern, the code highly inspired by RQALPHA

This library is named sharpe to respect William F. Sharpe

About

sharpe is a unified, interactive, general-purpose environment for backtesting or applying machine learning(supervised learning and reinforcement learning) in the context of quantitative trading


Languages

Language:Python 100.0%