Advertisement
wassnape

esg_window_dressing.py

Jul 1st, 2022
756
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[4]:
  5.  
  6.  
  7. import numpy as np
  8. import pandas as pd
  9. import matplotlib.pyplot as plt
  10.  
  11. import ffn
  12. import bt
  13.  
  14. get_ipython().run_line_magic('matplotlib', 'inline')
  15.  
  16. plt.style.use('ggplot')
  17. plt.rcParams['figure.figsize'] = (12.0, 4)
  18.  
  19.  
  20. # # Exploratory Data Analysis
  21.  
  22. # In[69]:
  23.  
  24.  
  25. tickers = [
  26.     # which tickers?
  27.      'BZ=F',
  28.      'NG=F',
  29.  
  30. #  '0P0000732C.TO', # nuttals fund
  31.  'vet.to',
  32.  'wcp.to',
  33.  'arch',
  34.     'btu',
  35.  'whc.ax'
  36. ]
  37.  
  38.  
  39. # In[79]:
  40.  
  41.  
  42. # fetch some data
  43. data = bt.get(
  44.     tickers=tickers,
  45.     start='2017-12-01',
  46.     end='2022-06-16',
  47. )
  48. data
  49.  
  50.  
  51. # ## backtest - good
  52.  
  53. # In[73]:
  54.  
  55.  
  56. buy_days_before_qend = 1
  57. sell_days_after_qend= 7
  58.  
  59. s = data.index.quarter.to_series().diff().abs().shift(-1).fillna(0)
  60. s = s.replace(0, np.nan).ffill(limit=sell_days_after_qend).bfill(limit=buy_days_before_qend).fillna(0)
  61. signal = data.copy()
  62. signal[:] =np.array([s]*7).T
  63. signal
  64.  
  65.  
  66. # In[74]:
  67.  
  68.  
  69. signal.loc['2021-03-25':'2021-04-15']
  70.  
  71.  
  72. # # try for each equity
  73.  
  74. # In[82]:
  75.  
  76.  
  77. data.columns
  78.  
  79.  
  80. # In[85]:
  81.  
  82.  
  83. tests = []
  84. for c in data.columns:
  85.     s1 = bt.Strategy(
  86.         c,
  87.         [
  88.             bt.algos.RunDaily(),
  89.             bt.algos.SelectWhere(signal[[c]]),
  90.             bt.algos.WeighEqually(),
  91.             bt.algos.Rebalance()
  92.         ]
  93.     )
  94.     t1 = bt.Backtest(s1, data[[c]])
  95.     tests.append(t1)
  96.    
  97. res = bt.run(*tests)
  98.  
  99. res.plot()
  100.  
  101. res.plot_security_weights()
  102.  
  103. res.display()
  104. # res.stats.T['daily_sharpe'].sort_values()[::-1]
  105.  
  106.  
  107. # # all
  108.  
  109. # In[75]:
  110.  
  111.  
  112.  
  113. s1 = bt.Strategy(
  114.     'esg',
  115.     [
  116.         bt.algos.RunDaily(),
  117.         bt.algos.SelectWhere(signal),
  118.         bt.algos.WeighEqually(),
  119.         bt.algos.Rebalance()
  120.     ]
  121. )
  122. t1 = bt.Backtest(s1, data)
  123.  
  124.  
  125. res = bt.run(t1)
  126.  
  127. res.plot()
  128.  
  129. res.plot_security_weights()
  130.  
  131. res.display()
  132. res.stats.T['daily_sharpe'].sort_values()[::-1]
  133.  
  134.  
  135. # In[76]:
  136.  
  137.  
  138. # Lets try just 2021 onwards
  139. s1 = bt.Strategy(
  140.     'esg',
  141.     [
  142.         bt.algos.RunDaily(),
  143.         bt.algos.SelectWhere(signal.loc['2021':]),
  144.         bt.algos.WeighEqually(),
  145.         bt.algos.Rebalance()
  146.     ]
  147. )
  148. t1 = bt.Backtest(s1, data.loc['2021':])
  149.  
  150.  
  151. res = bt.run(t1)
  152.  
  153. res.plot()
  154.  
  155. res.plot_security_weights()
  156.  
  157. res.display()
  158. res.stats.T['daily_sharpe'].sort_values()[::-1]
  159.  
  160.  
  161. # In[ ]:
  162.  
  163.  
  164.  
  165.  
  166.  
  167. # In[ ]:
  168.  
  169.  
  170.  
  171.  
  172.  
  173. # In[ ]:
  174.  
  175.  
  176.  
  177.  
  178.  
  179. # In[ ]:
  180.  
  181.  
  182.  
  183.  
Advertisement
RAW Paste Data Copied
Advertisement