def plot_animated_vol_market():
    market = Market(market_data_generator=MarketDataGenerator())

    cross = ['EURUSD']; start_date = '01 Mar 2017'; finish_date = '21 Apr 2017'; sampling = 'no'

    md_request = MarketDataRequest(start_date=start_date, finish_date=finish_date,
                                   data_source='bloomberg', cut='NYC', category='fx-implied-vol',
                                   tickers=cross, cache_algo='cache_algo_return')

    df = market.fetch_market(md_request)
    if sampling != 'no': df = df.resample(sampling).mean()
    fxvf = FXVolFactory()
    df_vs = []

    # Grab the vol surface for each date and create a dataframe for each date (could have used a panel)
    for i in range(0, len(df.index)): df_vs.append(fxvf.extract_vol_surface_for_date(df, cross[0], i))

    # Do static plot for first day using Plotly
    style = Style(title="FX vol surface of " + cross[0], source="chartpy", color='Blues')

    Chart(df=df_vs[0], chart_type='surface', style=style).plot(engine='plotly')

    # Now do animation (TODO: need to fix animation in chartpy for matplotlib)
    style = Style(title="FX vol surface of " + cross[0], source="chartpy", color='Blues',
                    animate_figure=True, animate_titles=df.index,
                    animate_frame_ms=500, normalize_colormap=False)

    Chart(df=df_vs, chart_type='surface', style=style).plot(engine='plotly')

    # Chart object is initialised with the dataframe and our chart style
    Chart(df=df_vs, chart_type='surface', style=style).plot(engine='plotly')
示例#2
0
    def _create_chart_html(self, chart, embed_chart):
        if chart != {} and chart is not None:
            html_output = [['<hr>']]
        else:
            html_output = []

        style = Style(plotly_plot_mode=embed_chart)

        for c in self._util_func.dict_key_list(chart.keys()):

            # Update chart size and padding (if it's Plotly), so it fits well on PDF
            try:
                chart[c].update_layout(
                    autosize=False,
                    width=self._chart_report_width,
                    height=self._chart_report_height,
                    margin=dict(l=10, r=10, b=10, t=60, pad=4),
                )
            except:
                pass

            if embed_chart == 'leave_as_fig':
                html_output.append([chart[c]])
            else:
                html_output.append([self._chart.plot(chart[c], style=style)])

        return html_output
示例#3
0
def venue_tca_aggregated_example():
    """Example of doing an aggregated TCA computation on a single ticker, and then later calculating the probability
    distribution function of slippage split by venue (when weighted by executed notional)
    """
    tca_engine = TCAEngineImpl(version=tca_version)

    tca_request = TCARequest(start_date=start_date,
                             finish_date=finish_date,
                             ticker=ticker,
                             tca_type='aggregated',
                             trade_data_store=trade_data_store,
                             market_data_store=market_data_store,
                             metric_calcs=MetricSlippage())

    dict_of_df = tca_engine.calculate_tca(tca_request)

    summary = ResultsSummary()

    summary_slippage_df = summary.field_distribution(
        dict_of_df['trade_df'],
        metric_name='slippage',
        aggregate_by_field='venue',
        pdf_only=True,
        weighting_field='executed_notional')

    # Plot PDF of slippage, split up by venue
    Chart(engine='plotly').plot(summary_slippage_df,
                                style=Style(plotly_plot_mode='offline_html',
                                            connect_line_gaps=True))
示例#4
0
    def plot_single_var_regression(self, y, x, y_variable_names, x_variable_names, statistic,
                                   tag = 'stats',
                                   title = None,
                                   pretty_index = None, output_path = None,
                                   scale_factor = None,
                                   silent_plot = False,
                                   shift=[0]):

        if not(isinstance(statistic, list)):
            statistic = [statistic]

        # TODO optimise loop so that we are calculating each regression *once* at present calculating it
        # for each statistic, which is redundant
        for st in statistic:
            stats_df = []

            for sh in shift:
                x_sh = x.shift(sh)
                stats_temp = self.report_single_var_regression(y, x_sh, y_variable_names, x_variable_names, st,
                                                             pretty_index)

                stats_temp.columns = [ x + "_" + str(sh) for x in stats_temp.columns]

                stats_df.append(stats_temp)

            stats_df = pandas.concat(stats_df, axis=1)
            stats_df = stats_df.dropna(how='all')

            if silent_plot: return stats_df

            chart = Chart()
            style = Style()

            if title is None: title = statistic

            style.title = title
            style.display_legend = True
            style.scale_factor = scale_factor
            # style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

            if output_path is not None:
                style.file_output = output_path + ' (' + tag + ' ' + st + ').png'

            chart.plot(stats_df, style=style)

        return stats_df
示例#5
0
def example_request_mid_benchmark():
    """Example of how to do a calculation to do market analysis to calculate mid, resample etc. without any trade data

    """
    from tcapy.analysis.algos.benchmark import BenchmarkMarketMid, BenchmarkMarketSpreadToMid, BenchmarkMarketResampleOffset, \
        BenchmarkMarketFilter
    from tcapy.analysis.algos.resultsform import BarResultsForm, TimelineResultsForm

    tca_request = get_tca_request()

    # Allow analysis to be done in a parallel approach day by day
    # (note: can't do analysis which requires data outside of the daily chunks to do this!)
    tca_request.multithreading_params['splice_request_by_dates'] = use_multithreading

    # Filter market data by time of day between 15:00-17:00 LDN
    # Then calculate the market mid, then calculate the spread to the mid,
    # Then resample the data into 1 minute, taking the mean of each minute (and TWAP) and calculating the absolute range
    tca_request.benchmark_calcs = [BenchmarkMarketFilter(time_of_day={'start_time' : "15:00", 'finish_time' : "17:00"},
                                                         time_zone='Europe/London'),
                                   BenchmarkMarketMid(), BenchmarkMarketSpreadToMid(),
                                   BenchmarkMarketResampleOffset(market_resample_freq='1', market_resample_unit='min',
                                        price_field='mid', resample_how=['mean', 'twap', 'absrange'], dropna=True),
                                   ]

    # Calculate the mean spread to mid for EURUSD by time of day during our sample (do not weight by any other field)
    # Calculate the mean absrange for EURUSD by time of day (London timezone)/month of _year (ie. proxy for volatility)
    tca_request.results_form = \
        [TimelineResultsForm(market_trade_order_list='EURUSD', metric_name='ask_mid_spread',
                             weighting_field=None, by_date='time', scalar=10000.0),
         TimelineResultsForm(market_trade_order_list='EURUSD', metric_name='absrange',
                             weighting_field=None, by_date=['month', 'timeldn'], scalar=10000.0)
        ]

    # return
    tca_request.use_multithreading = True

    tca_engine = TCAEngineImpl()

    dict_of_df = tca_engine.calculate_tca(tca_request)

    # Print out all keys for all the DataFrames returned
    print(dict_of_df.keys())

    # Print market data snapshots
    print(dict_of_df['EURUSD_df'])
    print(dict_of_df['USDJPY_df'])
    print(dict_of_df['EURUSD_df'].columns)
    print(dict_of_df['USDJPY_df'].columns)

    # Print out mean spread by time of day
    print(dict_of_df['timeline_EURUSD_ask_mid_spread_by/mean_time/all'])

    # Plot mean spread by time of day and absrange by time of day (in London timezone)
    Chart(engine='plotly').plot(dict_of_df['timeline_EURUSD_ask_mid_spread_by/mean_time/all'])

    # Plot absolute range over each minute, averaged by time of day and month of the _year
    Chart(engine='plotly').plot(dict_of_df['timeline_EURUSD_absrange_by/mean_month_timeldn/all'],
                                style=Style(title='EURUSD absolute range by time of day (LDN)', color='Reds', scale_factor=-1))
