示例#1
0
def timing_test_for_distribution_addition_2():
    # Define Events
    event1 = TakeoutEvent('CLVS', 1)
    event2 = SysEvt_PresElection('CLVS', .2)
    event3 = Earnings('CLVS', .2, 'Q2_2018')
    event4 = Earnings('CLVS', .005, 'Q3_2018')
    event5 = Earnings('CLVS', .3, 'Q4_2018')

    expiry = dt.date(2018, 5, 1)
    events = [event2, event3]
    added_distribution = event1.get_distribution(expiry)
    for event in events:
        added_distribution += event.get_distribution()
    rprint(added_distribution.mean_move)
示例#2
0
def create_earnings_events(stocks: 'list of stocks'):
    """Create Earnings Events for a List of Stocks based on Random Dates"""
    event_names = ['Q4_2018', 'Q1_2019', 'Q2_2019', 'Q3_2019']
    q4_date_range = list(
        pd.date_range(dt.date(2018, 10, 1), dt.date(2018, 12, 30)))

    earnings_events = []
    for stock in stocks:
        # Set Event Input
        event_input = random.uniform(.03, .08)

        # Set Earnings Dates
        q4_date = random.choice(q4_date_range)
        timing_descriptors = [
            q4_date, q4_date + timedelta(90), q4_date + timedelta(180),
            q4_date + timedelta(270)
        ]

        # Instantiate Earnings Events and append to main list
        for i in range(4):
            earnings_evt = Earnings(stock, event_input, timing_descriptors[i],
                                    event_names[i])

            earnings_events.append(earnings_evt)

    return earnings_events
示例#3
0
def get_earnings_events():
    earns_evts = []
    for earns in get_earnings_table().itertuples():
        earns_evt = Earnings(
            earns.stock, earns.event_input,
            dt.datetime.strptime(earns.timing_descriptor, '%Y-%m-%d').date(),
            earns.event_name)
        earns_evts.append(earns_evt)
    return earns_evts
示例#4
0
def get_earnings_events(symbol=None):
    conn = sqlite3.connect('earnings.db', check_same_thread=False)
    c = conn.cursor()
    with conn:
        if symbol is None:
            c.execute("SELECT * FROM earnings")
        else:
            c.execute("SELECT * FROM earnings WHERE stock=:stock",
                      {'stock': symbol})
        return [Earnings(*params) for params in c.fetchall()]
def get_earnings_events(symbol=None):
    if symbol is None:
        earns_evts = []
        for earns in get_earnings_table().itertuples():
            earns_evt = Earnings(
                earns.stock, earns.event_input,
                dt.datetime.strptime(earns.timing_descriptor,
                                     '%Y-%m-%d').date(), earns.event_name)
            earns_evts.append(earns_evt)
        return earns_evts
    else:
        c.execute("SELECT * FROM earnings WHERE stock=:stock",
                  {'stock': symbol})
        return [instantiate_earnings_event(params) for params in c.fetchall()]
示例#6
0
        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


if __name__ == '__main__':
    events = [Event('CRBP', .05, 'Q3_2018'), Earnings('CRBP', .075, 'Q3_2018')]
    get_event_timeline(events)
    mc_distribution = get_total_mc_distribution(event_grouping, expiry)
    return get_vol_surface_from_mc_distribution(mc_distribution, expiry)


def get_vol_surface(events, expiry):
    return get_vol_surface_from_event_grouping(events, expiry)


def get_vol_surface_spline(vol_surface):
    strikes = vol_surface.index.values.tolist()
    vols = vol_surface.iloc[:, 0].values.tolist()
    return interp1d(strikes, vols, kind='cubic')


#---------------------------------I optimize for speed again below here---------------------------------------------#
EarningsDist = Earnings('CRBP', .05, 'Q2_2018').get_distribution(
    dt.date(2018, 7, 1)).mc_simulation(10**4)
IdiosyncraticVolDist = IdiosyncraticVol(
    'CRBP', .10).get_distribution(dt.date.today() +
                                  timedelta(365)).mc_simulation(10**4)


