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
Пример #2
0
    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
Пример #3
0
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
Пример #4
0
                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'
Пример #5
0
        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()

    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])
    ]
Пример #6
0
from finmarketpy.economics import TechIndicator, TechParams

from findatapy.util.loggermanager import LoggerManager


logger = LoggerManager().getLogger(__name__)

chart = Chart(engine='bokeh')

tech_ind = TechIndicator()
###### Simple example loading local data and using finmarketpy engine
# Load data from local file
df = pd.read_csv("/Volumes/Data/s&p500.csv", index_col=0, parse_dates=['Date'],
                 date_parser=lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))

# Calculate Volume Weighted Average Price (VWAP)
tech_params = TechParams()
tech_ind.create_tech_ind(df, 'VWAP', tech_params)

df = tech_ind.get_techind()

print(df)

style = Style()
style.title = 'S&P500 VWAP'
style.scale_factor = 2

df = tech_ind.get_techind()

chart.plot(df, style=style)
Пример #7
0
if run_example == 1 or run_example == 0:

    # Downloaded S&P500
    md_request = MarketDataRequest(
        start_date="01 Jan 2000",  # start date
        data_source='quandl',  # use Quandl as data source
        tickers=['S&P500'],
        fields=['close', 'open', 'high', 'low'],  # which fields to download
        vendor_tickers=['YAHOO/INDEX_GSPC'],  # ticker (Bloomberg)
        vendor_fields=['close', 'open', 'high',
                       'low'],  # which Bloomberg fields to download
        cache_algo='internet_load_return')  # how to return data

    df = market.fetch_market(md_request)

    print(df)

    tech_params = TechParams()
    tech_params.atr_period = 14
    tech_ind.create_tech_ind(df, 'ATR', tech_params)

    style = Style()

    style.title = 'S&P500 ATR'
    style.scale_factor = 2
    style.file_output = "sp500.png"
    style.source = 'Quandl/Yahoo'

    df = tech_ind.get_techind()

    chart.plot(df, style=style)
Пример #8
0
from finmarketpy.economics import TechIndicator, TechParams

from findatapy.util.loggermanager import LoggerManager


logger = LoggerManager().getLogger(__name__)

chart = Chart(engine='bokeh')

tech_ind = TechIndicator()

# Load data from local file
df = pd.read_csv("/Volumes/Data/s&p500.csv", index_col=0, parse_dates=['Date'],
                 date_parser=lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))

# Calculate Volume Weighted Average Price (VWAP)
tech_params = TechParams()
tech_ind.create_tech_ind(df, 'VWAP', tech_params)

df = tech_ind.get_techind()

print(df)

style = Style()
style.title = 'S&P500 VWAP'
style.scale_factor = 2

df = tech_ind.get_techind()

chart.plot(df, style=style)
Пример #9
0
###### fetch data from Quandl for BoE rate (using Bloomberg data)
if run_example == 1 or run_example == 0:

    # downloaded S&P500
    md_request = MarketDataRequest(
                start_date = "01 Jan 2000",                         # start date
                data_source = 'quandl',                             # use Quandl as data source
                tickers = ['S&P500'],
                fields = ['close', 'open', 'high', 'low'],          # which fields to download
                vendor_tickers = ['YAHOO/INDEX_GSPC'],              # ticker (Bloomberg)
                vendor_fields = ['close', 'open', 'high', 'low'],   # which Bloomberg fields to download
                cache_algo = 'internet_load_return')                # how to return data

    df = market.fetch_market(md_request)

    print(df)

    tech_params = TechParams()
    tech_params.atr_period = 14
    tech_ind.create_tech_ind(df, 'ATR', tech_params)

    style = Style()

    style.title = 'S&P500 ATR'
    style.scale_factor = 2
    style.file_output = "sp500.png"
    style.source = 'Quandl/Yahoo'

    df = tech_ind.get_techind()

    chart.plot(df, style=style)