![]() ![]() VE.Bus Quick Configure (v9010153) - For installations up to 3 Multis. ![]() Proportion_of_new_shares = qtd / current_sharesĪdjusted_price = weighted_average(cost_per_share, adjusted_price, proportion_of_new_shares) """Take an average of a and b, with a weighted by a_weight"""ĭef get_adjusted_price_for_ticker(single_ticker_df):įor _, row in single_ticker_df.iterrows():Ĭost_per_share = (qtd * row + row) / qtd Then, it adds that list as a column in the dataframe. Then, it loops over all the rows in that group, and calculates a weighted average of the price to get the current shares, and the previous price. The problem is that calculating adjusted price depends on the previous value of adjusted price, which prevents the use of vectorization or shift().įirst step is to use a groupby, to separate it by ticker symbol. I couldn't figure out a nice, simple solution to this. groupby("ticker").apply() a function using shift() in order to use prior values from rows above, but I've failed. I did all these calculus in excel and the output should be the following:įor further clarification, the excel formula for that selected cell was: ![]() To be clear, when the transaction is a "sell", we must ignore adjusting the price and use the last adjusted price in the last buy type transaction for that particular stock. This would work perfectly if there is no "sell" between "buy"s (as it did for the stock EFGH in this example). Sample_df.fillna(method='ffill', inplace=True) I could partially correct the adjusted prices on sell rows with the following: # When it's a "sell", adjusted price is the same from above Sample_df = sample_df.groupby('ticker').cumsum().div(sample_df) Sample_df = sample_df.apply(lambda x: ((x.Sell_buy = "buy") - (x.Sell_buy = "sell")) * (x * x + x), axis = 1) Works good until I reach the row where a sell was made I've tried the following to achieve this goal: #Adjust Price. The condition here is if I sell a stock, my Adjusted Price should not change and remain the same as the last adjusted price when the buy was made for that stock. However, I am not able to get the correct Adjusted Price. Sample_df = sample_df.groupby('ticker').cumsum() ![]() Sample_df = sample_df.apply(lambda x: ((x.Sell_buy = "buy") - (x.Sell_buy = "sell")) * x, axis = 1) I was able to get the get an updated Adjusted quantity (based on buys and sells) without any issues: Sample_df=pd.to_datetime(sample_df, dayfirst=True).dt.date I am trying to adjust my portfolio of stocks and trying to calculate the adjusted mean price (which is a form of weighted-average).Ģ: 'ABCD', 3: 'ABCD', 4: 'ABCD', 5: 'ABCD', 6: 'ABCD', 8: 'EFGH',ĩ: 'EFGH', 10: 'EFGH', 11: 'EFGH', 12: 'EFGH', 13: 'EFGH'}, ![]()
0 Comments
Leave a Reply. |