-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS9_Binance.py
95 lines (75 loc) · 3.17 KB
/
CS9_Binance.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
import ccxt
from datetime import datetime, timedelta
# Set up Binance API credentials
binance_api_key = "ENTER YOUR API KEY HERE"
binance_api_secret = "ENTER YOUR API SECRET KEY HERE"
# Read coin data from the spreadsheet
coin_data = []
with open('data.csv', 'r') as file:
lines = file.readlines()
headers = lines[0].strip().split(',')
for line in lines[1:]:
data = line.strip().split(',')
coin_data.append(dict(zip(headers, data)))
# Instantiate the Binance API
binance = ccxt.binance({
'apiKey': binance_api_key,
'secret': binance_api_secret
})
# Load the market symbols supported by Binance
binance.load_markets()
# Fetch OHLCV data for each coin
for coin in coin_data:
symbol = coin['Symbol']
date_str = coin['Date']
days = int(coin['Days'])
# Set the start time for data collection
start_time = datetime.strptime(date_str, "%d/%m/%Y")
# Set the end time as the current datetime
end_time = datetime.now()
# Convert the start and end times to UNIX timestamps
start_timestamp = int(start_time.timestamp() * 1000)
end_timestamp = int(end_time.timestamp() * 1000)
# Set the desired timeframe
timeframe = '15m'
# Get the correct symbol for the coin on Binance
market_symbols = binance.symbols
if symbol not in market_symbols:
print(f"Skipping {symbol} - Symbol not found on Binance.")
continue
# Create a list to store the OHLCV data
filtered_data = []
# Fetch OHLCV data in chunks to cover the desired period
current_timestamp = start_timestamp
while current_timestamp <= end_timestamp:
# Limit the number of data points to fetch in each request to avoid exceeding the API rate limits
limit = 500
# Fetch OHLCV data from the Binance API
ohlcv_data = binance.fetch_ohlcv(symbol, timeframe, since=current_timestamp, limit=limit)
# Check if OHLCV data is empty
if len(ohlcv_data) == 0:
break
# Extract data from the fetched OHLCV data and filter based on the specified timeframe
for item in ohlcv_data:
timestamp = datetime.fromtimestamp(item[0] / 1000)
if start_time <= timestamp <= end_time:
filtered_data.append([
timestamp,
item[1],
item[2],
item[3],
item[4],
item[5]
])
# Update the current timestamp to the next available timestamp
current_timestamp = ohlcv_data[-1][0] + (ohlcv_data[1][0] - ohlcv_data[0][0])
# Break the loop if we've reached the end of the desired range
if current_timestamp > end_timestamp:
break
# Save the filtered OHLCV data to a CSV file
csv_filename = f"{symbol.replace('/', '_')}_ohlcv_data.csv"
with open(csv_filename, 'w') as file:
file.write("timestamp,open,high,low,close,volume\n")
for row in filtered_data:
file.write(",".join(str(value) for value in row) + "\n")
print(f"OHLCV data for {symbol} saved to {csv_filename}.")