Esempio n. 1
0
    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
Esempio n. 2
0
    def __init__(self):
        self._name = KOSPI_TICKER
        self._num_of_selected_stocks = 0
        self._genes = []

        for i in range(NUM_OF_STOCKS):
            if random.random() >= 0.5:
                self._genes.append(1)
                self._num_of_selected_stocks +=1
            else:
                self._genes.append(0)
        print("genes: ", self._genes)
        print("num of selected stocks: ", self._num_of_selected_stocks)
        if self._num_of_selected_stocks != 0:
            self._allocated_fund_amount = INITIAL_FUNDS // self._num_of_selected_stocks
        else:
            print("not buying any stocks")
            return
        print("allocated_fund_amount: ", self._allocated_fund_amount)
        self._allocated_fund = fund_standardization.allocated_funds(self._genes, self._allocated_fund_amount, NUM_OF_STOCKS)
        print("allocated fund: ", self._allocated_fund)
        self._remainder_of_pf = fund_standardization.remainder_of_pfs(INITIAL_FUNDS, self._allocated_fund_amount, self._num_of_selected_stocks)
        print("remainder_of_pf: ", self._remainder_of_pf)
        self._day = fund_standardization.days(KOSPI_TICKER, STARTDATE, ENDDATE)
        print("days of trading: ", self._day)
        self._stock_price = fund_standardization.stock_prices(self._genes, KOSPI_TICKER, STARTDATE, ENDDATE)
        print("stock_price: ", self._stock_price)
        self._share = fund_standardization.shares(self._genes, NUM_OF_STOCKS, self._allocated_fund, self._stock_price, FEE_RATE)
        print("share: ", self._share)
        self._handling_fee = fund_standardization.fees(self._genes, NUM_OF_STOCKS, self._share, self._stock_price, FEE_RATE)
        print("handling fee: ", self._handling_fee)
        self._remainder_of_stock = fund_standardization.remainder_of_stocks(self._genes, NUM_OF_STOCKS, self._allocated_fund, self._stock_price, self._share, self._handling_fee)
        print("remainder of stock: ", self._remainder_of_stock)
        self._return = fund_standardization.returns(self._genes, NUM_OF_STOCKS, self._share, self._stock_price)
        print("return: ", self._return)
        self._securities_transaction_tax = fund_standardization.securities_transaction_taxes(self._genes, NUM_OF_STOCKS, self._share, self._stock_price, TAX_RATE)
        print("transaction tax: ", self._securities_transaction_tax)
        self._first_funds_standardization = fund_standardization.funds_standardizations(self._genes, NUM_OF_STOCKS, 1, self._allocated_fund, self._handling_fee, self._return, self._securities_transaction_tax, self._remainder_of_stock)
        print("first funds standardization: ", self._first_funds_standardization)
        self._funds_standardization = fund_standardization.funds_standardizations(self._genes, NUM_OF_STOCKS, self._day, self._allocated_fund, self._handling_fee, self._return, self._securities_transaction_tax, self._remainder_of_stock)
        print("funds standardization: ", self._funds_standardization)
        self._first_pf_funds_standardization = fund_standardization.pf_funds_standardizations(self._genes, self._first_funds_standardization, self._remainder_of_pf)
        print("first pf_funds standardization: ", self._first_pf_funds_standardization)
        self._pf_funds_standardization = fund_standardization.pf_funds_standardizations(self._genes, self._funds_standardization, self._remainder_of_pf)
        print("pf_funds standardization: ", self._pf_funds_standardization)

        print("\n\n=====================================================")
        self._roi = fund_standardization.rois(self._pf_funds_standardization, INITIAL_FUNDS)
        print("roi: ", self._roi)
        self._risk = fund_standardization.risks(self._first_pf_funds_standardization, self._pf_funds_standardization)
        print("risk: ", self._risk)
        self._fitness = fund_standardization.fitnesses(self._roi, self._risk, RISK_FREE_RATE)
        print("fitness/ Sharpe Ratio: ", self._fitness)
        print("return: ", (self._pf_funds_standardization - INITIAL_FUNDS)/INITIAL_FUNDS)
    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)