fxmanager.optimization package

Package contents

This sub-package contains the mudules used for portfolio construction and optimization.

Modules:
  • eq_weight_optimizer : This module contains helper functions used internally in this module and other modules in fxmanager’s optimization sub-package.

  • eqw_optimization_test : This module contains a function that rans an equally weighted optimization test on historical data.

  • weight_optimizer : This module contains a function that rans a weighted optimization test on historical data.

  • w_optimization_test : This module contains helper functions used internally in this module and other modules in fxmanager’s optimization sub-package.

fxmanager.optimization.eq_weight_optimizer module

This module contains helper functions used internally in this module and other modules in fxmanager’s optimization sub-package.

the functions defined in this module are designed for constructing optimal equally weighted portfolios. a portfolio consists of one or more assets where an asset in this context refers to a currency pair being traded in a specefic time frame. for example, if the currency pair ‘EURUSD’ is being traded in two different time frames, 5 and 10 minutes, where a new position is opened every 5 or 10 minutes, in this case the ‘EURUSD_5min’ and ‘EURUSD_10min’ are two different assets.

Public Functions:
  • optimize(): runs equally weighted portfolio optimization using data from a single day given an optimization method and objective.

    ** this function is designed to be used internally in ‘fxmanager.optimization.eqw_optimization_test.run()’ function. However, it can be used by the user separately to construct a portfolio for a given day without running the optimization test.

fxmanager.optimization.eq_weight_optimizer.mixed_timframes_portfolio(bestN, wrstN, objective='min_win_rate', print_progress=False)

Helper function for ‘fxmanager.optimization.eq_weight_optimizer.optimize()’ function.

selects the optimal currency pair from each timeframe in the given data, then selects the optimal equally weighted combination of these timeframes.

fxmanager.optimization.eq_weight_optimizer.optimize(best_rets_df, wrst_rets_df, method='mixed_timeframes', objective='min_win_rate', test_run=True)

runs equally weighted portfolio optimization using data from a single day given an optimization method and objective.

Args:
  • best_rets_df: pandas dataframe of average best return for each asset, columns are time_frames and index is a range index.

  • wrst_rets_df: pandas dataframe of average worst return for each asset, columns are time_frames and index is a range index.

  • method: string with optimization method to be used. The supported optimization methods are:
    • mixed_timeframes: selects the optimal currency pair from each available timeframe, then selects the optimal equally weighted combination of these time frames.

    • single_timeframe: selects the optimal time frame from the available timeframes, then selects the optimal equally weighted combination of currency pairs in the selected time frame.

  • objective: string with optimization objective to be used. The supported optimization objectives are:
    • min_win_rate : minimizes the required win rate for the portfolio to acheive a positive returns.

    • min_risk_reward: minimizes the required risk reward ratio required for the portfolio to acheive a positive returns.

  • test_run: boolean flag used for testing purposes. If true, the constructed portfolios will consist of only one asset.

Returns:
  • optimized_portfolio_idxs : tuple with length equal to ‘optimized_n’ and values of integers that repesent the indices corresponding to the optimal currency pairs.

  • optimized_portfolio_tfs : numpy array with length of ‘optimized_n’ and values of optimal timeframes.

  • optimized_portfolio_weights: numpy array with length equal to ‘optimized_n’ and values of equal floats that sum up to 1.

  • optimized_n : integer indicating the number of assets in the constructed portfolio.

  • optimized_W_R : float indicating the minimum required win rate for the portfolio to acheive positive return.

  • optimized_R_R : float indicating the minimum required risk reward for the portfolio to acheive positive return.

** this function is designed to be used internally in ‘fxmanager.optimization.eqw_optimization_test.run()’ function. However, it can be used by the user separately to construct a portfolio for a given day without running the optimization test.

fxmanager.optimization.eq_weight_optimizer.single_timeframe_portfolio(best_tf, wrst_tf, num_assets, objective='min_win_rate', print_progress=False)

Helper function for ‘fxmanager.optimization.eq_weight_optimizer.optimize()’ function.

selects the optimal equally weighted combination of currency pairs in the given timeframe data.

fxmanager.optimization.eqw_optimization_test module

This module contains a function that rans an equally weighted optimization test on historical data.

Functions:
  • run_test(): Runs an optimization test on historical data given an ‘optimization_method’ and ‘optimization_objective’.

fxmanager.optimization.eqw_optimization_test.run_test(data_dir=None, optimization_method='', optimization_objective='', scenarios_per_day=100, risk_rewards=[], leverage=0.01, test_run=False, save_plots=False, save_logs=False)

Runs an optimization test given an ‘optimization_method’ and ‘optimization_objective’.

The test uses preprocessed historical data to construct a portfolio using data from day (n) and then we use this portfolio to calculate average expected returns in day (n+1). The expected returns are calculated for every single element in ‘risk_rewards’ list and then averaged over ‘scenarios_per_day’ every day.

