fxmanager.basic package

Package contents

This sub-package contains mudules of helper functions used intrnally in all FX-Manager’s sub-packages.

in addition to the helper functions, the sub-package contains functions used for setting up new FX-Manager projects and preprocessing data to be compaitable with FX-Manager. it also contains the module used for simulating the trading accounts.

Modules:
  • util : This module contains helper functions used internally in other fxmanager sub-packages. it also includes public functions to be used by the user to setup fxmanager project structure and pre-process the data.

  • account : This module contains ‘Account’ class which is used for creating a fully functional virtual forex trading accounts.

fxmanager.basic.account module

This module contains ‘Account’ class which is used for creating a fully functional virtual forex trading accounts.

used for trading simulation and strategies backtesting. first the user creates an account instance with the required paramaters, then this instance is passed to fxmanager’s built-in historical or live simulators.

Classes:
  • Account: Class that simulates forex trading accounts.

class fxmanager.basic.account.Account(balance=100000.0, account_type='standard', account_currency='usd', leverage=0.01, volume_bounds=(0.01, 8.0))

Bases: object

Class that simulates forex trading accounts.

Args:
  • balance : float with initial balance in the account.

  • account_typestring idicating account type, supported account types are
    • standard: lot size = 100,000

    • mini : lot size = 10,000

    • micro : lot size = 1000

    • nano : lot size = 100

  • account_currency: string indicating the account currency (currency of initial balance).

  • leverage : float indicating the leverage provided by the broker

  • volume_bounds : tuple of 2 floats, minimum and maximum availabe trading volumes per one position.

Public Methods:
  • open_pisition() : opens a new position and updates the state of the account accordingly.

  • close_position(): closes an opened position and updates the state of the account accordingly.

  • update() : updates the state of the account (including oppened positions) with the current market prices.

close_position(ticket, prices)

closes an opened position and updates the state of the account accordingly.

open_pisition(ticket, base_currency, quote_currency, time_frame, weight, SL, TP, order_type, prices, period, order_idx, risk_factor=0.8)

opens a new position and updates the state of the account accordingly.

update(prices, dynamic_sltp=False)

updates the state of the account (including oppened positions) with the current market prices.

fxmanager.basic.util module

This module contains helper functions used internally in other fxmanager sub-packages. it also includes public functions to be used by the user to setup fxmanager project structure and pre-process the data.

Public Functions:
  • get_portfolios(): gets the portfolios to be used in historical backtesting of strategies.

  • setup() : creates the required folders and files for any new project according to application type.

  • preprocess() : preprocesses the raw data files to prepare it for analysis and simumlations.

fxmanager.basic.util.expected_return(day=0, data_dir=None, time_frames=[], currency_pairs=[], weights=[], leverage=0.01, win_rate=0.5, risk_reward=1, n_scenarios=100)

calculates the average expected daily return from a portfolio given a win rate and a risk-reward ratio.

Args:
  • day : integer with range from 0 to (number of working days in the dataset-1).

  • 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.

  • time_frames : list of portfolio timeframes.

  • currency_pairs: list of portfolio currency pairs.

  • weights : list of portfolio weights.

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

  • win_rate : float indicating the percentage of winning positions to the total number of positions.

  • risk_reward : float indicating ratio of reward to risk.

  • n_scenarios : integer indicating the number of scenarios across which the expected return is averaged.

Returns:
  • avg_expected_return: float indicating the average expected return.

  • win_probabilityfloat indicating the ratio of number of scenarios in which the expected return is positive to the total number

    of scenarios.

Usage:
  • used in ‘fxmanage.optimization.eqw_optimization_test.run_test()’ function to calculate the expected returns using the optimized portfolios.

fxmanager.basic.util.findCombinations(n)

finds all possible combinations that sum up to n.

Args:
  • n: integer idicating the number that returned cambinations sum up to.

Returns:
  • every combination is printed in a new line to standard output. to save the results to a file, change sys.stdout to the path of the file.

Usage:
  • used in ‘fxmanager.basic.util.setup()’ function to save required weight combinations to ‘data_dirweigts’ directory.

