def main(training_file_name, year, month, start_date, end_date): day_list = scraper.days(KOSPI_TICKER, start_date, end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, start_date, end_date) population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, 0, training_file_name, year, month) generation_number = 1 while generation_number <= GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, generation_number, training_file_name, year, month) generation_number += 1 best_chromosome = population.get_chromosomes()[0] print("GA OPTIMIZATION ON KOSPI200 FINANCIAL DATA ON", month, ",", year, '-', training_file_name) print("BEST PF RESULT AFTER GA", best_chromosome) print("Number of stocks selected in the portfolio: ", best_chromosome.get_num_of_selected_stocks()) print("ROI: ", best_chromosome.get_roi(), " | RISK: ", best_chromosome.get_risk()) print("Fittest chromosome fitness:", best_chromosome.get_fitness()) if generation_number == GENERATION_SIZE: monthly_best = [] monthly_best_file_name = "Monthly Best-" + str(month) + "_" + str(year) + ".csv" monthly_best.append([year, month, best_chromosome, best_chromosome.get_fitness(), best_chromosome.get_roi()]) if os.path.exists('../Result/' + str(year) + '/' + monthly_best_file_name): append_write = 'a' # append if already exists else: append_write = 'w' # make a new file if not writer2 = csv.writer(open('../Result/' + str(year) + '/' + monthly_best_file_name, append_write, newline='')) writer2.writerows([monthly_best])
def main(file_name): days = scraper.days(KOSPI_TICKER, STARTDATE, ENDDATE) stock_prices = scraper.stock_prices(days, KOSPI_TICKER, STARTDATE, ENDDATE) print("Find " + file_name + " for the further details.") population = Population(POPULATION_SIZE, days, stock_prices) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, 1, file_name) generation_number = 2 while generation_number <= GENERATION_SIZE + 1: gen_start_time = time.time() population = GeneticAlgorithm.evolve(population, days, stock_prices) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, generation_number, file_name) generation_number += 1 gen_end_time = time.time() print("generation created in ", gen_end_time - gen_start_time, "Seconds.") for count in range(650): print("=", end="") print() print("BEST PF RESULT AFTER GA", population.get_chromosomes()[0]) print("Num of stocks: ", population.get_chromosomes()[0].get_num_of_selected_stocks(), " | ROI: ", population.get_chromosomes()[0].get_roi(), " | RISK: ", population.get_chromosomes()[0].get_risk())
def main(file_name, month, start_date, end_date): day_list = scraper.days(KOSPI_TICKER, start_date, end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, start_date, end_date) # print(month_count, ": Find " + file_name + " for the further details.") population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, 0, file_name, month) generation_number = 1 while generation_number <= GENERATION_SIZE + 1: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, generation_number, file_name, month) generation_number += 1 print("GA OPTIMIZATION ON KOSPI200 FINANCIAL DATA ON", month, ",", YEAR) print("BEST PF RESULT AFTER GA", population.get_chromosomes()[0]) print("Number of stocks selected in the portfolio: ", population.get_chromosomes()[0].get_num_of_selected_stocks(), " | ROI: ", population.get_chromosomes()[0].get_roi(), " | RISK: ", population.get_chromosomes()[0].get_risk()) print("Fittest chromosome fitness:", population.get_chromosomes()[0].get_fitness())
def main(year, month, ga_start_date, ga_end_date, test_start_date, test_end_date, index): initiated_time = time.time() training_file_name = "Kospi_200_portfolio_optimization-" + str( datetime.datetime.now().strftime("%Y%m%d_%H_%M_%S")) + ".csv" day_list = scraper.days(KOSPI_TICKER, ga_start_date, ga_end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, ga_start_date, ga_end_date) test_day_list = scraper.days(KOSPI_TICKER, test_start_date, test_end_date) test_stock_price_list = scraper.stock_prices(test_day_list, KOSPI_TICKER, test_start_date, test_end_date) population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, 0, year, month, training_file_name) generation_number = 1 while generation_number < GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, generation_number, year, month, training_file_name) generation_number += 1 if generation_number == GENERATION_SIZE: completed_time = time.time() print(index + 1, "th GA GENERATION OF PORTFOLIO OPTIMIZATION FOR ", test_start_date, ",", test_end_date, "COMPLETED IN", completed_time - initiated_time, "SECONDS.") best_chromosome = population.get_chromosomes()[0] test_fund.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)
def main(training_file_name, year, month, ga_start_date, ga_end_date, test_start_date, test_end_date): day_list = scraper.days(KOSPI_TICKER, ga_start_date, ga_end_date) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, ga_start_date, ga_end_date) test_day_list = scraper.days(KOSPI_TICKER, test_start_date, test_end_date) test_stock_price_list = scraper.stock_prices(test_day_list, KOSPI_TICKER, test_start_date, test_end_date) population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, 0, training_file_name, year, month) generation_number = 1 while generation_number < GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) save.save_population(population, generation_number, training_file_name, year, month) generation_number += 1 best_chromosome = population.get_chromosomes()[0] save.print_best_population(best_chromosome, year, month, training_file_name) if generation_number == GENERATION_SIZE: monthly_best_file_name = "Kospi_200_portfolio_optimization-Monthly Best.csv" save.save_best_generation(best_chromosome, year, month, monthly_best_file_name) test_fund.test_best_population(KOSPI_TICKER, best_chromosome, test_day_list, test_stock_price_list, year, month, monthly_best_file_name)
def main(file_name): days = scraper.days(KOSPI_TICKER, STARTDATE, ENDDATE) stock_prices = scraper.stock_prices(days, KOSPI_TICKER, STARTDATE, ENDDATE) print("Find " + file_name + " for the further details.") population = Population(POPULATION_SIZE, days, stock_prices) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, 0, file_name) generation_number = 1 while generation_number < GENERATION_SIZE: population = GeneticAlgorithm.evolve(population, days, stock_prices) population.get_chromosomes().sort( key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, generation_number, file_name) generation_number += 1
def main(file_name): day_list = scraper.days(KOSPI_TICKER, START_DATE, END_DATE) stock_price_list = scraper.stock_prices(day_list, KOSPI_TICKER, START_DATE, END_DATE) print("Find " + file_name + " for the further details.") population = Population(POPULATION_SIZE, day_list, stock_price_list) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, 0, file_name) generation_number = 1 while generation_number <= GENERATION_SIZE+1: population = GeneticAlgorithm.evolve(population, day_list, stock_price_list, POPULATION_SIZE) population.get_chromosomes().sort(key=lambda chromosome: chromosome.get_fitness(), reverse=True) _print_population(population, generation_number, file_name) generation_number += 1 print("BEST PF RESULT AFTER GA", population.get_chromosomes()[0]) print("Number of stocks selected in the portfolio: ", population.get_chromosomes()[0].get_num_of_selected_stocks(), " | ROI: ", population.get_chromosomes()[0].get_roi(), " | RISK: ", population.get_chromosomes()[0].get_risk()) print("Fittest chromosome fitness:", population.get_chromosomes()[0].get_fitness()) for count in range(650): print("=", end="") print()