Args:
  • data_dir : string with the directory or full path to the directory in which application data is kept. If the setup() function is used to create the recommended project structure, the default None value should be used.

  • optimization_methodstring with optimization method to be used. The supported optimization methods are:
    • mixed_timeframes : selects the optimal currency pair from each available timeframe, then selects the optimal equally weighted combination of these time frames.

    • single_timeframe : selects the optimal time frame from the available timeframes, then selects the optimal equally weighted combination of currency pairs in the selected time frame.

  • optimization_objective: string with optimization objective to be used. The supported optimization objectives are:
    • min_win_rate : minimizes the required win rate for the portfolio to acheive a positive returns.

    • min_risk_reward : minimizes the required risk reward ratio required for the portfolio to acheive a positive returns.

  • scenarios_per_day : integer indicating the number of scenarios over which the expected return is averaged every day.

  • risk_rewards : list of floats. the average expected return is calculated for each value in this list every day.

  • leverage : float indicating the leverage. if no leverage is used set to 1.

  • test_run : boolean flag used for testing purposes. If true, the constructed portfolios will consist of only one asset.

  • save_plots : boolean flag. If True, the program will save result plots to ‘data_dirvisualizations' directory.

  • save_logs : boolean flag. If True, the program will save result plots to ‘data_dirlogsequally_weighted_optimizer_logs.txt’ file.

Returns:
  • None

Saved Results: test results are saved to ‘data_dirvisualizations’ for plots and ‘data_dirstats’ for csv files.

  • Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png : image with 2 sub-plots sharing the X-axis for the offsets, and Y axis for win probabilites and average expected returns. Offsets are linearly separated floats added to the ‘min_win_rate’ argument, starting from (0.01, 0.02, …), the value of ‘min_win_rate’ is incremented until the win probabilty becomes greater than or equal to 1. the plot might also contain several lines per plot, each line represents calculations a single risk-reward ratio.

  • Optimizer_Test_Required_Win_Rates.png: image with a plot of constructed portfolios win rates on the Y-axis vs days on the X-axis.

  • Optimizer_Test_Risk _Reward_Ratios.png: image with a plot of constructed portfolios risk rewards on the Y-axis vs days on the X-axis.

  • Optimizer_Test_portfolios.csv : csv file with the constructed portfolios for every day. the index starts from 1 because every row represents the portfolio created using data from the day before so day 0 does not count.

  • Optimizer_Test_win_probs.csv : csv file with the data frame used for plotting the first sub-plot in ‘Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png’

  • Optimizer_Test_expexted_returns.csv : csv file with the data frame used for plotting the second sub-plot in ‘Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png’

fxmanager.optimization.w_optimization_test module

This module contains a function that rans a weighted optimization test on historical data.

Functions:
  • run_test(): Runs an optimization test on historical data given an ‘optimization_method’ and ‘optimization_objective’.

fxmanager.optimization.w_optimization_test.run_test(data_dir=None, optimization_method='', optimization_objective='', scenarios_per_day=100, risk_rewards=[], leverage=0.01, test_run=False, save_plots=False, save_logs=False)

Runs an optimization test given an ‘optimization_method’ and ‘optimization_objective’.

The test uses preprocessed historical data to construct a portfolio using data from day (n) and then we use this portfolio to calculate average expected returns in day (n+1). The expected returns are calculated for every single element in ‘risk_rewards’ list and then averaged over ‘scenarios_per_day’ every day.

Args:
  • data_dir : string with the directory or full path to the directory in which application data is kept. If the setup() function is used to create the recommended project structure, the default None value should be used.

  • optimization_methodstring with optimization method to be used. The supported optimization methods are:
    • mixed_timeframes : selects the optimal currency pair from each available timeframe, then selects the optimal combination of these time frames and the optimal weight for each time frame.

    • single_timeframe : selects the optimal time frame from the available timeframes, then selects the optimal combination of currency pairs in the selected time frame and the optimal weight for each currency pair.

  • optimization_objective: string with optimization objective to be used. The supported optimization objectives are:
    • min_win_rate : minimizes the required win rate for the portfolio to acheive a positive returns.

    • min_risk_reward : minimizes the required risk reward ratio required for the portfolio to acheive a positive returns.

  • scenarios_per_day : integer indicating the number of scenarios over which the expected return is averaged every day.

  • risk_rewards : list of integers. the average expected return is calculated for every integer in this list every day.

  • leverage : float indicating the leverage. if no leverage is used set to 1.

  • test_run : boolean flag used for testing purposes. If true, the constructed portfolios will consist of only one asset.

  • save_plots : boolean flag. If True, the program will save result plots to ‘data_dirvisualizations' directory.

  • save_logs : boolean flag. If True, the program will save result plots to ‘data_dirlogsequally_weighted_optimizer_logs.txt’ file.

Returns:
  • None