fxmanager.basic.util.findCombinationsUtil(arr, index, num, reducedNum)

helper function for findCombinations() function.

fxmanager.basic.util.get_avg_rets(day, data_dir=None, time_frames=[], currency_pairs=[])

gets the average best & worst returns of the passed currency pairs & timeframes from the preprocessed data in the ‘data_dirtime_frames’ directory.

Args:
  • day : integer indicating day number - starts from 0 and ends with the total number of work days in the dataset.

  • 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.

  • time_frames : a list of strings of the timeframes from which the data is read.

  • currency_pairs: a list of strings of the currency pairs from which the data is read.

Returns:
  • avg_best_rets: pandas dataframe of average best return for each asset, columns are time_frames and index is currency_pairs.

  • avg_wrst_rets: pandas dataframe of average worst return for each asset, columns are time_frames and index is currency_pairs.

Usage:
  • used in ‘fxmanager.simulation.live.run()’ function if the construct_portfolio is set to True.

  • used in ‘fxmanager.optimization.w_optimization_test’ and ‘fxmanager.optimization.eqw_optimization_test’ modules.

fxmanager.basic.util.get_portfolios(data_dir=None, num_days=1, single_portfolio={}, portfolios=[], optimized_portfolios=False, use_single_porfolio=False)

gets the portfolios to be used in historical backtesting of strategies.

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.

  • num_days : total number of work days in the dataset.

  • single_portfolio: dictionary of the portfolio to be used for backtesting with keys (currency_pairs, time_frames, weights) used if ‘optimized_portfolios’ is False and ‘use_single_porfolio’ is True.

  • portfolios : list of dictionries like ‘single_portfolio’. Lenght of the list = num_days-1. used if ‘optimized_portfolios’ is False and ‘use_single_porfolio’ is False.

  • optimized_portfolios: boolean flag, set to True if an optimization test is run and the portfolios to be used are saved in ‘data_dirstatsOptimizer_Test_portfolios.csv’ file.

  • use_single_porfolio: boolean flag, used if ‘optimized_portfolios’ is False, set to true if you passed a single portfolio in the ‘single_portfolio’ argument, if False, ‘portfolios’ argument is used instead.

Returns:
  • df: pandas dataframe with columns (currency_pairs, time_frames, weights) and range index of length = num_days

Usage:
  • used by user before calling the function ‘fxmanager.simulation.historic.run()’.

fxmanager.basic.util.get_weights(data_dir=None, num_assets=1)

reads the pre-calculated weights from ‘data_dirweights' directory to be used for weight optimization.

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.

  • num_assets : number of assets in the portfolio being optimized. An asset is a currency pair in a specific timeframe.

Returns:
  • weight_combinations_list: list of all possible weight combinations for the passed number of assets.

Usage:
  • used in ‘fxmanager.optimization.weight_optimizer’ module.

fxmanager.basic.util.ncr(n, r)

calculates the number of combinations nCrز

Args:
  • n: integer indicating the total number of items to be combined.

  • r: integer indicating the length of every combination.

Returns:
  • res: integer with the result.

fxmanager.basic.util.preprocess(data_dir=None, app_type='', raw_data_type='', raw_data_format={}, save_logs=True)

preprocesses the raw data files to prepare it for analysis and simumlations.

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.

  • app_typestring indicating the application type. supported app types are
    • back_tester : if you want to test trading strategies using histoical data on FX-Manager virtual account.

    • portfolio_optimizer : if you want to asses different portfolio optimization methods and objectives using histoical data.

    • live_simulator : if you want to test trading strategies in real time using MT4 Live data on FX-Manager virtual account.

    • all_in_one : if you want to use all the previous features.

  • raw_data_typestring with the type of raw data used, supported raw data types are
    • daily_bid_ask : Bid/Ask ticks in daily seperate files (a file for each currency_pair in each day)

    • daily_candles : 1-min candles in daily seperate files (a file for each currency_pair in each day)

    • period_candles : 1-min candles over a period (a file for each currency_pair for all the days)

  • raw_data_formatdictionary with information about row data files, the required keys vary according to raw_data_type argument, the following are exambles of how the dictionary should be formatted for each type
    • daily_bid_ask: {‘date_col’:’Gmt Time’, ‘date_format’:’%d.%m.%Y %H:%M:%S.%f’, ‘ask_col’:’Ask’, ‘bid_col’:’Bid’}

    • daily_candles or period_candles: {‘date_col’:’time’, ‘date_format’:’%Y.%m.%d %H:%M’, ‘open_col’:’open’, ‘high_col’:’high’, ‘low_col’:’low’, ‘close_col’:’close’, ‘tick_vol_col’:’tick_volume’}

  • save_logs : boolean flag, if True, the program logs are saved to ‘data_dirlogspreprocessing_logs.txt’ file.

Returns:
  • None

fxmanager.basic.util.process_daily_bid_ask(df, tf)

helper function for ‘fxmanager.basic.util.preprocess()’ function.

used to group the given Bid/Ask data by the given timeframe and calculate open and close prices for the new candle sticks in the new timeframe.

fxmanager.basic.util.process_ohlc(df, tf)

helper function for ‘fxmanager.basic.util.preprocess()’ function.

used to group the given OHLCV data by the given timeframe and calculate open and close prices for the new candle sticks in the new timeframe.

fxmanager.basic.util.setup(app_type='', raw_data_dir='', live_raw_data_dir='', raw_data_type='', file_name_format='', num_days=1, num_cps=1, num_tfs=1)

creates the required folders and files for any new project according to application type.

Args:
  • app_typestring indicating the application type. supported app types are:
    • back_tester : if you want to test trading strategies using histoical data on FX-Manager virtual account.

    • portfolio_optimizer : if you want to asses different portfolio optimization methods and objectives using histoical data.

    • live_simulator : if you want to test trading strategies in real time using MT4 Live data on FX-Manager virtual account.

    • all_in_one : if you want to use all the previous features.

  • raw_data_dir : string containig the path to directory in which the raw data files are kept.

  • live_raw_data_dir: string containig the path to directory in which the raw data files from the previous day are kept. used to construct a portfolio using built-in fxmanager optimization for live simulation.

  • raw_data_typestring with the type of raw data used, supported raw data types are:
    • daily_bid_ask : Bid/Ask ticks in daily seperate files (a file for each currency_pair in each day)

    • daily_candles : 1-min candles in daily seperate files (a file for each currency_pair in each day)

    • period_candles : 1-min candles over a period (a file for each currency_pair for all the days)

  • file_name_formatstring of white spaces with the same length as raw data file names. Givin that each file name has information about date and currency pair symbol, the white spaces in the same location as these information are replaced as follows:
    • day, month and year in the date are replaced with (dd), (mm), (yyyy) respectively.

    • currency pair symbol is replaced with (cccccc). For example, if the file name is ‘AUDUSD_Ticks_02.08.2021-02.08.2021.csv’ then the file_name_format is: ‘cccccc dd mm yyyy ‘

  • num_days : total number of work days in the dataset.

  • num_cps : integer indicating the number of currency pairs in the dataset

  • num_tfs : integer indicating the number of timeframes in the dataset.

fxmanager.basic.util.top_n_timeframes(best, wrst, objective='min_win_rate', n=1)

selects the best (n) of time frames according to the ‘objective’ argument.

Args:
  • best : pandas dataframe with the return of the ‘get_avg_rets()’ function defined above. note tha the index must be transformed first to range index.

  • wrst : pandas dataframe with the return of the ‘get_avg_rets()’ function defined above. note tha the index must be transformed first to range index.

  • objective: string indecating the criteria with which the best time frames will be chosed, supported objectives are (min_win_rate, min_risk_reward)

  • n : integer indicating the number of time frames to return

Returns:
  • top_n: numpy array with the names of selected best time frames.

  • W_R : pandas series with required win rates for every time frame.

  • R_R : pandas series with required risk rewards for every time frame.

Usage:
  • used in ‘fxmanager.optimization.weight_optimizer’ and ‘fxmanager.optimization.eq_weight_optimizer’ modules.