Skip to content

Instantly share code, notes, and snippets.

@leeschmalz
leeschmalz / generate.py
Created February 29, 2024 18:40
generates bitcoin mnemonic and associated keys
from mnemonic import Mnemonic
from bip32 import BIP32
import hashlib
import base58
from datetime import datetime
def privkey_to_wif_compressed(privkey_hex):
extended_key = "80" + privkey_hex + "01"
first_sha256 = hashlib.sha256(bytes.fromhex(extended_key)).hexdigest()
second_sha256 = hashlib.sha256(bytes.fromhex(first_sha256)).hexdigest()
# VIP level 0, paying fees with BNB = 0.075%
env = TradingEnv(balance_amount=100,balance_unit='USDT', trading_fee_multiplier=0.99925)
for i in range(len(df)):
if env.balance_unit == 'USDT':
for symbol in symbols:
if df[f'{symbol}-USD_Low'].iloc[i] < df[f'{symbol}_lower_band'].iloc[i]: #buy signal
env.buy(symbol, df[f'{symbol}_lower_band'].iloc[i], df['OpenTime'].iloc[i])
break
class TradingEnv:
def __init__(self, balance_amount, balance_unit, trading_fee_multiplier):
self.balance_amount = balance_amount
self.balance_unit = balance_unit
self.buys = []
self.sells = []
self.trading_fee_multiplier = trading_fee_multiplier
def buy(self, symbol, buy_price, time):
self.balance_amount = (self.balance_amount / buy_price) * self.trading_fee_multiplier
import pandas as pd
df = pd.read_csv('BTC_ETH_LTC_Jan2721_Jul2121_1h.csv')
def sma(data, window):
return(data.rolling(window = window).mean())
def bollinger_band(data, sma, window, nstd):
std = data.rolling(window = window).std()
upper_band = sma + std * nstd
import pandas as pd
from binance.client import Client
from binance_keys import api_key, secret_key
import time
from datetime import datetime
import plotly.graph_objects as go
client = Client(api_key, secret_key)
coins = ['BTC','ETH','LTC']
@leeschmalz
leeschmalz / get_action.py
Created June 23, 2020 20:54
Get Action Connect X
def get_action(model, observation, epsilon):
#determine whether model action or random action based on epsilon
act = np.random.choice(['model','random'], 1, p=[1-epsilon, epsilon])[0]
observation = np.array(observation).reshape(1,6,7,1)
logits = model.predict(observation)
prob_weights = tf.nn.softmax(logits).numpy()
if act == 'model':
action = list(prob_weights[0]).index(max(prob_weights[0]))
if act == 'random':
@leeschmalz
leeschmalz / TrainConnectX.py
Last active June 23, 2020 21:01
Train Connect X
class Memory:
def __init__(self):
self.clear()
# Resets/restarts the memory buffer
def clear(self):
self.observations = []
self.actions = []
self.rewards = []
self.info = []
@leeschmalz
leeschmalz / CreateAgent.py
Created June 23, 2020 20:25
Creating the Agent
def check_if_action_valid(obs,action):
if obs[action] == 0:
valid = True
else:
valid = False
return valid
def player_1_agent(observation, configuration):
action, prob_weights = get_action(player_1_model,observation['board'],0)
@leeschmalz
leeschmalz / ConnectX_Model.py
Created June 23, 2020 16:56
ConnectX Model, Loss, and Optimizer
def create_model():
model = models.Sequential()
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
def check_if_done(observation):
done = [False,'No Winner Yet']
#horizontal check
for i in range(6):
for j in range(4):
if observation[i][j] == observation[i][j+1] == observation[i][j+2] == observation[i][j+3] == 1:
done = [True,'Player 1 Wins Horizontal']
if observation[i][j] == observation[i][j+1] == observation[i][j+2] == observation[i][j+3] == 2:
done = [True,'Player 2 Wins Horizontal']
#vertical check