简体   繁体   English

Excel VBA-使用字典并遍历日期

[英]Excel VBA - using dictionary and looping through dates

I have created an excel that looks at historical daily data for a number of stocks and if they meet various requirements, the spreadsheet creates a signal of when to buy and sell the stock. 我创建了一个excel,可以查看许多股票的历史每日数据,如果它们满足各种要求,则电子表格会创建一个信号,指示何时买卖股票。 I have this working and a small example of the output looks like the below (1 is a buy, -1 is a sell): 我有这个工作,输出的一个小例子如下所示(1是买入,-1是卖出):

           AAPL   ABBV   ABMD   GOOG    AMZN
11/07/2015            1     1       
12/07/2015    1                            1
13/07/2015           -1            1      -1
14/07/2015   -1            -1     -1    

I now want to work out a way of allocating money to each of the trades with each stock allocation not being larger than $3 and the total portfolio not being larger than $9, so the allocation to each stock would be "=min(3, 9/n)" where n is the number of stocks in the portfolio. 我现在想设计一种分配资金给每笔交易的方式,每笔股票分配不大于$ 3,总投资组合不超过$ 9,所以分配给每只股票的方式为“ = min(3,9 / n)”,其中n是投资组合中的股票数量。

For example, in the above on 11/07 would allocate $3 to each of ABBV and ABMD. 例如,在上面的11/07中,将为ABBV和ABMD分配$ 3。 Then on 12/07 there are now four stocks in the portfolio so would add $2.25 (=9/4) to each AAPL and AMZN and would need to sell $0.75 of each ABBV and ABMD so the total amount in the portfolio is $9. 然后在12/07,投资组合中现在有四只股票,因此将为每个AAPL和AMZN加$ 2.25(= 9/4),并且需要卖出$ 0.75的每个ABBV和ABMD,所以投资组合中的总金额为$ 9。 On 13/07 would now have two stocks in the portfolio, sell the $2.25 of ABBV and AMZN in the portfolio and buy $3 of GOOG. 在13/07,现在投资组合中将有两只股票,卖出2.25美元的ABBV和AMZN并购买3美元的GOOG。

The number of stocks in the top row will vary. 第一行中的股票数量会有所不同。 I have been researching the best way of approaching this and think using dictionaries may be best, where I assign the stock names as keys. 我一直在研究解决此问题的最佳方法,并认为使用字典可能是最佳方法,在该方法中,我将股票名称指定为关键字。 The values of the keys would then be the money allocated to that stock. 密钥的值将是分配给该股票的钱。 I could then loop through each date to check if a trade needed to be made. 然后,我可以遍历每个日期以检查是否需要进行交易。 I am new to using dictionaries and have only used more basic vba in the past, so just wanted to check if this approach makes sense or if there would be a more simple of way of approaching it? 我是使用字典的新手,过去只使用过更多基本的vba,因此只想检查这种方法是否有意义,或者是否有更简单的方法?

Hopefully it is clear what I am trying to achieve. 希望我已经清楚要实现的目标。 Many thanks! 非常感谢!

I absolutely love dictionaries but they may not be best for you here. 我绝对喜欢词典,但在这里它们可能并不适合您。 Disadvantages: (i) you'd need a new dictionary for each row. 缺点:(i)每行都需要一个新字典。 (ii) how will you persist the data? (ii)您将如何保留数据?

I'd run with the table of data as you have it. 我会尽力处理数据表。 Sure, use a dictionary to store the column position for each stock so that you do not hard code GOOG as being in column E. Then you can as persist the simulation to disk simply by saving the workbook. 当然,使用字典存储每个股票的列位置,这样就不会像在E列中那样硬编码GOOG。然后,只需保存工作簿,就可以将仿真持久化到磁盘上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM