Search code examples
pythonalgorithmoptimizationparametersgenetic

fitness function for rsi using genetic algorithms


This code is implementing a fitness function for rsi indicators using genetic algorithms but i have no idea what are the output for every function

def strategy_return(trading_signal, asset_return):
    strat_ret = np.array(trading_signal[0:-1]) * np.array(asset_return[1::])
    strat_ret = np.insert(strat_ret, 0, np.nan)
    return strat_ret

def _cumulative_return(ret):
    cum_ret_list = [ret[0]]
    n = ret.shape[0]
    for i in range(1, n):
        cum_ret = (1 + ret[i]) * (1 + cum_ret_list[-1]) - 1
        cum_ret_list.append(cum_ret)
    cum_ret_list.insert(0, np.nan)
    return cum_ret_list

def fit_evaluation(strategy_return):
    strat_cum_ret = _cumulative_return(strat_ret[1::])
    return strat_cum_ret

Solution

  • It would be useful if you could show the complete code, but as far as I can see 'strategy_return' returns an array formed by the products between 'trading_signal' and the array 'asset_return' (without the index 0).

    strat_ret = np.array(trading_signal[0:-1]) * np.array(asset_return[1::])
    

    Then set the index 0 to NaN.

    strat_ret = np.insert(strat_ret, 0, np.nan)
    

    Basically returns an array

    '_cumulative_return' return a list also with index 0 = NaN

     cum_ret_list.insert(0, np.nan)
    

    but again, without the full code is dificult to explain what it for.

    'fit_evaluation' has as an argument 'strategy_return' funtion, wich returns an array, and aply the second funtion with such array but without index 0.

     strat_cum_ret = _cumulative_return(strat_ret[1::])