<s> '''<STR_LIT>''' <EOL> from datetime import timedelta <EOL> import pandas as pand <EOL> import numpy as np <EOL> from copy import deepcopy <EOL> from qstkutil import tsutil as tsu <EOL> def _calculate_leverage ( values_by_stock , ts_leverage , ts_long_exposure , ts_short_exposure , ts_net_exposure ) : <EOL> """<STR_LIT>""" <EOL> for r_index , r_val in values_by_stock . iterrows ( ) : <EOL> f_long = <NUM_LIT:0> <EOL> f_short = <NUM_LIT:0> <EOL> for val in r_val . values [ : - <NUM_LIT:1> ] : <EOL> if np . isnan ( val ) == False : <EOL> if val >= <NUM_LIT:0> : <EOL> f_long = f_long + val <EOL> else : <EOL> f_short = f_short + val <EOL> f_lev = ( f_long + abs ( f_short ) ) / ( f_long + r_val . values [ - <NUM_LIT:1> ] + f_short ) <EOL> f_net = ( f_long - abs ( f_short ) ) / ( f_long + r_val . values [ - <NUM_LIT:1> ] + f_short ) <EOL> f_long_ex = ( f_long ) / ( f_long + r_val . values [ - <NUM_LIT:1> ] + f_short ) <EOL> f_short_ex = ( abs ( f_short ) ) / ( f_long + r_val . values [ - <NUM_LIT:1> ] + f_short ) <EOL> if np . isnan ( f_lev ) : f_lev = <NUM_LIT:0> <EOL> if np . isnan ( f_net ) : f_net = <NUM_LIT:0> <EOL> if np . isnan ( f_long ) : f_long = <NUM_LIT:0> <EOL> if np . isnan ( f_short ) : f_short = <NUM_LIT:0> <EOL> ts_leverage = ts_leverage . append ( pand . Series ( f_lev , index = [ r_index ] ) ) <EOL> ts_long_exposure = ts_long_exposure . append ( pand . Series ( f_long_ex , index = [ r_index ] ) ) <EOL> ts_short_exposure = ts_short_exposure . append ( pand . Series ( f_short_ex , index = [ r_index ] ) ) <EOL> ts_net_exposure = ts_net_exposure . append ( pand . Series ( f_net , index = [ r_index ] ) ) <EOL> return ts_leverage , ts_long_exposure , ts_short_exposure , ts_net_exposure <EOL> def _monthly_turnover ( ts_orders , ts_fund ) : <EOL> order_val_month = <NUM_LIT:0> <EOL> last_date = ts_orders . index [ <NUM_LIT:0> ] <EOL> b_first_month = True <EOL> ts_turnover = "<STR_LIT:None>" <EOL> for date in ts_orders . index : <EOL> if last_date . month == date . month : <EOL> order_val_month += ts_orders . ix [ date ] <EOL> else : <EOL> if b_first_month == True : <EOL> ts_turnover = pand . Series ( order_val_month , index = [ last_date ] ) <EOL> b_first_month = False <EOL> else : <EOL> ts_turnover = ts_turnover . append ( pand . Series ( order_val_month , index = [ last_date ] ) ) <EOL> order_val_month = ts_orders . ix [ date ] <EOL> last_date = date <EOL> if type ( ts_turnover ) != type ( "<STR_LIT:None>" ) : <EOL> ts_turnover = ts_turnover . append ( pand . Series ( order_val_month , index = [ last_date ] ) ) <EOL> else : <EOL> ts_turnover = pand . Series ( order_val_month , index = [ last_date ] ) <EOL> order_month = <NUM_LIT:0> <EOL> len_orders = len ( ts_turnover . index ) <EOL> last_date = ts_fund . index [ <NUM_LIT:0> ] <EOL> for date in ts_fund . index : <EOL> if order_month < len_orders : <EOL> if ( ts_turnover . index [ order_month ] ) . month != date . month : <EOL> ts_turnover . ix [ ts_turnover . index [ order_month ] ] = ts_turnover . ix [ ts_turnover . index [ order_month ] ] / ( <NUM_LIT:2> * ts_fund . ix [ last_date ] ) <EOL> order_month += <NUM_LIT:1> <EOL> else : <EOL> last_date = date <EOL> else : <EOL> if date . month == last_date . month : <EOL> last_date = date <EOL> else : <EOL> break <EOL> ts_turnover . ix [ ts_turnover . index [ - <NUM_LIT:1> ] ] = ts_turnover . ix [ ts_turnover . index [ - <NUM_LIT:1> ] ] / ( <NUM_LIT:2> * ts_fund . ix [ last_date ] ) <EOL> return ts_turnover <EOL> def _normalize ( row ) : <EOL> """<STR_LIT>""" <EOL> total = row . abs ( ) . sum ( ) <EOL> proportion = row / total <EOL> return proportion <EOL> def _nearest_interger ( f_x ) : <EOL> """<STR_LIT>""" <EOL> if f_x >= <NUM_LIT:0> : <EOL> return np . floor ( f_x ) <EOL> else : <EOL> return np . ceil ( f_x ) <EOL> def tradesim ( alloc , df_historic , f_start_cash , i_leastcount = <NUM_LIT:1> , <EOL> b_followleastcount = False , f_slippage = <NUM_LIT:0.0> , <EOL> f_minimumcommision = <NUM_LIT:0.0> , f_commision_share = <NUM_LIT:0.0> , <EOL> i_target_leverage = <NUM_LIT:1> , f_rate_borrow = <NUM_LIT:0.0> , log = "<STR_LIT:false>" , b_exposure = False ) : <EOL> """<STR_LIT>""" <EOL> if alloc . index [ - <NUM_LIT:1> ] > df_historic . index [ - <NUM_LIT:1> ] : <EOL> print "<STR_LIT>" <EOL> indices , = np . where ( alloc . index <= df_historic . index [ - <NUM_LIT:1> ] ) <EOL> alloc = alloc . reindex ( index = alloc . index [ indices ] ) <EOL> if alloc . index [ <NUM_LIT:0> ] < df_historic . index [ <NUM_LIT:0> ] : <EOL> print "<STR_LIT>" <EOL> indices , = np . where ( alloc . index >= df_historic . index [ <NUM_LIT:0> ] ) <EOL> alloc = alloc . reindex ( index = alloc . index [ indices ] ) <EOL> if log != "<STR_LIT:false>" : <EOL> log_file = open ( log , "<STR_LIT:w>" ) <EOL> if log != "<STR_LIT:false>" : <EOL> print "<STR_LIT>" + log <EOL> log_file . write ( "<STR_LIT>" ) <EOL> df_historic [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> shares = ( alloc . ix [ <NUM_LIT:0> : <NUM_LIT:1> ] * <NUM_LIT:0.0> ) <EOL> shares [ '<STR_LIT>' ] = f_start_cash <EOL> prediction_shares = deepcopy ( shares ) <EOL> f_total_commision = <NUM_LIT:0.0> <EOL> f_total_slippage = <NUM_LIT:0.0> <EOL> f_total_borrow = <NUM_LIT:0.0> <EOL> cashleft = <NUM_LIT:0.0> <EOL> no_trans_fund = <NUM_LIT:0.0> <EOL> dt_last_date = None <EOL> f_last_borrow = <NUM_LIT:0.0> <EOL> b_first_iter = True <EOL> b_order_flag = True <EOL> for row_index , row in alloc . iterrows ( ) : <EOL> trade_price = df_historic . ix [ row_index : ] . ix [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> trade_index = df_historic . index . searchsorted ( trade_price . index [ <NUM_LIT:0> ] ) <EOL> pred_index = trade_index - <NUM_LIT:1> <EOL> prediction_price = df_historic . ix [ df_historic . index [ pred_index ] : ] . ix [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> prediction_date = prediction_price . index [ <NUM_LIT:0> ] <EOL> trade_date = trade_price . index [ <NUM_LIT:0> ] <EOL> if b_first_iter == True : <EOL> if log != "<STR_LIT:false>" : <EOL> log_file . write ( "<STR_LIT>" + str ( prediction_date ) + "<STR_LIT>" + str ( f_start_cash ) + "<STR_LIT>" ) <EOL> ts_fund = pand . Series ( f_start_cash , index = [ prediction_date ] ) <EOL> no_trans_fund = pand . Series ( f_start_cash , index = [ prediction_date ] ) <EOL> ts_leverage = pand . Series ( <NUM_LIT:0> , index = [ prediction_date ] ) <EOL> ts_long_exposure = pand . Series ( <NUM_LIT:0> , index = [ prediction_date ] ) <EOL> ts_short_exposure = pand . Series ( <NUM_LIT:0> , index = [ prediction_date ] ) <EOL> ts_net_exposure = pand . Series ( <NUM_LIT:0> , index = [ prediction_date ] ) <EOL> ts_orders = None <EOL> days_since_alloc = <NUM_LIT:0> <EOL> f_borrow_cost = <NUM_LIT:0.0> <EOL> b_first_iter = False <EOL> else : <EOL> to_calculate = df_historic [ ( df_historic . index <= prediction_date ) & ( df_historic . index > ts_fund . index [ - <NUM_LIT:1> ] ) ] <EOL> values_by_stock = to_calculate * shares . ix [ - <NUM_LIT:1> ] <EOL> ts_fund = ts_fund . append ( values_by_stock . sum ( axis = <NUM_LIT:1> ) ) <EOL> no_trans_fund = no_trans_fund . append ( values_by_stock . sum ( axis = <NUM_LIT:1> ) - cashleft ) <EOL> ts_leverage , ts_long_exposure , ts_short_exposure , ts_net_exposure = _calculate_leverage ( <EOL> values_by_stock , ts_leverage , ts_long_exposure , ts_short_exposure , ts_net_exposure ) <EOL> days_since_alloc = ( trade_date - dt_last_date ) . days <EOL> f_borrow_cost = abs ( ( days_since_alloc * f_last_borrow * f_rate_borrow ) / ( <NUM_LIT:100> * <NUM_LIT> ) ) <EOL> f_total_borrow = f_total_borrow + f_borrow_cost <EOL> proportion = _normalize ( row ) <EOL> '''<STR_LIT>''' <EOL> proportion = proportion * i_target_leverage <EOL> value_allotted = proportion * ts_fund . ix [ - <NUM_LIT:1> ] <EOL> value_before_trade = ts_fund . ix [ - <NUM_LIT:1> ] <EOL> prediction_shares = value_allotted / prediction_price <EOL> ntva = proportion * no_trans_fund . ix [ - <NUM_LIT:1> ] <EOL> no_trans_shares = ntva / prediction_price <EOL> if b_followleastcount == True : <EOL> prediction_shares /= i_leastcount <EOL> prediction_shares = prediction_shares . apply ( _nearest_interger ) <EOL> prediction_shares *= i_leastcount <EOL> no_trans_shares /= i_leastcount <EOL> no_trans_shares = no_trans_shares . apply ( _nearest_interger ) <EOL> no_trans_shares *= i_leastcount <EOL> cash_delta_less_shares = deepcopy ( shares ) <EOL> cash_delta_less_shares [ "<STR_LIT>" ] = cash_delta_less_shares [ "<STR_LIT>" ] - cashleft <EOL> same = <NUM_LIT:1> <EOL> for sym in shares : <EOL> if str ( cash_delta_less_shares [ sym ] . values [ <NUM_LIT:0> ] ) != str ( no_trans_shares [ sym ] . values [ <NUM_LIT:0> ] ) : <EOL> same = <NUM_LIT:0> <EOL> if same == <NUM_LIT:0> : <EOL> order = pand . Series ( ( ( prediction_shares . values - shares . values ) [ <NUM_LIT:0> ] ) [ : - <NUM_LIT:1> ] , index = row . index [ : - <NUM_LIT:1> ] ) <EOL> f_transaction_cost = <NUM_LIT:0> <EOL> for index in order . index : <EOL> val = abs ( order [ index ] ) <EOL> if ( val != <NUM_LIT:0> ) : <EOL> t_cost = max ( f_minimumcommision , f_commision_share * val ) <EOL> f_transaction_cost = f_transaction_cost + t_cost <EOL> f_total_commision = f_total_commision + f_transaction_cost <EOL> value_before_trade = ( ( trade_price * shares . ix [ - <NUM_LIT:1> ] ) . sum ( axis = <NUM_LIT:1> ) ) . ix [ - <NUM_LIT:1> ] <EOL> shares = prediction_shares <EOL> value_after_trade = ( ( trade_price * shares . ix [ - <NUM_LIT:1> ] ) . sum ( axis = <NUM_LIT:1> ) ) . ix [ - <NUM_LIT:1> ] <EOL> f_slippage_cost = f_slippage * ( trade_price . values [ <NUM_LIT:0> ] [ : - <NUM_LIT:1> ] ) * order . values <EOL> f_slippage_cost = abs ( f_slippage_cost ) <EOL> f_slippage_cost [ np . isnan ( f_slippage_cost ) ] = <NUM_LIT:0.0> <EOL> f_slippage_cost = f_slippage_cost . sum ( ) <EOL> f_order = ( <NUM_LIT:1> + f_slippage ) * ( trade_price . values [ <NUM_LIT:0> ] [ : - <NUM_LIT:1> ] ) * order . values <EOL> f_order = abs ( f_order ) <EOL> f_order [ np . isnan ( f_order ) ] = <NUM_LIT:0.0> <EOL> f_order = f_order . sum ( ) <EOL> if b_order_flag == True : <EOL> ts_orders = pand . Series ( f_order , index = [ trade_date ] ) <EOL> b_order_flag = False <EOL> else : <EOL> ts_orders = ts_orders . append ( pand . Series ( f_order , index = [ trade_date ] ) ) <EOL> if np . isnan ( f_slippage_cost ) == False : <EOL> f_total_slippage = f_total_slippage + f_slippage_cost <EOL> cashleft = value_before_trade - value_after_trade - f_transaction_cost - f_slippage_cost - f_borrow_cost <EOL> else : <EOL> cashleft = value_before_trade - value_after_trade - f_transaction_cost - f_borrow_cost <EOL> dt_last_date = trade_date <EOL> f_last_holding = ( ( trade_price * shares . ix [ - <NUM_LIT:1> ] ) . ix [ - <NUM_LIT:1> ] ) . values <EOL> indices = np . where ( f_last_holding < <NUM_LIT:0> ) <EOL> f_last_borrow = abs ( sum ( f_last_holding [ indices ] ) ) <EOL> shares [ '<STR_LIT>' ] = shares [ '<STR_LIT>' ] + cashleft <EOL> money_short = f_last_borrow <EOL> indices_long = np . where ( f_last_holding >= <NUM_LIT:0> ) <EOL> money_long = abs ( sum ( f_last_holding [ indices_long ] ) ) <EOL> money_cash = cashleft <EOL> GL = ( money_long + money_short ) / ( money_long - money_short + money_cash ) <EOL> NL = ( money_long - money_short ) / ( money_long - money_short + money_cash ) <EOL> for sym in shares : <EOL> if sym != "<STR_LIT>" : <EOL> f_stock_commission = max ( f_minimumcommision , f_commision_share * abs ( order [ sym ] ) ) <EOL> order_type = "<STR_LIT>" <EOL> if ( order [ sym ] < <NUM_LIT:0> ) : <EOL> if ( shares [ sym ] < <NUM_LIT:0> ) : <EOL> order_type = "<STR_LIT>" <EOL> else : <EOL> order_type = "<STR_LIT>" <EOL> elif shares [ sym ] < <NUM_LIT:0> : <EOL> order_type = "<STR_LIT>" <EOL> if log != "<STR_LIT:false>" : <EOL> if ( abs ( order [ sym ] ) != <NUM_LIT:0> ) : <EOL> log_file . write ( str ( sym ) + "<STR_LIT:U+002C>" + str ( sym ) + "<STR_LIT:U+002C>" + order_type + "<STR_LIT:U+002C>" + str ( trade_date ) + "<STR_LIT:U+002C>" + str ( GL ) + "<STR_LIT:U+002C>" + str ( NL ) + "<STR_LIT:U+002C>" + str ( order [ sym ] ) + "<STR_LIT:U+002C>" + str ( trade_price [ sym ] . values [ <NUM_LIT:0> ] ) + "<STR_LIT:U+002C>" + str ( trade_price [ sym ] . values [ <NUM_LIT:0> ] * order [ sym ] ) + "<STR_LIT:U+002C>" + str ( shares [ sym ] . ix [ - <NUM_LIT:1> ] ) + "<STR_LIT:U+002C>" + str ( value_after_trade ) + "<STR_LIT:U+002C>" + str ( f_stock_commission ) + "<STR_LIT:U+002C>" + str ( round ( f_slippage_cost , <NUM_LIT:2> ) ) + "<STR_LIT:U+002C>" ) <EOL> log_file . write ( "<STR_LIT:\n>" ) <EOL> if log != "<STR_LIT:false>" : <EOL> log_file . write ( "<STR_LIT>" + str ( prediction_date ) + "<STR_LIT>" + str ( <NUM_LIT:0> ) + "<STR_LIT>" ) <EOL> log_file . close ( ) <EOL> ts_turnover = _monthly_turnover ( ts_orders , ts_fund ) <EOL> if b_exposure : <EOL> return ( ts_fund , ts_leverage , f_total_commision , f_total_slippage , f_total_borrow , <EOL> ts_long_exposure , ts_short_exposure , ts_net_exposure , ts_turnover ) <EOL> return ( ts_fund , ts_leverage , f_total_commision , f_total_slippage , f_total_borrow ) <EOL> def tradesim_comb ( df_alloc , d_data , f_start_cash , i_leastcount = <NUM_LIT:1> , <EOL> b_followleastcount = False , f_slippage = <NUM_LIT:0.0> , <EOL> f_minimumcommision = <NUM_LIT:0.0> , f_commision_share = <NUM_LIT:0.0> , <EOL> i_target_leverage = <NUM_LIT:1> , f_rate_borrow = <NUM_LIT:0.0> , log = "<STR_LIT:false>" , b_exposure = False ) : <EOL> """<STR_LIT>""" <EOL> df_close = d_data [ '<STR_LIT>' ] <EOL> df_open = d_data [ '<STR_LIT>' ] <EOL> f_shift_close = <NUM_LIT> - df_close . index [ <NUM_LIT:0> ] . hour <EOL> f_shift_open = <NUM_LIT> - df_open . index [ <NUM_LIT:0> ] . hour <EOL> df_new_close = df_close . shift ( <NUM_LIT:1> , timedelta ( hours = f_shift_close ) ) <EOL> df_new_open = df_open . shift ( <NUM_LIT:1> , timedelta ( hours = f_shift_open ) ) <EOL> df_combined = df_new_close . append ( df_new_open ) . sort ( ) <EOL> return tradesim ( df_alloc , df_combined , f_start_cash , i_leastcount , <EOL> b_followleastcount , f_slippage , f_minimumcommision , <EOL> f_commision_share , i_target_leverage , f_rate_borrow , log , b_exposure ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print "<STR_LIT>" </s>
