def get_fitness(self): self._num_of_selected_stocks = fund_standardization.num_of_selected_stocks( self._genes, self._num_of_stocks) self._allocated_fund_amount = 0 if self._num_of_selected_stocks != 0: self._allocated_fund_amount = self._initial_funds // self._num_of_selected_stocks self._remainder_of_pf = 0 else: self._allocated_fund_amount = 0 self._remainder_of_pf = self._initial_funds self._allocated_fund = fund_standardization.allocated_funds( self._genes, self._allocated_fund_amount, self._num_of_stocks) # print("_allocated_fund", _allocated_fund) self._remainder_of_pf = fund_standardization.remainder_of_pfs( self._initial_funds, self._allocated_fund_amount, self._num_of_selected_stocks) # print("self._days", self._days) # print("_stock_price", _stock_price) self._share = fund_standardization.shares(self._genes, self._num_of_stocks, self._allocated_fund, self._stock_prices, FEE_RATE) # print("_share", _share) self._handling_fee = fund_standardization.handling_fees( self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices, FEE_RATE) # print("_handling_fee", _handling_fee) self._remainder_of_stock = fund_standardization.remainder_of_stocks( self._genes, self._num_of_stocks, self._allocated_fund, self._stock_prices, self._share, self._handling_fee) # print("_remainder_of_stock", _remainder_of_stock) self._return = fund_standardization.returns(self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices) # print("_return", _return) self._securities_transaction_tax = fund_standardization.securities_transaction_taxes( self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices, TAX_RATE) # print("_securities_transaction_tax", _securities_transaction_tax) self._funds_standardization = fund_standardization.funds_standardizations( self._genes, self._num_of_stocks, self._days, self._allocated_fund, self._handling_fee, self._return, self._securities_transaction_tax, self._remainder_of_stock) # print("_funds_standardization", _funds_standardization) self._pf_funds_standardization = fund_standardization.pf_funds_standardizations( self._days, self._num_of_stocks, self._funds_standardization, self._remainder_of_pf) # print("_pf_funds_standardization", _pf_funds_standardization) self._roi = fund_standardization.rois(self._pf_funds_standardization, self._initial_funds) # print("_roi", _roi) self._risk = fund_standardization.risks(self._days, self._pf_funds_standardization) # print("_risk", _risk) self._fitness = fund_standardization.fitnesses(self._roi, self._risk, RISK_FREE_RATE) # print("Fitness: ", self._fitness) return self._fitness
def get_fitness(self): _num_of_selected_stocks = fund_standardization.num_of_selected_stocks( self._genes, NUM_OF_STOCKS) _allocated_fund_amount = 0 if _num_of_selected_stocks != 0: _allocated_fund_amount = INITIAL_FUNDS // _num_of_selected_stocks else: # print("NO STOCK HAS BEEN SELECTED") _fitness = 0.0 _allocated_fund_amount = 0 _remainder_of_pf = INITIAL_FUNDS _allocated_fund = fund_standardization.allocated_funds( self._genes, _allocated_fund_amount, NUM_OF_STOCKS) print("_allocated_fund", _allocated_fund) _remainder_of_pf = fund_standardization.remainder_of_pfs( INITIAL_FUNDS, _allocated_fund_amount, _num_of_selected_stocks) print("_remainder_of_pf", _remainder_of_pf) _day = fund_standardization.days(KOSPI_TICKER, STARTDATE, ENDDATE) print("_day", _day) _stock_price = fund_standardization.stock_prices( self._genes, _day, KOSPI_TICKER, STARTDATE, ENDDATE) print("_stock_price", _stock_price) _share = fund_standardization.shares(self._genes, NUM_OF_STOCKS, _allocated_fund, _stock_price, FEE_RATE) print("_share", _share) _handling_fee = fund_standardization.handling_fees( self._genes, _day, NUM_OF_STOCKS, _share, _stock_price, FEE_RATE) print("_handling_fee", _handling_fee) _remainder_of_stock = fund_standardization.remainder_of_stocks( self._genes, NUM_OF_STOCKS, _allocated_fund, _stock_price, _share, _handling_fee) print("_remainder_of_stock", _remainder_of_stock) _return = fund_standardization.returns(self._genes, _day, NUM_OF_STOCKS, _share, _stock_price) print("_return", _return) _securities_transaction_tax = fund_standardization.securities_transaction_taxes( self._genes, _day, NUM_OF_STOCKS, _share, _stock_price, TAX_RATE) print("_securities_transaction_tax", _securities_transaction_tax) _funds_standardization = fund_standardization.funds_standardizations( self._genes, KOSPI_TICKER, NUM_OF_STOCKS, _day, _allocated_fund, _handling_fee, _return, _securities_transaction_tax, _remainder_of_stock) print("_funds_standardization", _funds_standardization) _pf_funds_standardization = fund_standardization.pf_funds_standardizations( self._genes, _day, NUM_OF_STOCKS, _funds_standardization, _remainder_of_pf) print("_pf_funds_standardization", _pf_funds_standardization) _roi = fund_standardization.rois(_pf_funds_standardization, INITIAL_FUNDS, _day) print("_roi", _roi) _risk = fund_standardization.risks(_day, _pf_funds_standardization) print("_risk", _risk) _fitness = fund_standardization.fitnesses(_roi, _risk, RISK_FREE_RATE) print("_fitness: ", _fitness) return _fitness
def test_best_population(kospi_ticker, best_chromosome, test_day_list, test_stock_price_list, year, month, monthly_best_file_name): # with open('../result/' + str(year) + "/" + str(month) + '/' + monthly_best_file_name, 'r') as in_file: # reader = csv.reader(in_file, delimiter=",") # for price in reader: # print(price) test_genes = best_chromosome.get_genes() # print("test_genes", test_genes) test_days = test_day_list # print("test_days", test_days) test_stock_prices = test_stock_price_list # print("test_stock_prices", test_stock_prices) test_num_of_stocks = best_chromosome.get_num_of_stocks() # print("test_num_of_stocks", test_num_of_stocks) test_num_of_stocks = len(kospi_ticker) test_num_of_selected_stocks = fund_standardization.num_of_selected_stocks(test_genes, test_num_of_stocks) test_allocated_fund_amount = 0 test_remainder_of_pf = 0 if test_num_of_selected_stocks != 0: test_allocated_fund_amount = INITIAL_FUNDS // test_num_of_selected_stocks test_remainder_of_pf = 0 else: test_allocated_fund_amount = 0 test_remainder_of_pf = INITIAL_FUNDS test_allocated_fund = fund_standardization.allocated_funds(test_genes, test_allocated_fund_amount, test_num_of_stocks) # print("_allocated_fund", test_allocated_fund ) test_remainder_of_pf = fund_standardization.remainder_of_pfs(INITIAL_FUNDS, test_allocated_fund_amount, test_num_of_selected_stocks) # print("test_days", test_days) # print("_stock_price", test_stock_prices) test_share = fund_standardization.shares(test_genes, test_num_of_stocks, test_allocated_fund, test_stock_prices, FEE_RATE) # print("_share", test_share) test_handling_fee = fund_standardization.handling_fees(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices, FEE_RATE) # print("_handling_fee", test_handling_fee) test_remainder_of_stock = fund_standardization.remainder_of_stocks(test_genes, test_num_of_stocks, test_allocated_fund, test_stock_prices, test_share, test_handling_fee) # print("_remainder_of_stock", test_remainder_of_stock) test_return = fund_standardization.returns(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices) # print("_return", test_return) test_securities_transaction_tax = fund_standardization.securities_transaction_taxes(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices, TAX_RATE) # print("_securities_transaction_tax", test_securities_transaction_tax) test_funds_standardization = fund_standardization.funds_standardizations(test_genes, test_num_of_stocks, test_days, test_allocated_fund, test_handling_fee, test_return, test_securities_transaction_tax, test_remainder_of_stock) # print("_funds_standardization", test_funds_standardization) test_pf_funds_standardization = fund_standardization.pf_funds_standardizations(test_days, test_num_of_stocks, test_funds_standardization, test_remainder_of_pf) # print("_pf_funds_standardization", test_pf_funds_standardization ) test_roi = fund_standardization.rois(test_pf_funds_standardization, INITIAL_FUNDS) test_risk = fund_standardization.risks(test_days, test_pf_funds_standardization) test_fitness = fund_standardization.fitnesses(test_roi, test_risk, RISK_FREE_RATE) for count in range(650): print("-", end="") print() print("TEST_roi", test_roi, "TEST_risk", test_risk) print("TEST_Fitness: ", test_fitness) for count in range(650): print("-", end="") print()
def get_fitness(self): _num_of_stocks = len(KOSPI_TICKER) _num_of_selected_stocks = fund_standardization.num_of_selected_stocks(self._genes, _num_of_stocks) self._num_of_selected_stocks = _num_of_selected_stocks _allocated_fund_amount = 0 if _num_of_selected_stocks != 0: _allocated_fund_amount = INITIAL_FUNDS // _num_of_selected_stocks else: _fitness = 0.0 _allocated_fund_amount = 0 _remainder_of_pf = INITIAL_FUNDS _allocated_fund = fund_standardization.allocated_funds(self._genes, _allocated_fund_amount, _num_of_stocks) self._allocated_fund = _allocated_fund # print("_allocated_fund", _allocated_fund) _remainder_of_pf = fund_standardization.remainder_of_pfs(INITIAL_FUNDS, _allocated_fund_amount, _num_of_selected_stocks) # print("_remainder_of_pf", _remainder_of_pf) self._remainder_of_pf = _remainder_of_pf _day = self._days # print("_day", _day) _stock_price = self.stock_prices # print("_stock_price", _stock_price) _share = fund_standardization.shares(self._genes, _num_of_stocks, _allocated_fund, _stock_price, FEE_RATE) self._share = _share # print("_share", _share) _handling_fee = fund_standardization.handling_fees(self._genes, _day, _num_of_stocks, _share, _stock_price, FEE_RATE) self._handling_fee = _handling_fee # print("_handling_fee", _handling_fee) _remainder_of_stock = fund_standardization.remainder_of_stocks(self._genes, _num_of_stocks, _allocated_fund, _stock_price, _share, _handling_fee) self.__remainder_of_stock = _remainder_of_stock # print("_remainder_of_stock", _remainder_of_stock) _return = fund_standardization.returns(self._genes, _day, _num_of_stocks, _share, _stock_price) self._return = _return # print("_return", _return) _securities_transaction_tax = fund_standardization.securities_transaction_taxes(self._genes, _day, _num_of_stocks, _share, _stock_price, TAX_RATE) self._securities_transaction_tax = _securities_transaction_tax # print("_securities_transaction_tax", _securities_transaction_tax) _funds_standardization = fund_standardization.funds_standardizations(self._genes, _num_of_stocks, _day, _allocated_fund, _handling_fee, _return, _securities_transaction_tax, _remainder_of_stock) self._funds_standardization = _funds_standardization # print("_funds_standardization", _funds_standardization) _pf_funds_standardization = fund_standardization.pf_funds_standardizations(_day, _num_of_stocks, _funds_standardization, _remainder_of_pf) self._pf_funds_standardization = _pf_funds_standardization # print("_pf_funds_standardization", _pf_funds_standardization) _roi = fund_standardization.rois(_pf_funds_standardization, INITIAL_FUNDS, _day) self._roi = _roi # print("_roi", _roi) _risk = fund_standardization.risks(_day, _pf_funds_standardization) self._risk = _risk # print("_risk", _risk) _fitness = fund_standardization.fitnesses(_roi, _risk, RISK_FREE_RATE) self._fitness = _fitness # print("Fitness: ", self._fitness) return _fitness
def __init__(self): self._genes = [] for i in range(NUM_OF_STOCKS): if random.random() <= STOCK_SELCTION_RATE: self._genes.append(1) else: self._genes.append(0) self._fitness = 0 _num_of_selected_stocks = fund_standardization.num_of_selected_stocks( self._genes, NUM_OF_STOCKS) if _num_of_selected_stocks != 0: _allocated_fund_amount = INITIAL_FUNDS // _num_of_selected_stocks else: print("NO STOCK HAS BEEN SELECTED") _fitness = 0.0 _allocated_fund_amount = 0 _remainder_of_pf = INITIAL_FUNDS _allocated_fund = fund_standardization.allocated_funds( self._genes, _allocated_fund_amount, NUM_OF_STOCKS) _remainder_of_pf = fund_standardization.remainder_of_pfs( INITIAL_FUNDS, _allocated_fund_amount, _num_of_selected_stocks) _day = fund_standardization.days(KOSPI_TICKER, STARTDATE, ENDDATE) _stock_price = fund_standardization.stock_prices( self._genes, _day, KOSPI_TICKER) _share = fund_standardization.shares(self._genes, NUM_OF_STOCKS, _allocated_fund, _stock_price, FEE_RATE) _handling_fee = fund_standardization.handling_fees( self._genes, _day, NUM_OF_STOCKS, _share, _stock_price, FEE_RATE) _remainder_of_stock = fund_standardization.remainder_of_stocks( self._genes, NUM_OF_STOCKS, _allocated_fund, _stock_price, _share, _handling_fee) _return = fund_standardization.returns(self._genes, _day, NUM_OF_STOCKS, _share, _stock_price) _securities_transaction_tax = fund_standardization.securities_transaction_taxes( self._genes, _day, NUM_OF_STOCKS, _share, _stock_price, TAX_RATE) _funds_standardization = fund_standardization.funds_standardizations( self._genes, KOSPI_TICKER, NUM_OF_STOCKS, _day, _allocated_fund, _handling_fee, _return, _securities_transaction_tax, _remainder_of_stock) _pf_funds_standardization = fund_standardization.pf_funds_standardizations( self._genes, _day, NUM_OF_STOCKS, _funds_standardization, _remainder_of_pf) _roi = fund_standardization.rois(_pf_funds_standardization, INITIAL_FUNDS, _day) _risk = fund_standardization.risks(_day, _pf_funds_standardization) self._fitness = fund_standardization.fitnesses(_roi, _risk, RISK_FREE_RATE)
def test_best_population(best_chromosome, test_day_list, test_stock_price_list, year, month, ga_start_date, ga_end_date, test_start_date, test_end_date): test_genes = best_chromosome.get_genes() # print("test_genes", test_genes) test_days = test_day_list # print("test_days", test_days) test_stock_prices = test_stock_price_list # print("test_stock_prices", test_stock_prices) test_num_of_stocks = best_chromosome.get_num_of_stocks() # print("test_num_of_stocks", test_num_of_stocks) test_num_of_selected_stocks = fund_standardization.num_of_selected_stocks(test_genes, test_num_of_stocks) test_allocated_fund_amount = 0 test_remainder_of_pf = 0 if test_num_of_selected_stocks != 0: test_allocated_fund_amount = INITIAL_FUNDS // test_num_of_selected_stocks test_remainder_of_pf = 0 else: test_allocated_fund_amount = 0 test_remainder_of_pf = INITIAL_FUNDS test_allocated_fund = fund_standardization.allocated_funds(test_genes, test_allocated_fund_amount, test_num_of_stocks) # print("_allocated_fund", test_allocated_fund ) test_remainder_of_pf = fund_standardization.remainder_of_pfs(INITIAL_FUNDS, test_allocated_fund_amount, test_num_of_selected_stocks) # print("test_days", test_days) # print("_stock_price", test_stock_prices) test_share = fund_standardization.shares(test_genes, test_num_of_stocks, test_allocated_fund, test_stock_prices, FEE_RATE) # print("_share", test_share) test_handling_fee = fund_standardization.handling_fees(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices, FEE_RATE) # print("_handling_fee", test_handling_fee) test_remainder_of_stock = fund_standardization.remainder_of_stocks(test_genes, test_num_of_stocks, test_allocated_fund, test_stock_prices, test_share, test_handling_fee) # print("_remainder_of_stock", test_remainder_of_stock) test_return = fund_standardization.returns(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices) # print("_return", test_return) test_securities_transaction_tax = fund_standardization.securities_transaction_taxes(test_genes, test_days, test_num_of_stocks, test_share, test_stock_prices, TAX_RATE) # print("_securities_transaction_tax", test_securities_transaction_tax) test_funds_standardization = fund_standardization.funds_standardizations(test_genes, test_num_of_stocks, test_days, test_allocated_fund, test_handling_fee, test_return, test_securities_transaction_tax, test_remainder_of_stock) # print("_funds_standardization", test_funds_standardization) test_pf_funds_standardization = fund_standardization.pf_funds_standardizations(test_days, test_num_of_stocks, test_funds_standardization, test_remainder_of_pf) # print("_pf_funds_standardization", test_pf_funds_standardization ) test_roi = fund_standardization.rois(test_pf_funds_standardization, INITIAL_FUNDS) test_risk = fund_standardization.risks(test_days, test_pf_funds_standardization) test_fitness = fund_standardization.fitnesses(test_roi, test_risk, RISK_FREE_RATE) save.print_test_population(best_chromosome, ga_start_date, ga_end_date, test_start_date, test_end_date, test_genes, test_num_of_selected_stocks, test_roi, test_risk, test_fitness) save.save_test_generation(best_chromosome, year, month, ga_start_date, ga_end_date, test_start_date, test_end_date, test_genes, test_roi, test_risk, test_fitness)
def __init__(self, days, stock_prices): self._genes = [] for i in range(len(KOSPI_TICKER)): if random.random() <= INITIAL_STOCK_SELCTION_RATE: self._genes.append(1) else: self._genes.append(0) self._days = days self._stock_prices = stock_prices self._initial_funds = INITIAL_FUNDS self._fee_rate = FEE_RATE self._tax_rate = TAX_RATE self._risk_free_rate = RISK_FREE_RATE self._ticker = KOSPI_TICKER self._num_of_stocks = len(KOSPI_TICKER) self._num_of_selected_stocks = fund_standardization.num_of_selected_stocks( self._genes, self._num_of_stocks) self._allocated_fund_amount = 0 if self._num_of_selected_stocks != 0: self._allocated_fund_amount = self._initial_funds // self._num_of_selected_stocks self._remainder_of_pf = 0 else: self._allocated_fund_amount = 0 self._remainder_of_pf = self._initial_funds self._allocated_fund = fund_standardization.allocated_funds( self._genes, self._allocated_fund_amount, self._num_of_stocks) # print("_allocated_fund", _allocated_fund) self._remainder_of_pf = fund_standardization.remainder_of_pfs( self._initial_funds, self._allocated_fund_amount, self._num_of_selected_stocks) # print("self._days", self._days) # print("_stock_price", _stock_price) self._share = fund_standardization.shares(self._genes, self._num_of_stocks, self._allocated_fund, self._stock_prices, FEE_RATE) # print("_share", _share) self._handling_fee = fund_standardization.handling_fees( self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices, FEE_RATE) # print("_handling_fee", _handling_fee) self._remainder_of_stock = fund_standardization.remainder_of_stocks( self._genes, self._num_of_stocks, self._allocated_fund, self._stock_prices, self._share, self._handling_fee) # print("_remainder_of_stock", _remainder_of_stock) self._return = fund_standardization.returns(self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices) # print("_return", _return) self._securities_transaction_tax = fund_standardization.securities_transaction_taxes( self._genes, self._days, self._num_of_stocks, self._share, self._stock_prices, TAX_RATE) # print("_securities_transaction_tax", _securities_transaction_tax) self._funds_standardization = fund_standardization.funds_standardizations( self._genes, self._num_of_stocks, self._days, self._allocated_fund, self._handling_fee, self._return, self._securities_transaction_tax, self._remainder_of_stock) # print("_funds_standardization", _funds_standardization) self._pf_funds_standardization = fund_standardization.pf_funds_standardizations( self._days, self._num_of_stocks, self._funds_standardization, self._remainder_of_pf) # print("_pf_funds_standardization", _pf_funds_standardization) self._roi = fund_standardization.rois(self._pf_funds_standardization, self._initial_funds) # print("_roi", _roi) self._risk = fund_standardization.risks(self._days, self._pf_funds_standardization) # print("_risk", _risk) self._fitness = fund_standardization.fitnesses(self._roi, self._risk, RISK_FREE_RATE)