fxmanager.simulation package

Package contents

This sub-package contains the modules used for historical and live trading sumulations.

Modules:
  • historic: This module contains helper functions used internally in this module and a public function to run historical simulation.

  • live : This module contains helper functions used internally in this module and a public function to run live simulation with live price feed from MT4.

fxmanager.simulation.historic module

This module contains helper functions used internally in this module and a public function to run historical simulation.

Public Functions:
  • run(): starts trading simulation with historic prices.

fxmanager.simulation.historic.close_position(account, ticket, portfolio_prices, portfolio_orders, wins, losses)

helper function to ‘fxmanager.simulation.historic.run()’ function. closes an opened position by ticket.

Returns:
  • portfolio_orders: pandas dataframe with history of closed positions. Each row represents a closed position.

  • wins : integer indicating number of closed positions with positive profit.

  • losses : integer indicating number of closed positions with nigative profit.

  • win_rate : float indicating percentage of wins to (wins+losses).

fxmanager.simulation.historic.get_prices(day, data_dir=None, time_frame='', currency_pair='')

helper function to ‘fxmanager.simulation.historic.run()’ function. gets the bid/ask prices of the given currency_pair & time_frame in the selected day from 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 (num_days-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_frame : a string of the timeframe from wich the prices is read.

  • currency_pair : a string of the currency pair from wich the prices is read.

Returns:
  • df: pandas dataframe of the bid & ask prices with columns (ask_open, bid_open, ask_close, bid_close, tick_volume) and a datetime index from the begining to the end off the day with frequency equal to time_frame.

fxmanager.simulation.historic.print_final_state(account, win_rate)

helper function to ‘fxmanager.simulation.historic.run()’ function. prints the final state of the portfolio after the simulation is finished.

fxmanager.simulation.historic.run(account, strategy, data_dir=None, portfolios={}, risk_factor=0.95, dynamic_sltp=False, save_logs=False, **kwargs)

starts trading simulation with historic prices.

Args:
  • account : account object with all account information.

  • strategy : strategy object with the trading strategy information.

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

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

  • risk_factor : a float with range from 0 to 1 indicating the percentage of reinvested balance.

  • dynamic_stlp : boolean flag, if True, stop losses and take profits of opened positions are updated with each simulation step.

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

  • kwargs : dictionary to hold any number of arguments required for the strategy object.

Returns:
  • None

fxmanager.simulation.live module

This module contains helper functions used internally in this module and a public function to run live simulation with live price feed from MT4.

Public Functions:
  • run(): starts trading simulation with live prices from MT4 EA.

fxmanager.simulation.live.close_position(account, ticket, portfolio_prices, portfolio_orders, wins, losses)

helper function to ‘fxmanager.simulation.live.run()’ function. closes an opened position by ticket.

Returns:
  • portfolio_orders: pandas dataframe with history of closed positions. Each row represents a closed position.

  • wins : integer indicating number of closed positions with positive profit.

  • losses : integer indicating number of closed positions with nigative profit.

  • win_rate : float indicating percentage of wins to (wins+losses).

fxmanager.simulation.live.get_price(currency_pairs, price_feed, sleep_time)

helper function to ‘fxmanager.simulation.live.run()’ function. gets the current prices of portfolio assets from price feed.

Returns:
  • price_every_iter_df: pandas dataframe with length = 1 and (4* number of currency pairs) columns. Each asset has 4 columns as follows, (000000_bid_open, 000000_ask_open, 000000_bid_close, 000000_ask_close) where 000000 is replaced with currency pair symbol.

fxmanager.simulation.live.optimize_portfolio(data_dir, raw_data_format, optimization_method, optimization_objective, weight_optimization=False, is_preprocessed=True)

helper function to ‘fxmanager.simulation.live.run()’ function. Does portfolio optimization using data from previous day. The constructed portfolio is used for live simulation.

Returns:
  • portfolio : dictionary with keys (‘currency_pairs’, ‘time_frames’, ‘weights’, ‘optimized_n’, ‘optimized_W_R’, ‘optimized_R_R’)

  • currency_pairs: list with portfolio currency pairs.

  • time_frames : list with portfolio timeframes.

  • weights : list with portfolio weights.

fxmanager.simulation.live.print_final_state(account, win_rate)

helper function to ‘fxmanager.simulation.live.run()’ function. prints the final state of the portfolio after the simulation is finished.

fxmanager.simulation.live.print_live_state(account, win_rate)

helper function to ‘fxmanager.simulation.live.run()’ function. prints the current state of the portfolio.

fxmanager.simulation.live.run(account, strategy, data_dir=None, construct_portfolio=False, portfolio={}, weight_optimization=False, is_preprocessed=True, raw_data_format='', optimization_method='', optimization_objective='', sleep_time=1, risk_factor=1, dynamic_sltp=False, sync_zero=False, save_logs=False, **kwargs)

starts trading simulation with live prices from MT4 EA.

Args:
  • account : account object with all account information.

  • strategy : strategy object with the trading strategy information.

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

  • construct_portfolio : boolean indicating whether portfolio optimization is used or not.

  • portfolio : dictionary with the portfolio to be used for trading, keys are: (currency_pairs, time_frames, weights). This argument is only used if ‘construct_portfolio’ is set to False.

  • weight_optimization : boolean indicating whether weight optimization is used in portfolio optimization. This argument is only used when ‘construct_portfolio’ is set to True.

  • is_preprocessed : boolean indicating if the data of used for portfolio optimization is preprocessed or not. This argument is only used when ‘construct_portfolio’ is set to True.

  • raw_data_format : dictionary with information about raw data used for portfolio optimization. This argument is only used when ‘construct_portfolio’ is set to True.

  • optimization_method : string with optimization method to be used. This argument is only used when ‘construct_portfolio’ is set to True.

  • optimization_objective: string with optimization objective to be used. This argument is only used when ‘construct_portfolio’ is set to True.

  • sleep_time : integer indicating the time every which the portfolio is updated (in minutes).

  • risk_factor : a float with range from 0 to 1 indicating the percentage of reinvested balance.

  • dynamic_stlp : boolean flag, if True, stop losses and take profits of opened positions are updated with each simulation step.

  • sync_zero : boolean flag, if True, the simulation starts when the seconds in current time = 0.

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

  • kwargs : dictionary to hold any number of arguments required for the strategy object.

Returns:
  • None