Skip to content

Instantly share code, notes, and snippets.

View pythonlessons's full-sized avatar
🏠
Working from home

Rokas Liuberskis pythonlessons

🏠
Working from home
View GitHub Profile
@pythonlessons
pythonlessons / BTC_trading_bot_2_tofile.py
Last active December 9, 2020 15:03
BTC_trading_bot_2_tofile
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")
@pythonlessons
pythonlessons / BTC_trading_bot_2_CustomEnv.py
Last active December 10, 2020 12:56
BTC_trading_bot_2_CustomEnv
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
@pythonlessons
pythonlessons / BTC_trading_bot_2_GraphInit.py
Last active December 10, 2020 17:39
BTC_trading_bot_2_GraphInit
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
@pythonlessons
pythonlessons / BTC_trading_bot_2_render_market.py
Last active December 10, 2020 17:34
BTC_trading_bot_2_render_market
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])
@pythonlessons
pythonlessons / BTC_trading_bot_2_render_volume.py
Last active December 10, 2020 17:34
BTC_trading_bot_2_render_volume
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)
@pythonlessons
pythonlessons / BTC_trading_bot_2_render_full.py
Last active December 10, 2020 17:48
BTC_trading_bot_2_render_full
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)
@pythonlessons
pythonlessons / BTC_trading_bot_3_model.py
Last active December 17, 2020 09:14
BTC_trading_bot_3_model
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:
@pythonlessons
pythonlessons / BTC_trading_bot_3_init.py
Last active December 20, 2020 13:48
BTC_trading_bot_3_init
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
@pythonlessons
pythonlessons / BTC_trading_bot_3_train_agent.py
Last active December 20, 2020 13:45
BTC_trading_bot_3_train_agent
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)
@pythonlessons
pythonlessons / BTC_trading_bot_3_test_agent.py
Created December 17, 2020 20:17
BTC_trading_bot_3_test_agent
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: