Skip to content

Instantly share code, notes, and snippets.

@tak-akashi
Last active April 11, 2024 05:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tak-akashi/142986ef7cc139ab25758129a634ec44 to your computer and use it in GitHub Desktop.
Save tak-akashi/142986ef7cc139ab25758129a634ec44 to your computer and use it in GitHub Desktop.
IMM通貨先物ポジション推移(ドル円)
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
with open('annual.txt') as f:
data = f.readlines()
listy = []
for i in range(len(data)):
if 'JAPANESE YEN - CHICAGO MERCANTILE EXCHANGE' in data[i]:
line_data = data[i].split(',')
dt = datetime.datetime.strptime(line_data[2],'%Y-%m-%d')
listy.append([dt,int(line_data[8])-int(line_data[9])])
x = []
y = []
for i, j in listy:
x.append(i)
y.append(j)
plt.style.use('ggplot')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x, y, width=4)
plt.title('IMM POSITION - JPY(NON-COMMERCIAL)', size=20)
plt.axhline(y=0, color='black')
# format your data to desired format. Here I chose YYYY-MM-DD but you can set it to whatever you want.
ax.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d'))
# rotate and align the tick labels so they look better
fig.autofmt_xdate()
plt.show()
import urllib3
import requests
import zipfile, io, certifi
urllib3.disable_warnings()
url = 'https://www.cftc.gov/files/dea/history/deacot2018.zip'
res = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(res.content))
z.extractall()
@tak-akashi
Copy link
Author

IMM通貨先物ポジション(ドル円)の推移を棒グラフで描画するスクリプトです。まず、CFTCのホームページからデータをスクレイピングして保存(import_cftc_file.py)。その後、データを取り出して描画する(imm_position.py)スクリプトです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment