Skip to content

Instantly share code, notes, and snippets.

@5j9
Last active April 25, 2023 15:54
Show Gist options
  • Save 5j9/197bb08bafaef4363f4849c98c5ee3b0 to your computer and use it in GitHub Desktop.
Save 5j9/197bb08bafaef4363f4849c98c5ee3b0 to your computer and use it in GitHub Desktop.
A script to find the answer of bashgah.com's competition number 140201025
# بیشترین بازده درصدی فصلی برای بهار سال 1401 مربوط به کدام سبد دارایی زیر بوده است؟
# https://bashgah.com/Question/140201025/
from asyncio import run
from tsetmc.instruments import Instrument
from iranetf.sites import RayanHamafza
from iranetf import Session as IranETFSession
from tsetmc import Session
from jdatetime import date
import pandas as pd
options = [
((0.7, 'مثقال'), (0.3, 'آگاس')),
((0.7, 'آگاس'), (0.3, 'همای')),
((0.7, 'همای'), (0.3, 'آگاس')),
((0.7, 'مثقال'), (0.3, 'همای')),
]
start_date = pd.Timestamp(date(1400, 12, 28).togregorian()) # last work day
end_date = pd.Timestamp(date(1401, 3, 31).togregorian())
cache = {}
async def get_gain(l18):
if c := cache.get(l18):
return c
async with Session():
i = await Instrument.from_l18(l18)
df = await i.price_history()
s = df.pc[start_date]
e = df.pc[end_date]
if l18 == 'همای':
async with IranETFSession():
p = await RayanHamafza('https://www.homayeagah.ir/').fund_profit()
p.set_index('ProfitDate', inplace=True)
p = p[start_date:end_date]
e += p.UnitProfit.sum()
c = cache[l18] = e / s
return c
async def main():
for i, option in enumerate(options, 1):
g = 0
for (weight, symbol) in option:
g += weight * await get_gain(symbol)
print(f'option {i}: {(g - 1) * 100}')
run(main())
@5j9
Copy link
Author

5j9 commented Apr 16, 2023

Program output:

option 1: 22.853099797230225
option 2: 9.397227791340024
option 3: 8.00550367728594
option 4: 21.80930671168968

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