Skip to content

Instantly share code, notes, and snippets.

@5j9
Last active April 25, 2023 15:46
Show Gist options
  • Save 5j9/650c08bd0affc444d6175fc818ced531 to your computer and use it in GitHub Desktop.
Save 5j9/650c08bd0affc444d6175fc818ced531 to your computer and use it in GitHub Desktop.
# بیشترین بازده درصدی فصلی برای تابستان سال 1401 مربوط به کدام سبد دارایی زیر بوده است؟
# https://bashgah.com/Question/140201027/
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(1401, 4, 1).togregorian()) # last work day
end_date = pd.Timestamp(date(1401, 6, 30).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 17, 2023

Output:

option 1: -8.522377423686155
option 2: -2.7904804492945523
option 3: 1.777357403712898
option 4: -5.096499033930568

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