Saved Results: test results are saved to ‘data_dirvisualizations’ for plots and ‘data_dirstats’ for csv files.

  • Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png : image with 2 sub-plots sharing the X-axis for the offsets, and Y axis for win probabilites and average expected returns. Offsets are linearly separated floats added to the ‘min_win_rate’ argument, starting from (0.01, 0.02, …), the value of ‘min_win_rate’ is incremented until the win probabilty becomes greater than or equal to 1. The plot might also contain several lines per plot, each line represents calculations a single risk-reward ratio.

  • Optimizer_Test_Required_Win_Rates.png : image with a plot of constructed portfolios win rates on the Y-axis vs days on the X-axis.

  • Optimizer_Test_Risk _Reward_Ratios.png : image with a plot of constructed portfolios risk rewards on the Y-axis vs days on the X-axis.

  • Optimizer_Test_portfolios.csv : csv file with the constructed portfolios for every day. the index starts from 1 because every row represents the portfolio created using data from the day before and tested on the current day.

  • Optimizer_Test_win_probs.csv : csv file with the data frame used for plotting the first sub-plot in ‘Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png’

  • Optimizer_Test_expexted_returns.csv : csv file with the data frame used for plotting the second sub-plot in ‘Optimizer_Test_Win_Probs_&_Expexted_Rets_vs_Offsets.png’

fxmanager.optimization.weight_optimizer module

This module contains helper functions used internally in this module and other modules in fxmanager’s optimization sub-package.

the functions defined in this module are designed for constructing optimal weighted portfolios. a portfolio consists of one or more assets where an asset in this context refers to a currency pair being traded in a specefic time frame. for example, if the currency pair ‘EURUSD’ is being traded in two different time frames, 5 and 10 minutes, where a new position is opened every 5 or 10 minutes, in this case the ‘EURUSD_5min’ and ‘EURUSD_10min’ are two different assets.

Public Functions:
  • optimize(): runs weighted portfolio optimization using data from a single day given an optimization method and objective.

    ** this function is designed to be used internally in ‘fxmanager.optimization.eqw_optimization_test.run()’ function. However, it can be used by the user separately to construct a portfolio for a given day without running the optimization test.

fxmanager.optimization.weight_optimizer.mixed_timeframes_portfolio(bestN, wrstN, data_dir, objective='min_win_rate', print_progress=False)

Helper function for ‘fxmanager.optimization.weight_optimizer.optimize()’ function.

selects the optimal currency pair from each timeframe in the given data, then selects the optimal weighted combination of these timeframes.

fxmanager.optimization.weight_optimizer.optimize(best_rets_df, wrst_rets_df, data_dir=None, method='mixed_timeframes', objective='min_win_rate', test_run=True)

runs weighted portfolio optimization using data from a single day given an optimization method and objective.

Args:
  • best_rets_df: pandas dataframe of average best return for each asset, columns are time_frames and index is a range index.

  • wrst_rets_df: pandas dataframe of average worst return for each asset, columns are time_frames and index is a range index.

  • data_dirstring with the directory name or full path to the directory in which application data is kept. If the setup()

    function is used to create the recommended project structure, the default None value should be used.

  • method: string with optimization method to be used. The supported optimization methods are:
    • mixed_timeframes: selects the optimal currency pair from each available timeframe, then selects the optimal equally weighted

      combination of these time frames.

    • single_timeframe: selects the optimal time frame from the available timeframes, then selects the optimal equally weighted

      combination of currency pairs in the selected time frame.

  • objective: string with optimization objective to be used. The supported optimization objectives are:
    • min_win_rate : minimizes the required win rate for the portfolio to acheive a positive returns.

    • min_risk_reward: minimizes the required risk reward ratio required for the portfolio to acheive a positive returns.

  • test_run: boolean flag used for testing purposes. If true, the constructed portfolios will consist of only one asset.

Returns:
  • optimized_portfolio_idxstuple with length equal to ‘optimized_n’ and values of integers that repesent the indices corresponding

    to the optimal currency pairs.

  • optimized_portfolio_tfs : numpy array with length of ‘optimized_n’ and values of optimal timeframes.

  • optimized_portfolio_weights: numpy array with length equal to ‘optimized_n’ and values of equal floats that sum up to 1.

  • optimized_n : integer indicating the number of assets in the constructed portfolio.

  • optimized_W_R : float indicating the minimum required win rate for the portfolio to acheive positive return.

  • optimized_R_R : float indicating the minimum required risk reward for the portfolio to acheive positive return.

** this function is designed to be used internally in ‘fxmanager.optimization.eqw_optimization_test.run()’ function. However,

it can be used by the user separately to construct a portfolio for a given day without running the optimization test.

fxmanager.optimization.weight_optimizer.single_timeframe_portfolio(best_tf, wrst_tf, data_dir, num_assets, objective='min_win_rate', print_progress=False)

Helper function for ‘fxmanager.optimization.eq_weight_optimizer.optimize()’ function.

selects the optimal weighted combination of currency pairs in the given timeframe data.