Ejemplo n.º 1
0
for idx in range(0, len(y_test)):
    current_price = y_test[idx]  # 重要
    buy_sell_num_flag = [
        1.0, 0.0, abs(buy_sell_count)
    ] if buy_sell_count >= 1 else [0.0, 1.0, abs(buy_sell_count)]
    state_data = np.array(X_test[idx] + buy_sell_num_flag, dtype='f')
    #state_data = state_data.reshape(state_data.shape[0], 1)
    action = agent.act(state_data)  # 教師が入力に入らないように。
    tradecl.update_trading_view(current_price, action)
    buy_sell_count, pass_count, money, ethereum, total_money = \
        action_if(action, buy_sell_count, pass_count, money, ethereum, total_money, current_price)
    # before_money = total_money

print("pass_count:" + str(pass_count))
print("buy_sell_count(at the end of TEST):" + str(buy_sell_count))
print("buy_sell_fee:" + str(buy_sell_fee))

print("====================TEST======================")
print("Initial MONEY:" + str(first_total_money))
print("Final Total MONEY:" + str(total_money))
print("TESTにおいて、Passは" + str(pass_count) + "回")
print("TESTにおいて、buy_sell_countは" + str(buy_sell_count) + "回" +
      "(買いの回数が多い)" if buy_sell_count > 0 else "(売りの回数が多い)")

try:
    tradecl.draw_trading_view()
except:
    pass

print(os.path.basename(__file__))
class FxEnv(gym.Env):
    def __init__(self):
        self.price_idx = 0
        self.trade = TradeClass()
        training_set = self.trade.read_bitflyer_json()
        print("price_data idx 0-10" + str(training_set[0:10]))
        print("price_data idx last 10" + str(training_set[-1]))

        self.input_len = 400
        self.n_actions = 3
        self.obs_size = self.input_len + self.n_actions  #env.observation_space.shape[0]

        self.X_train = []
        self.y_train = []
        for i in range(self.input_len, len(training_set) - 1001):
            # X_train.append(np.flipud(training_set_scaled[i-60:i]))
            self.X_train.append(training_set[i - self.input_len:i])
            self.y_train.append(training_set[i])

        self.price = self.y_train
        self.money = 300
        self.before_money = copy.deepcopy(self.money)
        self.cripto = 0.1
        self.total_money = self.money + np.float64(self.price[0] * self.cripto)
        self.first_total_money = self.total_money
        self.pass_count = 0
        self.buy_sell_count = 0  # buy+ sell -
        self.pass_renzoku_count = 0
        self.buy_sell_fee = 0.00001

    def _reset(self):
        self.price_idx = 0
        return self.X_train[self.price_idx]

    def _seed(self, seed=None):
        pass

    def buy_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = money * 0.1
        money -= spend * (1 + self.buy_sell_fee)
        if money <= 0.0:
            return first_money, first_cripto, first_total_money

        cripto += float(spend / current_price)
        total_money = money + cripto * current_price

        return money, cripto, total_money

    def sell_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = cripto * 0.1
        cripto -= spend * (1 + self.buy_sell_fee)
        if cripto <= 0.0:
            return first_money, first_cripto, first_total_money

        money += float(spend * current_price)
        total_money = money + float(cripto * current_price)

        return money, cripto, total_money

    def pass_simple(self, money, cripto, total_money, current_price):
        total_money = money + float(cripto * current_price)
        return money, cripto, total_money

    def _step(self, action):
        self.price_idx += 1

        current_price = self.X_train[self.price_idx][-1]
        buy_sell_num_array = [
            1.0, 0.0, self.buy_sell_count
        ] if self.buy_sell_count >= 1 else [0.0, 1.0, self.buy_sell_count]

        self.trade.update_trading_view(current_price, action)

        pass_reward = 0
        if action == 0:
            print("buy")
            self.buy_sell_count += 1
            self.money, self.cripto, self.total_money = self.buy_simple(
                self.money, self.cripto, self.total_money, current_price)
        elif action == 1:
            print("sell")
            self.buy_sell_count -= 1
            self.money, self.cripto, self.total_money = self.sell_simple(
                self.money, self.cripto, self.total_money, current_price)
        else:
            print("PASS")
            self.money, self.cripto, self.total_money = self.pass_simple(
                self.money, self.cripto, self.total_money, current_price)
            pass_reward = 0.0  # -0.001)#0.01 is default
            self.pass_count += 1

        reward = self.total_money - self.before_money + pass_reward
        if self.buy_sell_count >= 5 and action == 0:
            print("buy_sell" + str(self.buy_sell_count) + "回 action==" +
                  str(action))
            reward -= (float(abs(self.buy_sell_count)**2))
            print(reward)
        elif self.buy_sell_count <= -5 and action == 1:
            print("buy_sell" + str(self.buy_sell_count) + "回 action==" +
                  str(action))
            reward -= (float(abs(self.buy_sell_count)**2))
            print(reward)
        else:
            # reward 1.0がちょうど良い!
            reward += 1.1

        self.before_money = copy.deepcopy(self.total_money)

        if self.price_idx % 2000 == 1000:
            print("last action:" + str(action))
            print("TOTAL MONEY" + str(self.total_money))
            print("100回中passは" + str(self.pass_count) + "回")
            # print("100回中buy_sell_countは" + str(self.buy_sell_count) + "回")
            self.pass_count = 0
            self.trade.draw_trading_view()

        current_asset = [self.cripto, self.money]

        # obs, reward, done, infoを返す
        return [
            self.X_train[self.price_idx], buy_sell_num_array, current_asset
        ], reward, False, None
