forked from xmd79/trading-bot-using-time-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hft107.py
409 lines (315 loc) · 13.6 KB
/
hft107.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
##################################################
##################################################
# Start code:
print()
##################################################
##################################################
print("Init code: ")
print()
##################################################
##################################################
print("Test")
print()
##################################################
##################################################
# Import modules:
import math
import time
import numpy as np
import requests
import talib
import json
import datetime
from datetime import timedelta
from decimal import Decimal
import decimal
import random
import statistics
from statistics import mean
import scipy.fftpack as fftpack
##################################################
##################################################
# binance module imports
from binance.client import Client as BinanceClient
from binance.exceptions import BinanceAPIException, BinanceOrderException
from binance.enums import *
##################################################
##################################################
# Load credentials from file
with open("credentials.txt", "r") as f:
lines = f.readlines()
api_key = lines[0].strip()
api_secret = lines[1].strip()
# Instantiate Binance client
client = BinanceClient(api_key, api_secret)
##################################################
##################################################
# Define a function to get the account balance in BUSD
def get_account_balance():
accounts = client.futures_account_balance()
for account in accounts:
if account['asset'] == 'USDT':
bUSD_balance = float(account['balance'])
return bUSD_balance
# Get the USDT balance of the futures account
bUSD_balance = float(get_account_balance())
# Print account balance
print("USDT Futures balance:", bUSD_balance)
print()
##################################################
##################################################
# Define Binance client reading api key and secret from local file:
def get_client():
# Read credentials from file
with open("credentials.txt", "r") as f:
lines = f.readlines()
api_key = lines[0].strip()
api_secret = lines[1].strip()
# Instantiate client
client = BinanceClient(api_key, api_secret)
return client
# Call the function to get the client
client = get_client()
##################################################
##################################################
# Initialize variables for tracking trade state:
TRADE_SYMBOL = "BTCUSDT"
##################################################
##################################################
# Define timeframes
timeframes = ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d']
##################################################
##################################################
# Get current price as <class 'float'>
def get_price(symbol):
try:
client = get_client()
url = "https://fapi.binance.com/fapi/v1/ticker/price"
params = {
"symbol": symbol
}
response = requests.get(url, params=params)
data = response.json()
if "price" in data:
price = float(data["price"])
else:
raise KeyError("price key not found in API response")
return price
except (BinanceAPIException, KeyError) as e:
print(f"Error fetching price for {symbol}: {e}")
return 0
price = get_price("BTCUSDT")
print(price)
print()
##################################################
##################################################
# Define function to retrieve candles from Binance API
def get_candles(symbol, timeframes):
candles = []
for timeframe in timeframes:
limit = 10000 # default limit
tf_value = int(timeframe[:-1]) # extract numeric value of timeframe
if tf_value >= 4: # check if timeframe is 4h or above
limit = 20000 # increase limit for 4h timeframe and above
klines = client.get_klines(
symbol=symbol,
interval=timeframe,
limit=limit
)
# Convert klines to candle dict
for k in klines:
candle = {
"time": k[0] / 1000,
"open": float(k[1]),
"high": float(k[2]),
"low": float(k[3]),
"close": float(k[4]),
"volume": float(k[5]),
"timeframe": timeframe
}
candles.append(candle)
return candles
# Get candles
candles = get_candles(TRADE_SYMBOL, timeframes)
# Organize candles by timeframe
candle_map = {}
for candle in candles:
timeframe = candle["timeframe"]
candle_map.setdefault(timeframe, []).append(candle)
# Get entire list of close prices as <class 'list'> type
def get_closes(timeframe):
closes = []
candles = candle_map[timeframe]
for c in candles:
close = c['close']
if not np.isnan(close):
closes.append(close)
# Append current price to the list of closing prices
current_price = get_price(TRADE_SYMBOL)
closes.append(current_price)
return closes
closes = get_closes('1m')
#print(closes)
##################################################
##################################################
def generate_momentum_sinewave(timeframes):
# Initialize variables
momentum_sorter = []
market_mood = []
last_reversal = None
last_reversal_value_on_sine = None
last_reversal_value_on_price = None
next_reversal = None
next_reversal_value_on_sine = None
next_reversal_value_on_price = None
# Loop over timeframes
for timeframe in timeframes:
# Get close prices for current timeframe
close_prices = np.array(get_closes(timeframe))
# Get last close price
current_close = close_prices[-1]
# Calculate sine wave for current timeframe
sine_wave, leadsine = talib.HT_SINE(close_prices)
# Replace NaN values with 0
sine_wave = np.nan_to_num(sine_wave)
sine_wave = -sine_wave
# Get the sine value for last close
current_sine = sine_wave[-1]
# Calculate the min and max sine
sine_wave_min = np.nanmin(sine_wave) # Use nanmin to ignore NaN values
sine_wave_max = np.nanmax(sine_wave)
# Calculate price values at min and max sine
sine_wave_min_price = close_prices[sine_wave == sine_wave_min][0]
sine_wave_max_price = close_prices[sine_wave == sine_wave_max][0]
# Calculate the difference between the max and min sine
sine_wave_diff = sine_wave_max - sine_wave_min
# If last close was the lowest, set as last reversal
if current_sine == sine_wave_min:
last_reversal = 'dip'
last_reversal_value_on_sine = sine_wave_min
last_reversal_value_on_price = sine_wave_min_price
# If last close was the highest, set as last reversal
if current_sine == sine_wave_max:
last_reversal = 'top'
last_reversal_value_on_sine = sine_wave_max
last_reversal_value_on_price = sine_wave_max_price
# Calculate % distances
newsine_dist_min, newsine_dist_max = [], []
for close in close_prices:
# Calculate distances as percentages
dist_from_close_to_min = ((current_sine - sine_wave_min) /
sine_wave_diff) * 100
dist_from_close_to_max = ((sine_wave_max - current_sine) /
sine_wave_diff) * 100
newsine_dist_min.append(dist_from_close_to_min)
newsine_dist_max.append(dist_from_close_to_max)
# Take average % distances
avg_dist_min = sum(newsine_dist_min) / len(newsine_dist_min)
avg_dist_max = sum(newsine_dist_max) / len(newsine_dist_max)
# Determine market mood based on % distances
if avg_dist_min <= 15:
mood = "At DIP Reversal and Up to Bullish"
if last_reversal != 'dip':
next_reversal = 'dip'
next_reversal_value_on_sine = sine_wave_min
next_reversal_value_on_price = close_prices[sine_wave == sine_wave_min][0]
elif avg_dist_max <= 15:
mood = "At TOP Reversal and Down to Bearish"
if last_reversal != 'top':
next_reversal = 'top'
next_reversal_value_on_sine = sine_wave_max
next_reversal_value_on_price = close_prices[sine_wave == sine_wave_max][0]
elif avg_dist_min < avg_dist_max:
mood = "Bullish"
else:
mood = "Bearish"
# Append momentum score and market mood to lists
momentum_score = avg_dist_max - avg_dist_min
momentum_sorter.append(momentum_score)
market_mood.append(mood)
# Print distances and market mood
print(f"{timeframe} Close is now at "
f"dist. to min: {avg_dist_min:.2f}% "
f"and at "
f"dist. to max: {avg_dist_max:.2f}%. "
f"Market mood: {mood}")
# Update last and next reversal info
if next_reversal:
last_reversal = next_reversal
last_reversal_value_on_sine = next_reversal_value_on_sine
last_reversal_value_on_price = next_reversal_value_on_price
next_reversal = None
next_reversal_value_on_sine = None
next_reversal_value_on_price = None
# Get close prices for the 1-minute timeframe and last 3 closes
close_prices = np.array(get_closes('1m'))
# Calculate sine wave
sine_wave, leadsine = talib.HT_SINE(close_prices)
# Replace NaN values with 0
sine_wave = np.nan_to_num(sine_wave)
sine_wave = -sine_wave
# Get the sine value for last close
current_sine = sine_wave[-1]
# Get current date and time
now = datetime.datetime.now()
# Calculate the min and max sine
sine_wave_min = np.min(sine_wave)
sine_wave_max = np.max(sine_wave)
# Calculate the difference between the maxand min sine
sine_wave_diff = sine_wave_max - sine_wave_min
# Calculate % distances
dist_from_close_to_min = ((current_sine - sine_wave_min) /
sine_wave_diff) * 100
dist_from_close_to_max = ((sine_wave_max - current_sine) /
sine_wave_diff) * 100
# Determine market mood based on % distances
if dist_from_close_to_min <= 15:
mood = "At DIP Reversal and Up to Bullish"
if last_reversal != 'dip':
next_reversal = 'dip'
next_reversal_value_on_sine = sine_wave_min
elif dist_from_close_to_max <= 15:
mood = "At TOP Reversal and Down to Bearish"
if last_reversal != 'top':
next_reversal = 'top'
next_reversal_value_on_sine = sine_wave_max
elif dist_from_close_to_min < dist_from_close_to_max:
mood = "Bullish"
else:
mood = "Bearish"
# Get the close prices that correspond to the min and max sine values
close_prices_between_min_and_max = close_prices[(sine_wave >= sine_wave_min) & (sine_wave <= sine_wave_max)]
print()
# Print distances and market mood for 1-minute timeframe
print(f"On 1min timeframe,Close is now at "
f"dist. to min: {dist_from_close_to_min:.2f}% "
f"and at "
f"dist. to max:{dist_from_close_to_max:.2f}%. "
f"Market mood: {mood}")
min_val = min(close_prices_between_min_and_max)
max_val = max(close_prices_between_min_and_max)
print()
print("The lowest value in the array is:", min_val)
print("The highest value in the array is:", max_val)
print()
# Update last and next reversal info
#if next_reversal:
#last_reversal = next_reversal
#last_reversal_value_on_sine = next_reversal_value_on_sine
#next_reversal = None
#next_reversal_value_on_sine = None
# Print last and next reversal info
#if last_reversal:
#print(f"Last reversal was at {last_reversal} on the sine wave at {last_reversal_value_on_sine:.2f} ")
# Return the momentum sorter, market mood, close prices between min and max sine, and reversal info
return momentum_sorter, market_mood, sine_wave_diff, dist_from_close_to_min, dist_from_close_to_max, now, close_prices, current_sine, close_prices_between_min_and_max
momentum_sorter, market_mood, sine_wave_diff, dist_from_close_to_min, dist_from_close_to_max, now, close_prices, current_sine, close_prices_between_min_and_max = generate_momentum_sinewave(timeframes)
print()
print("Current close on sine value now at: ", current_sine)
print("distances as percentages from close to min: ", dist_from_close_to_min, "%")
print("distances as percentages from close to max: ", dist_from_close_to_max, "%")
print("Momentum on 1min timeframe is now at: ", momentum_sorter[-12])
print("Mood on 1min timeframe is now at: ", market_mood[-12])
print()
##################################################
##################################################