示例#6
0
    def _create_chart_html(self, chart, embed_chart):
        if chart != {} and chart is not None:
            html_output = [['<hr>']]
        else:
            html_output = []

        style = Style(plotly_plot_mode=embed_chart)

        for c in self._util_func.dict_key_list(chart.keys()):
            html_output.append([self._chart.plot(chart[c], style=style)])

        return html_output
示例#7
0
    def create_style(self, title, file_add):
        style = Style()

        style.title = self.FINAL_STRATEGY + " " + title
        style.display_legend = True
        style.scale_factor = self.SCALE_FACTOR
        style.source = self.CHART_SOURCE

        if self.DEFAULT_PLOT_ENGINE not in ['plotly', 'cufflinks'] and self.SAVE_FIGURES:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(style.scale_factor) + '.png'

        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(style.scale_factor) + '.html'

        try:
            style.silent_display = self.SILENT_DISPLAY
        except: pass

        return style
示例#8
0
def plot_event(df_event, title, factor=100):
    # Plotting spot over economic data event
    style = Style(title=title,
                  plotly_plot_mode='dash',
                  width=1000,
                  height=500,
                  scale_factor=1)

    # Plot in shades of blue (so earlier releases are lighter, later releases are darker)
    style.color = 'Blues'
    style.color_2 = []
    style.y_axis_2_series = []
    style.display_legend = False

    # Last release will be in red, average move in green
    style.color_2_series = [df_event.columns[-2], df_event.columns[-1]]
    style.color_2 = ['red', 'green']
    style.linewidth_2 = 2
    style.linewidth_2_series = style.color_2_series

    # Finally do plot
    return chart.plot(df_event * factor, style=style)
示例#9
0
    def calculate_button(self, *args):

        n_clicks, ticker = args

        if ticker == '':
            return {}, "Here is an example of using chartpy with dash"

        try:
            df = pd.DataFrame(quandl.get(ticker))

            df_vol = (df / df.shift(1)).rolling(
                window=20).std() * math.sqrt(252) * 100.0

            spot_fig = Chart(engine="plotly").plot(df,
                                                   style=Style(
                                                       title='Spot',
                                                       plotly_plot_mode='dash',
                                                       width=980,
                                                       height=480,
                                                       scale_factor=-1))

            vol_fig = Chart(engine="plotly").plot(df_vol,
                                                  style=Style(
                                                      title='Realized Vol 1M',
                                                      plotly_plot_mode='dash',
                                                      width=980,
                                                      height=480,
                                                      scale_factor=-1))
            msg = "Plotted " + ticker + " at " + datetime.datetime.utcnow(
            ).strftime("%b %d %Y %H:%M:%S")

            return spot_fig, vol_fig, msg
        except Exception as e:
            print(str(e))
            pass

        return {}, "Failed to download"
示例#10
0
    def plot_single_var_regression(self,
                                   y,
                                   x,
                                   y_variable_names,
                                   x_variable_names,
                                   statistic,
                                   tag='stats',
                                   title=None,
                                   pretty_index=None,
                                   output_path=None,
                                   scale_factor=None,
                                   silent_plot=False,
                                   shift=[0]):

        if not (isinstance(statistic, list)):
            statistic = [statistic]

        # TODO optimise loop so that we are calculating each regression *once* at present calculating it
        # for each statistic, which is redundant
        for st in statistic:
            stats_df = []

            for sh in shift:
                x_sh = x.shift(sh)
                stats_temp = self.report_single_var_regression(
                    y, x_sh, y_variable_names, x_variable_names, st,
                    pretty_index)

                stats_temp.columns = [
                    x + "_" + str(sh) for x in stats_temp.columns
                ]

                stats_df.append(stats_temp)

            stats_df = pandas.concat(stats_df, axis=1)
            stats_df = stats_df.dropna(how='all')

            if silent_plot: return stats_df

            chart = Chart()
            style = Style()

            if title is None: title = statistic

            style.title = title
            style.display_legend = True
            style.scale_factor = scale_factor
            # style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

            if output_path is not None:
                style.file_output = output_path + ' (' + tag + ' ' + st + ').png'

            chart.plot(stats_df, style=style)

        return stats_df
示例#11
0
class BacktestComparison(object):

    DEFAULT_PLOT_ENGINE = ChartConstants().chartfactory_default_engine
    SCALE_FACTOR = ChartConstants().chartfactory_scale_factor
    CHART_SOURCE = ChartConstants().chartfactory_source
    CHART_STYLE = Style()

    def __init__(self, models, ref_index=0, labels=None):
        """

        :param models: iterable of TradingModel instances.
        :param ref_index: index of the reference model in the list (for difference).
        """
        if hasattr(models, "__iter__") and all(
            [isinstance(x, TradingModel) for x in models]):
            self.models = models
            self.ref_index = ref_index
        else:
            raise AttributeError(
                "Models need to be an iterable of TradingModel instances.")

        self.labels = labels

    def plot_pnl(self, diff=True, silent_plot=False, reduce_plot=True):
        style = self.models[self.ref_index]._create_style(
            "", "Strategy PnL", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        pnls = [model._strategy_pnl for model in models]

        df = pd.concat(pnls, axis=1)

        if diff:
            df = df.subtract(pnls[ref], axis='index')
        if self.labels is not None:
            df.columns = self.labels

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='line',
                      style=style)
        if not silent_plot:
            chart.plot()

        return chart
