end = datetime.datetime.utcnow()
    start_date = end.replace(hour=0, minute=0, second=0, microsecond=0) # Returns a copy

    time_series_request = TimeSeriesRequest(
                start_date = start_date,         # start date
                finish_date = datetime.datetime.utcnow(),                       # finish date
                freq = 'intraday',                                              # intraday data
                data_source = 'bloomberg',                      # use Bloomberg as data source
                tickers = ['EURUSD'] ,                          # ticker (Thalesians)
                fields = ['close'],                             # which fields to download
                vendor_tickers = ['EURUSD BGN Curncy'],         # ticker (Bloomberg)
                vendor_fields = ['close'],                      # which Bloomberg fields to download
                cache_algo = 'internet_load_return')            # how to return data

    ltsf = LightTimeSeriesFactory()

    df = ltsf.harvest_time_series(time_series_request)
    df.columns = [x.replace('.close', '') for x in df.columns.values]

    gp = GraphProperties()

    gp.title = 'EURUSD stuff!'
    gp.file_output = 'EURUSD.png'
    gp.source = 'Thalesians/BBG (created with PyThalesians Python library)'

    pf = PlotFactory()
    pf.plot_line_graph(df, adapter = 'pythalesians', gp = gp)

    pytwitter.update_status("check out my plot of EUR/USD!", picture = gp.file_output)
        df_fred = ltsf.harvest_time_series(time_series_request)
        df_fred.columns = [
            x.replace('.close', '') for x in df_fred.columns.values
        ]

        # convert to USD bn
        # df_fred = (df_fred * 10000000)
        df = df.join(df_fred, how="outer")
        df['USDJPY'] = df['USDJPY'].ffill()

        # data is in 100 million JPY, divide by 10 to get into 1000 million (ie. 1 billion)
        # divide by USD/JPY spot to get into USD
        df['USDJPY purchases (bn USD)'] = (df['USDJPY purchases (bn USD)'] /
                                           df['USDJPY']) / 10

        gp = GraphProperties()
        gp.scale_factor = 3

        gp.title = "BoJ USDJPY buying"
        gp.file_output = "output_data/" + datetime.date.today().strftime("%Y%m%d") + " USDJPY BoJ intervention " \
                         + str(gp.scale_factor) + ".png"

        gp.source = 'Thalesians/BBG (created with PyThalesians Python library)'

        gp.y_axis_2_series = ['USDJPY purchases (bn USD)']
        gp.color_2_series = gp.y_axis_2_series
        gp.color_2 = ['blue']

        pf = PlotFactory()
        pf.plot_line_graph(df, adapter='pythalesians', gp=gp)
    # 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)'
    gp.scale_factor = 1
    gp.file_output = 'output_data/fx-trend-example.png'

    pf.plot_line_graph(port, adapter='pythalesians', gp=gp)

###### backtest simple trend following strategy for FX spot basket
if True:
    # for backtest and loading data
    from pythalesians.market.requests.backtestrequest import BacktestRequest
    from pythalesians.backtest.cash.cashbacktest import CashBacktest
    from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest
    from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory
    from pythalesians.util.fxconv import FXConv
    from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs
Exemplo n.º 4
0
    import pandas

    df.columns = [x.replace('.close', '') for x in df.columns.values]

    short_dates = df[["EURUSDV1M", "USDJPYV1M"]]
    long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]]
    short_dates, long_dates = short_dates.align(long_dates, join='left', axis = 0)

    slope = pandas.DataFrame(data = short_dates.values - long_dates.values, index = short_dates.index,
            columns = ["EURUSDV1M-1Y", "USDJPYV1M-1Y"])

    # resample fand calculate average over month
    slope_monthly = slope.resample('M').mean()

    slope_monthly.index = [str(x.year) + '/' + str(x.month) for x in slope_monthly.index]

    pf = PlotFactory()

    gp = GraphProperties()

    gp.source = 'Thalesians/BBG'
    gp.title = 'Vol slopes in EUR/USD and USD/JPY recently'
    gp.scale_factor = 2
    gp.display_legend = True
    gp.chart_type = 'bar'
    gp.x_title = 'Dates'
    gp.y_title = 'Pc'

    # plot using Cufflinks
    pf.plot_bar_graph(slope_monthly, adapter = 'cufflinks', gp = gp)
Exemplo n.º 5
0
    short_dates = df[["EURUSDV1M", "USDJPYV1M"]]
    long_dates = df[["EURUSDV1Y", "USDJPYV1Y"]]
    short_dates, long_dates = short_dates.align(long_dates,
                                                join='left',
                                                axis=0)

    slope = pandas.DataFrame(data=short_dates.values - long_dates.values,
                             index=short_dates.index,
                             columns=["EURUSDV1M-1Y", "USDJPYV1M-1Y"])

    # resample fand calculate average over month
    slope_monthly = slope.resample('M').mean()

    slope_monthly.index = [
        str(x.year) + '/' + str(x.month) for x in slope_monthly.index
    ]

    pf = PlotFactory()

    gp = GraphProperties()

    gp.source = 'Thalesians/BBG'
    gp.title = 'Vol slopes in EUR/USD and USD/JPY recently'
    gp.scale_factor = 2
    gp.display_legend = True
    gp.chart_type = 'bar'
    gp.x_title = 'Dates'
    gp.y_title = 'Pc'

    # plot using Cufflinks
    pf.plot_bar_graph(slope_monthly, adapter='cufflinks', gp=gp)
    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)'
    gp.scale_factor = 1
    gp.file_output = 'output_data/fx-trend-example.png'

    pf.plot_line_graph(port, adapter = 'pythalesians', gp = gp)

###### backtest simple trend following strategy for FX spot basket
if True:
    # for backtest and loading data
    from pythalesians.market.requests.backtestrequest import BacktestRequest
    from pythalesians.backtest.cash.cashbacktest import CashBacktest
    from pythalesians.market.requests.timeseriesrequest import TimeSeriesRequest
    from pythalesians.market.loaders.lighttimeseriesfactory import LightTimeSeriesFactory
    from pythalesians.util.fxconv import FXConv
    from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs