Example #1
0
def get_event_center_dates(events):
    return [Timing(evt.timing_descriptor).center_date for evt in events]
Example #2
0
def get_event_timeline(events: 'list of events' = None,
                       symbol: 'str' = '',
                       expiries=None):
    """Get the Event Timeline in the form of a graph for a list of events.
    Symbol is an optional parameters to display in the Graph Title"""

    # Establish Event Information: events, center_dates, mean_moves
    events = filter_discrete_events(events)
    dates = get_event_center_dates(events)
    event_mean_moves = get_event_mean_moves(events)

    # Establish Color Scheme for Scatter Plot. Each unique event type has a different color (e.g. all Earnings events are the same color)
    unique_event_types = set([type(evt) for evt in events])
    color_scheme = OrderedDict([
        (t[1], t[0]) for t in enumerate(unique_event_types, start=1)
    ])

    # Assign color and size for each scatter point
    event_types = [type(evt) for evt in events]
    scatter_colors = [color_scheme[event_type] for event_type in event_types]
    scatter_sizes = [mean_move * 100 * 50 for mean_move in event_mean_moves]

    # Instantiate the subplot
    fig, ax1 = plt.subplots(1)

    # Instantiate the Scatter Plot
    # The height of each scatter point is proportional to the event magnitude.
    bar_heights = event_mean_moves

    ax1.scatter(dates,
                bar_heights,
                c=scatter_colors,
                marker='s',
                s=scatter_sizes)

    # Set Error Bars for the scatter plot; for now no y_error_bars, although depending on how it looks, I would like to display the magnitude of events scenarios.
    x_error_bars = [
        Timing(evt.timing_descriptor).timing_duration * .475 for evt in events
    ]
    y_error_bars = [0 for i in range(len(dates))]
    #y_error_bars = [evt.event_width*.475 for evt in events]

    ax1.errorbar(dates,
                 bar_heights,
                 xerr=x_error_bars,
                 yerr=y_error_bars,
                 ls='none')

    # Create a thin bar graph extending the x-axis to each scatter point to enhance visualization.
    bar_width = .5

    ax1.bar(dates,
            bar_heights,
            width=bar_width,
            label='Event Mean Move',
            color='black'
            #marker='s',
            #s = 250
            )

    # Annotate the Scatter Points with the Event Names
    for i in range(len(events)):
        ax1.annotate(text=repr(events[i]),
                     xy=(dates[i], event_mean_moves[i]),
                     xytext=(dates[i], event_mean_moves[i] + .025075 +
                             scatter_sizes[i] * .01 * .01 * .025),
                     ha='center',
                     fontsize=10.0)
    """
    texts = []
    for i in range(len(events)):
        txt = ax1.text(dates[i],
                       event_mean_moves[i],
                      repr(events[i]),
                       ha='center',
                       fontsize=10.0)
        texts.append(txt)
    adjust_text(texts)
    """

    #fig.autofmt_xdate()
    # Set x and y Tick Marks, Tick Labels, and Axis Labels.
    # Set Tick Marks
    # X Ticks
    xticks = [
        dt.date(2018,
                dt.date.today().month, 1) + relativedelta(months=i)
        for i in range(13)
    ]
    ax1.set_xticks(xticks)

    # Y Ticks
    yticks = np.arange(0, max(event_mean_moves) + .05, .05)
    ax1.set_yticks(yticks)

    # Set Tick Labels
    ax1.set_xticklabels([t.strftime('%-m/%-d/%y') for t in xticks])
    ax1.set_yticklabels(["{:.1f}%".format(y * 100) for y in ax1.get_yticks()])

    # Set Axis Labels
    ax1.xaxis.label.set_color('darkblue')
    ax1.yaxis.label.set_color('darkblue')
    ax1.title.set_color('saddlebrown')

    # Set Ticks Font Size, tick Rotations, and y-axis Location
    ticks_fontsize = 10.0
    plt.xticks(rotation=45, fontsize=ticks_fontsize)
    plt.yticks(fontsize=ticks_fontsize)
    ax1.xaxis.set_ticks_position('bottom')
    ax1.yaxis.tick_right()

    # Set X-Axis Min and Max Dates.
    min_date = min(
        [Timing(evt.timing_descriptor).event_start_date for evt in events])
    max_date = max(
        [Timing(evt.timing_descriptor).event_end_date for evt in events])
    timeD = timedelta(20)
    plt.xlim(min_date - timeD, max_date + timeD)

    # Set Font and Size for x, y Axis Labels and Title
    axis_label_fontsize = 12
    plt.xlabel('Date', fontsize=axis_label_fontsize, fontweight='bold')
    plt.ylabel('Event Magnitude',
               fontsize=axis_label_fontsize,
               fontweight='bold')
    plt.title('{} Event Calendar'.format(symbol),
              fontsize=axis_label_fontsize * 1.5,
              fontweight='bold')

    # Potential options I do not want to use for now. These commands turn off default features.
    """
    ax1.yaxis.set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.spines['left'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    """

    # For now I don't want to see term structure in the graph, but keeping the code here as an option for later.
    """ 
    # Show Term Structure as a Line Graph
    if expiries is not None:
        expiries = [date for date in xticks if date > dt.date.today()]
        term_struc = term_structure(events, expiries, metric = 'IV', mc_iterations = 10**5)
        vols = term_struc.iloc[[term_struc.index.get_loc(1.00, method='nearest')], :].values.tolist()[0]
        print(zip(expiries, vols))
        ax1.plot(expiries,
                 vols,
                 #label = 'Term_Structure',
                 #color = 'black'
                 #marker='s',
                 #s = 250
                 )
        
        ax1.set_yticks(np.arange(0, max(vols)+.05, .05))
    """

    # Set High-Level Graph Parameters; Show Graph
    #ax1.grid(True)
    ax1.title.set_position([.525, 1.025])
    fig.patch.set_facecolor('xkcd:off white')
    ax1.patch.set_facecolor('xkcd:pale grey')
    fig.tight_layout()
    fig.set_size_inches(8, 5)
    plt.legend(loc='best')
    plt.show()
    return fig, ax1
