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)