Ejemplo n.º 1
0
 def _load_data_from_db(self, conn: MongoConnection, db: str,
                        collection: str, time_key: str):
     """
     helper function to load data from database
     :param conn:
     :param db:
     :param collection:
     :param time_key:
     :return:
     """
     if self.start is not None and self.end is not None:
         bar = conn.read_mongo_df(
             db, collection,
             {time_key: {
                 '$gt': self.start,
                 '$lt': self.end
             }})
     elif self.start is not None and self.end is None:
         bar = conn.read_mongo_df(
             db,
             collection,
             {time_key: {
                 '$gt': self.start
             }},
         )
     elif self.start is not None and self.end is None:
         bar = conn.read_mongo_df(db, collection,
                                  {time_key: {
                                      '$lt': self.end
                                  }})
     bar[time_key] = pd.to_datetime(bar[time_key])
     bar.set_index(time_key, inplace=True)
     return bar
Ejemplo n.º 2
0
 def _load_data(self):
     """
     helper function to load the data
     :return:
     """
     self.data = dict()
     if self.backtesting_setting['data_source'] == 'csv':
         time_key = self.backtesting_setting['time_key']
         for symbol, bar_data in self.backtesting_setting['data'].items():
             self.data[symbol] = dict()
             for bar_type, path in bar_data.items():
                 self.data[symbol][bar_type] = self._load_data_from_csv(
                     path, time_key)
         if 'benchmark' in self.backtesting_setting.keys():
             self.benchmark = self._load_data_from_csv(
                 self.backtesting_setting['benchmark'], time_key)
             self.benchmark = self.benchmark['close']
     elif self.backtesting_setting['data_source'] == 'mongo':
         time_key = self.backtesting_setting['time_key']
         host = self.backtesting_setting['host']
         port = self.backtesting_setting['port']
         user = self.backtesting_setting['user']
         password = self.backtesting_setting['password']
         conn = MongoConnection(host, port, user, password)
         for symbol, bar_data in self.backtesting_setting['data'].items():
             self.data[symbol] = dict()
             for bar_type, dbs in bar_data.items():
                 self.data[symbol][bar_type] = self._load_data_from_db(
                     conn, dbs['db'], dbs['collections'], time_key)
         if 'benchmark' in self.backtesting_setting.keys():
             self.benchmark = self._load_data_from_db(
                 self.backtesting_setting['benchmark']['db'],
                 self.backtesting_setting['benchmark']['collections'],
                 time_key)
             self.benchmark = self.benchmark['close']
     self.quote_ctx.set_history_data(self.data)
Ejemplo n.º 3
0
            "HK_FUTURE.999010": {
                "K_1M": {
                    "MA1": {
                        "indicator": "MA",
                        "period": 20
                    },
                    "MA2": {
                        "indicator": "MA",
                        "period": 30,
                        "matype": "MA_Type.SMA",
                        "price_type": "'close'"
                    },
                }
            }
        },
        "traded_code": "HK_FUTURE.999010"
    }

    backtesting = VectorizedBacktesting(
        quote,
        broker,
        strategy,
        strategy_parameter,
        backtesting_setting=backtesting_setting)

    backtesting.run()
    # backtesting.backtesting_result_save_pickle(strategy.strategy_name + '_' + strategy.strategy_version + '_2.pickle')
    conn = MongoConnection('120.55.45.12', 27017, 'root', 'AlphaFactory2020')
    print('save db')
    backtesting.save_backtesting_result_to_db(conn, 'backtesting', 'ljy')
Ejemplo n.º 4
0
 def connect_server(self, ip, port, username, password):
     self.connect = MongoConnection(ip, port, username, password)
     self.manager = AlphaManager(self.connect)
     return self.connect.client.list_database_names()
Ejemplo n.º 5
0
 def save_backtesting_result_to_db(
     self,
     conn: MongoConnection,
     db: str,
     collections: str,
 ):
     d = {
         'strategy_name':
         self.strategy.strategy_name,
         'strategy_version':
         self.strategy.strategy_version,
         'strategy_para':
         json.dumps(self.strategy.strategy_parameters).replace(),
         # because key has '.' mongodb doesn't allow this.
         'strategy_logic':
         inspect.getsource(self.strategy.strategy_logic),
         'backtesting_date':
         datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
         'backtesting_setting':
         json.dumps(self.backtesting_setting),
         # because key has '.' mongodb doesn't allow this.
         'backtesting_result': {
             'first_traded':
             self.backtesting_result['first_traded'],
             'last_traded':
             self.backtesting_result['last_traded'],
             'num_trade':
             self.backtesting_result['num_trade'],
             'time_in_market':
             self.backtesting_result['time_in_market'],
             'win_rate':
             self.backtesting_result['win_rate'],
             'avg_win':
             self.backtesting_result['avg_win'],
             'avg_loss':
             self.backtesting_result['avg_loss'],
             'payoff_ratio':
             self.backtesting_result['payoff_ratio'],
             'cagr':
             self.backtesting_result['cagr'],
             'cumulative_return':
             self.backtesting_result['cumulative_return'],
             'sharpe':
             self.backtesting_result['sharpe'],
             'sortino':
             self.backtesting_result['sortino'],
             'volatility':
             self.backtesting_result['volatility'],
             'skew':
             self.backtesting_result['skew'],
             'Kurtosis':
             self.backtesting_result['Kurtosis'],
             'kelly':
             self.backtesting_result['kelly'],
             'value_at_risk':
             self.backtesting_result['value_at_risk'],
             'max_drawdown_value':
             self.backtesting_result['drawdown_value'].min(),
             'max_drawdown_percent':
             self.backtesting_result['drawdown_percent'].min(),
             'Avg Drawdown Days':
             "{:.2f}".format(
                 self.backtesting_result['drawdown_detail']['days'].mean()),
             'Max Drawdown Days':
             "{:.2f}".format(
                 self.backtesting_result['drawdown_detail']['days'].max()),
         }
     }
     r = conn.insert_from_dict(db, collections, d)
     print('finish insert. Object id {}'.format(r.inserted_id))