def create_company_simulation(simulation_id, stock_id): getcontext().prec = 4 mu = 0.02 sigma = 0.1 simulation = Simulation.objects.get(pk=simulation_id) stock = Stock.objects.get(pk=stock_id) ticker = Ticker.objects.get(simulation=simulation) company = TickerCompany(ticker=ticker, stock=stock, symbol=stock.symbol, name="Company %s" % stock.symbol) company.save() rounds = ticker.nb_rounds + 1 brownian_motion = geometric_brownian(rounds, mu, sigma, ticker.initial_value, rounds/(ticker.nb_days*rounds), stock_id) i = 0 simulation_dividends = [] simulation_net_income = [] for sim_round in range(0, rounds): round_dividend = 0 round_net_income = 0 for sim_day in range(1, ticker.nb_days+1): # We have each round/day and the corresponding dividend daily_dividend = brownian_motion[i] daily_net_income = brownian_motion[i] / float(ticker.dividend_payoff_rate) * 100 * stock.quantity c = CompanyFinancial(company=company, daily_dividend=Decimal(daily_dividend), daily_net_income=Decimal(daily_net_income), sim_round=sim_round, sim_day=sim_day, sim_date=sim_round*100+sim_day) c.save() round_dividend += daily_dividend round_net_income += daily_net_income i += 1 simulation_dividends.append(round_dividend) simulation_net_income.append(round_net_income) # Share price estimation G = simulation_dividends[-1]/simulation_dividends[-2]-1 R = 0.15 g = R drift = 0 simulation_stock_price = [] previous_company_income = None for sim_round in range(0, rounds): stock_price = np.npv(0.15, simulation_dividends[sim_round:rounds]) + (simulation_dividends[-1]*(1+g)/(R-G))/np.power(1+R, rounds-sim_round) simulation_stock_price.append(stock_price) if previous_company_income: drift = simulation_net_income[sim_round] / float(previous_company_income.net_income) - 1 previous_company_income.drift = Decimal(drift) previous_company_income.save() company_share = CompanyShare(company=company, share_value=Decimal(stock_price), dividends=Decimal(simulation_dividends[sim_round]), net_income=Decimal(simulation_net_income[sim_round]), drift=Decimal(drift), sim_round=sim_round) company_share.save() previous_company_income = company_share if sim_round == 0: stock.price = Decimal(stock_price) stock.save() return company.id
def create_company_live(simulation_id, stock_id): simulation = Simulation.objects.get(pk=simulation_id) stock = Stock.objects.get(pk=stock_id) ticker = Ticker.objects.get(simulation=simulation) company = TickerCompany(ticker=ticker, stock=stock, symbol=stock.symbol, name="Company %s" % stock.symbol) company.save()