Exemple #1
0
def main(argv):
    from quotation.quotation import Quotation
    q = Quotation()
    d = q.get_daily_data('000001')
    d = d.reset_index()
    # d = pd.read_csv('/Users/zhangyunsheng/Dev/sonata/data/AAPL.csv')
    d = d.sort_values('date')
    e = StockTradingEnv(d)
    e.reset()
    print(e._nextObservation())
    e.render()
Exemple #2
0
 def predict(self, code):
     if self.model == None:
         self.load()
     q = Quotation()
     d = q.get_daily_data(code)
     d = d.reset_index()
     n = len(d.loc[:, 'open'].values)
     last_five_d = d.loc[n - 6:, :]
     obs = StockTradingEnv.observation(last_five_d)
     action, _states = self.model.predict(obs)
     print(action)
     print(_states)
     return
Exemple #3
0
 def load(self):
     try:
         q = Quotation()
         d = q.get_daily_data('000001')
         d = d.reset_index()
         # d = pd.read_csv('/Users/zhangyunsheng/Dev/sonata/data/AAPL.csv')
         d = d.sort_values('date')
         env = DummyVecEnv([lambda: StockTradingEnv(d)])
         self.model = PPO2(MlpPolicy, env, verbose=1)
         self.model = PPO2.load(self.model_dir)
     except Exception as e:
         Logger.warn(e)
         return False
     return True
Exemple #4
0
    def train(self):
        q = Quotation()
        d = q.get_daily_data('000001')
        #d = q.get_daily_data('000651')
        d = d.reset_index()
        # d = pd.read_csv('/Users/zhangyunsheng/Dev/sonata/data/AAPL.csv')
        d = d.sort_values('date')

        #print(d.head(10).loc[0: 6, 'open'].values)
        #return True
        train_data = d.head(3718)
        test_data = d.tail(1500).reset_index()

        # The algorithms require a vectorized environment to run
        #env = DummyVecEnv([lambda: StockTradingEnv(d)])
        #env = StockTradingEnv(d)
        env = StockTradingEnv(train_data)

        m = PPO2(MlpPolicy, env, verbose=1)
        m.learn(total_timesteps=200000)
        m.save(self.model_dir)

        env = StockTradingEnv(test_data)
        m = PPO2.load(self.model_dir)
        #m = PPO2.load('/Users/zhangyunsheng/Dev/sonata/data/model/demo.2M.zip')
        obs = env.reset()
        for i in range(1500):
            action, _states = m.predict(obs)
            #print(action)  # TODO
            act = ''
            #if action[0][0] < 1:
            #    if action[0][1] != 0:
            if action[0] < 1:
                if action[1] != 0:
                    act = 'BUY'
                    print('BUY')  # TODO
            #elif action[0][0] < 2:
            #    if action[0][1] != 0:
            elif action[0] < 2:
                if action[1] != 0:
                    act = 'SELL'
                    print('SELL')  # TODO
            obs, rewards, done, info = env.step(action)
            if act != '':
                env.render()
        env.render()
        return True
    #strats = cerebo.optstrategy(
    #    TestStrategy,
    #    maperiod=range(10, 31))

    # modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    # datapath = os.path.join(modpath, 'orcl-1995-2014.txt')
    # data = bt.feeds.YahooFinanceCSVData(
    #     dataname=datapath,
    #     fromdate=datetime.datetime(2000, 1, 1),
    #     todate=datetime.datetime(2000, 12, 31),
    #     reverse=False
    # )

    from quotation.quotation import Quotation
    q = Quotation()
    d = q.get_daily_data('000001')
    print(d)

    # import pandas
    # #d = pandas.read_csv(datapath, skiprows=0, parse_dates=True,  header=0, index_col=0)
    # datapath = "/Users/zhangyunsheng/Dev/sonata/data/daily/000001"
    # d = pandas.read_csv(datapath, sep='\t', skiprows=0, parse_dates=True,  header=0, index_col=0)
    # print(d)
    # exit()

    #data = bt.feeds.PandasData(
    #    dataname=d,
    #    fromdate=datetime.datetime(2001, 1, 1),
    #    todate=datetime.datetime(2001, 12, 31),
    #    )
    data = MyPandasData(