from All_Events import sorted_events
from option_model.Event_Module import IdiosyncraticVol, TakeoutEvent, Earnings
from option_model.Timing_Module import Timing
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
from datetime import timedelta

events = [
    evt for evt in sorted_events
    if not isinstance(evt, (IdiosyncraticVol, TakeoutEvent))
]
dates = [Timing(evt.timing_descriptor).center_date for evt in events]
event_mean_moves = [evt.get_distribution().mean_move for evt in events]

color_scheme = {'Earnings': 1, 'Other': 2}

event_types = []
for evt in events:
    if isinstance(evt, Earnings):
        event_types.append('Earnings')
    else:
        event_types.append('Other')

scatter_colors = [color_scheme[event_type] for event_type in event_types]
scatter_sizes = [(mean_move * 100) * 50 for mean_move in event_mean_moves]
print(events, dates, event_mean_moves, event_types, sep='\n')

#X = pd.to_datetime(dates)
bar_heights = event_mean_moves
fig, ax1 = plt.subplots(1)
Example #4
0
 def sorted_events(self):
     return sorted(
         self.events,
         key=lambda evt: Timing(evt.timing_descriptor).center_date)
        spread_price = spread_pricing(options, quantities, event_groupings[i], event_grouping_names[i], mc_iterations)
        spread_prices.append(spread_price)
    
    info = {'Level': event_grouping_names,
            'Spread': spread_prices}

    info = pd.DataFrame(info).set_index('Level')
    return info

option_type = 'Put'
expiry = dt.date(2018, 8, 1)

option1 = Option(option_type, .975, expiry)
option2 = Option(option_type, .925, expiry)
option3 = Option(option_type, .95, expiry)

options = [option1, option2, option3]
quantities = [1, -1, 0]

spread_prices = spread_pricing_bid_ask(options, quantities, event_groupings, event_grouping_names, mc_iterations = 10**6)
print(spread_prices.round(3))
print(events_bid, events, events_ask, end = "\n")

sorted_events = sorted(events, key=lambda evt: Timing(evt.timing_descriptor).center_date)
pprint(sorted_events)

