def time_guarantee(my_contract, year_ini, year_end, my_df): if my_contract == 'ES': my_guarantee = 12000 * 2 # between 4000$ and 30000$ my_multiplier = 50 my_tic = 0.25 if my_contract == 'YM': my_guarantee = 10000 * 2 # between 3000$ and 25000$ my_multiplier = 5 my_tic = 1 if my_contract == 'NQ': my_guarantee = 15000 * 2 # between 5000$ and 45000$ my_multiplier = 20 my_tic = 0.25 if my_contract == 'CL': my_guarantee = 5000 * 2 # between 2000$ and 18000$ my_multiplier = 1000 my_tic = 0.01 if my_contract == 'RTY': my_guarantee = 7000 * 2 # between 2000$ and 20000$ my_multiplier = 5 my_tic = 1 int_date_0 = year_ini * 10000 + 1 * 100 + 1 # the day d0 = date(2008, 8, 18) int_date_1 = year_end * 10000 + 12 * 100 + 31 # the day d0 = date(2008, 8, 18) int_date_01 = mda.locate_1st_day('Y', year_ini, my_df, 'date') int_date_11 = mda.locate_lst_day('Y', year_end, my_df, 'date') d1 = my_df['date'][int_date_11] d0 = my_df['date'][int_date_01] delta = d1 - d0 delta_sec = delta.total_seconds() delta_years = delta_sec / (365 * 24 * 60 * 60) return my_multiplier, my_guarantee, delta_years, my_tic
def short_long_operationLSEX(my_contract, year_ini, year_end, my_df, limit_qty, target_qty, my_multiplier): pd.options.mode.chained_assignment = None # default='warn' This is to avoid warnings of chained-assignment # creation of new columns in datframe my_df['operation'] = ' ' my_df['target'] = np.zeros(len(my_df)).astype('float') my_df['limit'] = np.zeros(len(my_df)).astype('float') my_df['result-pts'] = np.zeros(len(my_df)).astype('float') my_df['result-accum-pts'] = np.zeros(len(my_df)).astype('float') my_df['contract-qty'] = np.zeros(len(my_df)).astype('float') my_df['multiplier'] = np.zeros(len(my_df)).astype('float') my_df['result-amt'] = np.zeros(len(my_df)).astype('float') my_df['result-accum-amt'] = np.zeros(len(my_df)).astype('float') # define variables to use long = 'L' # short = 'S' # operation = long # operation_ant = long # limit = 0.0 # limit_ant = 0.0 # target = 0.0 # target_ant = 0.0 # limit_qty_rng = limit_qty ########## target_qty_rng = target_qty ############ range_trail = 0.0 # axis_size = 0.0 # tic = 0.0 # contract_qty = 1.0 # # define variable for operation long_accum = 0.0 # short_accum = 0.0 # result_accum_amt = 0.0 # result_accum_pts = 0.0 # limit_nxt = 0 - limit_qty_rng * axis_size # target_nxt = 0 + target_qty_rng * axis_size # operation_nxt = long # target_reached = 0 # # locate the initial and final position pt_ini = mda.locate_1st_day('Y', year_ini, my_df, 'date') # locate the initial index pt_end = mda.locate_lst_day('Y', year_end, my_df, 'date') # locate the final index # the loop for ind in range(pt_ini, pt_end + 1): range_trail = my_df['range-trail'][ind] long_result = my_df['long-rst'][ind] short_result = my_df['short-rst'][ind] axis_size = range_trail if ind == pt_ini: limit_nxt = 0 - limit_qty_rng * axis_size target_nxt = 0 + target_qty_rng * axis_size operation = operation_nxt limit = limit_nxt target = target_nxt long_accum = long_accum + long_result short_accum = short_accum + short_result continue_no_changes = 1 if operation == long: result_accum_pts = result_accum_pts + long_result - tic result_accum_amt = result_accum_amt + ( long_result - tic) * contract_qty * my_multiplier if (long_accum < limit) & (continue_no_changes == 1): # overcome the limit and change to shorts operation_ant = long operation_nxt = short limit_ant = limit limit_nxt = short_accum - (limit_qty_rng * axis_size) target_ant = target target_nxt = short_accum + (target_qty_rng * axis_size) continue_no_changes = 0 if ((target_reached == 1) & (long_result <= ((0 - 1) * range_trail * 0.2)) & (continue_no_changes == 1)): # target reached with reverse of 20% operation_ant = long operation_nxt = short limit_ant = limit limit_nxt = short_accum - (limit_qty_rng * axis_size) target_ant = target target_nxt = short_accum + (target_qty_rng * axis_size) target_reached = 0 continue_no_changes = 0 if ((long_accum > target) & (target_reached == 0) & (continue_no_changes == 1)): # target reached but waiting reverse target_reached = 1 operation_ant = operation operation_nxt = operation_ant limit_ant = limit limit_nxt = limit_ant target_ant = target target_nxt = target_ant continue_no_changes = 0 if operation == short: result_accum_pts = result_accum_pts + short_result - tic result_accum_amt = result_accum_amt + short_result * contract_qty * my_multiplier if (short_accum < limit) & (continue_no_changes == 1): # overcome limit and change operation_ant = short operation_nxt = long limit_ant = limit limit_nxt = long_accum - (limit_qty_rng * axis_size) target_ant = target target_nxt = long_accum + (target_qty_rng * axis_size) continue_no_changes = 0 if ((target_reached == 1) & (short_result <= ((0 - 1) * range_trail * 0.2)) & (continue_no_changes == 1)): # target reached with reverse of 20% and change operation_ant = short operation_nxt = long limit_ant = limit limit_nxt = long_accum - (limit_qty_rng * axis_size) target_ant = target target_nxt = long_accum + (target_qty_rng * axis_size) target_reached = 0 continue_no_changes = 0 if ((short_accum > target) & (target_reached == 0) & (continue_no_changes == 1)): # target reached but waiting reverse target_reached = 1 operation_ant = operation operation_nxt = operation_ant limit_ant = limit limit_nxt = limit target_ant = target target_nxt = target continue_no_changes = 0 if continue_no_changes == 1: # nothing happen, no target reached, no limit overcame, continue the same operation_ant = operation operation_nxt = operation_ant limit_ant = limit limit_nxt = limit_ant target_ant = target target_nxt = target_ant # save the daily operation my_df['operation'][ind] = operation_ant my_df['target'][ind] = target_ant my_df['limit'][ind] = limit_ant my_df['result-pts'][ ind] = result_accum_pts - my_df['result-accum-pts'][ind - 1] my_df['result-accum-pts'][ind] = result_accum_pts my_df['contract-qty'][ind] = contract_qty my_df['multiplier'][ind] = my_multiplier my_df['result-amt'][ ind] = result_accum_amt - my_df['result-accum-amt'][ind - 1] my_df['result-accum-amt'][ind] = result_accum_amt return my_df
def short_long_operationFLIP(my_contract, year_ini, year_end, my_df, my_multiplier): pd.options.mode.chained_assignment = None # default='warn' This is to avoid warnings of chained-assignment # creation of new columns in datframe my_df['operation'] = ' ' my_df['target'] = np.zeros(len(my_df)).astype('float') my_df['limit'] = np.zeros(len(my_df)).astype('float') my_df['result-pts'] = np.zeros(len(my_df)).astype('float') my_df['result-accum-pts'] = np.zeros(len(my_df)).astype('float') my_df['contract-qty'] = np.zeros(len(my_df)).astype('float') my_df['multiplier'] = np.zeros(len(my_df)).astype('float') my_df['result-amt'] = np.zeros(len(my_df)).astype('float') my_df['result-accum-amt'] = np.zeros(len(my_df)).astype('float') # define variables to use long = 'L' # short = 'S' # operation_ant = long # tic = 0.0 # contract_qty = 1.0 # # define variable for operation long_accum = 0.0 # short_accum = 0.0 # result_accum_amt = 0.0 # result_accum_pts = 0.0 # operation_nxt = long # # locate the initial and final position pt_ini = mda.locate_1st_day('Y', year_ini, my_df, 'date') # locate the initial index pt_end = mda.locate_lst_day('Y', year_end, my_df, 'date') # locate the final index # the loop for ind in range(pt_ini, pt_end + 1): range_trail = my_df['range-trail'][ind] long_result = my_df['long-rst'][ind] short_result = my_df['short-rst'][ind] axis_size = range_trail if ind == pt_ini: operation_nxt = long operation = operation_nxt long_accum = long_accum + long_result short_accum = short_accum + short_result continue_no_changes = 1 if operation == long: result_accum_pts = result_accum_pts + long_result - tic result_accum_amt = result_accum_amt + ( long_result - tic) * contract_qty * my_multiplier if (long_result < 0) & (continue_no_changes == 1): # bad long performance and change to shorts operation_ant = long operation_nxt = short continue_no_changes = 0 if operation == short: result_accum_pts = result_accum_pts + short_result - tic result_accum_amt = result_accum_amt + short_result * contract_qty * my_multiplier if (short_result < 0) & (continue_no_changes == 1): # bad short performance and change to long operation_ant = short operation_nxt = long continue_no_changes = 0 if continue_no_changes == 1: # nothing happen, continue the same operation_ant = operation operation_nxt = operation_ant # save the daily operation my_df['operation'][ind] = operation_ant my_df['target'][ind] = 0 my_df['limit'][ind] = 0 my_df['result-pts'][ ind] = result_accum_pts - my_df['result-accum-pts'][ind - 1] my_df['result-accum-pts'][ind] = result_accum_pts my_df['contract-qty'][ind] = contract_qty my_df['multiplier'][ind] = my_multiplier my_df['result-amt'][ ind] = result_accum_amt - my_df['result-accum-amt'][ind - 1] my_df['result-accum-amt'][ind] = result_accum_amt return my_df
def market_operation(df_int, df_day, timetable_ini, timetable_end): """ This function complete a dataframe df_day with open, close-long, close-short for a daily base in a specific timetable You can realise that we are going to operate long and short. On the other hand we will use the order STOP-TRAIL -Parameters ---------- df_int : dataframe with original data (intraday movements of a future contract) ['date', 'time', 'open', 'max', 'min', 'close', 'volume'] df_day : new dataframe to create with columns ['date', 'timetable', 'max', 'min', 'range', range-avg, range-trail]. We will add the following columns ['open', 'close-long', 'close-short', 'close', long-rst, short-rst, long-acc, short-acc] timetable_ini : time the operation start everyday. Example='09:00' timetable_end : time the operation finish everyday. Example='18:00' -Returns ------- nothing The function fill the dataframe df_day with daily ['open', 'long-close', 'short-close', 'close', 'long-rst', 'short-rst', 'long-acc', 'short-acc'] """ pd.options.mode.chained_assignment = None # default='warn' This is to avoid warnings of chained-assignment # Create new columns in the dataframe and fill them with zeros df_day['open'] = np.zeros(len(df_day)).astype('float') df_day['long-close'] = np.zeros(len(df_day)).astype('float') df_day['short-close'] = np.zeros(len(df_day)).astype('float') df_day['close'] = np.zeros(len(df_day)).astype('float') df_day['long-rst'] = np.zeros(len(df_day)).astype('float') df_day['short-rst'] = np.zeros(len(df_day)).astype('float') df_day['long-acc'] = np.zeros(len(df_day)).astype('float') df_day['short-acc'] = np.zeros(len(df_day)).astype('float') pt_ini_date = df_day['date'][df_day[df_day['range-trail'] != 0].index[0]].year * 10000 + \ df_day['date'][df_day[df_day['range-trail'] != 0].index[0]].month * 100 + \ df_day['date'][df_day[df_day['range-trail'] != 0].index[0]].day pt_end_date = df_day['date'][df_day[df_day['range-trail'] != 0].index[-1]].year * 10000 + \ df_day['date'][df_day[df_day['range-trail'] != 0].index[-1]].month * 100 + \ df_day['date'][df_day[df_day['range-trail'] != 0].index[-1]].day pt_ini = mda.locate_1st_day('D', pt_ini_date, df_int, 'datetime_EU') pt_end = mda.locate_1st_day('D', pt_end_date, df_int, 'datetime_EU') # testing dataframe is create only for testing purpose. We will delete this part after good perform confirmation testing = pd.DataFrame(columns=[ 'datetime_EU', 'open', 'max', 'min', 'close', 'long-stop', 'short-stop', 'long-exit', 'short-exit', 'long-operation', 'short-operation', 'max-day', 'min-day', 'range-trail' ]) testing['open'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['max'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['min'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['close'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['long-stop'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['short-stop'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['long-exit'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['short-exit'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['long-operation'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['short-operation'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['max-day'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['min-day'] = np.zeros(pt_end - pt_ini + 1).astype('float') testing['range-trail'] = np.zeros(pt_end - pt_ini + 1).astype('float') # Define variables to use operation = 0 # (1 in market, 0 out market) entry = 0.0 long_stop = 0.0 short_stop = 0.0 long_exit = 0.0 short_exit = 0.0 long_operation = 0 short_operation = 0 maximum = 0.0 minimum = 0.0 market_exit = 0.0 hour_ini = int(timetable_ini[0:2]) # initial values minu_ini = int(timetable_ini[3:]) hour_end = int(timetable_end[0:2]) minu_end = int(timetable_end[3:]) tmtb_ini = hour_ini * 100 + minu_ini # integer value tmtb_end = hour_end * 100 + minu_end # integer value my_int_date_ant = 0 # define my date ant my_long_acc = 0.0 # long result accumulator my_short_acc = 0.0 # short result accumulator j = 0 # counter for testing for ind in range(pt_ini, pt_end + 1): # the main loop my_int_date = df_int['datetime_EU'][ind].year * 10000 + df_int['datetime_EU'][ind].month * 100 + \ df_int['datetime_EU'][ind].day # the day if my_int_date > my_int_date_ant: # This is a new day my_int_date_ant = my_int_date my_loc = mda.locate_1st_day('D', my_int_date, df_day, 'date') # locate the day in the df_day range_trail = df_day['range-trail'][my_loc] # take the range-trail my_int_time = df_int['datetime_EU'][ind].hour * 100 + df_int[ 'datetime_EU'][ind].minute # the time # checking the bar time is >= than start time of timetable and < than end time of timetable ==>> operation in market if (my_int_time >= tmtb_ini) & (my_int_time < tmtb_end): if (my_int_time == tmtb_ini) & ( operation == 0): # The moment to start operation entry = df_int['open'][ind] long_stop = entry - range_trail short_stop = entry + range_trail long_exit = df_int['close'][ind] short_exit = df_int['close'][ind] long_operation = 1 short_operation = 1 maximum = df_int['max'][ind] minimum = df_int['min'][ind] operation = 1 if operation == 1: if short_operation == 1: short_exit = df_int['close'][ind] if long_operation == 1: long_exit = df_int['close'][ind] if (df_int['max'][ind] > short_stop) & ( short_operation == 1): # if the price overcome short_stop (up) short_exit = short_stop short_operation = 0 if (df_int['min'][ind] < long_stop) & ( long_operation == 1): # if the price overcome long_stop (down) long_exit = long_stop long_operation = 0 if (long_stop < df_int['max'][ind] - range_trail) & ( long_operation == 1): # up long stop long_stop = df_int['max'][ind] - range_trail if (short_stop > df_int['min'][ind] + range_trail) & ( short_operation == 1): # down short stop short_stop = df_int['min'][ind] + range_trail if (my_int_time == tmtb_end) & ( long_operation == 1): # long operation end because finish timetable long_operation = 0 if (my_int_time == tmtb_end) & ( short_operation == 1): # short operation end because finish timetable short_operation = 0 if df_int['max'][ ind] > maximum: # update maximum if it is the case maximum = df_int['max'][ind] if df_int['min'][ ind] < minimum: # update minimum if it is the case minimum = df_int['min'][ind] market_exit = df_int['close'][ind] elif (my_int_time == tmtb_end) & ( operation == 1): # timetable finish and save the result df_day['open'][my_loc] = entry df_day['long-close'][my_loc] = long_exit df_day['short-close'][my_loc] = short_exit df_day['close'][my_loc] = market_exit df_day['long-rst'][my_loc] = long_exit - entry df_day['short-rst'][my_loc] = entry - short_exit my_long_acc = my_long_acc + long_exit - entry df_day['long-acc'][my_loc] = my_long_acc my_short_acc = my_short_acc + entry - short_exit df_day['short-acc'][my_loc] = my_short_acc operation = 0 # Re-init variables entry = 0.0 long_stop = 0.0 short_stop = 0.0 long_exit = 0.0 short_exit = 0.0 long_operation = 0 short_operation = 0 maximum = 0.0 minimum = 0.0 market_exit = 0.0 # Save tracking information to check the function works only for debuging and testing j += 1 testing['datetime_EU'][j] = df_int['datetime_EU'][ind] testing['open'][j] = df_int['open'][ind] testing['max'][j] = df_int['max'][ind] testing['min'][j] = df_int['min'][ind] testing['close'][j] = df_int['close'][ind] testing['long-stop'][j] = long_stop testing['short-stop'][j] = short_stop testing['long-exit'][j] = long_exit testing['short-exit'][j] = short_exit testing['long-operation'][j] = long_operation testing['short-operation'][j] = short_operation testing['max-day'][j] = maximum testing['min-day'][j] = minimum testing['range-trail'][j] = range_trail return df_day, testing