Ejemplo n.º 1
0
    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
                tickers = tickers,                              # ticker (Thalesians)
                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

    ltsf = LightTimeSeriesFactory()

    asset_df = ltsf.harvest_time_series(time_series_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
    cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
    port = cash_backtest.get_cumportfolio()
    port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])]
    signals = cash_backtest.get_porfolio_signal()

    # print the last positions (we could also save as CSV etc.)
    print(signals.tail(1))

    pf = PlotFactory()
    gp = GraphProperties()
    gp.title = "Thalesians FX trend strategy"
    gp.source = 'Thalesians/BBG (calc with PyThalesians Python library)'
    time_series_request = TimeSeriesRequest(
        start_date="01 Jan 1970",  # start date
        finish_date=datetime.date.today(),  # finish date
        freq='daily',  # daily data
        data_source='quandl',  # use Quandl as data source
        tickers=['EURUSD',  # ticker (Thalesians)
                 'GBPUSD'],
        fields=['close'],  # which fields to download
        vendor_tickers=['FRED/DEXUSEU', 'FRED/DEXUSUK'],  # ticker (Quandl)
        vendor_fields=['close'],  # which Bloomberg fields to download
        cache_algo='internet_load_return')  # how to return data

    ltsf = LightTimeSeriesFactory()

    daily_vals = ltsf.harvest_time_series(time_series_request)

    techind = TechIndicator()
    tech_params = TechParams()
    tech_params.sma_period = 20

    techind.create_tech_ind(daily_vals, 'SMA', tech_params=tech_params)

    sma = techind.get_techind()
    signal = techind.get_signal()

    combine = daily_vals.join(sma, how='outer')

    pf = PlotFactory()
    pf.plot_line_graph(combine, adapter='pythalesians')
Ejemplo n.º 4
0
        start_date="01 Jan 1970",  # start date
        finish_date=datetime.date.today(),  # finish date
        freq='daily',  # daily data
        data_source='quandl',  # use Quandl as data source
        tickers=[
            'EURUSD',  # ticker (Thalesians)
            'GBPUSD'
        ],
        fields=['close'],  # which fields to download
        vendor_tickers=['FRED/DEXUSEU', 'FRED/DEXUSUK'],  # ticker (Quandl)
        vendor_fields=['close'],  # which Bloomberg fields to download
        cache_algo='internet_load_return')  # how to return data

    ltsf = LightTimeSeriesFactory()

    daily_vals = ltsf.harvest_time_series(time_series_request)

    techind = TechIndicator()
    tech_params = TechParams()
    tech_params.sma_period = 20

    techind.create_tech_ind(daily_vals, 'SMA', tech_params=tech_params)

    sma = techind.get_techind()
    signal = techind.get_signal()

    combine = daily_vals.join(sma, how='outer')

    pf = PlotFactory()
    pf.plot_line_graph(combine, adapter='pythalesians')
Ejemplo n.º 5
0
                tickers = tickers,                              # ticker (Thalesians)
                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

    ltsf = LightTimeSeriesFactory()

    asset_df = ltsf.harvest_time_series(time_series_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
    cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
    port = cash_backtest.get_cumportfolio()
    port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])]
    signals = cash_backtest.get_porfolio_signal()

    # print the last positions (we could also save as CSV etc.)
    print(signals.tail(1))

    pf = PlotFactory()
    gp = GraphProperties()
    gp.title = "Thalesians FX trend strategy"
    gp.source = 'Thalesians/BBG (calc with PyThalesians Python library)'