示例#12
0
    def create_style(self, title, file_add):
        style = Style()

        style.title = self.FINAL_STRATEGY + " " + title
        style.display_legend = True
        style.scale_factor = self.SCALE_FACTOR
        style.source = self.CHART_SOURCE

        if self.DEFAULT_PLOT_ENGINE not in ['plotly', 'cufflinks'] and self.SAVE_FIGURES:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(style.scale_factor) + '.png'

        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (' + file_add + ') ' + str(style.scale_factor) + '.html'

        try:
            style.silent_display = self.SILENT_DISPLAY
        except: pass

        return style
示例#13
0
def plot_animated_vol_market():
    market = Market(market_data_generator=MarketDataGenerator())

    cross = ['GBPUSD']
    start_date = '01 Jun 2016'
    finish_date = '01 Aug 2016'
    sampling = 'no'

    md_request = MarketDataRequest(start_date=start_date,
                                   finish_date=finish_date,
                                   data_source='bloomberg',
                                   cut='LDN',
                                   category='fx-implied-vol',
                                   tickers=cross,
                                   cache_algo='internet_load_return')

    df = market.fetch_market(md_request)
    if sampling != 'no': df = df.resample(sampling).mean()
    fxvf = FXVolFactory()
    df_vs = []

    # grab the vol surface for each date and create a dataframe for each date (could have used a panel)
    for i in range(0, len(df.index)):
        df_vs.append(fxvf.extract_vol_surface_for_date(df, cross[0], i))

    style = Style(title="FX vol surface of " + cross[0],
                  source="chartpy",
                  color='Blues',
                  animate_figure=True,
                  animate_titles=df.index,
                  animate_frame_ms=500,
                  normalize_colormap=False)

    # Chart object is initialised with the dataframe and our chart style
    Chart(df=df_vs, chart_type='surface',
          style=style).plot(engine='matplotlib')
示例#14
0
except:
    quandl_api_key = "x"

# choose run_example = 0 for everything
# run_example = 1 - create a plain and Keen.io based template for a chart webpage

run_example = 0

if run_example == 1 or run_example == 0:

    df = Quandl.get(["FRED/A191RL1Q225SBEA"], authtoken=quandl_api_key)
    df.columns = ["Real QoQ"]

    # Chart object is initialised with the dataframe and our chart style
    chart_bokeh = Chart(df=df, chart_type='line', engine='bokeh',
                        style=Style(title="US GDP", source="Quandl/Fred", scale_factor=-2, width=500, height=300, silent_display=True))

    chart_plotly = Chart(df=df, chart_type='line', engine='plotly',
                         style=Style(title="US GDP", source="Quandl/Fred", scale_factor=-2, width=500, height=300, silent_display=True))

    chart_matplotlib = Chart(df=df, chart_type='line', engine='matplotlib',
                             style=Style(title="US GDP", source="Quandl/Fred", scale_factor=-2, width=500, height=300, silent_display=True))

    text = "A demo of chartpy canvas!!"

    # using plain template
    canvas = Canvas([[text, chart_bokeh], [chart_plotly, df.tail(n=5)]])

    canvas.generate_canvas(silent_display=False, canvas_plotter='plain')

    # using the Keen template (needs static folder in the same place as final HTML file)
                start_date = "01 Jan 1996",                         # start date
                data_source = 'bloomberg',                          # use Bloomberg as data source
                tickers = ['Gold'],
                fields = ['close'],                                 # which fields to download
                vendor_tickers = ['XAUUSD Curncy'],                 # ticker (Bloomberg)
                vendor_fields = ['PX_LAST'],                        # which Bloomberg fields to download
                cache_algo = 'internet_load_return')                # how to return data

    df = market.fetch_market(md_request)

    df_ret = calc.calculate_returns(df)

    day_of_month_seasonality = seasonality.bus_day_of_month_seasonality(df_ret, partition_by_month = False)
    day_of_month_seasonality = calc.convert_month_day_to_date_time(day_of_month_seasonality)

    style = Style()
    style.date_formatter = '%b'
    style.title = 'Gold seasonality'
    style.scale_factor = 3
    style.file_output = "gold-seasonality.png"

    chart.plot(day_of_month_seasonality, style=style)

###### calculate seasonal moves in FX vol (using Bloomberg data)
if run_example == 2 or run_example == 0:
    tickers = ['EURUSDV1M', 'USDJPYV1M', 'GBPUSDV1M', 'AUDUSDV1M']

    md_request = MarketDataRequest(
                start_date = "01 Jan 1996",                         # start date
                data_source = 'bloomberg',                          # use Bloomberg as data source
                tickers = tickers,
示例#16
0
# choose run_example = 0 for everything
# run_example = 1 - download BoE data from quandl

run_example = 0

###### fetch data from Quandl for BoE rate (using Bloomberg data)
if run_example == 1 or run_example == 0:
    # Monthly average of UK resident monetary financial institutions' (excl. Central Bank) sterling
    # Weighted average interest rate, other loans, new advances, on a fixed rate to private non-financial corporations (in percent)
    # not seasonally adjusted
    md_request = MarketDataRequest(
        start_date="01 Jan 2000",  # start date
        data_source='quandl',  # use Quandl as data source
        tickers=['Weighted interest rate'],
        fields=['close'],  # which fields to download
        vendor_tickers=['BOE/CFMBJ84'],  # ticker (Bloomberg)
        vendor_fields=['close'],  # which Bloomberg fields to download
        cache_algo='internet_load_return')  # how to return data

    df = market.fetch_market(md_request)

    style = Style()

    style.title = 'BoE weighted interest rate'
    style.scale_factor = 3
    style.file_output = "boe-rate.png"
    style.source = 'Quandl/BoE'

    chart.plot(df, style=style)
示例#17
0
    cred = ChartCred()
    quandl_api_key = cred.quandl_api_key
except:
    quandl_api_key = "x"

# choose run_example = 0 for everything
# run_example = 1 - xkcd example
# run_example = 2 - fun xkcd example
run_example = 0

if run_example == 1 or run_example == 0:
    df = Quandl.get(["FRED/A191RL1Q225SBEA"], authtoken=quandl_api_key)
    df.columns = ["Real QoQ"]

    # set the style of the plot
    style = Style(title="US GDP", source="Quandl/Fred", xkcd=True)

    # Chart object is initialised with the dataframe and our chart style
    chart = Chart(df=df, chart_type='line', style=style, engine='matplotlib')

    chart.plot()

if run_example == 2 or run_example == 0:
    import pandas, numpy

    dt = pandas.date_range(start="1 Jan 1950", end="1 Apr 2017", freq='M')
    data = numpy.arange(len(dt))
    df = pandas.DataFrame(index=dt, data=data, columns=['Importance'])

    # set the style of the plot
    style = Style(title="Importance of puns",
示例#18
0
###### Fetch market data for pricing GBPUSD FX options over Brexit vote (ie. FX spot, FX forwards, FX deposits and FX vol quotes)
###### Show how to plot ATM 1M implied_vol vol time series
if run_example == 1 or run_example == 0:

    # Download the whole all market data for GBPUSD for pricing options (vol surface)
    md_request = MarketDataRequest(start_date='01 May 2016',
                                   finish_date='01 Aug 2016',
                                   data_source='bloomberg',
                                   cut='LDN',
                                   category='fx-vol-market',
                                   tickers=['GBPUSD'],
                                   cache_algo='cache_algo_return')

    df = market.fetch_market(md_request)

    style = Style()

    style.title = 'GBPUSD 1M Implied Vol'
    style.scale_factor = 3
    style.source = 'Bloomberg'

    chart.plot(df['GBPUSDV1M.close'], style=style)

###### Fetch market data for pricing GBPUSD FX options over Brexit vote (ie. FX spot, FX forwards, FX deposits and FX vol quotes)
###### Construct volatility surface using FinancePy library underneath, using polynomial interpolation
if run_example == 2 or run_example == 0:

    horizon_date = '23 Jun 2016'

    # Download the whole all market data for GBPUSD for pricing options (vol surface)
    md_request = MarketDataRequest(start_date=horizon_date,
示例#19
0
    def run_day_of_month_analysis(self, trading_model, resample_freq='B'):
        from finmarketpy.economics.seasonality import Seasonality

        logger = LoggerManager().getLogger(__name__)

        calculations = Calculations()
        seas = Seasonality()
        trading_model.construct_strategy()
        pnl = trading_model.strategy_pnl()

        # Get seasonality by day of the month
        pnl = pnl.resample('B').mean()
        rets = calculations.calculate_returns(pnl).tz_localize(None)

        bus_day = seas.bus_day_of_month_seasonality(
            rets, add_average=True, resample_freq=resample_freq)

        # Get seasonality by month
        pnl = pnl.resample('BM').mean()
        rets = calculations.calculate_returns(pnl).tz_localize(None)
        month = seas.monthly_seasonality(rets)

        logger.info("About to plot seasonality...")
        style = Style()

        # Plotting spot over day of month/month of year
        style.color = 'Blues'
        style.scale_factor = trading_model.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.html'
        style.title = trading_model.FINAL_STRATEGY + ' day of month seasonality'
        style.display_legend = False
        style.color_2_series = [bus_day.columns[-1]]
        style.color_2 = ['red']  # red, pink
        style.linewidth_2 = 4
        style.linewidth_2_series = [bus_day.columns[-1]]
        style.y_axis_2_series = [bus_day.columns[-1]]

        self.chart.plot(bus_day, chart_type='line', style=style)

        style = Style()

        style.scale_factor = trading_model.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.html'
        style.title = trading_model.FINAL_STRATEGY + ' month of year seasonality'

        self.chart.plot(month, chart_type='line', style=style)

        return month
示例#20
0
    df = None
    df = market.fetch_market(md_request)

    calc = Calculations()
    df = calc.calculate_returns(df)

    es = EventStudy()

    # Work out cumulative asset price moves moves over the event
    df_event = es.get_intraday_moves_over_custom_event(df, df_event_times)

    # Create an average move
    df_event['Avg'] = df_event.mean(axis=1)

    # Plotting spot over economic data event
    style = Style()
    style.scale_factor = 3
    style.file_output = 'usdjpy-nfp.png'

    style.title = 'USDJPY spot moves over recent NFP'

    # Plot in shades of blue (so earlier releases are lighter, later releases are darker)
    style.color = 'Blues'
    style.color_2 = []
    style.y_axis_2_series = []
    style.display_legend = False

    # Last release will be in red, average move in orange
    style.color_2_series = [df_event.columns[-2], df_event.columns[-1]]
    style.color_2 = ['red', 'orange']  # red, pink
    style.linewidth_2 = 2
示例#21
0
    def run_day_of_month_analysis(self, trading_model):
        from finmarketpy.economics.seasonality import Seasonality

        calculations = Calculations()
        seas = Seasonality()
        trading_model.construct_strategy()
        pnl = trading_model.get_strategy_pnl()

        # get seasonality by day of the month
        pnl = pnl.resample('B').mean()
        rets = calculations.calculate_returns(pnl)
        bus_day = seas.bus_day_of_month_seasonality(rets, add_average = True)

        # get seasonality by month
        pnl = pnl.resample('BM').mean()
        rets = calculations.calculate_returns(pnl)
        month = seas.monthly_seasonality(rets)

        self.logger.info("About to plot seasonality...")
        style = Style()

        # Plotting spot over day of month/month of year
        style.color = 'Blues'
        style.scale_factor = self.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.html'
        style.title = trading_model.FINAL_STRATEGY + ' day of month seasonality'
        style.display_legend = False
        style.color_2_series = [bus_day.columns[-1]]
        style.color_2 = ['red'] # red, pink
        style.linewidth_2 = 4
        style.linewidth_2_series = [bus_day.columns[-1]]
        style.y_axis_2_series = [bus_day.columns[-1]]

        self.chart.plot(bus_day, chart_type='line', style=style)

        style = Style()

        style.scale_factor = self.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.html'
        style.title = trading_model.FINAL_STRATEGY + ' month of year seasonality'

        self.chart.plot(month, chart_type='line', style=style)

        return month
示例#22
0
import dash
import dash_core_components as dcc
import dash_html_components as html

# Imports for downloading market data and generating charts
from findatapy.market import Market, MarketDataRequest, MarketDataGenerator
from chartpy import Chart, Style

# For scaling realized volatility later
import math

# For plotting
style_vol = Style(plotly_plot_mode='dash',
                  title='Realized Volatilty',
                  width=1000,
                  height=500,
                  scale_factor=1)
style_spot = Style(plotly_plot_mode='dash',
                   title='Spot',
                   width=1000,
                   height=500,
                   scale_factor=1)
chart = Chart(engine='plotly')

# external CSS stylesheets
external_stylesheets = [
    'https://codepen.io/chriddyp/pen/bWLwgP.css', {
        'href':
        'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css',
        'rel': 'stylesheet',
示例#23
0
    # 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'

    Chart().plot(port, style = style)

###### backtest simple trend following strategy for FX spot basket
if run_example == 2 or run_example == 0:
    # for backtest and loading data
    from finmarketpy.backtest import Backtest, BacktestRequest
    from findatapy.market import Market, MarketDataRequest, MarketDataGenerator
    from findatapy.util.fxconv import FXConv
    from findatapy.timeseries import Calculations
                start_date = "01 Jan 1996",                         # start date
                data_source = 'bloomberg',                          # use Bloomberg as data source
                tickers = ['Gold'],
                fields = ['close'],                                 # which fields to download
                vendor_tickers = ['XAUUSD Curncy'],                 # ticker (Bloomberg)
                vendor_fields = ['PX_LAST'],                        # which Bloomberg fields to download
                cache_algo = 'internet_load_return')                # how to return data

    df = market.fetch_market(md_request)

    df_ret = calc.calculate_returns(df)

    day_of_month_seasonality = seasonality.bus_day_of_month_seasonality(df_ret, partition_by_month = False)
    day_of_month_seasonality = calc.convert_month_day_to_date_time(day_of_month_seasonality)

    style = Style()
    style.date_formatter = '%b'
    style.title = 'Gold seasonality'
    style.scale_factor = 3
    style.file_output = "gold-seasonality.png"

    chart.plot(day_of_month_seasonality, style=style)

###### Calculate seasonal moves in FX vol (using Bloomberg data)
if run_example == 2 or run_example == 0:
    tickers = ['EURUSDV1M', 'USDJPYV1M', 'GBPUSDV1M', 'AUDUSDV1M']

    md_request = MarketDataRequest(
                start_date = "01 Jan 1996",                         # start date
                data_source = 'bloomberg',                          # use Bloomberg as data source
                tickers = tickers,
示例#25
0
    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()

    # 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'

    Chart().plot(port, style=style)

###### backtest simple trend following strategy for FX spot basket
if run_example == 2 or run_example == 0:
    # for backtest and loading data
    from finmarketpy.backtest import Backtest, BacktestRequest
    from findatapy.market import Market, MarketDataRequest, MarketDataGenerator
    from findatapy.util.fxconv import FXConv
    from findatapy.timeseries import Calculations
示例#26
0
def dataframe_compliance_tca_example():
    """Get a DataFrame of trades and apply compliance based TCA to it
    """

    tca_engine = TCAEngineImpl(version=tca_version)

    spread_to_mid_bp = 0.1
    trade_order_list = ['trade_df']

    # Read in CSV file as a DataFrame
    trade_df = DatabaseSourceCSV(
        trade_data_database_csv=csv_trade_order_mapping['trade_df']
    ).fetch_trade_order_data()

    data_frame_trade_order_mapping = OrderedDict([('trade_df', trade_df)])

    ticker_list = FXConv().correct_unique_notation_list(
        trade_df['ticker'].unique().tolist())

    start_date = trade_df.index[0]
    finish_date = trade_df.index[-1]

    # Specify the TCA request
    tca_request = TCARequest(
        start_date=start_date,
        finish_date=finish_date,
        ticker=ticker_list,
        tca_type='aggregated',
        dummy_market=True,
        trade_data_store='dataframe',
        market_data_store=market_data_store,
        metric_calcs=[
            MetricSlippage(trade_order_list=trade_order_list),
            MetricTransientMarketImpact(
                transient_market_impact_gap={'ms': 100},
                trade_order_list=trade_order_list),
            MetricPermanentMarketImpact(permanent_market_impact_gap={'h': 1},
                                        trade_order_list=trade_order_list)
        ],
        benchmark_calcs=[  # add spread to mid fields for every market data spot
            BenchmarkSpreadToMid(bid_mid_bp=spread_to_mid_bp,
                                 ask_mid_bp=spread_to_mid_bp),
        ],
        results_form=[
            # Display a table of all the anomalous trades by slippage (ie. outside bid/ask)
            TableResultsForm(
                trade_order_list=['trade_df'],
                metric_name='slippage',
                filter_by='worst_all',  # Order by the worst slippage
                tag_value_combinations={'slippage_anomalous': 1.0},

                # Only flag trades outside bid/ask
                keep_fields=[
                    'executed_notional_in_reporting_currency', 'side'
                ],

                # Display only side and executed notionals
                round_figures_by=None),

            # Get the total notional executed by broker (in reporting currency)
            BarResultsForm(
                trade_order_list=['trade_df'],  # trade
                aggregate_by_field='broker_id',  # aggregate by broker name
                # keep_fields=['executed_notional_in_reporting_currency', 'executed_notional', 'side'],
                metric_name='executed_notional_in_reporting_currency',
                # analyse notional
                aggregation_metric='sum',  # sum the notional
                scalar=1,  # no need for a multipler
                round_figures_by=0),  # round to nearest unit

            # Get average slippage per broker (weighted by notional)
            BarResultsForm(
                trade_order_list=['trade_df'],
                aggregate_by_field='broker_id',
                metric_name='slippage',
                aggregation_metric='mean',
                # keep_fields=['executed_notional_in_reporting_currency', 'executed_notional',
                #             'side'],
                weighting_field='executed_notional_in_reporting_currency',
                # weight results by notional
                scalar=10000.0,
                round_figures_by=2)
        ],

        # Aggregate the results (total notional and slippage) by broker
        # into a single table for easy display to the user
        join_tables=[
            JoinTables(
                tables_dict={
                    'table_name':
                    'jointables_broker_id',

                    # fetch the following calculated tables
                    'table_list': [
                        'bar_trade_df_executed_notional_in_reporting_currency_by_broker_id',
                        'bar_trade_df_slippage_by_broker_id'
                    ],

                    # append to the columns of each table
                    'column_list': ['notional (rep cur)', 'slippage (bp)']
                })
        ],
        trade_order_mapping=data_frame_trade_order_mapping,
        use_multithreading=False)

    # Dictionary of dataframes as output from TCA calculation
    dict_of_df = tca_engine.calculate_tca(tca_request)

    # print all the output tables
    print(dict_of_df.keys())

    print('All trades')
    print(dict_of_df['trade_df'])

    print('Notional by broker ID')
    print(dict_of_df[
        'bar_trade_df_executed_notional_in_reporting_currency_by_broker_id'])

    print('Notional by broker ID and weighted slippage')
    print(dict_of_df['jointables_broker_id'])

    print('Trades by worst slippage')
    print(dict_of_df['table_trade_df_slippage_by_worst_all'])

    from chartpy import Canvas, Chart

    broker_notional_chart = Chart(
        engine='plotly',
        df=dict_of_df[
            'bar_trade_df_executed_notional_in_reporting_currency_by_broker_id'],
        chart_type='bar',
        style=Style(title='Notional in USD per broker'))

    broker_slippage_chart = Chart(
        engine='plotly',
        df=dict_of_df['bar_trade_df_slippage_by_broker_id'],
        chart_type='bar',
        style=Style(title='Slippage by broker (bp)'))

    # Using plain template
    canvas = Canvas([[broker_notional_chart, broker_slippage_chart]])

    canvas.generate_canvas(silent_display=False, canvas_plotter='plain')
示例#27
0
]

us_states_fred = [x + 'UR' for x in us_states]

# Either set API keys as an environment variable (preferred for security reasons)
# or replace these below, with your own keys (I can give you a FRED key because it's by application)
try:
    import os
    FRED_API_KEY = os.environ['FRED_API_KEY']
except:
    pass

# For plotting
style_une = Style(plotly_plot_mode='dash',
                  title='Unemployment Rate by state',
                  width=1000,
                  height=500,
                  scale_factor=1)
chart = Chart(engine='plotly')

# Create the layout
app_une.layout = html.Div(
    [
        html.H1('US unemployment by state'),

        # Drop down for selecting the asset
        dcc.Dropdown(id='dropdown-state',
                     options=[{
                         'label': c,
                         'value': c
                     } for c in us_states],
示例#28
0
    def run_arbitrary_sensitivity(self,
                                  trading_model,
                                  parameter_list=None,
                                  parameter_names=None,
                                  pretty_portfolio_names=None,
                                  parameter_type=None):

        assets = trading_model.load_assets()

        asset_df = assets[0]
        spot_df = assets[1]
        spot_df2 = assets[2]
        basket_dict = assets[3]

        contract_value_df = None

        if len(assets) == 5:  # for future use
            contract_value_df = assets[4]

        port_list = None
        ret_stats_list = []

        for i in range(0, len(parameter_list)):
            br = trading_model.load_parameters()

            current_parameter = parameter_list[i]

            # for calculating P&L
            for k in current_parameter.keys():
                setattr(br, k, current_parameter[k])

            trading_model.br = br  # for calculating signals

            signal_df = trading_model.construct_signal(spot_df, spot_df2,
                                                       br.tech_params, br)

            backtest = Backtest()
            self.logger.info("Calculating... " +
                             str(pretty_portfolio_names[i]))

            backtest.calculate_trading_PnL(br,
                                           asset_df,
                                           signal_df,
                                           contract_value_df=contract_value_df)
            ret_stats_list.append(backtest.portfolio_pnl_ret_stats())
            stats = str(backtest.portfolio_pnl_desc()[0])

            port = backtest.portfolio_cum().resample('B').mean()
            port.columns = [str(pretty_portfolio_names[i]) + ' ' + stats]

            if port_list is None:
                port_list = port
            else:
                port_list = port_list.join(port)

        # reset the parameters of the strategy
        trading_model.br = trading_model.load_parameters()

        style = Style()

        ir = [t.inforatio()[0] for t in ret_stats_list]

        # if we have too many combinations remove legend and use scaled shaded colour
        # if len(port_list) > 10:
        # style.color = 'Blues'
        # style.display_legend = False

        # plot all the variations
        style.resample = 'B'
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.html'
        style.scale_factor = trading_model.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type

        self.chart.plot(port_list, chart_type='line', style=style)

        # plot all the IR in a bar chart form (can be easier to read!)
        style = Style()
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.html'
        style.scale_factor = trading_model.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type
        summary = pandas.DataFrame(index=pretty_portfolio_names,
                                   data=ir,
                                   columns=['IR'])

        self.chart.plot(summary, chart_type='bar', style=style)

        return port_list
示例#29
0
# choose run_example = 0 for everything
# run_example = 1 - plot US GDP with multiple libraries
# run_example = 2 - plot US and UK unemployment demonstrating multiple line types
# run_example = 3 - correlations of a few different stocks in USA
run_example = 0

if run_example == 1 or run_example == 0:
    df = Quandl.get("FRED/GDP", authtoken=quandl_api_key)

    # we can use the charting tools in several ways
    chart = Chart()
    chart.plot(df=df)

    # set the style of the plot
    style = Style(title="US GDP", source="Quandl/Fred")

    # Chart object is initialised with the dataframe and our chart style
    chart = Chart(df=df, chart_type='line', style=style)

    # we now plot using multiple plotting libraries, with the same dataframe
    chart.plot(engine='matplotlib')
    chart.plot(engine='bokeh')
    chart.plot(engine='plotly')

if run_example == 2 or run_example == 0:
    # download US and Texas unemployment rate
    df = Quandl.get(["FRED/UNRATE", "FRED/TXUR"],
                    authtoken=quandl_api_key,
                    trim_start="2015-12-01")
示例#30
0
    def run_arbitrary_sensitivity(self,
                                  trading_model,
                                  parameter_list=None,
                                  pretty_portfolio_names=None,
                                  parameter_type=None,
                                  run_in_parallel=False,
                                  reload_market_data=True,
                                  plot=True):

        if not (reload_market_data):
            asset_df, spot_df, spot_df2, basket_dict, contract_value_df = self._load_assets(
                trading_model)

        port_list = []
        ret_stats_list = []

        if market_constants.backtest_thread_no[
                market_constants.generic_plat] > 1 and run_in_parallel:
            swim_pool = SwimPool(multiprocessing_library=market_constants.
                                 multiprocessing_library)

            pool = swim_pool.create_pool(
                thread_technique=market_constants.backtest_thread_technique,
                thread_no=market_constants.backtest_thread_no[
                    market_constants.generic_plat])

            mult_results = []

            for i in range(0, len(parameter_list)):
                # br = copy.copy(trading_model.load_parameters())
                # reset all parameters
                br = copy.copy(trading_model.load_parameters())

                current_parameter = parameter_list[i]

                # for calculating P&L, change the assets
                for k in current_parameter.keys():
                    setattr(br, k, current_parameter[k])
                    setattr(br.tech_params, k, current_parameter[k])

                # should specify reloading the data, if our parameters impact which assets we are fetching
                if reload_market_data:
                    asset_df, spot_df, spot_df2, basket_dict, contract_value_df = self._load_assets(
                        trading_model, br=br)

                mult_results.append(
                    pool.apply_async(self._run_strategy,
                                     args=(
                                         trading_model,
                                         asset_df,
                                         spot_df,
                                         spot_df2,
                                         br,
                                         contract_value_df,
                                         pretty_portfolio_names[i],
                                     )))

            for p in mult_results:
                port, ret_stats = p.get()

                port_list.append(port)
                ret_stats_list.append(ret_stats)

            try:
                swim_pool.close_pool(pool)
            except:
                pass

        else:
            for i in range(0, len(parameter_list)):
                # reset all parameters
                br = copy.copy(trading_model.load_parameters())

                current_parameter = parameter_list[i]

                # for calculating P&L
                for k in current_parameter.keys():
                    setattr(br, k, current_parameter[k])
                    setattr(br.tech_params, k, current_parameter[k])

                # should specify reloading the data, if our parameters impact which assets we are fetching
                if reload_market_data:
                    asset_df, spot_df, spot_df2, basket_dict, contract_value_df = self._load_assets(
                        trading_model, br=br)

                # br = copy.copy(trading_model.br)

                port, ret_stats = self._run_strategy(trading_model, asset_df,
                                                     spot_df, spot_df2, br,
                                                     contract_value_df,
                                                     pretty_portfolio_names[i])

                port_list.append(port)
                ret_stats_list.append(ret_stats)

        port_list = Calculations().join(port_list, how='outer')

        # reset the parameters of the strategy
        trading_model.br = trading_model.load_parameters()

        style = Style()

        ir = [t.inforatio()[0] for t in ret_stats_list]
        rets = [t.ann_returns()[0] for t in ret_stats_list]

        # if we have too many combinations remove legend and use scaled shaded colour
        # if len(port_list) > 10:
        # style.color = 'Blues'
        # style.display_legend = False

        # careful with plotting labels, may need to convert to strings
        pretty_portfolio_names = [str(p) for p in pretty_portfolio_names]

        # plot all the variations
        style.resample = 'B'
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.html'
        style.scale_factor = trading_model.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type

        if plot:
            self.chart.plot(port_list, chart_type='line', style=style)

        # plot all the IR in a bar chart form (can be easier to read!)
        style = Style()
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.html'
        style.scale_factor = trading_model.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type
        summary_ir = pandas.DataFrame(index=pretty_portfolio_names,
                                      data=ir,
                                      columns=['IR'])

        if plot:
            self.chart.plot(summary_ir, chart_type='bar', style=style)

        # plot all the rets
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' Rets.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' Rets.html'

        summary_rets = pandas.DataFrame(index=pretty_portfolio_names,
                                        data=rets,
                                        columns=['Rets (%)']) * 100

        if plot:
            self.chart.plot(summary_rets, chart_type='bar', style=style)

        return port_list, summary_ir, summary_rets
示例#31
0
    import quandl as Quandl
except:
    # if import fails use Quandl 2.x.x
    import Quandl

from chartpy import Chart, Style

# get your own free bQuandl API key from https://www.quandl.com/
try:
    from chartpy.chartcred import ChartCred

    cred = ChartCred()
    quandl_api_key = cred.quandl_api_key
except:
    quandl_api_key = "x"

# choose run_example = 0 for everything
# run_example = 1 - xkcd example
run_example = 0

if run_example == 1 or run_example == 0:
    df = Quandl.get(["FRED/A191RL1Q225SBEA"], authtoken=quandl_api_key)
    df.columns = ["Real QoQ"]

    # set the style of the plot
    style = Style(title="US GDP", source="Quandl/Fred", xkcd=True)

    # Chart object is initialised with the dataframe and our chart style
    chart = Chart(df=df, chart_type='line', style=style, engine='matplotlib')

    chart.plot()
示例#32
0
    quandl_api_key = cred.quandl_api_key
except:
    quandl_api_key = "x"

# choose run_example = 0 for everything
# run_example = 1 - plot US GDP QoQ (real) and nominal with Plotly/Bokeh/Matplotlib with subplots for each line
# run_example = 2 - plot US GDP QoQ (real + nominal) in two double plots (passing an array of dataframes)
run_example = 0

if run_example == 1 or run_example == 0:
    df = Quandl.get(["FRED/A191RL1Q225SBEA", "FRED/A191RP1Q027SBEA"],
                    authtoken=quandl_api_key)
    df.columns = ["Real QoQ", "Nominal QoQ"]

    # set the style of the plot
    style = Style(title="US GDP", source="Quandl/Fred", subplots=True)

    # Chart object is initialised with the dataframe and our chart style
    chart = Chart(df=df, chart_type='line', style=style)

    chart.plot(engine='matplotlib')
    chart.plot(engine='bokeh')
    chart.plot(engine='plotly')

if run_example == 2 or run_example == 0:
    df = Quandl.get(["FRED/A191RL1Q225SBEA", "FRED/A191RP1Q027SBEA"],
                    authtoken=quandl_api_key)
    df.columns = ["Real QoQ", "Nominal QoQ"]

    df = [df, df]
示例#33
0
    def run_arbitrary_sensitivity(self, trading_model, parameter_list = None, parameter_names = None,
                                  pretty_portfolio_names = None, parameter_type = None):

        asset_df, spot_df, spot_df2, basket_dict = trading_model.load_assets()

        port_list = None
        ret_stats_list = []

        for i in range(0, len(parameter_list)):
            br = trading_model.load_parameters()

            current_parameter = parameter_list[i]

            # for calculating P&L
            for k in current_parameter.keys():
                setattr(br, k, current_parameter[k])

            trading_model.br = br   # for calculating signals

            signal_df = trading_model.construct_signal(spot_df, spot_df2, br.tech_params, br)

            backtest = Backtest()
            self.logger.info("Calculating... " + str(pretty_portfolio_names[i]))

            backtest.calculate_trading_PnL(br, asset_df, signal_df)
            ret_stats_list.append(backtest.get_portfolio_pnl_ret_stats())
            stats = str(backtest.get_portfolio_pnl_desc()[0])

            port = backtest.get_cumportfolio().resample('B').mean()
            port.columns = [str(pretty_portfolio_names[i]) + ' ' + stats]

            if port_list is None:
                port_list = port
            else:
                port_list = port_list.join(port)

        # reset the parameters of the strategy
        trading_model.br = trading_model.load_parameters()

        style = Style()

        ir = [t.inforatio()[0] for t in ret_stats_list]

        # if we have too many combinations remove legend and use scaled shaded colour
        # if len(port_list) > 10:
            # style.color = 'Blues'
            # style.display_legend = False

        # plot all the variations
        style.resample = 'B'
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.html'
        style.scale_factor = self.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type

        self.chart.plot(port_list, chart_type='line', style=style)

        # plot all the IR in a bar chart form (can be easier to read!)
        style = Style()
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.html'
        style.scale_factor = self.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type
        summary = pandas.DataFrame(index = pretty_portfolio_names, data = ir, columns = ['IR'])

        self.chart.plot(summary, chart_type='bar', style=style)

        return port_list
示例#34
0
    print(df)

    # Compare the close and actual release of US GDP (and the final)
    md_request = MarketDataRequest(
        start_date="01 Jun 2000",                                                      # start date (download data over past decade)
        data_source='alfred',                                                          # use ALFRED/FRED as data source
        tickers=['US GDP'],                                                            # ticker
        fields=['actual-release', 'close'],                                            # which fields to download
        vendor_tickers=['GDP'],                                                        # ticker (FRED)
        vendor_fields=['actual-release', 'close'])                                     # which FRED fields to download

    df = market.fetch_market(md_request)

    from chartpy import Chart, Style

    style = Style(title="US GDP first vs last")

    Chart().plot(df, style=style)

    # Get the change NFP SA (need to calculate that from the acutal-release and first-revision)
    md_request = MarketDataRequest(
        start_date="01 Jun 2000",                                                      # start date (download data over past decade)
        data_source='alfred',                                                          # use ALFRED/FRED as data source
        tickers=['US NFP'],                                                            # ticker
        fields=['actual-release', 'first-revision', 'release-date-time-full'],         # which fields to download
        vendor_tickers=['PAYEMS'],                                                     # ticker (FRED)
        vendor_fields=['actual-release', 'first-revision', 'release-date-time-full'])  # which FRED fields to download

    df = market.fetch_market(md_request)

    # calculate the headline change in NFP
示例#35
0
    from findatapy.timeseries import Calculations
    calc = Calculations()

    tick_data = market.fetch_market(md_request)
    intraday_spot_df = calc.resample_tick_data_ohlc(tick_data, 'GBPUSD', freq='1min')

    vol_stats = VolStats(market_df=market_df, intraday_spot_df=intraday_spot_df)

    realized_vol = vol_stats.calculate_realized_vol('GBPUSD', tenor_label="ON", freq='intraday', freq_min_mult=1,
                                                    hour_of_day=10, minute_of_day=0, field='close', timezone_hour_minute='America/New_York') * 100

    implied_vol = pd.DataFrame(market_df['GBPUSDVON.close'])

    vrp = vol_stats.calculate_vol_risk_premium('GBPUSD', tenor_label='ON', implied_vol=implied_vol, realized_vol=realized_vol)

    style = Style()

    style.title = 'GBPUSD ON volatility over Brexit'
    style.scale_factor = 3
    style.source = 'Bloomberg'

    # Plot all the volatility metrics
    chart.plot(vrp, style=style)

    # Plot the implied volatility bumped forward a day against the realized volatility calculated over that day
    chart.plot(vrp[['GBPUSDUON.close', 'GBPUSDHON.close']], style=style)

###### Calculating realized volatility over Brexit vote in GBPUSD in ON/overnight tenor
# Showing the difference between time frequencies
if run_example == 2 or run_example == 0:
示例#36
0
        vendor_tickers=['NFP TCH Index'],   # ticker (Bloomberg)
        cache_algo='internet_load_return')  # how to return data

    df_event_times = market.fetch_market(md_request)
    df_event_times = pandas.DataFrame(index=df_event_times['NFP.release-date-time-full'])

    es = EventStudy()

    # work out cumulative asset price moves moves over the event
    df_event = es.get_intraday_moves_over_custom_event(df, df_event_times)

    # create an average move
    df_event['Avg'] = df_event.mean(axis=1)

    # plotting spot over economic data event
    style = Style()
    style.scale_factor = 3
    style.file_output = 'usdjpy-nfp.png'

    style.title = 'USDJPY spot moves over recent NFP'

    # plot in shades of blue (so earlier releases are lighter, later releases are darker)
    style.color = 'Blues';
    style.color_2 = []
    style.y_axis_2_series = []
    style.display_legend = False

    # last release will be in red, average move in orange
    style.color_2_series = [df_event.columns[-2], df_event.columns[-1]]
    style.color_2 = ['red', 'orange']  # red, pink
    style.linewidth_2 = 2
###### 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)
示例#38
0
    def plot_chart(self,
                   tickers=None,
                   tickers_rhs=None,
                   start_date=None,
                   finish_date=None,
                   chart_file=None,
                   chart_type='line',
                   title='',
                   fields={'close': 'PX_LAST'},
                   freq='daily',
                   source='Web',
                   brand_label='Cuemacro',
                   display_brand_label=True,
                   reindex=False,
                   additive_index=False,
                   yoy=False,
                   plotly_plot_mode='offline_png',
                   quandl_api_key=dataconstants.quandl_api_key,
                   fred_api_key=dataconstants.fred_api_key,
                   alpha_vantage_api_key=dataconstants.alpha_vantage_api_key,
                   df=None):

        if start_date is None:
            start_date = datetime.datetime.utcnow().date() - timedelta(days=60)
        if finish_date is None:
            finish_date = datetime.datetime.utcnow()

        if isinstance(tickers, str):
            tickers = {tickers: tickers}
        elif isinstance(tickers, list):
            tickers_dict = {}

            for t in tickers:
                tickers_dict[t] = t

            tickers = tickers_dict

        if tickers_rhs is not None:
            if isinstance(tickers_rhs, str):
                tickers_rhs = {tickers_rhs: tickers_rhs}
            elif isinstance(tickers, list):
                tickers_rhs_dict = {}

                for t in tickers_rhs:
                    tickers_rhs_dict[t] = t

                tickers_rhs = tickers_rhs_dict

            tickers.update(tickers_rhs)
        else:
            tickers_rhs = {}

        if df is None:
            md_request = MarketDataRequest(
                start_date=start_date,
                finish_date=finish_date,
                freq=freq,
                data_source=self._data_source,
                tickers=list(tickers.keys()),
                vendor_tickers=list(tickers.values()),
                fields=list(fields.keys()),
                vendor_fields=list(fields.values()),
                quandl_api_key=quandl_api_key,
                fred_api_key=fred_api_key,
                alpha_vantage_api_key=alpha_vantage_api_key)

            df = self._market.fetch_market(md_request=md_request)

        df = df.fillna(method='ffill')
        df.columns = [x.split('.')[0] for x in df.columns]

        style = Style(title=title,
                      chart_type=chart_type,
                      html_file_output=chart_file,
                      scale_factor=-1,
                      height=400,
                      width=600,
                      file_output=datetime.date.today().strftime("%Y%m%d") +
                      " " + title + ".png",
                      plotly_plot_mode=plotly_plot_mode,
                      source=source,
                      brand_label=brand_label,
                      display_brand_label=display_brand_label)

        if reindex:
            df = Calculations().create_mult_index_from_prices(df)

            style.y_title = 'Reindexed from 100'

        if additive_index:
            df = (df - df.shift(1)).cumsum()

            style.y_title = 'Additive changes from 0'

        if yoy:
            if freq == 'daily':
                obs_in_year = 252
            elif freq == 'intraday':
                obs_in_year = 1440

            df_rets = Calculations().calculate_returns(df)
            df = Calculations().average_by_annualised_year(
                df_rets, obs_in_year=obs_in_year) * 100

            style.y_title = 'Annualized % YoY'

        if list(tickers_rhs.keys()) != []:
            style.y_axis_2_series = list(tickers_rhs.keys())
            style.y_axis_2_showgrid = False
            style.y_axis_showgrid = False

        return self._chart.plot(df, style=style), df
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)