<jupyter_start><jupyter_text><jupyter_code># Google ドライブをマウントするには、このセルを実行してください。
from google.colab import drive
drive.mount('/content/drive')

%cd drive/My Drive
%tensorflow_version 2.x
import tensorflow as tf
%cd /content/drive/My Drive

try:
  import imp
  imp.reload(dqn)
except:
  import dqn

agent = dqn.Agent(spread=10, pip_cost=1000, leverage=500, min_lots=0.01, assets=10000, available_assets_rate=0.4,
                 restore=not True, step_size=96, n=4, lr=1e-3)
%cd /content
agent.run()
import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

# agent.test(spread=10, pip_cost=1000, los_cut=150,test_data=not True)
for _ in range(1):
  tree_idx, replay = agent.memory.sample(128)

  states = np.array([a[0][0] for a in replay], np.float32)
  new_states = np.array([a[0][3] for a in replay], np.float32)
  actions = np.array([a[0][1] for a in replay])
  rewards = np.array([a[0][2] for a in replay], np.float32).reshape((-1, 1))

  with tf.GradientTape() as tape:
    q = agent.model(states)
    target_q = agent.target_model(new_states).numpy()
    arg_q = agent.model(new_states).numpy()
    random = np.random.rand(actions.shape[0])
    arg_q = np.argmax(arg_q, 1)
    # arg_q = np.array([np.argmax(arg_q[i]) if random[i] > 0.1 else np.random.randint(arg_q.shape[1]) for i in
    #                   range(arg_q.shape[0])])

    q_backup = q.numpy()

    for i in range(len(rewards)):
        # q_backup[i, actions[i]] = rewards[i] if I < 1010 and not self.restore else rewards[i] + 0.2 * target_q[i, np.argmax(arg_q[i])]
        q_backup[i, actions[i]] = rewards[i] + 0.1 * target_q[i, arg_q[i]]
    mse = tf.reduce_mean(tf.reduce_sum(tf.abs(q_backup - q) ** 1.5, -1))

  ae = np.array([sum(i) for i in np.abs(q_backup - q.numpy())])
  agent.memory.batch_update(tree_idx, ae)

  gradients = tape.gradient(mse, agent.model.trainable_variables)
  # gradients = [(tf.clip_by_value(grad, -10.0, 10.0))
  #                                   for grad in gradients]
  # agent.model.optimizer.apply_gradients(zip(gradients,agent.model.trainable_variables))
  # print(np.mean(ae))
  # print(q[0:5])
  print(mse)
print(q)
# print(np.mean(rewards)))
rewards
!pip install ta

import numpy as np
import pandas as pd
import ta
from sklearn.preprocessing import MinMaxScaler
def gen_data(file_path="gbpjpy15.csv"):
    try:
        print("load file")
        df = pd.read_csv(file_path)
    except:
        print("Use 'python gen_data.py 'file_path''")
        return

    df["Close1"] = df["Close"] * 100
    ma = np.array(ta.trend.ema(df["Close1"], 7) - ta.trend.ema(df["Close1"], 4)).reshape((-1,1))
    ma2 = np.array(df["Close1"] - ta.trend.ema(df["Close1"], 7)).reshape((-1,1))
    b1 = np.array(ta.volatility.bollinger_hband(df["Close1"]) - df["Close1"]).reshape((-1,1))
    b2 = np.array(ta.volatility.bollinger_lband(df["Close1"]) - df["Close1"]).reshape((-1,1))
    macd = np.array(ta.trend.macd_diff(df["Close1"])).reshape((-1,1))
    rsi = np.array(ta.momentum.rsi(df["Close"]) - ta.momentum.rsi(df["Close"], 7)).reshape((-1,1))
    stoch = np.array(ta.momentum.stoch_signal(df["High"], df["Low"], df["Close"]) - ta.momentum.stoch(df["High"], df["Low"], df["Close"])).reshape((-1,1))

    x = np.concatenate([ma, macd, rsi, stoch], -1)

    y = np.array(df[["Open"]])
    atr = np.array(ta.volatility.average_true_range(df["High"], df["Low"], df["Close"]))
    high = np.array(df[["High"]])
    low = np.array(df[["Low"]])

    print("gen time series data")
    gen = tf.keras.preprocessing.sequence.TimeseriesGenerator(x, y, 30)
    x = []
    y = []
    for i in gen:
        x.extend(i[0].tolist())
        y.extend(i[1].tolist())
    x = np.asanyarray(x)[100:]
    y = np.asanyarray(y)[100:]
    atr = atr[-len(y):].reshape((-1, 1))
    scale_atr = atr
    high = high[-len(y):].reshape((-1, 1))
    low = low[-len(y):].reshape((-1, 1))

    np.save("x", x)
    np.save("target", np.array([y, atr, scale_atr, high, low]))

    print("done\n")

%cd /content/drive/My Drive
gen_data()
def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()
a1 = np.argmax(q,1)
q1 = np.abs(q) / np.sum(np.abs(q),1).reshape((-1,1)) * (np.abs(q) / q)
q1 += .05 * np.random.randn(q1.shape[0], q1.shape[1])
a2 = np.argmax(q1,-1)
np.mean(a1 == a2)
tree_idx, replay = agent.memory.sample(128)
3 in [1,2]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup