This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| def Write_to_file(Date, net_worth, filename='{}.txt'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))): | |
| for i in net_worth: | |
| Date += " {}".format(i) | |
| #print(Date) | |
| if not os.path.exists('logs'): | |
| os.makedirs('logs') | |
| file = open("logs/"+filename, 'a+') | |
| file.write(Date+"\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomEnv: | |
| def __init__(self, Render_range = 100): | |
| self.Render_range = Render_range # render range in visualization | |
| def reset(self): | |
| self.visualization = TradingGraph(Render_range=self.Render_range) # init visualization | |
| self.trades = deque(maxlen=self.Render_range) # limited orders memory for visualization | |
| def step(self, action): | |
| Date = self.df.loc[self.current_step, 'Date'] # for visualization |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from collections import deque | |
| import matplotlib.pyplot as plt | |
| from mplfinance.original_flavor import candlestick_ohlc | |
| import matplotlib.dates as mpl_dates | |
| from datetime import datetime | |
| import os | |
| import cv2 | |
| import numpy as np |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TradingGraph: | |
| def __init__(self, Render_range): | |
| ... | |
| # Render the environment to the screen | |
| def render(self, Date, Open, High, Low, Close, Volume, net_worth, trades): | |
| # before appending to deque list, need to convert Date to special format | |
| Date = mpl_dates.date2num([pd.to_datetime(Date)])[0] | |
| self.render_data.append([Date, Open, High, Low, Close]) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TradingGraph: | |
| def __init__(self, Render_range): | |
| ... | |
| # Render the environment to the screen | |
| def render(self, Date, Open, High, Low, Close, Volume, net_worth, trades): | |
| # append volume and net_worth to deque list | |
| self.Volume.append(Volume) | |
| self.net_worth.append(net_worth) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TradingGraph: | |
| def __init__(self, Render_range): | |
| ... | |
| # Render the environment to the screen | |
| def render(self, Date, Open, High, Low, Close, Volume, net_worth, trades): | |
| # append volume and net_worth to deque list | |
| self.Volume.append(Volume) | |
| self.net_worth.append(net_worth) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.models import Model | |
| from tensorflow.keras.layers import Input, Dense, Flatten | |
| from tensorflow.keras import backend as K | |
| #tf.config.experimental_run_functions_eagerly(True) # used for debuging and development | |
| tf.compat.v1.disable_eager_execution() # usually using this for fastest performance | |
| gpus = tf.config.experimental.list_physical_devices('GPU') | |
| if len(gpus) > 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def __init__(self, df, initial_balance=1000, lookback_window_size=50, Render_range = 100): | |
| ... | |
| # Neural Networks part bellow | |
| self.lr = 0.0001 | |
| self.epochs = 1 | |
| self.normalize_value = 100000 | |
| self.optimizer = Adam | |
| # Create Actor-Critic network model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train_agent(env, visualize=False, train_episodes = 50, training_batch_size=500): | |
| env.create_writer() # create TensorBoard writer | |
| total_average = deque(maxlen=100) # save recent 100 episodes net worth | |
| best_average = 0 # used to track best average net worth | |
| for episode in range(train_episodes): | |
| state = env.reset(env_steps_size = training_batch_size) | |
| states, actions, rewards, predictions, dones, next_states = [], [], [], [], [], [] | |
| for t in range(training_batch_size): | |
| env.render(visualize) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def test_agent(env, visualize=True, test_episodes=10): | |
| env.load() # load the model | |
| average_net_worth = 0 | |
| for episode in range(test_episodes): | |
| state = env.reset() | |
| while True: | |
| env.render(visualize) | |
| action, prediction = env.act(state) | |
| state, reward, done = env.step(action) | |
| if env.current_step == env.end_step: |