timing_descriptors = [evt.timing_descriptor for evt in events]
pprint(timing_descriptors)
center_dates = [Timing(timing_descriptor).center_date for timing_descriptor in timing_descriptors]
pprint(center_dates)
def get_event_timeline(events: 'list of events', symbol: 'str' = ''):
    events = [
        evt for evt in events
        if not isinstance(evt, (IdiosyncraticVol, TakeoutEvent))
    ]
    dates = [Timing(evt.timing_descriptor).center_date for evt in events]
    event_mean_moves = [evt.get_distribution().mean_move for evt in events]

    color_scheme = {'Earnings': 1, 'Other': 2}
    event_classes = set([type(evt) for evt in events])

    color_scheme = {}
    i = 1
    for cls in event_classes:
        color_scheme[cls] = i
        i += 1

    event_types = []
    for evt in events:
        event_types.append(type(evt))

    scatter_colors = [color_scheme[event_type] for event_type in event_types]
    scatter_sizes = [(mean_move * 100) * 50 for mean_move in event_mean_moves]

    print(events, dates, event_mean_moves, event_types, sep='\n')

    bar_heights = event_mean_moves
    fig, ax1 = plt.subplots(1)
    ax1.scatter(dates,
                bar_heights,
                c=scatter_colors,
                marker='s',
                s=scatter_sizes)

    # Set Error Bars
    x_error_bars = [timedelta(5) for i in range(len(dates))]
    y_error_bars = [0 for i in range(len(dates))]
    x_error_bars = [
        Timing(evt.timing_descriptor).timing_duration * .475 for evt in events
    ]
    #y_error_bars = [evt.event_width*.475 for evt in events]

    ax1.errorbar(dates,
                 bar_heights,
                 xerr=x_error_bars,
                 yerr=y_error_bars,
                 ls='none')

    bar_width = .5

    ax1.bar(dates,
            bar_heights,
            width=bar_width,
            label='Event Mean Move',
            color='black'
            #marker='s',
            #s = 250
            )

    for i in range(len(events)):
        ax1.annotate(s=repr(events[i]),
                     xy=(dates[i], event_mean_moves[i]),
                     xytext=(dates[i], event_mean_moves[i] + .00875 +
                             scatter_sizes[i] * .01 * .01 * .025),
                     ha='center',
                     fontsize=10.0)

    #fig.autofmt_xdate()
    # everything after this is turning off stuff that's plotted by default
    """
    ax1.yaxis.set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.spines['left'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    """
    #xticks = [dt.date.today(), dt.date(2018, 7, 1), dt.date(2018, 10, 1), dt.date(2019, 1, 1)]
    #xticks = dates
    xticks = [dt.date(2018, m, 1) for m in [4, 5, 6, 6, 7, 8, 9, 10, 11, 12]]
    ax1.set_xticks(xticks)
    ax1.set_yticks(np.arange(0, .20, .05))
    ax1.set_xticklabels([t.strftime('%-m/%-d/%y') for t in xticks])
    ax1.set_yticklabels(["{:.1f}%".format(y * 100) for y in ax1.get_yticks()])
    ax1.xaxis.label.set_color('darkblue')
    ax1.yaxis.label.set_color('darkblue')
    ax1.title.set_color('saddlebrown')

    axes_fontsize = 10.0
    plt.xticks(rotation=45, fontsize=axes_fontsize)
    plt.yticks(fontsize=axes_fontsize)
    ax1.xaxis.set_ticks_position('bottom')
    #ax1.tick_params(axis ='y', direction = 'in', pad = -35)
    ax1.yaxis.tick_right()
    #ax1.get_yaxis().set_ticklabels([])

    min_date = min(
        [Timing(evt.timing_descriptor).event_start_date for evt in events])
    max_date = max(
        [Timing(evt.timing_descriptor).event_end_date for evt in events])
    timeD = timedelta(20)
    plt.xlim(min_date - timeD, max_date + timeD)

    label_fontsize = 12
    plt.xlabel('Date', fontsize=label_fontsize, fontweight='bold')
    plt.ylabel('Event Magnitude', fontsize=label_fontsize, fontweight='bold')
    #plt.yticks(np.arange(0, .3, .025))
    plt.title('{} Event Calendar'.format(symbol),
              fontsize=label_fontsize * 1.5,
              fontweight='bold')

    ax1.title.set_position([.525, 1.025])
    #ax1.grid(True)
    fig.patch.set_facecolor('xkcd:off white')
    ax1.patch.set_facecolor('xkcd:pale grey')
    fig.tight_layout()
    fig.set_size_inches(8, 5)
    plt.legend()
    plt.show()
from All_Events import sorted_events
from option_model.Event_Module import IdiosyncraticVol, TakeoutEvent
from option_model.Timing_Module import Timing
import matplotlib.pyplot as plt
#plt.style.use('bmh')
import numpy as np
import datetime as dt
from datetime import timedelta


events = [evt for evt in sorted_events if not isinstance(evt, (IdiosyncraticVol, TakeoutEvent))]
dates = [Timing(evt.timing_descriptor).center_date for evt in events]
event_mean_moves = [evt.get_distribution().mean_move for evt in events]

color_scheme = {'Earnings': 1, 'Other': 2}
event_classes = set([type(evt) for evt in events])

color_scheme = {}
i = 1
for cls in event_classes:
    color_scheme[cls] = i
    i += 1
    
event_types = []
for evt in events:
    event_types.append(type(evt))

scatter_colors = [color_scheme[event_type] for event_type in event_types]
scatter_sizes = [(mean_move*100)*50 for mean_move in event_mean_moves] 

print(events, dates, event_mean_moves, event_types, sep='\n')