def construct_signal(self, spot_df, spot_df2, tech_params, br, run_in_parallel=False): ##### FILL IN WITH YOUR OWN SIGNALS # Use technical indicator to create signals # (we could obviously create whatever function we wanted for generating the signal dataframe) tech_ind = TechIndicator() tech_ind.create_tech_ind(spot_df, 'SMA', tech_params); signal_df = tech_ind.get_signal() return signal_df
def construct_signal(self, spot_df, spot_df2, tech_params, br): ##### FILL IN WITH YOUR OWN SIGNALS # use technical indicator to create signals # (we could obviously create whatever function we wanted for generating the signal dataframe) tech_ind = TechIndicator() tech_ind.create_tech_ind(spot_df, 'SMA', tech_params); signal_df = tech_ind.get_signal() return signal_df
def construct_backtest(ticker, vendor_ticker, sma_period, data_source, start_date, quandl_api_key): backtest = Backtest() br = BacktestRequest() # Set all the parameters for the backtest br.start_date = start_date br.finish_date = datetime.datetime.utcnow() br.spot_tc_bp = 2.5 # 2.5 bps bid/ask spread br.ann_factor = 252 tech_params = TechParams() tech_params.sma_period = sma_period indicator = 'SMA' md_request = MarketDataRequest( start_date=start_date, finish_date=datetime.date.today(), freq='daily', data_source=data_source, tickers=ticker, fields=['close'], vendor_tickers=vendor_ticker, quandl_api_key=quandl_api_key) market = Market(market_data_generator=MarketDataGenerator()) # Download the market data (the asset we are trading is also # being used to generate the signal) asset_df = market.fetch_market(md_request) spot_df = asset_df # Use technical indicator to create signals # (we could obviously create whatever function we wanted for generating the signal dataframe) # However, finmarketpy has some technical indicators built in (and some signals too) tech_ind = TechIndicator() tech_ind.create_tech_ind(spot_df, indicator, tech_params); signal_df = tech_ind.get_signal() # use the same data for generating signals backtest.calculate_trading_PnL(br, asset_df, signal_df, None, False) # Get the returns and signals for the portfolio port = backtest.portfolio_cum() port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(backtest.portfolio_pnl_desc()[0])] signals = backtest.portfolio_signal() # returns = backtest.pnl() return port, signals
fields = ['close'], # which fields to download vendor_tickers = vendor_tickers, # ticker (Quandl) vendor_fields = ['close'], # which Bloomberg fields to download cache_algo = 'internet_load_return') # how to return data market = Market(market_data_generator=MarketDataGenerator()) asset_df = market.fetch_market(md_request) spot_df = asset_df logger.info("Running backtest...") # use technical indicator to create signals # (we could obviously create whatever function we wanted for generating the signal dataframe) tech_ind = TechIndicator() tech_ind.create_tech_ind(spot_df, indicator, tech_params); signal_df = tech_ind.get_signal() # use the same data for generating signals backtest.calculate_trading_PnL(br, asset_df, signal_df) port = backtest.get_cumportfolio() port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(backtest.get_portfolio_pnl_desc()[0])] signals = backtest.get_portfolio_signal() # print the last positions (we could also save as CSV etc.) print(signals.tail(1)) style = Style() style.title = "FX trend strategy" style.source = 'Quandl' style.scale_factor = 1 style.file_output = 'fx-trend-example.png'
vendor_tickers=vendor_tickers, # ticker (Quandl) vendor_fields=['close'], # which Bloomberg fields to download cache_algo='internet_load_return') # how to return data market = Market(market_data_generator=MarketDataGenerator()) asset_df = market.fetch_market(md_request) spot_df = asset_df logger.info("Running backtest...") # use technical indicator to create signals # (we could obviously create whatever function we wanted for generating the signal dataframe) tech_ind = TechIndicator() tech_ind.create_tech_ind(spot_df, indicator, tech_params) signal_df = tech_ind.get_signal() contract_value_df = None # use the same data for generating signals backtest.calculate_trading_PnL(br, asset_df, signal_df, contract_value_df, run_in_parallel=False) port = backtest.portfolio_cum() port.columns = [ indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(backtest.portfolio_pnl_desc()[0]) ] signals = backtest.portfolio_signal()