SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     def returns_in_Fxn_1(n, return_all=False):
  2.         df_in = IndFxn_Speed(df_data_in, n, iMethod, s, sPrice)
  3.         df_in = df_in.replace(' ', np.nan, regex = True)  
  4.        
  5.         if df_in.empty:
  6.             return False;
  7.        
  8.        
  9.         df_in['MA'] = np.around(df_in['MA'], decimals=8) # Round to avoid floating point issues with Excel & VBA
  10.         df_in['MA_Position'] = np.where(df_in[sPrice] > df_in['MA'], 1, 0)                    
  11.         df_in['MA_Position'] = np.where(df_in[sPrice] < df_in['MA'], -1, df_in.MA_Position)
  12.         df_in['MA_Position'] = np.where(df_in[sPrice] == df_in['MA'], np.where(df_in['MA_Position'].shift(1)== 0, 0, df_in['MA_Position'].shift(1)), df_in.MA_Position)
  13.         df_in['MA_Return'] = np.log(df_in[sPrice] / df_in[sPrice].shift(1))
  14.         df_in['MA_Strategy'] = df_in.MA_Position.shift(1) * df_in.MA_Return
  15.         df_in['MA_PnL'] = df_in.MA_Position.shift(1) * (df_in[sPrice] - df_in[sPrice].shift(1)) #PnL per share
  16.        
  17.        
  18.         ret = -np.exp(df_in['MA_Strategy'].cumsum())[-1]
  19.         PnL = -(df_in['MA_PnL'].cumsum())[-1]
  20.  
  21.         # Option to write to Excel
  22.         stgy = (df_in['MA_Strategy'].cumsum())[-1]
  23.         pfile = False;
  24.         pfile2 = False;
  25.         if outputExcel == 1:
  26.             stgy = (df_in['MA_Strategy'].cumsum())[-1]
  27.             date_index = pd.Series(df_in.index.format())
  28.             df_in.index = date_index
  29.             pfile = '/xlsx/MAOutput_StrategyBackTest_InReturns_'+fn_append+'.xlsx'
  30.             df_in.to_excel('/xlsx/MAOutput_StrategyBackTest_InReturns_'+fn_append+'.xlsx', sheet_name = 'Testing_Strategy_MA')
  31.             import xlwt
  32.             wb = xlwt.Workbook()
  33.             ws = wb.add_sheet('A Test Sheet')
  34.             ws.write(0,0,'n')        
  35.             ws.write(0,1,n)
  36.             ws.write(1,0,'MA_StrategyCumSum')        
  37.             ws.write(1,1,stgy)
  38.             ws.write(2,0,'MA_StrategyCumRet')
  39.             ws.write(2,1,-ret)  
  40.             ws.write(3,0,'MA_R')
  41.             ws.write(3,1,-ret-1)  
  42.             ws.write(4,0,'MA_CumPnL')
  43.             ws.write(4,1,-PnL)
  44.             pfile2 = '/xlsx/exampleIn_'+fn_append+'.xls';
  45.             wb.save(pfile2)
  46.         # End: write output to Excel
  47.         #return ret
  48.  
  49.         if iOpt == 1:
  50.             #ret = -np.exp(df_in['MA_Strategy'].cumsum())[-1]
  51.             ret_pnl = ret
  52.         elif iOpt == 2:
  53.             #PnL = -np.cumsum(df_in['MA_PnL'])[-1]
  54.             #PnL = -(df_in['MA_PnL'].cumsum())[-1]
  55.             ret_pnl = PnL
  56.  
  57.        
  58.         if return_all:
  59.             return {
  60.             "MA": df_in['MA'].fillna(0).tolist(),
  61.             "MA_Position": df_in['MA_Position'].fillna(0).tolist(),
  62.             "MA_Return": df_in['MA_Return'].fillna(0).tolist(),
  63.             "MA_Strategy": df_in['MA_Strategy'].fillna(0).tolist(),
  64.             "MA_PnL": df_in['MA_PnL'].fillna(0).tolist(),
  65.  
  66.             "MA": df_in['MA'].fillna(0).tolist(),
  67.             "MA_StrategyCumSum": stgy,
  68.             "MA_StrategyCumRet": -ret, "MA_R":-ret-1, "MA_CumPnL": -PnL, "ret": ret,
  69.             "PnL": ret if iOpt == 1 else PnL,
  70.             "file": pfile,
  71.             "file2": pfile2
  72.             }
  73.        
  74.         if iOpt == 1:
  75.             #ret = -np.exp(df_in['MA_Strategy'].cumsum())[-1]
  76.             return ret
  77.         elif iOpt == 2:
  78.             #PnL = -np.cumsum(df_in['MA_PnL'])[-1]
  79.             #PnL = -(df_in['MA_PnL'].cumsum())[-1]
  80.             return PnL
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top