Best Python code snippet using playwright-python
Quandldata.py
Source:Quandldata.py
1import Quandl2import numpy as np3from pandas import *4#assets5start = "December 2001"6end = "December 2012"7freq = "monthly"8#ik moet telkens het laatste object van de array verwijderen aangezien Quandl niet overweg kan met een eindmaand van November.9BEL = np.delete(np.array(Quandl.get("YAHOO/INDEX_BFX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)10PX = np.delete(np.array(Quandl.get("PRAGUESE/PX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"]),132)11y = np.vstack((BEL,PX))12y = np.vstack((y,np.delete(np.array(Quandl.get("NASOMXNORDIC/FI0008900212",collapse= freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"]),132)))13y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_FCHI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))14y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_GDAXI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))15y = np.vstack((y,np.delete(np.array(Quandl.get("BUDAPESTSE/BUX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))16y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_FTSEMIB_MI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))17y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_IBEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))18y = np.vstack((y,np.delete(np.array(Quandl.get("NASOMXNORDIC/SE0000337842",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"]),132)))19y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_SSMI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))20y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_FTSE",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))21y = np.vstack((y,np.delete(np.array(Quandl.get("PFTS/INDEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"]),132)))22y = np.vstack((y,np.delete(np.array(Quandl.get("YAHOO/INDEX_AEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)))23y=y.T24print y.shape25'''26BEL = np.delete(np.array(Quandl.get("YAHOO/INDEX_BFX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)27PX = np.delete(np.array(Quandl.get("PRAGUESE/PX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"]),132)28FIN = np.delete(np.array(Quandl.get("NASOMXNORDIC/FI0008900212",collapse= freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"]),132)29CAC = np.delete(np.array(Quandl.get("YAHOO/INDEX_FCHI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)30DAX = np.delete(np.array(Quandl.get("YAHOO/INDEX_GDAXI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)31BUX = np.delete(np.array(Quandl.get("BUDAPESTSE/BUX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)32ITA = np.delete(np.array(Quandl.get("YAHOO/INDEX_FTSEMIB_MI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)33IBEX = np.delete(np.array(Quandl.get("YAHOO/INDEX_IBEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)34SWE = np.delete(np.array(Quandl.get("NASOMXNORDIC/SE0000337842",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"]),132)35SMI = np.delete(np.array(Quandl.get("YAHOO/INDEX_SSMI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)36UK = np.delete(np.array(Quandl.get("YAHOO/INDEX_FTSE",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)37UKR = np.delete(np.array(Quandl.get("PFTS/INDEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"]),132)38AEX = np.delete(np.array(Quandl.get("YAHOO/INDEX_AEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"]),132)39'''40#determinants, de basisset voor voorspellingen41'''42#technische determinanten:43start = "November 2001"44end = "November 2012"45freq = "monthly"46BELlag = np.array(Quandl.get("YAHOO/INDEX_BFX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])47PXlag = np.array(Quandl.get("PRAGUESE/PX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"])48FINlag = np.array(Quandl.get("NASOMXNORDIC/FI0008900212",collapse= freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"])49CAClag = np.array(Quandl.get("YAHOO/INDEX_FCHI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])50DAXlag = np.array(Quandl.get("YAHOO/INDEX_GDAXI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])51BUXlag = np.array(Quandl.get("BUDAPESTSE/BUX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])52ITAlag = np.array(Quandl.get("YAHOO/INDEX_FTSEMIB_MI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])53IBEXlag = np.array(Quandl.get("YAHOO/INDEX_IBEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])54SWElag = np.array(Quandl.get("NASOMXNORDIC/SE0000337842",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Closing Price"])55SMIlag = np.array(Quandl.get("YAHOO/INDEX_SSMI",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])56UKlag = np.array(Quandl.get("YAHOO/INDEX_FTSE",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])57UKRlag = np.array(Quandl.get("PFTS/INDEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Index"])58AEXlag = np.array(Quandl.get("YAHOO/INDEX_AEX",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])59SP500 = np.array(Quandl.get("YAHOO/INDEX_GSPC",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])60Nik = np.array(Quandl.get("YAHOO/INDEX_N225",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Close"])61DJIA = np.array(Quandl.get("BCB/UDJIAD1",collapse=freq,trim_start = start, trim_end= end,transformation = "rdiff")["Value"])62'''63#fundamentele determinanten64st = "December 2001"65en = "November 2012"66freq = "monthly"67def jaarlijks(arr):68 new = []69 for i in range(arr.size):70 for j in range(12):71 new.append(arr[i])72 return np.array(new)73 74def kwartaal(arr):75 new = []76 for i in range(arr.size):77 for j in range(3):78 new.append(arr[i])79 return np.array(new)80 81 82#inflation related: 83#global inflation based on CPI, 84cpi = jaarlijks(np.array(Quandl.get("WORLDBANK/WLD_FP_CPI_TOTL_ZG",collapse=freq,trim_start = st, trim_end= en)["Value"]))85#misschien moet collapse monthly hier weggelaten worden?86#global inflation based on GDP deflator87defl = jaarlijks(np.array(Quandl.get("WORLDBANK/WLD_NY_GDP_DEFL_KD_ZG",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))88#CPI per country89cpibel = jaarlijks(np.array(Quandl.get("WORLDBANK/BEL_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))90cpipx = jaarlijks(np.array(Quandl.get("WORLDBANK/CZE_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))91cpifin = jaarlijks(np.array(Quandl.get("WORLDBANK/FIN_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))92cpicac = jaarlijks(np.array(Quandl.get("WORLDBANK/FRA_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))93cpidax = jaarlijks(np.array(Quandl.get("WORLDBANK/DEU_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))94cpibux = jaarlijks(np.array(Quandl.get("WORLDBANK/HUN_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))95cpiita = jaarlijks(np.array(Quandl.get("WORLDBANK/ITA_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))96cpiibex = jaarlijks(np.array(Quandl.get("WORLDBANK/ESP_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))97cpiswe = jaarlijks(np.array(Quandl.get("WORLDBANK/SWE_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))98cpismi = jaarlijks(np.array(Quandl.get("WORLDBANK/CHE_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))99cpiuk = jaarlijks(np.array(Quandl.get("WORLDBANK/GBR_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))100cpiukr = jaarlijks(np.array(Quandl.get("WORLDBANK/UKR_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))101cpiaex = jaarlijks(np.array(Quandl.get("WORLDBANK/NLD_CPTOTSAXNZGY",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"]))102cpinat = np.vstack((cpibel,cpipx,cpifin,cpicac,cpidax,cpibux,cpiita,cpiibex,cpiswe,cpismi,cpiuk,cpiukr,cpiaex))103X = np.vstack((cpi,defl,cpinat))104print X.shape105'''106#industrial production per country107indbel = jaarlijks(np.array(Quandl.get("WORLDBANK/BEL_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))108indpx = jaarlijks(np.array(Quandl.get("WORLDBANK/CZE_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))109indfin = jaarlijks(np.array(Quandl.get("WORLDBANK/FIN_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))110indcac = jaarlijks(np.array(Quandl.get("WORLDBANK/FRA_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))111inddax = jaarlijks(np.array(Quandl.get("WORLDBANK/DEU_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))112indbux = jaarlijks(np.array(Quandl.get("WORLDBANK/HUN_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))113indita = jaarlijks(np.array(Quandl.get("WORLDBANK/ITA_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))114indibex = jaarlijks(np.array(Quandl.get("WORLDBANK/ESP_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))115indswe = jaarlijks(np.array(Quandl.get("WORLDBANK/SWE_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))116indsmi = jaarlijks(np.array(Quandl.get("WORLDBANK/CHE_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))117induk = jaarlijks(np.array(Quandl.get("WORLDBANK/GBR_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))118indukr = jaarlijks(np.array(Quandl.get("WORLDBANK/UKR_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))119indaex = jaarlijks(np.array(Quandl.get("WORLDBANK/NLD_IPTOTSAKD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))120#unemployment rate per country121empbel = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_BEL",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))122emppx = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_CZE",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))123empfin = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_FIN",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))124empcac = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_FRA",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))125empdax = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_DEU",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))126empbux = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_HUN",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))127empita = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_ITA",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))128empibex = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_ESP",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))129empswe = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_SWE",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))130empsmi = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_CHE",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))131empuk = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_GBR",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))132empukr = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_UKR",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))133empaex = jaarlijks(np.array(Quandl.get("IMF/MAP_WEO_UNEMP_NLD",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))134#composite leading indicator per country (Ukraine unavailable)135clibel = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_BEL_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])136clipx = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_CZE_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])137clifin = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_FIN_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])138clicac = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_FRA_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])139clidax = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_DEU_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])140clibux = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_HUN_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])141cliita = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_ITA_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])142cliibex = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_ESP_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])143cliswe = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_SWE_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])144clismi = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_CHE_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])145cliuk = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_GBR_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])146#cliukr = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_UKR_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])147cliaex = np.array(Quandl.get("OECD/MEI_CLI_LOLITOAA_NLD_M",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])148#USA leading indicator149uslead = np.array(Quandl.get("ECRI/USLEADING",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Level"])150#Consumer confidence index151cci = np.array(Quandl.get("BCB/4393",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])152#Business confidence index153bci = kwartaal(np.array(Quandl.get("BCB/7341",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))154#Nominal Effective Exchange Rate per country155neerbel = jaarlijks(np.array(Quandl.get("WORLDBANK/BEL_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))156neerpx = np.array(Quandl.get("WORLDBANK/CZE_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])157neerfin = np.array(Quandl.get("WORLDBANK/FIN_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])158neercac = np.array(Quandl.get("WORLDBANK/FRA_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])159neerdax = np.array(Quandl.get("WORLDBANK/DEU_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])160neerbux = np.array(Quandl.get("WORLDBANK/HUN_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])161neerita = np.array(Quandl.get("WORLDBANK/ITA_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])162neeribex = np.array(Quandl.get("WORLDBANK/ESP_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])163neerswe = np.array(Quandl.get("WORLDBANK/SWE_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])164neersmi = np.array(Quandl.get("WORLDBANK/CHE_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])165neeruk = np.array(Quandl.get("WORLDBANK/GBR_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])166neerukr = np.array(Quandl.get("WORLDBANK/UKR_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])167neeraex = np.array(Quandl.get("WORLDBANK/NLD_NEER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])168#Real Effective Exchange Rate per country169reerbel = jaarlijks(np.array(Quandl.get("WORLDBANK/BEL_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))170reerpx = np.array(Quandl.get("WORLDBANK/CZE_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])171reerfin = np.array(Quandl.get("WORLDBANK/FIN_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])172reercac = np.array(Quandl.get("WORLDBANK/FRA_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])173reerdax = np.array(Quandl.get("WORLDBANK/DEU_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])174reerbux = np.array(Quandl.get("WORLDBANK/HUN_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])175reerita = np.array(Quandl.get("WORLDBANK/ITA_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])176reeribex = np.array(Quandl.get("WORLDBANK/ESP_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])177reerswe = np.array(Quandl.get("WORLDBANK/SWE_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])178reersmi = np.array(Quandl.get("WORLDBANK/CHE_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])179reeruk = np.array(Quandl.get("WORLDBANK/GBR_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])180reerukr = np.array(Quandl.get("WORLDBANK/UKR_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])181reeraex = np.array(Quandl.get("WORLDBANK/NLD_REER",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])182#General government net debt, Percent of GDP, per country, Czech rep. unavailable183debtbel = jaarlijks(np.array(Quandl.get("IMF/GGXWDN_NGDP_124",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))184#debtpx = np.array(Quandl.get("IMF/GGXWDN_NGDP_935",collapse=freq,trim_start = st, trim_end= en,transformation = "none")["Value"])185debtfin = np.array(Quandl.get("IMF/GGXWDN_NGDP_172",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])186debtcac = np.array(Quandl.get("IMF/GGXWDN_NGDP_132",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])187debtdax = np.array(Quandl.get("IMF/GGXWDN_NGDP_134",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])188debtbux = np.array(Quandl.get("IMF/GGXWDN_NGDP_944",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])189debtita = np.array(Quandl.get("IMF/GGXWDN_NGDP_136",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])190debtibex = np.array(Quandl.get("IMF/GGXWDN_NGDP_184",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])191debtswe = np.array(Quandl.get("IMF/GGXWDN_NGDP_144",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])192debtsmi = np.array(Quandl.get("IMF/GGXWDN_NGDP_146",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])193debtuk = np.array(Quandl.get("IMF/GGXWDN_NGDP_112",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])194debtukr = np.array(Quandl.get("IMF/GGXWDN_NGDP_926",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])195debtaex = np.array(Quandl.get("IMF/GGXWDN_NGDP_138",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])196#Real GDP per country197gdpbel = jaarlijks(np.array(Quandl.get("IMF/NGDP_R_124",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))198gdppx = np.array(Quandl.get("IMF/NGDP_R_935",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])199gdpfin = np.array(Quandl.get("IMF/NGDP_R_172",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])200gdpcac = np.array(Quandl.get("IMF/NGDP_R_132",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])201gdpdax = np.array(Quandl.get("IMF/NGDP_R_134",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])202gdpbux = np.array(Quandl.get("IMF/NGDP_R_944",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])203gdpita = np.array(Quandl.get("IMF/NGDP_R_136",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])204gdpibex = np.array(Quandl.get("IMF/NGDP_R_184",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])205gdpswe = np.array(Quandl.get("IMF/NGDP_R_144",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])206gdpsmi = np.array(Quandl.get("IMF/NGDP_R_146",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])207gdpuk = np.array(Quandl.get("IMF/NGDP_R_112",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])208gdpukr = np.array(Quandl.get("IMF/NGDP_R_926",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])209gdpaex = np.array(Quandl.get("IMF/NGDP_R_138",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])210#M2 Money Supply per country211monbel = jaarlijks(np.array(Quandl.get("WORLDBANK/BEL_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"]))212monpx = np.array(Quandl.get("WORLDBANK/CZE_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])213monfin = np.array(Quandl.get("WORLDBANK/FIN_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])214moncac = np.array(Quandl.get("WORLDBANK/FRA_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])215mondax = np.array(Quandl.get("WORLDBANK/DEU_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])216monbux = np.array(Quandl.get("WORLDBANK/HUN_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])217monita = np.array(Quandl.get("WORLDBANK/ITA_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])218monibex = np.array(Quandl.get("WORLDBANK/ESP_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])219monswe = np.array(Quandl.get("WORLDBANK/SWE_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])220monsmi = np.array(Quandl.get("WORLDBANK/CHE_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])221monuk = np.array(Quandl.get("WORLDBANK/GBR_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])222monukr = np.array(Quandl.get("WORLDBANK/UKR_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])223monaex = np.array(Quandl.get("WORLDBANK/NLD_FM_LBL_MQMY_GD_ZS",collapse=freq,trim_start = st, trim_end= en,transformation = "rdiff")["Value"])224#Europe Brent oil spot price225oil = np.array(Quandl.get("DOE/RBRTE",collapse=freq,trim_start = st, trim_end= en,transformation= "rdiff")["Value"])226#copper price227copper = np.array(Quandl.get("WORLDBANK/WLD_COPPER",collapse=freq,trim_start = st, trim_end= en,transformation= "rdiff")["Value"])228#gold price229gold = np.array(Quandl.get("BUNDESBANK/BBK01_WT5511",collapse=freq,trim_start = st, trim_end= en,transformation= "rdiff")["Value"])230#energy index231energy = np.array(Quandl.get("WORLDBANK/WLD_IENERGY",collapse=freq,trim_start = st, trim_end= en,transformation= "rdiff")["Value"])232#metals and minerals index233metals = np.array(Quandl.get("WORLDBANK/WLD_IMETMIN",collapse=freq,trim_start = st, trim_end= en,transformation= "rdiff")["Value"])234#treasury bill yields235bill3mo = np.array(Quandl.get("USTREASURY/YIELD",collapse=freq,trim_start = st, trim_end= en)["3 Mo"])236bill2y = np.array(Quandl.get("USTREASURY/YIELD",collapse=freq,trim_start = st, trim_end= en)["2 Yr"])237bill10y = np.array(Quandl.get("USTREASURY/YIELD",collapse=freq,trim_start = st, trim_end= en)["10 Yr"])238billdif = bill10y-bill2y...
backtest.py
Source:backtest.py
1import quandl2import numpy as np3import pandas as pd4from datetime import datetime, timedelta5import matplotlib.pyplot as plt6from sklearn import preprocessing7import talib as tb8import plotly as py9import plotly.graph_objs as go10#from tabulate import tabulate11quandl.ApiConfig.api_key = "Zx2MBnZf7N3scjxqfiGv"12# GhFWCbsLK_Qn8sZBVt3R13df1 = quandl.get("NSE/RELIANCE",trim_start = "2010-01-01", trim_end = "2017-12-01")14df2 = quandl.get("NSE/HDFCBANK",trim_start = "2010-01-01", trim_end = "2017-12-01")15df3 = quandl.get("NSE/TCS",trim_start = "2010-01-01", trim_end = "2017-12-01")16df4 = quandl.get("NSE/ITC",trim_start = "2010-01-01", trim_end = "2017-12-01")17df5 = quandl.get("NSE/AUROPHARMA",trim_start = "2010-01-01", trim_end = "2017-12-01")18df6 = quandl.get("NSE/HDFC",trim_start = "2010-01-01", trim_end = "2017-12-01")19df7 = quandl.get("NSE/HINDUNILVR",trim_start = "2010-01-01", trim_end = "2017-12-01")20df8 = quandl.get("NSE/MARUTI",trim_start = "2010-01-01", trim_end = "2017-12-01")21df9 = quandl.get("NSE/INFY",trim_start = "2010-01-01", trim_end = "2017-12-01")22df10 = quandl.get("NSE/ONGC",trim_start = "2010-01-01", trim_end = "2017-12-01")23df11 = quandl.get("NSE/SBIN",trim_start = "2010-01-01", trim_end = "2017-12-01")24df12 = quandl.get("NSE/KOTAKBANK",trim_start = "2010-01-01", trim_end = "2017-12-01")25df13 = quandl.get("NSE/IOC",trim_start = "2010-01-01", trim_end = "2017-12-01")26df14 = quandl.get("NSE/ICICIBANK",trim_start = "2010-01-01", trim_end = "2017-12-01")27df15 = quandl.get("NSE/COALINDIA",trim_start = "2010-01-01", trim_end = "2017-12-01")28df16 = quandl.get("NSE/LT",trim_start = "2010-01-01", trim_end = "2017-12-01")29df17 = quandl.get("NSE/BHARTIARTL",trim_start = "2010-01-01", trim_end = "2017-12-01")30df18 = quandl.get("NSE/NTPC",trim_start = "2010-01-01", trim_end = "2017-12-01")31df19 = quandl.get("NSE/HCLTECH",trim_start = "2010-01-01", trim_end = "2017-12-01")32df20 = quandl.get("NSE/AXISBANK",trim_start = "2010-01-01", trim_end = "2017-12-01")33df21 = quandl.get("NSE/WIPRO",trim_start = "2010-01-01", trim_end = "2017-12-01")34df22 = quandl.get("NSE/SUNPHARMA",trim_start = "2010-01-01", trim_end = "2017-12-01")35df23 = quandl.get("NSE/VEDL",trim_start = "2010-01-01", trim_end = "2017-12-01")36df24 = quandl.get("NSE/ULTRACEMCO",trim_start = "2010-01-01", trim_end = "2017-12-01")37df25 = quandl.get("NSE/ASIANPAINT",trim_start = "2010-01-01", trim_end = "2017-12-01")38df26 = quandl.get("NSE/INDUSINDBK",trim_start = "2010-01-01", trim_end = "2017-12-01")39df27 = quandl.get("NSE/POWERGRID",trim_start = "2010-01-01", trim_end = "2017-12-01")40df28 = quandl.get("NSE/TATAMOTORS",trim_start = "2010-01-01", trim_end = "2017-12-01")41df29 = quandl.get("NSE/BPCL",trim_start = "2010-01-01", trim_end = "2017-12-01")42df30 = quandl.get("NSE/BAJFINANCE",trim_start = "2010-01-01", trim_end = "2017-12-01")43df31 = quandl.get("NSE/MM",trim_start = "2010-01-01", trim_end = "2017-12-01")44df32 = quandl.get("NSE/BAJAJ_AUTO",trim_start = "2010-01-01", trim_end = "2017-12-01")45df33 = quandl.get("NSE/ADANIPORTS",trim_start = "2010-01-01", trim_end = "2017-12-01")46df34 = quandl.get("NSE/EICHERMOT",trim_start = "2010-01-01", trim_end = "2017-12-01")47df35 = quandl.get("NSE/GAIL",trim_start = "2010-01-01", trim_end = "2017-12-01")48df36 = quandl.get("NSE/TATASTEEL",trim_start = "2010-01-01", trim_end = "2017-12-01")49df37 = quandl.get("NSE/HEROMOTOCO",trim_start = "2010-01-01", trim_end = "2017-12-01")50df38 = quandl.get("NSE/YESBANK",trim_start = "2010-01-01", trim_end = "2017-12-01")51df39 = quandl.get("NSE/INFRATEL",trim_start = "2010-01-01", trim_end = "2017-12-01")52df40 = quandl.get("NSE/TECHM",trim_start = "2010-01-01", trim_end = "2017-12-01")53df41 = quandl.get("NSE/HINDPETRO",trim_start = "2010-01-01", trim_end = "2017-12-01")54df42 = quandl.get("NSE/BOSCHLTD",trim_start = "2010-01-01", trim_end = "2017-12-01")55df43 = quandl.get("NSE/ZEEL",trim_start = "2010-01-01", trim_end = "2017-12-01")56df44 = quandl.get("NSE/IBULHSGFIN",trim_start = "2010-01-01", trim_end = "2017-12-01")57df45 = quandl.get("NSE/HINDALCO",trim_start = "2010-01-01", trim_end = "2017-12-01")58df46 = quandl.get("NSE/AMBUJACEM",trim_start = "2010-01-01", trim_end = "2017-12-01")59df47 = quandl.get("NSE/CIPLA",trim_start = "2010-01-01", trim_end = "2017-12-01")60df48 = quandl.get("NSE/UPL",trim_start = "2010-01-01", trim_end = "2017-12-01")61df49 = quandl.get("NSE/DRREDDY",trim_start = "2010-01-01", trim_end = "2017-12-01")62df50 = quandl.get("NSE/LUPIN",trim_start = "2010-01-01", trim_end = "2017-12-01")63# print("alpha=")64# alpha=input()65# alpha=open-close66c=pd.DataFrame()67o=pd.DataFrame()68h=pd.DataFrame()69l=pd.DataFrame()70lt=pd.DataFrame()71a=pd.DataFrame()72v=pd.DataFrame()73my_dict = {"df1":df1, "df2":df2, "df3":df3, "df4":df4, "df5":df5,"df6":df674,"df7":df7,"df8":df8, "df9":df9, "df10":df10,"df11":df11, "df12":df12, "df13":df13, "df14":df14,"df15":df1575,"df16":df16,"df17":df17, "df18":df18, "df19":df19,"df20":df20, "df21":df21, "df22":df22, "df23":df23, "df24":df2476,"df25":df25,"df26":df26, "df27":df27, "df28":df28,"df29":df29, "df30":df30, "df31":df31, "df32":df32, "df33":df3377,"df34":df34,"df35":df35, "df36":df36, "df37":df37,"df38":df38, "df39":df39, "df40":df40, "df41":df41, "df42":df4278,"df43":df43,"df44":df44, "df45":df45, "df46":df46,"df47":df47, "df48":df48, "df49":df49, "df50":df50}79def mean_sma(x=pd.DataFrame(),k=30):80 y=pd.DataFrame(columns = x.columns)81 for i in range(0,len(x.columns)):82 y.iloc[:,i]=pd.Series(tb.SMA((x.iloc[:,i]).values,timeperiod=k))83 y2=y.set_index(x.index)84 return y285def mean_ema(x=pd.DataFrame(),k=30):86 y=pd.DataFrame(columns = x.columns)87 for i in range(0,len(x.columns)):88 y.iloc[:,i]=pd.Series(tb.EMA((x.iloc[:,i]).values,timeperiod=k))89 y2=y.set_index(x.index)90 return y291def cs_rank(x=pd.DataFrame()):92 y2=x.rank(axis=1,pct=True)93 return y294def cs_mean(x=pd.DataFrame()):95 y=pd.DataFrame(columns=x.columns,index=x.index)96 for i in range(len(x.columns)):97 y.iloc[:,i]=x.mean(axis=1)98 return y99def ts_rank(x=pd.DataFrame(),k=30):100 y2=pd.DataFrame(index=x.index,columns=x.columns)101 for i in range(k,len(x)+1):102 y=(x.iloc[i-k:i,:]).rank(axis=0,pct=True)103 y2.iloc[i-1,:]=y.iloc[-1,:]104 return y2105def sum(x=pd.DataFrame(),k=30):106 y2=pd.DataFrame(index=x.index,columns=x.columns)107 for i in range(k,len(x)+1):108 y=(x.iloc[i-k:i,:]).sum(axis=0)109 y2.iloc[i-1,:]=y110 return y2111def ts_max(x=pd.DataFrame(),k=30):112 y2=pd.DataFrame(index=x.index,columns=x.columns)113 for i in range(k,len(x)+1):114 y=(x.iloc[i-k:i,:]).max(axis=0)115 y2.iloc[i-1,:]=y116 return y2117def cs_max(x=pd.DataFrame()):118 y=pd.DataFrame(columns=x.columns,index=x.index)119 for i in range(len(x.columns)):120 y.iloc[:,i]=x.max(axis=1)121 return y122def ts_min(x=pd.DataFrame(),k=30):123 y2=pd.DataFrame(index=x.index,columns=x.columns)124 for i in range(k,len(x)+1):125 y=(x.iloc[i-k:i,:]).min(axis=0)126 y2.iloc[i-1,:]=y127 return y2128def cs_min(x=pd.DataFrame()):129 y=pd.DataFrame(columns=x.columns,index=x.index)130 for i in range(len(x.columns)):131 y.iloc[:,i]=x.min(axis=1)132 return y133def stddev(x=pd.DataFrame(),k=30,nbdev=1):134 y=pd.DataFrame(columns = x.columns)135 for i in range(0,len(x.columns)):136 y.iloc[:,i]=pd.Series(tb.STDDEV((x.iloc[:,i]).values,timeperiod=k))137 y2=y.set_index(x.index)138 return y2139def ts_zscore(x=pd.DataFrame(),k=30):140 y = pd.DataFrame(columns=x.columns, index=x.index)141 y = (x-mean_sma(x,k))/stddev(x,k)142 return y143def delay(x=pd.DataFrame(),k=30):144 y=x.shift(periods=k)145 return y146def delta(x=pd.DataFrame(),k=30):147 y=x-delay(x,k)148 return y149def momentum(x=pd.DataFrame(),k=30):150 y=pd.DataFrame(columns = x.columns)151 for i in range(0,len(x.columns)):152 y.iloc[:,i]=pd.Series(tb.MOM((x.iloc[:,i]).values,timeperiod=k))153 y2=y.set_index(x.index)154 return y2155for variable in my_dict:156 o[[variable]]=(my_dict[variable])[['Open']]157 h[[variable]]=(my_dict[variable])[['High']]158 l[[variable]]=(my_dict[variable])[['Low']]159 c[[variable]]=(my_dict[variable])[['Close']]160 lt[[variable]]=(my_dict[variable])[['Last']]161 v[[variable]]=(my_dict[variable])[['Total Trade Quantity']]162o=o.dropna(axis=1, how='any')163h=h.dropna(axis=1, how='any')164l=l.dropna(axis=1, how='any')165c=c.dropna(axis=1, how='any')166v=v.dropna(axis=1, how='any')167lt=lt.dropna(axis=1, how='any')168# o=o.fillna(method='ffill')169# h=h.fillna(method='ffill')170# l=l.fillna(method='ffill')171# c=c.fillna(method='ffill')172# v=v.fillna(method='ffill')173# lt=lt.fillna(method='ffill')174# o=o.fillna(method='bfill')175# h=h.fillna(method='bfill')176# l=l.fillna(method='bfill')177# c=c.fillna(method='bfill')178# v=v.fillna(method='bfill')179# lt=lt.fillna(method='bfill')180# print(c)181# # print(v)182# print(h)183temp=c.copy()184temp.iloc[:,:]=1185def plot_graph(a):186 #a=input("alpha=")187 w=str(a)188 a=eval(a)189 if(type(a)==int or type(a)==float):190 a=temp191 # a=(h+l+c)/3-c192 # a = v/mean(v,20)193 # a=mean_exp(mean(c,30),30)194 count=0195 for i in range(0,len(a)):196 if ((a.iloc[i,:]).isnull().all().all()):197 count=count+1198 elif((a.iloc[i,:]).isnull().all().all()==0):199 break200 a=a.dropna()201 # print(count)202 # print(a)203 # a = a.div(abs(sum(axis = 1)),axis = 0)204 cnt=0205 for i in range(len(a)):206 divider =abs(a.iloc[[i]]).sum(axis=1)207 if ((divider.values==0).all()):208 divider=1209 a.iloc[[i]]=a.iloc[[i]]/float(divider)210 # p=a.copy()211 # x = a.values #returns a numpy array212 # min_max_scaler = preprocessing.MinMaxScaler(feature_range=(-1,1))213 # x_scaled = min_max_scaler.fit_transform(a)214 # a = pd.DataFrame(x_scaled,index= p.index,columns = p.columns)215 # print(a)216 b=100000217 n=pd.DataFrame()218 diff=pd.DataFrame()219 # for index, row in a.iterrows():220 # if index!=datetime.strptime('2010-01-04 00:00:00','%Y-%m-%d %H:%M:%S'):221 # n=b*a[[index-timedelta(days=1)]]/c[[index-timedelta(days=1)]]222 # pnl=n*(c[[index]]-c[[index-timedelta(days=1)]])223 # print(pnl)224 for i in range(1,len(a)):225 t = (a.iloc[[i-1]]*b/c.iloc[[i-1+count]])226 x= np.floor(t.astype(np.double))227 # print(x)228 y =c.iloc[[i+count]]-c.iloc[[i-1+count]].values.squeeze()229 n = pd.concat([n,x])230 diff = pd.concat([diff,y])231 # print(diff)232 # print('**')233 # print(n)234 my_dict2 = {}235 my_dict2 = my_dict2.fromkeys(a.columns)236 header2 = n.columns.tolist()237 header2 = ['Date'] + header2238 Date = n.index.tolist()239 for i in range(len(Date)):240 Date[i]=str(Date[i].strftime('%Y-%m-%d'))241 j=[]242 for i,t in zip(range(1,len(my_dict2)+1),my_dict2.items()):243 variable=t[0]244 my_dict2[variable] = n.iloc[:,i-1].values.tolist()245 j.append(my_dict2[variable])246 values2=[Date]+j247 pnl=diff*n.shift()248 # print(pnl)249 pnl=pnl.dropna()250 pnl=np.around(pnl.astype(np.double),2)251 header3 = pnl.columns.tolist()252 header3 = ['Date'] + header3253 Date2 = pnl.index.tolist()254 for i in range(len(Date2)):255 Date2[i]=str(Date2[i].strftime('%Y-%m-%d'))256 j2=[]257 for i,t in zip(range(1,len(my_dict2)+1),my_dict2.items()):258 variable=t[0]259 my_dict2[variable] = pnl.iloc[:,i-1].values.tolist()260 j2.append(my_dict2[variable])261 values3=[Date2]+j2262 pnl=pnl.sum(axis=1)263 gross_pnl=pd.DataFrame(columns=['profit'])264 for i in range(0,len(pnl)):265 k=pnl[0:i+1].sum(axis=0)266 gross_pnl.loc[i]=k267 total_pnl=pnl.sum(axis=0)268 gross_pnl=gross_pnl.set_index(pnl.index)269 # print(pnl)270 # print(gross_pnl)271 # plt.plot(gross_pnl.index,gross_pnl.values)272 # plt.show()273 cal = pd.DataFrame(columns=gross_pnl.columns, index=gross_pnl.index)274 for i in range(0, 7):275 if i != 0:276 cal.iloc[i, 0] = gross_pnl.iloc[250 * i-1, 0] - gross_pnl.iloc[250 * (i - 1)-1, 0]277 elif i == 0:278 cal.iloc[i, 0] = gross_pnl.iloc[250 * i-1, 0]279 cal=cal.dropna()280 cal= pd.concat([cal, gross_pnl.iloc[[-1]].values-gross_pnl.iloc[[250*7]]],ignore_index=True)281 cal = cal.dropna()282 cal['year'] = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']283 cal = cal.set_index('year')284 yearly_pnl = cal285 # yearly_pnl.columns=['Yearly PNL']286 yearly_ret =yearly_pnl*100/b287 yearly_ret=np.around(yearly_ret.astype(np.double),2)288 #yearly_ret=round(yearly_ret,2)289 yearly_ret.columns=['Yearly Returns']290 #needs editing b+grosspnl291 # yearly_ret.columns=['Yearly returns']292 series_frame = pnl.to_frame()293 new_2 = stddev(series_frame,250)294 std = pd.DataFrame(columns=gross_pnl.columns,index=gross_pnl.index)295 for i in range(1, 8):296 std.iloc[i - 1, 0] = new_2.iloc[250 * i - 1, 0]297 new_3=stddev(series_frame,1968-250*7)298 new_3.columns=['profit']299 std=std.dropna()300 e = new_3.iloc[[-1]]301 std= pd.concat([std, e],ignore_index=True)302 std= std.dropna()303 std['year'] = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']304 std = std.set_index('year')305 # std.columns=['Yearly standard deviation of PNL']306 ir=(yearly_pnl/250)/std307 #ir=round(ir,2)308 ir.columns=['IR']309 ir=np.around(ir.astype(np.double),2)310 # print(ir)311 sharpe=(ir*15.8)312 #sharpe=round(sharpe,2)313 sharpe.columns=['Sharpe Ratio']314 sharpe=np.around(sharpe.astype(np.double),2)315 # print(sharpe)316 tvr=pd.DataFrame(columns=a.columns,index=a.index)317 for i in range(1,len(a)):318 tvr.iloc[[i]] = abs(a.iloc[[i]] - a.iloc[[i - 1]].values)319 tvr = tvr.dropna()320 tvr_cs = tvr.sum(axis=1)321 x=mean_sma(tvr_cs.to_frame(),250)322 tvr_col = pd.DataFrame(columns=x.columns, index=x.index)323 for i in range(0, 7):324 tvr_col.iloc[i, 0] = x.iloc[250 * i-1, 0]325 new=pd.DataFrame(columns=x.columns, index=x.index)326 new=mean_sma(tvr_cs.to_frame(),1968-250*7)327 tvr_col=tvr_col.dropna()328 tvr_col = pd.concat([tvr_col,new.iloc[[-1]]])329 tvr_col=tvr_col*100330 tvr_col=tvr_col.dropna()331 tvr_col['year'] = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']332 tvr_col = tvr_col.set_index('year')333 tvr_col.columns=['Turnover']334 tvr_col=np.around(tvr_col.astype(np.double),2)335 # print(tvr_col)336 gross_pnl['HighValue'] = gross_pnl['profit'].cummax()337 gross_pnl['Drawdown'] = (gross_pnl['HighValue'] - gross_pnl['profit'])/(gross_pnl['HighValue']+b)338 mdd =pd.DataFrame()339 for i in range(1, 8):340 mdd = pd.concat([mdd, pd.DataFrame(data=[gross_pnl.iloc[250 * (i - 1):250 * i, 2].max(axis=0)])],ignore_index=True)341 mdd=pd.concat([mdd,pd.DataFrame(data=[gross_pnl.iloc[250*7:1967,2].max(axis=0)])])342 mdd = round(mdd*100, 2)343 # print(mdd)344 mdd['year'] = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']345 mdd = mdd.set_index('year')346 mdd.columns=['Max Drawdown']347 mdd=np.around(mdd.astype(np.double),2)348 # gross_pnl['HighValue']349 # print(mdd)350 fit=(((yearly_ret/abs(yearly_ret))*(abs(yearly_ret)/tvr_col.values)**0.5)*abs(sharpe.values))351 # fit=round(fit,2)352 fit['year'] = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017']353 fit = fit.set_index('year')354 fit.columns=['Fitness']355 fit=np.around(fit.astype(np.double),2)356 stats=[ir,sharpe,tvr_col,mdd,yearly_ret,fit]357 stat=pd.concat(stats,axis=1)358 #print (tabulate(stat, headers='keys', tablefmt='psql'))359 headers=stat.columns360 data = [go.Scatter(x=gross_pnl.index, y=gross_pnl['profit'],fill='tonexty',361 mode='lines',362 line=dict(363 color='rgb(143, 19, 131)'))]364 layout = go.Layout(title=w,yaxis=dict(title='Gross PNL'),xaxis=dict(title='Dates'))365 fig = go.Figure(data=data, layout=layout)366 #py.offline.plot(fig, filename='backtested.html')367 header = stat.columns.tolist()368 header = ['Year'] + header369 Year = stat.index.tolist()370 IR = stat.iloc[:,0].values.tolist()371 Sharpe_Ratio = stat.iloc[:,1].values.tolist()372 Turnover = stat.iloc[:,2].values.tolist()373 Max_Drawdown = stat.iloc[:,3].values.tolist()374 Yearly_Returns = stat.iloc[:,4].values.tolist()375 Fitness = stat.iloc[:,5].values.tolist()376 values = [Year, IR, Sharpe_Ratio, Turnover, Max_Drawdown, Yearly_Returns, Fitness]377 data_for_x = gross_pnl.index.tolist()378 data_for_y = gross_pnl.iloc[:,0].values.tolist()379 coordinates = [data_for_x, data_for_y, values, values2, values3, header2, header3]380 return coordinates...
CorporateDaily.py
Source:CorporateDaily.py
1__author__ = 'marcopereira'2import numpy as np3import pandas as pd4import quandl5import pickle, os6from Scheduler.Scheduler import Scheduler7from parameters import WORKING_DIR, x0Vas, t_step, simNumber, trim_start, trim_end, freq, referenceDate8QUANDL_API_KEY = "J_3ufFECZVffPxNDZuWf"9FRED_API_KEY = "90746840bcdc4851bf3ad921ded8a212"10class CorporateRates(object):11 def __init__(self):12 self.OIS = []13 self.filename = WORKING_DIR + '/CorpData.dat'14 self.corporates = []15 self.ratings = ['AAA', 'AA', 'A', 'BBB', 'BB', 'B', 'CCC']16 self.corpSpreads = {}17 self.corporates = pd.DataFrame()18 self.tenors = []19 self.unPickleMe(file = self.filename)20 self.myScheduler=Scheduler(start=trim_start, end=trim_end, freq=freq, reference=referenceDate)21 def getCorporates(self, trim_start, trim_end):22 curr_trim_end = trim_start23 if(self.corporates.size != 0):24 self.trim_start = self.corporates['OIS'].index.min().date()25 curr_trim_end = self.corporates['OIS'].index.max().date()26 if trim_end <= curr_trim_end:27 self.trim_end = curr_trim_end28 return self.corporates29 self.trim_start = trim_start30 self.trim_end = trim_end31 self.OIS = OIS(trim_start=trim_start, trim_end=trim_end)32 self.datesAll = self.OIS.datesAll33 self.datesAll.columns= [x.upper() for x in self.datesAll.columns]34 self.OISData = self.OIS.getOIS()35 for i in np.arange(len(self.OISData.columns)):36 freq = self.OISData.columns[i]37 self.tenors.append(self.myScheduler.extractDelay(freq=freq))38 for rating in self.ratings:39 index = 'ML/' + rating + 'TRI'40 try:41 corpSpreads = 1e-4 * (quandl.get(index, authtoken=QUANDL_API_KEY, trim_start=trim_start, trim_end=trim_end))42 corpSpreads.reset_index(level=0, inplace=True)43 corpSpreads = pd.merge(left=self.datesAll, right=corpSpreads, how='left')44 corpSpreads = corpSpreads.fillna(method='ffill').fillna(method='bfill')45 self.corpSpreads[rating] = corpSpreads.T.fillna(method='ffill').fillna(method='bfill').T46 except:47 print(index, " not found")48 self.corpSpreads = pd.Panel.from_dict(self.corpSpreads)49 self.corporates = {}50 self.OISData.drop('DATE', axis=1, inplace=True)51 ntenors = np.shape(self.OISData)[1]52 for rating in self.ratings:53 try:54 tiledCorps = np.tile(self.corpSpreads[rating]['VALUE'], ntenors).reshape(np.shape(self.OISData))55 self.corporates[rating] = pd.DataFrame(data=(tiledCorps + self.OISData.values), index=self.OISData.index, columns=self.OISData.columns)56 except:57 print("Error in addition of Corp Spreads")58 self.corporates['OIS'] = self.OISData59 self.corporates = pd.Panel(self.corporates)60 return self.corporates61 def getOISData(self, datelist=[]):62 if (len(datelist) != 0):63 return self.corporates["OIS"].loc[datelist]64 else:65 return self.self.corporates["OIS"]66 def getCorporateData(self, rating, datelist=[]):67 if (len(datelist) != 0):68 return self.corporates[rating].loc[datelist]69 else:70 return self.corporates[rating]71 def pickleMe(self):72 data = [self.corporates, self.corpSpreads]73 with open(self.filename, "wb") as f:74 pickle.dump(len(data), f)75 for value in data:76 pickle.dump(value, f)77 def unPickleMe(self, file):78 data = []79 if (os.path.exists(file)):80 with open(file, "rb") as f:81 for _ in range(pickle.load(f)):82 data.append(pickle.load(f))83 self.corporates = data[0]84 self.corpSpreads = data[1]85 def saveMeExcel(self, whichdata, fileName):86 try:87 df = pd.DataFrame(whichdata)88 except:89 df = whichdata90 df.to_excel(fileName)91# Class OIS92class OIS(object):93 def __init__(self, trim_start="2005-01-10", trim_end="2010-01-10"):94 self.OIS = 0.01 * quandl.get("USTREASURY/YIELD", authtoken=QUANDL_API_KEY, trim_start=trim_start, trim_end=trim_end)95 self.OIS.reset_index(level=0, inplace=True)96 self.datesAll = pd.DataFrame(pd.date_range(trim_start, trim_end), columns=['DATE'])97 self.OIS.columns = [x.upper() for x in self.OIS.columns]98 self.OIS = pd.merge(left=self.datesAll, right=self.OIS, how='left')99 self.OIS = self.OIS.fillna(method='ffill').fillna(method='bfill')100 self.OIS = self.OIS.T.fillna(method='ffill').fillna(method='bfill').T101 self.OIS.index = self.datesAll.DATE102 def getOIS(self, datelist=[]):103 if (len(datelist) != 0):104 return self.OIS.loc[datelist]105 else:106 return self.OIS107if(__name__ == "__main__"):108 import quandl109 from parameters import WORKING_DIR, x0Vas, t_step, simNumber, trim_start, trim_end, freq, referenceDate110 #a = quandl.get("USTREASURY/YIELD", authtoken="J_3ufFECZVffPxNDZuWf", trim_start="2016-01-10", trim_end="2017-01-10")111 from Scheduler.Scheduler2 import Scheduler112 import pandas as pd113 import numpy as np114 mydatelist = Scheduler(start=trim_start, end=trim_end, freq=freq, reference=referenceDate).datelist1115 mycr = CorporateRates()116 bb = mycr.getCorporates(trim_start, trim_end)117 myois = OIS()118 aa = myois.getOIS(mydatelist)...
CorporateDailyVasicek.py
Source:CorporateDailyVasicek.py
1__author__ = 'marcopereira'2import numpy as np3import pandas as pd4import quandl5import pickle, os6from Scheduler.Scheduler import Scheduler7from parameters import WORKING_DIR8class CorporateRates(object):9 def __init__(self):10 self.OIS = []11 self.filename = WORKING_DIR + '/CorpData.dat'12 self.corporates = []13 self.ratings = ['AAA', 'AA', 'A', 'BBB', 'BB', 'B', 'CCC']14 self.corpSpreads = {}15 self.corporates = pd.DataFrame()16 self.tenors = []17 self.unPickleMe(file=self.filename)18 self.myScheduler=Scheduler()19 def getCorporates(self, trim_start, trim_end):20 curr_trim_end=trim_start21 if(self.corporates.size!=0):22 self.trim_start = self.corporates['OIS'].index.min().date()23 curr_trim_end = self.corporates['OIS'].index.max().date()24 if trim_end<=curr_trim_end:25 self.trim_end = curr_trim_end26 return self.corporates27 self.trim_start = trim_start28 self.trim_end = trim_end29 self.OIS = OIS(trim_start=trim_start, trim_end=trim_end)30 self.datesAll = self.OIS.datesAll31 self.datesAll.columns= [x.upper() for x in self.datesAll.columns]32 self.OISData = self.OIS.getOIS()33 for i in np.arange(len(self.OISData.columns)):34 freq = self.OISData.columns[i]35 self.tenors.append(self.myScheduler.extractDelay(freq=freq))36 for rating in self.ratings:37 index = 'ML/' + rating + 'TRI'38 try:39 corpSpreads = 1e-4 * (40 quandl.get(index, authtoken="Lqsxas8ieaKqpztgYHxk", trim_start=trim_start, trim_end=trim_end))41 corpSpreads.reset_index(level=0, inplace=True)42 corpSpreads = pd.merge(left=self.datesAll, right=corpSpreads, how='left')43 corpSpreads = corpSpreads.fillna(method='ffill').fillna(method='bfill')44 self.corpSpreads[rating] = corpSpreads.T.fillna(method='ffill').fillna(method='bfill').T45 except:46 print(index, " not found")47 self.corpSpreads = pd.Panel.from_dict(self.corpSpreads)48 self.corporates = {}49 self.OISData.drop('DATE', axis=1, inplace=True)50 ntenors = np.shape(self.OISData)[1]51 for rating in self.ratings:52 try:53 tiledCorps = np.tile(self.corpSpreads[rating]['VALUE'], ntenors).reshape(np.shape(self.OISData))54 self.corporates[rating] = pd.DataFrame(data=(tiledCorps + self.OISData.values),55 index=self.OISData.index, columns=self.OISData.columns)56 except:57 print("Error in addition of Corp Spreads")58 self.corporates['OIS'] = self.OISData59 self.corporates = pd.Panel(self.corporates)60 return self.corporates61 def getOISData(self, datelist=[]):62 if (len(datelist) != 0):63 return self.corporates["OIS"].loc[datelist]64 else:65 return self.self.corporates["OIS"]66 def getCorporateData(self, rating, datelist=[]):67 if (len(datelist) != 0):68 return self.corporates[rating].loc[datelist]69 else:70 return self.corporates[rating]71 def pickleMe(self):72 data = [self.corporates, self.corpSpreads]73 with open(self.filename, "wb") as f:74 pickle.dump(len(data), f)75 for value in data:76 pickle.dump(value, f)77 def unPickleMe(self, file):78 data = []79 if (os.path.exists(file)):80 with open(file, "rb") as f:81 for _ in range(pickle.load(f)):82 data.append(pickle.load(f))83 self.corporates = data[0]84 self.corpSpreads = data[1]85 def saveMeExcel(self, whichdata, fileName):86 try:87 df = pd.DataFrame(whichdata)88 except:89 df = whichdata90 df.to_excel(fileName)91# Class OIS92class OIS(object):93 def __init__(self, trim_start="2005-01-10", trim_end="2010-01-10"):94 self.OIS = 0.01 * quandl.get("USTREASURY/YIELD", authtoken="Lqsxas8ieaKqpztgYHxk", trim_start=trim_start,95 trim_end=trim_end)96 self.OIS.reset_index(level=0, inplace=True)97 self.datesAll = pd.DataFrame(pd.date_range(trim_start, trim_end), columns=['DATE'])98 self.OIS.columns = [x.upper() for x in self.OIS.columns]99 self.OIS = pd.merge(left=self.datesAll, right=self.OIS, how='left')100 self.OIS = self.OIS.fillna(method='ffill').fillna(method='bfill')101 self.OIS = self.OIS.T.fillna(method='ffill').fillna(method='bfill').T102 self.OIS.index = self.datesAll.DATE103 def getOIS(self, datelist=[]):104 if (len(datelist) != 0):105 return self.OIS.iloc[datelist]106 else:...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!