Last active
April 4, 2023 16:15
-
-
Save 5j9/24466f71984afede46badbd15cbeeb7f to your computer and use it in GitHub Desktop.
A script to find the answer of bashgah.com's competition number 140201013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a short script to solve the following problem | |
# https://bashgah.com/Question/140201013/%D8%B3%D9%88%D8%A7%D9%84%D8%A7%D8%AA-%D9%88%DB%8C%DA%98%D9%87-%D9%86%D9%88%D8%B1%D9%88%D8%B2-1402 | |
# اگر شخصی از 1 بهمن 1400 و در اولین روز کاری هر ماه مبلغ 1میلیون تومان صندوق سرمایهگذاری با پشتوانه طلای زرین آگاه (با نماد مثقال) خریداری کرده باشد، در تاریخ 20 اسفند 1401 ارزش روز دارایی وی چقدر است؟ | |
from itertools import pairwise | |
import pandas as pd | |
from jdatetime import date | |
# download history from | |
# http://www.tsetmc.com/loader.aspx?ParTree=151311&i=32469128621155736# | |
df = pd.read_csv("F:\downloads\Zarin.Agah.ETF.csv") | |
df['<DTYYYYMMDD>'] = pd.to_datetime(df['<DTYYYYMMDD>'], format='%Y%m%d') | |
df.set_index('<DTYYYYMMDD>', inplace=True) | |
df.sort_index(inplace=True) | |
df['gain'] = df['<CLOSE>'].pct_change() + 1 | |
dates = [ | |
date(1400, 11, 1), | |
date(1400, 12, 1), | |
date(1401, 1, 1), | |
date(1401, 2, 1), | |
date(1401, 3, 1), | |
date(1401, 4, 1), | |
date(1401, 5, 1), | |
date(1401, 6, 1), | |
date(1401, 7, 1), | |
date(1401, 8, 1), | |
date(1401, 9, 1), | |
date(1401, 10, 1), | |
date(1401, 11, 1), | |
date(1401, 12, 1), | |
date(1401, 12, 20), | |
] | |
dates = [pd.Timestamp(d.togregorian()) for d in dates] | |
nav = 1e6 | |
previous_date = dates[0] | |
for start, end in pairwise(dates): | |
print(f'\nfrom {start:%Y-%m-%d} to {end:%Y-%m-%d}\n{nav=:,.0f}') | |
month_gain = df[start:end - pd.Timedelta(days=1)].gain.prod() | |
print(f'{month_gain=}') | |
nav *= month_gain | |
nav += 1e6 | |
print(f'\nfinal nav: {nav:,.0f}') # 24,410,419 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full output: