一种更pythonic的排列方式?

问题描述

我正在尝试建立一个回溯测试算法,以根据市场从历史最高点下跌了多少来组合确定投资组合应具有的现金头寸。尝试查看是否有一种节省时间的方法,因为在此设置下运行需要几天的时间。我运行了几个小时,并没有沿着组合路径走下去哈哈

这将在public github

上进行更新

我正在尝试做的简化版本

    if (current_market_status > 0):  #greater than 0
        current_cash_required_equity = 0.3
    elif (current_market_status > -0.05): #less than 5%
        current_cash_required_equity = 0.25
    elif (current_market_status > -0.10): #less than 10%
        current_cash_required_equity = 0.20 
    elif (current_market_status > -0.15): #less than 15%
        current_cash_required_equity = 0.15
    elif (current_market_status > -0.20): #less than 20%
        current_cash_required_equity = 0.10
    elif (current_market_status > -0.25): #less than 25%
        current_cash_required_equity = 0.05
    elif (current_market_status > -0.30): #less than 30%
        current_cash_required_equity = 0

这是算法的工作原理。

  1. 从2000年1月1日开始,拥有10,000个拥有100%现金头寸的投资组合。它只能选择购买SPY。
  2. 每个月有1000个存入投资组合。
  3. 每天跟踪当前市场状况
    1. 基本上,市场地位可以是0%到-50%之间的任何值。 0%表示它处于历史最高点,而0到-50%之间的任何数字都是从最近的历史最高点下跌了多少。
  4. 每天,它会根据当前市场状况确定投资组合应拥有的现金头寸。可以是50%到0%之间的任何数字。
  5. 每天重复一次,直到2019年12月31日

所以有50个选项,每个选项有50个选项。

理论是,如果市场下跌。由于贴现,我们应保留较少的现金。因此,如果市场每天都在上涨,那么我们手头有30%左右。如果下降10%,我们可能要保留20%。如果跌幅达到50%,我们可能不愿保留现金,而是将100%的存款推入市场。

最佳策略的最终结果是,其复合年增长率或复合年增长率最高。

我只是在学习编码,对此非常陌生。因此,很有可能我完全搞砸了,并且为我想要的东西做错了整个循环。非常感谢您提供任何帮助。

我首先通过使用此循环来设置14个指标,然后再运行20年的整个脚本,来进行此操作。因此,这20年的每次迭代都有其自己的7种可能的市场地位,并据此持有多少现金。

status = np.arange(0,-0.50,-0.01)
req = np.arange(0.5,-0.01,-1)

    # for every fixed market status,go trough every single cash requirment. 
        # for every single market status,try every single cash req

for first in range(len(req)):
    first_req = round(req[first],2) # this is the 1st element,rounded to 2 digit
    for second in range(first,len(req)):
        second_req = round(req[second],2) # same with 2nd element
        for third in range(second,len(req)):
            third_req = round(req[third],2)
            for fourth in range(third,len(req)):
                fourth_req = round(req[fourth],2)
                for fifth in range(fourth,len(req)):
                    fifth_req = round(req[fifth],2)
                    for sixth in range(fifth,len(req)):
                        sixth_req = round(req[fifth],2)
                        for seventh in range(sixth,len(req)):
                            seventh_req = round(req[sixth],2)
                            for first in range(len(status)):
                                first_status = round(status[first],2) 
                                for second in range(first,len(status)):
                                    second_status = round(status[second],2) 
                                    for third in range(second,len(status)):
                                        third_status = round(status[third],2)
                                        for fourth in range(third,len(status)):
                                            fourth_status = round(status[fourth],2)
                                            for fifth in range(fourth,len(status)):
                                                fifth_status = round(status[fifth],2)
                                                for sixth in range(fifth,len(status)):
                                                    sixth_status = round(status[fifth],2)
                                                    for seventh in range(sixth,len(status)):
                                                        seventh_status = round(status[sixth],2)



                                                        print('')
                                                        print(first_req,second_req,third_req,fourth_req,fifth_req,sixth_req,seventh_req,) # you can replace this with your function
                                                        print(first_status,second_status,third_status,fourth_status,fifth_status,sixth_status,seventh_status,)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)