Skip to content

Instantly share code, notes, and snippets.

@5j9
Last active April 4, 2023 16:15
Show Gist options
  • Save 5j9/24466f71984afede46badbd15cbeeb7f to your computer and use it in GitHub Desktop.
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
# 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
@5j9
Copy link
Author

5j9 commented Apr 4, 2023

Full output:


from 2022-01-21 to 2022-02-20
nav=1,000,000
month_gain=0.9781530343007914

from 2022-02-20 to 2022-03-21
nav=1,978,153
month_gain=1.0279456193353473

from 2022-03-21 to 2022-04-21
nav=3,033,434
month_gain=1.0733704209089954

from 2022-04-21 to 2022-05-22
nav=4,255,998
month_gain=1.0882065323684726

from 2022-05-22 to 2022-06-22
nav=5,631,405
month_gain=1.0973220704529114

from 2022-06-22 to 2022-07-23
nav=7,179,465
month_gain=0.9494717877323725

from 2022-07-23 to 2022-08-23
nav=7,816,699
month_gain=0.9084008970156973

from 2022-08-23 to 2022-09-23
nav=8,100,697
month_gain=1.0587732624382837

from 2022-09-23 to 2022-10-23
nav=9,576,801
month_gain=1.0518339162406958

from 2022-10-23 to 2022-11-22
nav=11,073,204
month_gain=1.0942961889334126

from 2022-11-22 to 2022-12-22
nav=13,117,365
month_gain=1.115387611998441

from 2022-12-22 to 2023-01-21
nav=15,630,947
month_gain=1.2307907236658293

from 2023-01-21 to 2023-02-20
nav=20,238,424
month_gain=1.348127128263337

from 2023-02-20 to 2023-03-11
nav=28,283,968
month_gain=0.8276921781594679

final nav: 24,410,419

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