Last active
April 25, 2023 15:54
-
-
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
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
# بیشترین بازده درصدی فصلی برای بهار سال 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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Program output: