Skip to content

Instantly share code, notes, and snippets.

@5j9
Last active April 25, 2023 15:44
Show Gist options
  • Save 5j9/3b86aee48fbd951e91b7fc7f71f73834 to your computer and use it in GitHub Desktop.
Save 5j9/3b86aee48fbd951e91b7fc7f71f73834 to your computer and use it in GitHub Desktop.
# بیشترین بازده درصدی فصلی برای زمستان سال 1401 مربوط به کدام سبد دارایی زیر بوده است؟
# https://bashgah.com/Question/140201029/
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 is the last work day of the previous month
start_date = pd.Timestamp(date(1401, 9, 30).togregorian())
# end_date is the last work day of the last month
end_date = pd.Timestamp(date(1401, 12, 28).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 18, 2023

Output:

option 1: 51.10301467860503
option 2: 24.097120113936477
option 3: 13.408707250760688
option 4: 43.08670503122318

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