Ejemplo n.º 3
0
class FxEnv(gym.Env):
    def __init__(self):
        self.price_idx = 0
        self.trade = TradeClass()
        training_set = self.trade.read_bitflyer_json()
        print("price_data idx 0-10" + str(training_set[0:10]))
        print("price_data idx last 10" + str(training_set[-1]))
        self.length_data = len(training_set)

        self.input_len = 400
        self.n_actions = 3
        self.asset_info_len = 2
        self.buy_sell_count_len = 4
        #TODO
        self.observe_size = self.input_len + self.buy_sell_count_len  #+self.asset_info_len  #env.observation_space.shape[0]

        self.data = []
        self.y_train = []
        for i in range(self.input_len, self.length_data - 1001):
            # data.append(np.flipud(training_set_scaled[i-60:i]))
            self.data.append(training_set[i - self.input_len:i])
            self.y_train.append(training_set[i])

        self.price = self.y_train
        self.money = 500
        self.before_money = copy.deepcopy(self.money)
        self.cripto = 0.00001
        self.total_money = self.money + np.float64(self.price[0] * self.cripto)
        self.first_total_money = self.total_money
        self.pass_count = 0
        self.buy_sell_count = 0  # buy+ sell -
        self.pass_renzoku_count = 0
        self.buy_sell_fee = 0.00001
        self.current_asset = [self.cripto, self.money]
        self.action_space = gym.spaces.Discrete(3)  # 東西南北
        self.MAP = np.array([0 for idx in range(0, self.observe_size)])
        self.inventory = []
        self.observation_space = gym.spaces.Box(low=0,
                                                high=3,
                                                shape=self.MAP.shape)
        self.begin_total_money = self.money + self.cripto * self.price[0]
        print("LENGTH OF LOOP NUM:" + str(len(self.data)))
        self.buy_inventory = []
        self.sell_inventory = []
        self.total_profit = 0

    def _reset(self):
        self.price_idx = 0
        return self.data[self.price_idx] + [0, 0, 0, 0
                                            ]  #TODO: +self.current_asset

    def _seed(self, seed=None):
        return self.length_data

    def return_lenghth_steps(self):
        return self.length_data

    def _render(self, mode='', close=False):
        #画面への表示 主にGUI
        pass

    def buy_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = money * 0.1
        money -= spend * (1 + self.buy_sell_fee)
        if money <= 0.0:
            return first_money, first_cripto, first_total_money

        cripto += float(spend / current_price)
        total_money = money + cripto * current_price

        return money, cripto, total_money

    def sell_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = cripto * 0.1
        cripto -= spend * (1 + self.buy_sell_fee)
        if cripto <= 0.0:
            return first_money, first_cripto, first_total_money

        money += float(spend * current_price)
        total_money = money + float(cripto * current_price)

        return money, cripto, total_money

    def pass_simple(self, money, cripto, total_money, current_price):
        total_money = money + float(cripto * current_price)
        return money, cripto, total_money

    def buy_lot(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = current_price * 0.001
        money -= spend * (1 + self.buy_sell_fee)
        EMPTY_MONEY_FLAG = False
        if money <= 0.0:
            EMPTY_MONEY_FLAG = True
            return first_money, first_cripto, first_total_money, EMPTY_MONEY_FLAG

        cripto += float(spend / current_price)
        total_money = money + cripto * current_price

        return money, cripto, total_money, EMPTY_MONEY_FLAG

    def sell_lot(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = cripto * 0.001
        cripto -= spend * (1 + self.buy_sell_fee)
        EMPTY_MONEY_FLAG = False
        if cripto <= 0.0:
            EMPTY_MONEY_FLAG = True
            return first_money, first_cripto, first_total_money, EMPTY_MONEY_FLAG

        money += float(spend * current_price)
        total_money = money + float(cripto * current_price)

        return money, cripto, total_money, EMPTY_MONEY_FLAG

    def _step(self, action):

        if type(action) is list or type(action) is np.ndarray:
            action = action.tolist()
            action = action.index(max(action))
        else:
            pass
        self.price_idx += 1

        current_price = self.data[self.price_idx][-1]

        self.trade.update_trading_view(current_price, action)

        len_buy = len(self.buy_inventory)
        len_sell = len(self.sell_inventory)
        if len_buy > 40:
            buy_flag = 1
            sell_flag = 0
        elif len_sell > 40:
            buy_flag = 0
            sell_flag = 1
        else:
            buy_flag = 0
            sell_flag = 0

        buy_sell_array = [len_buy, len_sell, buy_flag, sell_flag]

        #TODO idx + 1じゃなくて良いか? バグの可能性あり。=>修正済み
        #next_state = getStateFromCsvData(self.data, self.price_idx+1, window_size)
        reward = 0
        if action == 1 and len(self.sell_inventory) > 0:  # sell
            i = 0
            for i in range(0, int(len(self.sell_inventory) / 10)):
                sold_price = self.sell_inventory.pop(0)
                profit = sold_price - current_price
                reward = profit  #max(profit, 0)
                self.total_profit += profit
                #print("Buy(空売りの決済): " + str(current_price) + " | Profit: " + str(profit))
            reward = reward / (i + 1)
        elif action == 1 and len(self.buy_inventory) < 50:  # buy
            self.buy_inventory.append(current_price)
            #print("Buy: " + str(current_price))
        elif action == 2 and len(self.buy_inventory) > 0:  # sell
            i = 0
            for i in range(0, int(len(self.buy_inventory) / 10)):
                bought_price = self.buy_inventory.pop(0)
                profit = current_price - bought_price
                reward = profit  # max(profit, 0)
                self.total_profit += profit
                #print("Sell: " + str(current_price) + " | Profit: " + formatPrice(profit))
            reward = reward / (i + 1)
        elif action == 2 and len(self.sell_inventory) < 50:
            self.sell_inventory.append(current_price)
            #print("Sell(空売り): " + formatPrice(current_price))

        #print("Reward: "+str(reward))
        print("inventory(sell) : " + str(len(self.sell_inventory)) +
              "       inventory(buy)  : " + str(len(self.buy_inventory)))
        print("TOTAL PROFIT:  " + str(self.total_profit))
        if False:  #self.price_idx % 10000 == 1000:
            try:
                print("TOTAL PROFIT:  " + str(self.total_profit))
                print("inventory(sell) : " + str(len(self.sell_inventory)) +
                      "       inventory(buy)  : " +
                      str(len(self.buy_inventory)))
                self.trade.draw_trading_view()
            except:
                pass
        done = True if self.price_idx == self.length_data - 1 else False

        # obs, reward, done, infoを返す
        return self.data[self.price_idx] + buy_sell_array, reward, done, {}
            money, ethereum, total_money = pass_simple(money, ethereum,
                                                       total_money,
                                                       current_price)
            reward += 0.00000
            pass_count += 1

        before_money = total_money

        if idx % 10000 == 500:
            print("BEGGINING MONEY:" + str(first_total_money))
            print("FINAL MONEY:" + str(total_money))
            print("1000回中passは" + str(pass_count) + "回")
            print("1000回終わった後のbuy_sell_countは" + str(buy_sell_count) + "回")
            print("total_reward:" + str(total_reward))
            pass_count = 0
            trade.draw_trading_view()
            agent.save('chainerRLAgent')

    #agent.stop_episode_and_train(X_train[-1], reward, True)

    # Save an agent to the 'agent' directory
    agent.save('chainerRLAgent')
    print("START MONEY" + str(first_total_money))

#テスト
'''
for i in range(0,1):
    reward=0

    price = y_train
    money = 300
Ejemplo n.º 5
0
class FxEnv(gym.Env):
    def __init__(self):
        self.price_idx = 0
        self.trade = TradeClass()
        training_set = self.trade.read_bitflyer_json()
        print("price_data idx 0-10" + str(training_set[0:10]))
        print("price_data idx last 10" + str(training_set[-1]))

        self.input_len = 400
        self.n_actions = 3
        self.asset_info_len = 2
        self.buy_sell_count_len = 4
        self.observe_size = self.input_len + self.buy_sell_count_len + self.asset_info_len  # env.observation_space.shape[0]

        self.X_train = []
        self.y_train = []
        for i in range(self.input_len, len(training_set) - 1001):
            # X_train.append(np.flipud(training_set_scaled[i-60:i]))
            self.X_train.append(training_set[i - self.input_len:i])
            self.y_train.append(training_set[i])

        self.price = self.y_train
        self.money = 500
        self.before_money = copy.deepcopy(self.money)
        self.cripto = 0.00001
        self.total_money = self.money + np.float64(self.price[0] * self.cripto)
        self.first_total_money = self.total_money
        self.pass_count = 0
        self.buy_sell_count = 0  # buy+ sell -
        self.pass_renzoku_count = 0
        self.buy_sell_fee = 0.00001
        self.current_asset = [self.cripto, self.money]
        self.action_space = gym.spaces.Discrete(3)  # 東西南北
        self.MAP = np.array([0 for idx in range(0, self.observe_size)])
        self.inventory = []
        self.observation_space = gym.spaces.Box(
            low=0,
            high=3,
            shape=self.MAP.shape
        )
        self.begin_total_money = self.y_train[0]
        print("LENGTH OF LOOP NUM:" + str(len(self.X_train)))

    def _reset(self):
        self.price_idx = 0
        return self.X_train[self.price_idx] + [0, 0, 0, 0] + self.current_asset

    def _seed(self, seed=None):
        pass

    def _render(self, mode='', close=False):
        # 画面への表示 主にGUI
        pass

    def buy_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = money * 0.1
        money -= spend * (1 + self.buy_sell_fee)
        if money <= 0.0:
            return first_money, first_cripto, first_total_money

        cripto += float(spend / current_price)
        total_money = money + cripto * current_price

        return money, cripto, total_money

    def sell_simple(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = cripto * 0.1
        cripto -= spend * (1 + self.buy_sell_fee)
        if cripto <= 0.0:
            return first_money, first_cripto, first_total_money

        money += float(spend * current_price)
        total_money = money + float(cripto * current_price)

        return money, cripto, total_money

    def pass_simple(self, money, cripto, total_money, current_price):
        total_money = money + float(cripto * current_price)
        return money, cripto, total_money

    def buy_lot(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = current_price * 0.001
        money -= spend * (1 + self.buy_sell_fee)
        EMPTY_MONEY_FLAG = False
        if money <= 0.0:
            EMPTY_MONEY_FLAG = True
            return first_money, first_cripto, first_total_money, EMPTY_MONEY_FLAG

        cripto += float(spend / current_price)
        total_money = money + cripto * current_price

        return money, cripto, total_money, EMPTY_MONEY_FLAG

    def sell_lot(self, money, cripto, total_money, current_price):
        first_money, first_cripto, first_total_money = money, cripto, total_money
        spend = cripto * 0.001
        cripto -= spend * (1 + self.buy_sell_fee)
        EMPTY_MONEY_FLAG = False
        if cripto <= 0.0:
            EMPTY_MONEY_FLAG = True
            return first_money, first_cripto, first_total_money, EMPTY_MONEY_FLAG

        money += float(spend * current_price)
        total_money = money + float(cripto * current_price)

        return money, cripto, total_money, EMPTY_MONEY_FLAG

    def _step(self, action):

        if type(action) is list or type(action) is np.ndarray:
            action = action.tolist()
            action = action.index(max(action))
        else:
            pass
        self.price_idx += 1

        current_price = self.X_train[self.price_idx][-1]

        if abs(self.buy_sell_count) >= 10:
            between_range = 0.0
        else:
            between_range = 1.0

        buy_sell_num_array = [1.0, 0.0, abs(self.buy_sell_count), between_range] if self.buy_sell_count >= 0 else [0.0,
                                                                                                                   1.0,
                                                                                                                   abs(
                                                                                                                       self.buy_sell_count),
                                                                                                                   between_range]

        self.trade.update_trading_view(current_price, action)

        reward = 0

        if action == 0:
            print("buy")
            self.buy_sell_count += 1
            self.money, self.cripto, self.total_money = self.buy_simple(self.money, self.cripto, self.total_money,
                                                                        current_price)
            if self.buy_sell_count < 0:
                reward += 0.01
            else:
                reward -= 0.01
        elif action == 1 and len(self.inventory) > 0:
            print("sell")
            self.buy_sell_count -= 1
            self.money, self.cripto, self.total_money = self.sell_simple(self.money, self.cripto, self.total_money,
                                                                         current_price)
            if self.buy_sell_count > 0:
                reward += 0.01
            else:
                reward -= 0.01
        else:
            print("PASS")
            self.money, self.cripto, self.total_money = self.pass_simple(self.money, self.cripto, self.total_money,
                                                                         current_price)
            reward += 0.000000  # -0.001)#0.01 is default
            self.pass_count += 1

        reward += 0.01 * (self.total_money - self.before_money)
        print("buy_sell" + str(self.buy_sell_count) + "回 action==" + str(action))

        self.before_money = self.total_money

        if False:  # self.price_idx % 50000 == 1000:
            print("last action:" + str(action))
            print("TOTAL MONEY" + str(self.total_money))
            print("100回中passは" + str(self.pass_count) + "回")
            # print("100回中buy_sell_countは" + str(self.buy_sell_count) + "回")
            self.pass_count = 0
            try:
                self.trade.draw_trading_view()
            except:
                pass

        print("begin  MONEY: " + str(self.begin_total_money))
        print("current MONEY: " + str(self.total_money))
        print("price_IDX: " + str(self.price_idx))
        print("Reward: " + str(reward))
        self.current_asset = [self.cripto, self.money]

        # obs, reward, done, infoを返す
        return self.X_train[self.price_idx] + buy_sell_num_array + self.current_asset, reward, False, {}
Ejemplo n.º 6
0
    pred = pred_array[idx][0]

    if pred - before_pred > 0.00005:
        action = 0
    elif pred - before_pred < -0.00005:
        action = 1
    else:
        action = 2

    tradecl.update_trading_view(current_price, action)
    buy_sell_count, pass_count, money, cripto, total_money = \
            action_if(action,buy_sell_count,pass_count,money,cripto,total_money,current_price)
    before_money = total_money
    before_price = current_price
    before_pred = pred
    before_action = action

print("====================TEST======================")
print("START MONEY" + str(first_total_money))
print("FINAL MONEY:" + str(total_money))
print("pass_count:" + str(pass_count))
print("buy_sell_count(at the end of TEST):" + str(buy_sell_count))

#matploblibでトレードの結果をグラフで可視化
try:
    tradecl.draw_trading_view("main_price_prediction.png")
except:
    print(traceback.format_exc())
    print("tradecl.draw_trading_view FAILED!!")