Skip to content

Commit 6b9c323

Browse files
authored
Update README.md
1 parent 62efb8b commit 6b9c323

File tree

1 file changed

+259
-2
lines changed

1 file changed

+259
-2
lines changed

README.md

+259-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,259 @@
1-
# ai-neural-trader-kucoin
2-
Neural Trader App is a powerful tool that helps you trade Bitcoin on the Kucoin exchange using machine learning. The app connects to the Kucoin API using the ccxt library and retrieves realtime data on the current market price of Bitcoin.
1+
Intro to the Neural Trader Python Application
2+
Neural Trader App is a powerful tool that helps you trade Bitcoin on the Kucoin exchange using
3+
machine learning. The app connects to the Kucoin API using the ccxt library and retrieves realtime
4+
data on the current market price of Bitcoin. It then uses a neural network model to predict
5+
whether the market will trend upwards or downwards, and based on this prediction, the app will
6+
either buy or sell a small amount of Bitcoin.
7+
8+
The app is designed to be easy to use, with a straightforward interface and user-friendly
9+
controls. It begins by importing several libraries that it will use throughout its execution, including
10+
the ccxt library for interacting with the Kucoin API, the time library for adding delays, the numpy
11+
library for numerical computations, and the scikit-learn library for machine learning. The app
12+
also defines several variables such as the exchange name, API key and secret, symbol to trade,
13+
and the amount of Bitcoin to trade.
14+
15+
Once set up, the app 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, and
17+
uses a neural network model to predict the market direction. If the prediction is bullish, the app
18+
will place a buy order at the ask price and add the premium to the price. If the prediction is
19+
bearish, the app will place a sell order at the bid price and subtract the premium from the price.
20+
The app also includes a try-except block to handle any errors that may occur during the trading
21+
process, such as a failed trade or an API error. If an error does occur, the app will log the error
22+
and continue with the next iteration of the loop.
23+
24+
The app uses a neural network model to predict whether the market will trend upwards or
25+
downwards. The neural network model is trained on historical data, using a supervised learning
26+
technique called regression analysis. This allows the model to learn the relationship between
27+
various features (such as the price of Bitcoin, the volume of trades, and the overall market
28+
sentiment) and the direction of the market (bullish or bearish).
29+
30+
Based on the prediction made by the neural network model, the app will either buy or sell a
31+
small amount of Bitcoin. If the prediction is bullish (i.e., the market is expected to trend
32+
upwards), the app will place a buy order at the ask price and add the premium to the price. If the
33+
prediction is bearish (i.e., the market is expected to trend downwards), the app will place a sell
34+
order at the bid price and subtract the premium from the price. If the market is bullish it will price
35+
a 0 if it is bearish it’ll print a 1. The script uses these to decide to buy or sell if no open orders
36+
exist.
37+
38+
In addition, the app saves the trained neural network model and the best values for the
39+
hyperparameters to a file for later use. This allows the app to continue using the same model
40+
and hyperparameters on future runs without the need to retrain the model each time. This is
41+
useful for ensuring consistent performance and minimizing the time and resources needed for
42+
training the model.
43+
44+
Overall, Neural Trader App is a powerful and reliable tool for trading Bitcoin on the Kucoin
45+
exchange, using advanced machine learning techniques to make informed decisions and
46+
maximize returns.
47+
48+
This application can be used for 90 exchanges including Coinbase, Coinbase Pro, etc. You may
49+
need to changes some of the ccxt functions if the exchange uses something different. You can
50+
also use any cryptocurrency pair you’d like to use, refer to the ccxt documentation to learn how
51+
to print a response of exchanges and the cryptocurrencies you can trade.
52+
User Guide Page 2 of 8 Cody Krecicki
53+
54+
How to Setup
55+
If the bot is going to trade the BTC/USDT pair, you can use any pair, make sure the have the
56+
second part of the pair or the first part available. It needs to have USDT in the account in order
57+
to initiate a buy order of bitcoin and if you hold bitcoin it will sell it instead when you start the
58+
script. The script could be containerized into functions. I have purposely not done that to
59+
continue to experiment with the script, this is my personal opinion, you are welcome to
60+
containerized functions.
61+
62+
You will first need to run the learn.py file. This will create a file called model.pkl this is the neural
63+
network model that we will use to begin the main.py to use this model and use new streaming
64+
information download it in batches make a decision based on both the new incoming information
65+
it learned about and the model that it saved from its previous learning sessions.
66+
Model is being trained on a 15 minute time frame downloaded in streaming batches using the
67+
ccxt libraries access to the exchanges OHLVC.
68+
69+
After running the train.py file for about an hour you have enough data to begin using the main.py
70+
file from now on forward unless you want to create a new model based on a new timeframe. I
71+
don't know if this is necessary but this is how you should go about it when training on new time
72+
frames.
73+
74+
These are the time frame you can use for KuCoin:
75+
{'1m': '1min', '3m': '3min', '5m': '5min', '15m': '15min', '30m': '30min', '1h': '1hour', '2h': '2hour',
76+
'4h': '4hour', '6h': '6hour', '8h': '8hour', '12h': '12hour', '1d': '1day', '1w': ‘1week’}
77+
User Guide Page 3 of 8 Cody Krecicki
78+
79+
User Guide for learn.py
80+
This script is a trading bot that uses a machine learning model to predict the direction of the
81+
market (bullish or bearish) and makes trades accordingly.
82+
The script begins by importing several modules:
83+
• ccxt is a library for interacting with cryptocurrency exchanges
84+
• time is a built-in Python library for working with time
85+
• numpy is a library for working with arrays and numerical data
86+
• sklearn is a library for machine learning tasks
87+
• datetime is a built-in Python library for working with dates and times
88+
• pickle is a built-in Python library for serializing and deserializing objects
89+
• json is a built-in Python library for working with JSON data
90+
Next, the script sets some variables:
91+
• exchange_name is the name of the exchange that the script will use. In this case, it is set to
92+
'kucoin'.
93+
• exchange is an instance of the ccxt.Exchange class for the specified exchange.
94+
• exchange.set_sandbox_mode is a method that sets the sandbox mode for the exchange.
95+
Sandbox mode is a simulated trading environment that allows you to test the script without
96+
actually making trades. In this case, sandbox mode is disabled.
97+
• exchange.apiKey, exchange.secret, and exchange.password are the API keys that the script
98+
will use to authenticate with the exchange.
99+
• symbol is the symbol that the script will trade on the exchange. In this case, it is set to 'BTC/
100+
USDT'.
101+
• amount is the amount of BTC that the script will trade. In this case, it is set to 0.005 BTC.
102+
• fee is the fee that the exchange charges for each trade. In this case, it is set to 0.001 BTC.
103+
• premium is the premium that the script will add to the sell order. In this case, it is set to 0.002
104+
BTC plus the fee.
105+
The script then enters an infinite loop with a try/except block. The try block contains the main
106+
logic for the script, and the except block handles any errors that might occur.
107+
Inside the try block, the script fetches the OHLCV (Open-High-Low-Close-Volume) data for the
108+
specified symbol and time frame. This data is used to train the machine learning model.
109+
Next, the script defines a function called predict_market_direction that takes the OHLCV data as
110+
an input and returns a prediction for the market direction (0 for bullish, 1 for bearish).
111+
112+
This function does the following:
113+
• Extracts the features and target variable from the data
114+
• Specifies the values for the hyperparameters that the script wants to tune
115+
• Creates a neural network model using the MLPClassifier class from sklearn
116+
• Uses the GridSearchCV class to search through combinations of the hyperparameters and
117+
find the best combination
118+
• Saves the best hyperparameters to a JSON file
119+
• Updates the model with the best hyperparameters
120+
• Trains the model using the features and target
121+
• Saves the trained model to a file using pickle
122+
• Makes a prediction using the trained model and the most recent OHLCV data
123+
User Guide Page 4 of 8 Cody Krecicki
124+
125+
After the predict_market_direction function is defined, the script enters another infinite loop.
126+
Inside this loop, the script does the following:
127+
• Fetches the current ticker information for the symbol
128+
• Checks the following:
129+
• The current bid and ask prices for the symbol
130+
• The current market direction prediction
131+
• If the prediction is 0 (bullish), the script creates a sell order with a price that is premium
132+
higher than the current bid price.
133+
• If the prediction is 1 (bearish), the script creates a buy order with a price that is premium
134+
lower than the current ask price.
135+
After the trade is made, the script sleeps for 15 minutes before starting the loop again. This is to
136+
give the market time to move before making another trade.
137+
The script continues to run indefinitely, making trades and retraining the model as necessary. If
138+
an error occurs during the execution of the script, the except block will handle the error and print
139+
an error message to the console.
140+
User Guide Page 5 of 8 Cody Krecicki
141+
142+
User Guide for main.py
143+
This script is a trading bot that uses machine learning to predict the direction of the market and
144+
makes trades accordingly on the KuCoin exchange. The bot is built using the ccxt library, which
145+
allows it to interact with the KuCoin API, and the sklearn library, which provides machine
146+
learning functionality.
147+
148+
The script starts by importing the necessary libraries and setting some initial variables, including
149+
the exchange name, symbol to trade, and amount of the trade. It then instantiates the Exchange
150+
class from the ccxt library and sets the sandbox mode to False, which means it will be making
151+
real trades on the KuCoin exchange. It also sets the API keys for the KuCoin account that will
152+
be used for trading.
153+
154+
The script then enters an infinite loop, in which it continuously fetches data from the exchange,
155+
predicts the direction of the market using a machine learning model, and places trades
156+
accordingly. The model is trained and updated using the data from the exchange, and the
157+
prediction is made based on the most recent data. If the prediction is bullish (indicating an
158+
upward trend in the market), the bot will place a limit buy order at the midpoint between the
159+
current bid and ask prices. If the prediction is bearish (indicating a downward trend in the
160+
market), the bot will place a limit sell order at a price that is slightly higher than the current bid
161+
price, with a premium added to account for the trading fee.
162+
163+
The script also includes some error handling and sleep statements to ensure that it continues to
164+
run smoothly and does not make too many requests to the exchange in a short period of time.
165+
The first part of the script imports the necessary libraries:
166+
• The ccxt library is used to interact with various cryptocurrency exchanges. In this case, it
167+
is used to communicate with the KuCoin exchange.
168+
• The time library is used to pause the script for a certain amount of time using the sleep()
169+
function.
170+
• The numpy library is used to work with arrays and perform mathematical operations on
171+
them.
172+
• The sklearn library contains a variety of machine learning algorithms and tools, including
173+
the MLPClassifier, which is a neural network classifier that is used in this script to predict
174+
the direction of the market.
175+
• The datetime library is used to get the current date and time.
176+
• The pickle library is used to save and load the machine learning model to and from a file.
177+
User Guide Page 6 of 8 Cody Krecicki
178+
The next part of the script sets some initial variables
179+
• The exchange_name variable is set to 'kucoin', which tells the script to use the KuCoin
180+
exchange.
181+
• The exchange variable is set to an instance of the ccxt.kucoin class, which allows the
182+
script to communicate with the KuCoin API.
183+
• The set_sandbox_mode() method is called on the exchange object and passed the value
184+
False, which means the bot will be making real trades on the KuCoin exchange.
185+
• The apiKey, secret, and password properties of the exchange object are set to the API
186+
keys for the KuCoin account that will be used for trading.
187+
• The symbol variable is set to 'BTC/USDT', which is the symbol for the BTC/USDT pair on
188+
the KuCoin exchange. This is the pair that the bot will be trading.
189+
• The amount variable is set to 0.005, which is the amount of BTC that the bot will be
190+
trading each time it places an order.
191+
• The fee variable is set to 0.001, which is the fee that KuCoin charges for each trade.
192+
• The premium variable is set to .002, which is the profit that bot will take plus the fee.
193+
The next part of the script enters an infinite loop, in which it continuously fetches data from the
194+
exchange and makes predictions about the market direction using a machine learning model:
195+
• The script enters an infinite loop using the while True statement. This means that the
196+
script will run indefinitely until it is manually stopped.
197+
• The try and except statements are used to handle any errors that may occur when the
198+
script is running. If an error occurs, the script will pause for 60 seconds and then continue
199+
running.
200+
• The fetch_ohlcv() method of the exchange object is called to fetch data from the
201+
exchange. The 'BTC/USDT' argument specifies the symbol for the BTC/USDT pair on the
202+
KuCoin exchange, and the '15m' argument specifies the time frame for the data. The
203+
fetch_ohlcv() method returns a list of "candlestick" data points, which contain information
204+
about the open, high, low, close, and volume of the BTC/USDT pair over a 15-minute
205+
period.
206+
• The model_file variable is set to 'model.pkl', which is the name of the file that will be used
207+
to save and load the machine learning model.
208+
• The predict_market_direction() function is defined. This function takes two arguments:
209+
data, which is the list of candlestick data points that was fetched from the exchange, and
210+
model_file, which is the name of the file that will be used to save and load the model.
211+
• The features variable is set to an array of the open, high, low, and close values from the
212+
data argument. The target variable
213+
The next part of the script enters an infinite loop, in which it continuously fetches data from the
214+
exchange and makes predictions about the market direction using a machine learning model:
215+
• The script enters an infinite loop using the while True statement. This means that the
216+
script will run indefinitely until it is manually stopped.
217+
• The try and except statements are used to handle any errors that may occur when the
218+
script is running. If an error occurs, the script will pause for 60 seconds and then continue
219+
running.
220+
• The fetch_ohlcv() method of the exchange object is called to fetch data from the
221+
exchange. The 'BTC/USDT' argument specifies the symbol for the BTC/USDT pair on the
222+
User Guide Page 7 of 8 Cody Krecicki
223+
KuCoin exchange, and the '15m' argument specifies the time frame for the data. The
224+
fetch_ohlcv() method returns a list of "candlestick" data points, which contain information
225+
about the open, high, low, close, and volume of the BTC/USDT pair over a 15-minute
226+
period.
227+
• The model_file variable is set to 'model.pkl', which is the name of the file that will be used
228+
to save and load the machine learning model.
229+
• The predict_market_direction() function is defined. This function takes two arguments:
230+
data, which is the list of candlestick data points that was fetched from the exchange, and
231+
model_file, which is the name of the file that will be used to save and load the model.
232+
• The features variable is set to an array of the open, high, low, and close values from the
233+
data argument. The target variable
234+
The predict_market_direction() function loads the machine learning model from the model_file,
235+
updates it with the new data using the partial_fit() method, and then saves the updated model
236+
back to the model_file. It then uses the updated model to make a prediction about the market
237+
direction based on the most recent data point in the data argument. If the prediction is 0, the
238+
market is considered to be bullish (indicating an upward trend), and if the prediction is 1, the
239+
market is considered to be bearish (indicating a downward trend).
240+
The script then enters another infinite loop, in which it continuously checks the current market
241+
conditions and makes trades based on the prediction of the market direction.
242+
• The script enters another infinite loop using the while True statement. This means that the
243+
bot will continuously check the market conditions and make trades until the script is
244+
manually stopped.
245+
• The fetch_ticker() method of the exchange object is called to fetch the current ticker
246+
information for the BTC/USDT
247+
248+
The script then checks if there are any open orders (orders that have been placed but have not
249+
yet been filled or cancelled). If there are no open orders, the script will place a limit buy order if
250+
the market is predicted to be bullish, or a limit sell order if the market is predicted to be bearish.
251+
The limit buy order will be placed at the midpoint between the current bid and ask prices, and
252+
the limit sell order will be placed at a price that is slightly higher than the current bid price, with a
253+
premium added to account for the trading fee.
254+
255+
If there are open orders, the script will cancel them. This is to ensure that there are no
256+
conflicting orders that could cause problems when the bot is making trades.
257+
Finally, the script includes some error handling and sleep statements to ensure that it continues
258+
to run smoothly and does not make too many requests to the exchange in a short period of time.
259+
User Guide Page 8 of 8 Cody Krecicki

0 commit comments

Comments
 (0)