#@my_time_decorator
def get_total_mc_distribution(events,
                              expiry=None,
                              symbol=None,
                              mc_iterations=10**4):
    """Add the simulation results of individual events to return the total simulated distribution."""
    """
    events = [evt for evt in events if event_prob_by_expiry(evt.timing_descriptor, expiry) > 0]
    distributions = map(lambda evt: evt.get_distribution(expiry), events)
    mc_distributions = map(lambda dist: dist.mc_simulation(mc_iterations), distributions)
from option_model.Event_Module import Earnings
import datetime as dt
from utility.decorators import my_time_decorator

event = Earnings('CRBP', .075, 'Q2_2018')

distribution = event.get_distribution(dt.date(2018, 5, 20))


@my_time_decorator
def get_simulation_results(mc_iterations):
    return distribution.mc_simulation(mc_iterations)


simulation_results = list(get_simulation_results(10**7))
print(type(simulation_results), len(simulation_results))


@my_time_decorator
def list_of_list(my_list):
    return list(my_list)


list_of_list(simulation_results)
示例#9
0
def instantiate_earnings_event(
        params: 'tuple of Earnings params from sqlite db'):
    return Earnings(*params)
示例#10
0
def instantiate_timer(params, n=1):
    for i in range(n):
        evt = Earnings(*params)
示例#11
0
def get_earnings_events(symbol=None):
    if symbol is None:
        c.execute("SELECT * FROM earnings")
    else:
        c.execute("SELECT * FROM earnings WHERE stock=:stock", {'stock': symbol})
    return [Earnings(*params) for params in c.fetchall()]
示例#12
0
def instantiate_earnings_event_2(params):
    return Earnings(*params)
示例#13
0
def instantiate(n=1):
    for i in range(n):
        evt = Earnings(*params)
expiry7 = dt.date(2018, 11, 21)
expiry8 = dt.date(2018, 12, 21)
expiries = [expiry1, expiry2, expiry3, expiry4, expiry5, expiry6]
expiries = [expiry1, expiry3, expiry5]
expiries = [expiry3]

# Define Events
event8_info = pd.read_excel('CLVS_RiskScenarios.xlsx',
                         header = [0],
                         index_col = [0,1],
                         sheet_name = 'Sub_States')

idio = IdiosyncraticVol('CLVS', .05)
takeout = TakeoutEvent('CLVS', 2)
pres_elec = SysEvt_PresElection('CLVS', .02)
earns_q2 = Earnings('CLVS', .05, 'Q2_2018', 'Q2_Earnings')
earns_q3 = Earnings('CLVS', .05, 'Q3_2018', 'Q3_Earnings')
earns_q4 = Earnings('CLVS', .05, 'Q4_2018', 'Q4_Earnings')
event5 = Event('CLVS', .1, 'Q2_2018', 'FDA_Approval')
data = Event('CLVS', Distribution(pd.read_csv('CLVS.csv')), 'Q2_2018', 'Ph3_Data')
elagolix = ComplexEvent('CLVS', Distribution_MultiIndex(event8_info), 'Q2_2018', 'Elagolix_Approval')
events = [idio, takeout,  earns_q2, earns_q3, earns_q4, elagolix]
#events = [takeout]
#events = [idio, elagolix]

events_bid = [event.event_bid for event in events]
events_ask = [event.event_ask for event in events]
events_high_POS = [idio, elagolix.event_high_prob_success]
events_low_POS = [idio, elagolix.event_low_prob_success]
events_max_optionality = [idio, elagolix.event_max_optionality]
示例#15
0
def instantiate_earnings_event(params: 'tuple of Earnings params'):
    evt = Earnings(params[0], params[1],
                   dt.datetime.strptime(params[2], '%Y-%m-%d').date(),
                   params[3])
    return evt
示例#16
0
def earnings_params_to_earnings_event(params):
    evt = Earnings(params[0], params[1],
                   dt.datetime.strptime(params[2], '%Y-%m-%d').date(),
                   params[3])
    return evt