Skip to content

Commit b646b45

Browse files
authored
Add files via upload
1 parent 6b9c323 commit b646b45

11 files changed

+448
-0
lines changed

15mincheck15minochlvmodel.pkl

11.6 KB
Binary file not shown.

5mincheck15minochlvmodel.pkl

1.81 MB
Binary file not shown.

Pitch Deck(draft).pdf

1.17 MB
Binary file not shown.

Pitch Deck.pages

1.44 MB
Binary file not shown.

User Guide.pages

6.81 MB
Binary file not shown.

User Guide.pdf

1.17 MB
Binary file not shown.

hyperparameters.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"alpha": 0.001, "hidden_layer_sizes": [50], "learning_rate_init": 0.001}

learn.py

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Neural Trader App connects to the Kucoin exchange API using the ccxt library, retrieves data about the
2+
# current market price of Bitcoin, and uses a neural network model to predict whether the market will
3+
# trend upwards or downwards. Based on this prediction, the script will either buy or sell a small
4+
# amount of Bitcoin.
5+
6+
# The script begins by importing several libraries that it will use throughout its execution,
7+
# including the ccxt library for interacting with the Kucoin API, the time library for adding delays,
8+
# the numpy library for numerical computations, the scikit-learn library for machine learning, and
9+
# the pickle and json libraries for saving and loading data.
10+
11+
# Next, the script defines several variables such as the exchange name, API key and secret,
12+
# symbol to trade, and the amount of Bitcoin to trade. It also instantiates the Kucoin exchange
13+
# class from ccxt and sets the sandbox mode to False.
14+
15+
# The script then enters an infinite loop to continuously trade Bitcoin. Within this loop, it
16+
# retrieves the current ticker information for the symbol, gets the current bid and ask prices,
17+
# and uses a neural network model to predict the market direction. If the prediction is bullish,
18+
# the script will place a buy order at the ask price and add the premium to the price. If the
19+
# prediction is bearish, the script will place a sell order at the bid price and subtract the
20+
# premium from the price. If the market is bullish it uses a 0 if it is bearish it uses a 1
21+
22+
# The script also includes a try-except block to handle any errors that may occur during the
23+
# trading process, such as a failed trade or an API error.
24+
25+
# Finally, the script saves the trained neural network model and the best values for the hyperparameters
26+
# to a file for later use. This allows the script to continue using the same model and
27+
# hyperparameters on future runs without the need to retrain the model each time.
28+
29+
# These are the time frame you can use for KuCoin
30+
# {'1m': '1min', '3m': '3min', '5m': '5min', '15m': '15min', '30m': '30min',
31+
# '1h': '1hour', '2h': '2hour', '4h': '4hour', '6h': '6hour', '8h': '8hour',
32+
# '12h': '12hour', '1d': '1day', '1w': '1week'}
33+
34+
## Things to improve on:
35+
# 1) use bayes for chosing hyperparamters or evolutions algorithm
36+
37+
import ccxt
38+
from time import sleep
39+
import numpy as np
40+
from sklearn.neural_network import MLPClassifier
41+
from sklearn.model_selection import GridSearchCV
42+
from datetime import datetime
43+
import pickle
44+
import json
45+
46+
47+
# Replace EXCHANGE_NAME with the name of the exchange you want to use
48+
exchange_name = 'kucoin'
49+
50+
# Instantiate the Exchange class
51+
exchange = getattr(ccxt, exchange_name)()
52+
53+
# Set sandbox mode to True or False
54+
exchange.set_sandbox_mode(enabled=False)
55+
56+
# Set your API keys
57+
exchange.apiKey = '63976e0702c544000126a8ca'
58+
exchange.secret = '9655f65f-16f0-4790-9610-cfd61c908101'
59+
exchange.password = 'meowmeow'
60+
61+
# Set the symbol you want to trade on Kucoin
62+
symbol = 'BTC/USDT'
63+
64+
# Set the amount of BTC you want to trade
65+
amount = .005
66+
67+
# KuCoin fee per transcation
68+
fee = .001
69+
70+
# Set the premium for the sell order
71+
#print('# Set the premium for the sell order')
72+
premium = 0.002 + fee
73+
74+
## Start the trading script
75+
while True:
76+
try:
77+
# Batch streaming data from KuCoin it uses a pair
78+
# and a window time frame
79+
data = exchange.fetch_ohlcv('BTC/USDT', '15m')
80+
81+
# predict if bullish or bearish
82+
def predict_market_direction(data):
83+
# extract the features and the target variable from the data (IMPROTRANT: chagned > to <)
84+
features = np.array([d[1:5] for d in data])
85+
target = np.array([1 if d[4] < d[1] else 0 for d in data])
86+
87+
# specify the values for the hyperparameters that you want to tune
88+
hyperparameters = {
89+
'hidden_layer_sizes': [(10,), (50,), (100,)],
90+
'learning_rate_init': [0.001, 0.01, 0.1],
91+
'alpha': [0.001, 0.01, 0.1]
92+
}
93+
# create a neural network model
94+
mlp = MLPClassifier()
95+
96+
# use the GridSearchCV class to search through the combinations of hyperparameters
97+
# and evaluate the performance of the model on a validation set to find the best combination
98+
grid_search = GridSearchCV(mlp, hyperparameters, cv=5, n_jobs=-1)
99+
grid_search.fit(features, target)
100+
101+
# save the best hyperparameters to a JSON file
102+
with open('hyperparameters.json', 'w') as f:
103+
json.dump(grid_search.best_params_, f)
104+
105+
# print the best values for the hyperparameters
106+
#print(grid_search.best_params_)
107+
108+
# update the model with the best values for the hyperparameters
109+
mlp.set_params(**grid_search.best_params_)
110+
111+
# train the model using the updated hyperparameters and the features and target
112+
mlp.fit(features, target)
113+
114+
# open the file in write mode
115+
f = open('15mincheck15minochlvmodel.pkl', 'wb')
116+
117+
# save the trained model to the model file
118+
pickle.dump(mlp, f)
119+
120+
# close the file
121+
f.close()
122+
123+
# predict the market direction using the trained model
124+
# returns 0 for bullish
125+
# returns 1 for bearish
126+
# Use the reshape() method to convert the features[-1] array to a 2D shape with 4 features
127+
features_2d = np.array(features[-1]).reshape(-1, 4)
128+
129+
# Use the predict() method with the reshaped array
130+
prediction = mlp.predict(features_2d)
131+
return prediction
132+
133+
134+
# Create an infinite loop to trade continuously
135+
while True:
136+
# Fetch the current ticker information for the symbol
137+
print('# Fetch the current ticker information for the symbol')
138+
ticker = exchange.fetch_ticker(symbol)
139+
140+
# Check the current bid and ask prices
141+
print('# Check the current bid and ask prices')
142+
bid = ticker['bid']
143+
ask = ticker['ask']
144+
145+
# Calculate the midpoint of the bid and ask prices
146+
print('# Calculate the midpoint of the bid and ask prices')
147+
midpoint = (bid + ask) / 2
148+
149+
150+
# Market Data Print
151+
current_time = datetime.now()
152+
print('# Market Data Print: Bullish [0] vs Bearish [1]')
153+
154+
print("The market is ---> {}".format(predict_market_direction(data)))
155+
156+
print(current_time.strftime("%B %d, %Y %I:%M %p"))
157+
# Market Data Print
158+
159+
# Check if there are any open orders
160+
print('# Check if there are any open orders')
161+
try:
162+
open_orders = exchange.fetch_open_orders(symbol)
163+
except:
164+
sleep(60)
165+
open_orders = exchange.fetch_open_orders(symbol)
166+
167+
#if not open_orders:
168+
#print('# Place a limit buy order at the midpoint price')
169+
#try:
170+
# Check if it is bullish 1 or bearish 0 before buying
171+
#if predict_market_direction(data).tolist()[0] == 0:
172+
# Place a limit buy order at the midpoint price
173+
#order_id = exchange.create_order(symbol, 'limit', 'buy', amount, midpoint)
174+
#except:
175+
# We must own bitcoin and we want to sell it if the script
176+
# tries to buy more bitcoin and has insufficent funds
177+
#if not open_orders:
178+
# Check if it is bullish 0 or bearish 1 before buying
179+
#if predict_market_direction(data).tolist()[0] == 1:
180+
# Place a limit sell order at the midpoint price plus the premium which includes the fee
181+
#order_id = exchange.create_order(symbol, 'limit', 'sell', amount, midpoint * (1 + premium))
182+
#order_id = exchange.create_order(symbol, 'limit', 'sell', amount, midpoint)
183+
#else:
184+
##Always run script even if error restart
185+
#auto_start = True
186+
187+
# Pause for a few seconds and check the status of the open orders before selling
188+
print('# Pause for a few seconds and check the status of the open orders')
189+
sleep(5)
190+
open_orders = exchange.fetch_open_orders(symbol)
191+
192+
# Check if there are any open orders
193+
print('# Check if there are any open orders')
194+
#if not open_orders:
195+
# Place a limit sell order at the midpoint price plus the premium
196+
#try:
197+
#if predict_market_direction(data).tolist()[0] == 1:
198+
#order_id = exchange.create_order(symbol, 'limit', 'sell', amount, midpoint * (1 + premium))
199+
#order_id = exchange.create_order(symbol, 'limit', 'sell', amount, midpoint)
200+
#except:
201+
# Place a limit buy order at the midpoint price
202+
# If for some reason the script doesnt have anything to sell
203+
# It'll just buy it
204+
#if predict_market_direction(data).tolist()[0] == 0:
205+
#order_id = exchange.create_order(symbol, 'limit', 'buy', amount, midpoint)
206+
207+
208+
# Pause for a few seconds and check the status of the open orders XYZ
209+
print('# Pause for a few seconds and check the status of the open orders - checks every 60*15 min')
210+
sleep(60 * 15)
211+
try:
212+
open_orders = exchange.fetch_open_orders(symbol)
213+
except:
214+
sleep(5)
215+
open_orders = exchange.fetch_open_orders(symbol)
216+
except:
217+
sleep(60)
218+
continue

log.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)