-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcot_data_single.py
51 lines (34 loc) · 1.51 KB
/
cot_data_single.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
from bs4 import BeautifulSoup
import requests
import re
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# Extracted COT Data from Single Website
# Website: https://www.tradingster.com/cot/legacy-futures
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# Enter URL of single website
# GBP COT Data
url = "https://www.tradingster.com/cot/legacy-futures/096742"
result = requests.get(url).text
doc = BeautifulSoup(result, "html.parser")
chart = doc.find(["div"], class_="chart-section")
table = chart.table
table_rows = table.find_all(["tr"])
COT_Data_Single_Percentages = []
# Ignore the variable "a"
for tr in table_rows[3:4]:
long, a, short = tr.contents[1:4]
# Getting rid of commas and converting to int
long_value = int(long.string.replace(',', ''))
short_value = int(short.string.replace(',', ''))
long_percentage = ((long_value)/(long_value + short_value))*100
long_percentage_value = str(round(long_percentage, 2)) + '%'
short_percentage = ((short_value)/(long_value + short_value))*100
short_percentage_value = str(round(short_percentage, 2)) + '%'
record = {}
record["symbol"] = 'GBP'
record["long_percent"] = long_percentage_value
record["short_percent"] = short_percentage_value
COT_Data_Single_Percentages.append(record)
print(COT_Data_Single_Percentages)