def main():
    offline = True
    project_path = os.path.dirname(os.path.abspath(__file__))

    sas_api.config_set('NOSQL_DB_HOST', 'localhost')
    sas_api.config_set('NOSQL_DB_PORT', '27017')
    sas_api.config_set('TS_TOKEN', 'xxxxxxxxxxxx')

    clock = Clock()
    if not sas_api.init(project_path, True):
        print('sas init fail.')
        print('\n'.join(sas_api.error_log()))
        quit(1)

    stock_list = sas_api.get_data_utility().get_stock_identities()
    if len(stock_list) == 0:
        sas_api.get_data_center().update_local_data('Market.SecuritiesInfo')
        sas_api.get_data_utility().refresh_cache()
        stock_list = sas_api.get_data_utility().get_stock_identities()
    if len(stock_list) == 0:
        print('Cannot get stock list.')
        quit(1)

    score_dict = {}
    for stock_identity in stock_list:
        if offline:
            df = sas_api.get_data_center().query('TradeData.Stock.Daily',
                                                 stock_identity,
                                                 (days_ago(60), now()))
        else:
            df = sas_api.get_data_center().query_from_plugin(
                'TradeData.Stock.Daily', stock_identity, (days_ago(60), now()))

        if df is None or len(df) == 0:
            print('No data for %s' % stock_identity)
            continue

        # Only keep 2 rows
        df = df[['trade_date', 'close', 'vol']]
        calc_stock_trade_score(df, 'close', 'vol')
        score_dict[stock_identity] = df['score'].sum()

    print('Analysis finished, time spending: %.2fs' % clock.elapsed())
    result = sorted(score_dict.items(), key=lambda x: x[1], reverse=True)
    print(result)

    print('Process Quit.')
def main():
    project_path = os.path.dirname(os.path.abspath(__file__))

    sas_api.config_set('NOSQL_DB_HOST', 'localhost')
    sas_api.config_set('NOSQL_DB_PORT', '27017')
    sas_api.config_set('TS_TOKEN', 'xxxxxxxxxxxx')

    if not sas_api.init(project_path, True):
        print('sas init fail.')
        print('\n'.join(sas_api.error_log()))
        quit(1)

    stock_identity = '000001.SZSE'
    period = (years_ago(5), now())

    df = sas_api.get_data_center().query('TradeData.Stock.Daily',
                                         stock_identity, period)
    if df is None or df.empty:
        print('No data.')
        exit(2)
    df.set_index('trade_date', drop=True, inplace=True)
    df['openinterest'] = 0
    df['volume'] = df['vol']

    cerebro = bt.Cerebro()

    data = bt.feeds.PandasData(dataname=df,
                               fromdate=period[0],
                               todate=period[1])
    cerebro.adddata(data)

    cerebro.addsizer(bt.sizers.PercentSizer, percents=10)
    cerebro.broker.set_cash(100000)
    cerebro.addstrategy(StrategyGrid)

    results = cerebro.run()
    print(results)

    cerebro.plot()
        else:

            # Already in the market ... we might sell
            if len(self) >= (self.bar_executed + 5):
                # SELL, SELL, SELL!!! (with all possible default parameters)
                self.log('SELL CREATE, %.2f' % self.dataclose[0])

                # Keep track of the created order to avoid a 2nd order
                self.order = self.sell()


if __name__ == '__main__':
    project_path = os.path.dirname(os.path.abspath(__file__))

    sas_api.config_set('NOSQL_DB_HOST', 'localhost')
    sas_api.config_set('NOSQL_DB_PORT', '27017')
    sas_api.config_set('TS_TOKEN', 'xxxxxxxxxxxx')

    if not sas_api.init(project_path, True):
        print('sas init fail.')
        print('\n'.join(sas_api.error_log()))
        quit(1)

    stock_identity = '000001.SZSE'
    period = (years_ago(5), now())

    df = sas_api.get_data_center().query('TradeData.Stock.Daily',
                                         stock_identity, period)
    if df is None or df.empty:
        print('No data.')