error fetching historical data
-
bktst_start_dt = "2021-06-01 09:15"
bktst_end_dt = "2022-10-21 15:30"if u look into this the start date is from 2021 but it doesn't fetch data from 2021 and throws me some error
@moderator
ONGC 1.694915
HDFCLIFE 1.646978
dtype: float64
top 5 gap stocks on 2022-05-05 00:00:00
WIPRO 1.881037
HINDALCO 1.808730
INDUSINDBK 1.707043
TECHM 1.653199
INFY 1.595520
dtype: float64
top 5 gap stocks on 2022-05-06 00:00:00
HEROMOTOCO 3.663582
HDFCLIFE 2.588855
HINDALCO 2.512998
BAJAJFINSV 2.399694
KOTAKBANK 2.361482
dtype: float64
top 5 gap stocks on 2022-05-09 00:00:00
SBILIFE 2.374546
HINDALCO 2.230986
AXISBANK 1.960202
ICICIBANK 1.939520
UPL 1.887272
dtype: float64
top 5 gap stocks on 2022-05-10 00:00:00
ONGC 2.610015
HINDALCO 2.049088
UPL 1.851852
ASIANPAINT 1.522336
dtype: float64
top 5 gap stocks on 2022-05-11 00:00:00
CIPLA 1.52783
dtype: float64
top 5 gap stocks on 2022-05-12 00:00:00
MARUTI 2.365324
HDFCBANK 2.039152
TATAMOTORS 1.584439
HINDALCO 1.550112
dtype: float64
top 5 gap stocks on 2022-05-13 00:00:00
TATAMOTORS 3.948429
ONGC 2.236422
BAJAJFINSV 2.165641
DIVISLAB 1.927529
ADANIENT 1.921572
dtype: float64
top 5 gap stocks on 2022-05-16 00:00:00
EICHERMOT 3.586624
JSWSTEEL 2.589724
HINDALCO 2.010309
TATASTEEL 2.005469
TATAMOTORS 1.879792
dtype: float64
top 5 gap stocks on 2022-05-17 00:00:00
HINDALCO 2.059087
TATASTEEL 1.857725
TITAN 1.678043
ONGC 1.628664
dtype: float64
top 5 gap stocks on 2022-05-18 00:00:00
BHARTIARTL 1.972986
BPCL 1.752081
dtype: float64
top 5 gap stocks on 2022-05-19 00:00:00
TATAMOTORS 3.649283
APOLLOHOSP 3.357430
ASIANPAINT 3.212800
HINDALCO 3.210273
WIPRO 3.137662
dtype: float64
top 5 gap stocks on 2022-05-20 00:00:00
HINDALCO 3.597122
TATASTEEL 3.582249
JSWSTEEL 3.465511
TATAMOTORS 2.847109
INDUSINDBK 2.510823
dtype: float64
top 5 gap stocks on 2022-05-23 00:00:00
TATASTEEL 6.287374
JSWSTEEL 5.878625
NTPC 3.208556
MARUTI 2.814172
HINDALCO 2.586810
dtype: float64
top 5 gap stocks on 2022-05-24 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-05-25 00:00:00
ADANIPORTS 2.280130
ASIANPAINT 1.550124
dtype: float64
top 5 gap stocks on 2022-05-26 00:00:00
APOLLOHOSP 3.924184
HINDALCO 2.431649
COALINDIA 1.798561
ITC 1.631830
dtype: float64
top 5 gap stocks on 2022-05-27 00:00:00
HINDALCO 3.739120
ONGC 2.040816
ADANIPORTS 1.796151
ADANIENT 1.641908
DIVISLAB 1.632499
dtype: float64
top 5 gap stocks on 2022-05-30 00:00:00
JSWSTEEL 3.355215
M&M 2.035997
ADANIENT 1.915727
INFY 1.755226
TECHM 1.749388
dtype: float64
top 5 gap stocks on 2022-05-31 00:00:00
SUNPHARMA 2.369695
dtype: float64
top 5 gap stocks on 2022-06-01 00:00:00
TITAN 1.759928
SUNPHARMA 1.673251
dtype: float64
top 5 gap stocks on 2022-06-02 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-06-03 00:00:00
ULTRACEMCO 1.7835
dtype: float64
top 5 gap stocks on 2022-06-06 00:00:00
JSWSTEEL 1.680149
dtype: float64
top 5 gap stocks on 2022-06-07 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-06-08 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-06-09 00:00:00
LT 1.789151
ONGC 1.591187
dtype: float64
top 5 gap stocks on 2022-06-10 00:00:00
HINDALCO 2.439024
BAJAJ-AUTO 2.001985
INFY 1.907591
TECHM 1.721412
WIPRO 1.652458
dtype: float64
top 5 gap stocks on 2022-06-13 00:00:00
TATAMOTORS 3.072071
KOTAKBANK 2.723518
TECHM 2.581430
BAJFINANCE 2.513454
ICICIBANK 2.465278
dtype: float64
top 5 gap stocks on 2022-06-14 00:00:00
ADANIENT 9.340065
TECHM 1.706355
ASIANPAINT 1.644273
dtype: float64
top 5 gap stocks on 2022-06-15 00:00:00
TATASTEEL 4.236097
dtype: float64
top 5 gap stocks on 2022-06-16 00:00:00
ICICIBANK 1.855220
BAJAJFINSV 1.823078
HCLTECH 1.727447
TATASTEEL 1.604835
JSWSTEEL 1.509769
dtype: float64
top 5 gap stocks on 2022-06-17 00:00:00
APOLLOHOSP 1.737338
TCS 1.591343
dtype: float64
top 5 gap stocks on 2022-06-20 00:00:00
ONGC 2.014846
dtype: float64
top 5 gap stocks on 2022-06-21 00:00:00
HINDALCO 1.897356
TATAMOTORS 1.646198
TATASTEEL 1.578825
INDUSINDBK 1.530515
dtype: float64
top 5 gap stocks on 2022-06-22 00:00:00
JSWSTEEL 1.553280
HINDALCO 1.547531
ONGC 1.545651
dtype: float64
top 5 gap stocks on 2022-06-23 00:00:00
HEROMOTOCO 1.905366
dtype: float64
top 5 gap stocks on 2022-06-24 00:00:00
ICICIBANK 1.680612
POWERGRID 1.655904
ONGC 1.632047
HEROMOTOCO 1.529487
dtype: float64
top 5 gap stocks on 2022-06-27 00:00:00
HINDALCO 2.854483
ICICIBANK 2.158525
POWERGRID 2.105801
DRREDDY 2.092977
TATASTEEL 2.063548
dtype: float64
top 5 gap stocks on 2022-06-28 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-06-29 00:00:00
APOLLOHOSP 1.828084
WIPRO 1.696899
HCLTECH 1.681721
BAJFINANCE 1.636404
BPCL 1.569366
dtype: float64
top 5 gap stocks on 2022-06-30 00:00:00
BAJAJ-AUTO 3.039392
dtype: float64
top 5 gap stocks on 2022-07-01 00:00:00
TITAN 2.382486
ONGC 1.715605
BRITANNIA 1.569351
dtype: float64
top 5 gap stocks on 2022-07-04 00:00:00
JSWSTEEL 2.893668
dtype: float64
top 5 gap stocks on 2022-07-05 00:00:00
TATAMOTORS 1.603623
dtype: float64
top 5 gap stocks on 2022-07-06 00:00:00
ONGC 3.453689
ASIANPAINT 2.311501
HINDALCO 1.578566
dtype: float64
top 5 gap stocks on 2022-07-07 00:00:00
TITAN 5.783318
HINDALCO 2.023757
dtype: float64
top 5 gap stocks on 2022-07-08 00:00:00
M&M 4.557487
ONGC 2.307692
TATASTEEL 1.988889
HINDALCO 1.755841
dtype: float64
top 5 gap stocks on 2022-07-11 00:00:00
BHARTIARTL 2.898655
TCS 1.815982
HINDALCO 1.552230
dtype: float64
top 5 gap stocks on 2022-07-12 00:00:00
HINDALCO 1.740595
dtype: float64
top 5 gap stocks on 2022-07-13 00:00:00
ASIANPAINT 1.92866
dtype: float64
top 5 gap stocks on 2022-07-14 00:00:00
POWERGRID 2.238249
HINDALCO 1.605044
APOLLOHOSP 1.517462
dtype: float64
top 5 gap stocks on 2022-07-15 00:00:00
ADANIPORTS 1.828974
dtype: float64
top 5 gap stocks on 2022-07-18 00:00:00
HINDALCO 1.526173
dtype: float64
top 5 gap stocks on 2022-07-19 00:00:00
BHARTIARTL 1.802277
dtype: float64
top 5 gap stocks on 2022-07-20 00:00:00
ONGC 5.551212
RELIANCE 4.222297
INDUSINDBK 2.061974
HINDALCO 1.916021
JSWSTEEL 1.820980
dtype: float64
top 5 gap stocks on 2022-07-21 00:00:00
INDUSINDBK 2.695326
TECHM 1.960784
dtype: float64
top 5 gap stocks on 2022-07-22 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-07-25 00:00:00
INFY 1.739361
ULTRACEMCO 1.698484
JSWSTEEL 1.536613
dtype: float64
top 5 gap stocks on 2022-07-26 00:00:00
BAJAJFINSV 3.014359
dtype: float64
top 5 gap stocks on 2022-07-27 00:00:00
UPL 1.562608
dtype: float64
top 5 gap stocks on 2022-07-28 00:00:00
TATASTEEL 2.251407
BAJFINANCE 1.964402
INFY 1.723259
HINDUNILVR 1.667344
BAJAJFINSV 1.502206
dtype: float64
top 5 gap stocks on 2022-07-29 00:00:00
SBILIFE 5.980108
TATASTEEL 2.640757
BAJAJFINSV 2.444322
DRREDDY 1.809838
TECHM 1.637765
dtype: float64
top 5 gap stocks on 2022-08-01 00:00:00
TATASTEEL 2.695167
M&M 2.142642
UPL 1.605721
dtype: float64
top 5 gap stocks on 2022-08-02 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-03 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-04 00:00:00
HINDALCO 3.077852
TATACONSUM 2.516149
dtype: float64
top 5 gap stocks on 2022-08-05 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-08 00:00:00
BPCL 2.05113
dtype: float64
top 5 gap stocks on 2022-08-10 00:00:00
HEROMOTOCO 1.714724
NTPC 1.568381
dtype: float64
top 5 gap stocks on 2022-08-11 00:00:00
EICHERMOT 3.215990
COALINDIA 2.797362
BAJAJFINSV 1.833384
WIPRO 1.824945
INFY 1.719242
dtype: float64
top 5 gap stocks on 2022-08-12 00:00:00
APOLLOHOSP 1.796982
dtype: float64
top 5 gap stocks on 2022-08-16 00:00:00
DIVISLAB 1.712200
M&M 1.679304
HEROMOTOCO 1.560520
dtype: float64
top 5 gap stocks on 2022-08-17 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-18 00:00:00
DRREDDY 1.758613
dtype: float64
top 5 gap stocks on 2022-08-19 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-22 00:00:00
KOTAKBANK 2.666489
dtype: float64
top 5 gap stocks on 2022-08-23 00:00:00
INDUSINDBK 2.099886
INFY 1.754831
TECHM 1.548160
dtype: float64
top 5 gap stocks on 2022-08-24 00:00:00
BHARTIARTL 1.999729
dtype: float64
top 5 gap stocks on 2022-08-25 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-08-26 00:00:00
M&M 1.607589
dtype: float64
top 5 gap stocks on 2022-08-29 00:00:00
HINDALCO 4.999432
SBILIFE 4.392175
COALINDIA 3.718200
TECHM 3.643314
HCLTECH 3.494951
dtype: float64
top 5 gap stocks on 2022-08-30 00:00:00
BAJAJFINSV 1.887309
TATASTEEL 1.573677
dtype: float64
top 5 gap stocks on 2022-09-01 00:00:00
EICHERMOT 3.505769
HINDALCO 3.416856
SBILIFE 2.859078
ICICIBANK 2.738645
ONGC 2.597403
dtype: float64
top 5 gap stocks on 2022-09-02 00:00:00
BAJAJFINSV 1.658011
dtype: float64
top 5 gap stocks on 2022-09-05 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-06 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-07 00:00:00
HINDALCO 1.554705
dtype: float64
top 5 gap stocks on 2022-09-08 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-09 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-12 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-13 00:00:00
HDFCLIFE 4.571774
BAJAJFINSV 2.403417
dtype: float64
top 5 gap stocks on 2022-09-14 00:00:00
WIPRO 3.229623
INFY 3.103660
TECHM 3.089459
TCS 2.921641
HINDALCO 2.439024
dtype: float64
top 5 gap stocks on 2022-09-15 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-16 00:00:00
TECHM 1.66205
dtype: float64
top 5 gap stocks on 2022-09-19 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-20 00:00:00
ADANIPORTS 1.521467
dtype: float64
top 5 gap stocks on 2022-09-21 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-22 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-23 00:00:00
TATASTEEL 2.509653
dtype: float64
top 5 gap stocks on 2022-09-26 00:00:00
RELIANCE 1.862989
M&M 1.752731
TATAMOTORS 1.678090
HINDALCO 1.602119
dtype: float64
top 5 gap stocks on 2022-09-27 00:00:00
POWERGRID 2.462825
TATAMOTORS 1.610063
TATASTEEL 1.502253
dtype: float64
top 5 gap stocks on 2022-09-28 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-09-29 00:00:00
TATAMOTORS 2.981709
TATASTEEL 1.890756
INDUSINDBK 1.674117
HINDALCO 1.663202
BAJFINANCE 1.527247
dtype: float64
top 5 gap stocks on 2022-09-30 00:00:00
GRASIM 1.642635
dtype: float64
top 5 gap stocks on 2022-10-03 00:00:00
ONGC 3.864353
EICHERMOT 2.979627
MARUTI 1.606792
dtype: float64
top 5 gap stocks on 2022-10-04 00:00:00
HINDALCO 3.950650
SBIN 2.549303
ADANIPORTS 2.498725
ADANIENT 2.302600
BAJFINANCE 2.275579
dtype: float64
top 5 gap stocks on 2022-10-06 00:00:00
HINDALCO 2.017510
ONGC 1.941748
dtype: float64
top 5 gap stocks on 2022-10-07 00:00:00
TITAN 3.746842
dtype: float64
top 5 gap stocks on 2022-10-10 00:00:00
TATAMOTORS 2.123013
WIPRO 1.911296
ASIANPAINT 1.905075
HINDALCO 1.848699
CIPLA 1.813357
dtype: float64
top 5 gap stocks on 2022-10-11 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-10-12 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-10-13 00:00:00
WIPRO 5.135433
HINDALCO 2.698651
HCLTECH 2.106092
dtype: float64
top 5 gap stocks on 2022-10-14 00:00:00
INFY 4.584830
ICICIBANK 3.537957
HDFCLIFE 2.640930
ADANIPORTS 2.453011
HINDALCO 2.415639
dtype: float64
top 5 gap stocks on 2022-10-17 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-10-18 00:00:00
WIPRO 2.292722
dtype: float64
top 5 gap stocks on 2022-10-19 00:00:00
Series([], dtype: float64)
top 5 gap stocks on 2022-10-20 00:00:00
HINDALCO 1.870356
dtype: float64
top 5 gap stocks on 2022-10-21 00:00:00
AXISBANK 4.147242
dtype: float64
[E 240602 10:31:08 smartConnect:243] Error occurred while making a POST request to https://apiconnect.angelbroking.com/rest/secure/angelbroking/historical/v1/getCandleData. Error: Something Went Wrong, Please Try After Sometime. URL: https://apiconnect.angelbroking.com/rest/secure/angelbroking/historical/v1/getCandleData, Headers: {'Content-type': 'application/json', 'X-ClientLocalIP': '127.0.0.1', 'X-ClientPublicIP': '106.193.147.98', 'X-MACAddress': 'a9:31:1f:9d:a7:f3', 'Accept': 'application/json', 'X-PrivateKey': 'te2Wr9fI', 'X-UserType': 'USER', 'X-SourceID': 'WEB'}, Request: {'exchange': 'NSE', 'symboltoken': '910', 'interval': 'FIVE_MINUTE', 'fromdate': '2021-11-04 09:15', 'todate': '2021-11-04 15:30'}, Response: {'message': 'Something Went Wrong, Please Try After Sometime', 'errorcode': 'AB1004', 'status': False, 'data': None}
EICHERMOT 2021-11-04 00:00:00@Moderator_3 this is my code please check and give me an answer
from SmartApi import SmartConnect
import os
import urllib
import json
import pandas as pd
import time
from pyotp import TOTPkey_path = r"C:\Users\kswat\angelone"
os.chdir(key_path)key_secret = open("angeloneapi.txt","r").read().split()
obj=SmartConnect(api_key=key_secret[0])
data = obj.generateSession(key_secret[2],key_secret[3],TOTP(key_secret[4]).now())instrument_url = "https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json"
response = urllib.request.urlopen(instrument_url)
instrument_list = json.loads(response.read())tickers = ["WIPRO","ULTRACEMCO","UPL","TITAN","TECHM","TATASTEEL","TATAMOTORS",
"TATACONSUM","TCS","SUNPHARMA","SBIN","SBILIFE","RELIANCE","POWERGRID",
"ONGC","NESTLEIND","NTPC","MARUTI","M&M","LT","KOTAKBANK","JSWSTEEL",
"INFY","INDUSINDBK","ITC","ICICIBANK","HINDUNILVR","HINDALCO",
"HEROMOTOCO","HDFCLIFE","HDFCBANK","HCLTECH","GRASIM","EICHERMOT",
"DRREDDY","DIVISLAB","COALINDIA","CIPLA","BRITANNIA","BHARTIARTL",
"BPCL","BAJAJFINSV","BAJFINANCE","BAJAJ-AUTO","AXISBANK","ASIANPAINT",
"APOLLOHOSP","ADANIPORTS","ADANIENT"]bktst_start_dt = "2021-06-01 09:15"
bktst_end_dt = "2022-10-21 15:30"def token_lookup(ticker, instrument_list, exchange="NSE"):
for instrument in instrument_list:
if instrument["name"] == ticker and instrument["exch_seg"] == exchange and instrument["symbol"].split('-')[-1] == "EQ":
return instrument["token"]def symbol_lookup(token, instrument_list, exchange="NSE"):
for instrument in instrument_list:
if instrument["token"] == token and instrument["exch_seg"] == exchange and instrument["symbol"].split('-')[-1] == "EQ":
return instrument["name"]def hist_data_daily(tickers,instrument_list,exchange="NSE"):
hist_data_tickers = {}
for ticker in tickers:
time.sleep(0.3)
params = {
"exchange": exchange,
"symboltoken": token_lookup(ticker,instrument_list),
"interval": "ONE_DAY",
"fromdate": bktst_start_dt,
"todate": bktst_end_dt
}
hist_data = obj.getCandleData(params)
df_data = pd.DataFrame(hist_data["data"],
columns = ["date","open","high","low","close","volume"])
df_data.set_index("date",inplace=True)
df_data.index = pd.to_datetime(df_data.index)
df_data.index = df_data.index.tz_localize(None)
df_data["gap"] = ((df_data["open"]/df_data["close"].shift(1))-1)*100
df_data["avvol"] = df_data["volume"].rolling(10).mean().shift(1)
hist_data_tickers[ticker] = df_data
return hist_data_tickersdef hist_data_intraday(ticker,datestamp,interval,instrument_list,exchange="NSE"):
params = {
"exchange": exchange,
"symboltoken": token_lookup(ticker,instrument_list),
"interval": interval,
"fromdate": datestamp.strftime("%Y-%m-%d")+ " 09:15",
"todate": datestamp.strftime("%Y-%m-%d") + " 15:30"
}
hist_data = obj.getCandleData(params)
df_data = pd.DataFrame(hist_data["data"],
columns = ["date","open","high","low","close","volume"])
df_data.set_index("date",inplace=True)
df_data.index = pd.to_datetime(df_data.index)
df_data.index = df_data.index.tz_localize(None)
return df_datacandle_data = hist_data_daily(tickers, instrument_list)
def topGap(data):
top_gap_by_date = {}
dates = data[tickers[0]].index.to_list()
for date in dates:
temp = pd.Series()
for ticker in data:
try:
temp.loc[ticker] = data[ticker].loc[date,"gap"]
except:
pass
top_gap_by_date[date] = (abs(temp[abs(temp)>1.5])).sort_values(ascending=False)[:5].index.to_list()
print("top 5 gap stocks on {}".format(date))
print((abs(temp[abs(temp)>1.5])).sort_values(ascending=False)[:5])return top_gap_by_date
top_gap_by_date = topGap(candle_data)
def backtest(top_gap_by_date, candle_data):
date_stats = {}
for date in top_gap_by_date:
date_orgnl = date.strftime("%Y-%m-%d %H:%M")
date_stats[date] = {}
for ticker in top_gap_by_date[date]:
try:
intraday_df = hist_data_intraday(ticker,date,'FIVE_MINUTE',instrument_list)
hi_price = intraday_df.iloc[0]['high']
lo_price = intraday_df.iloc[0]['low']
open_price = ''
direction = ''
date_stats[date][ticker] = 0
for i in range(1,len(intraday_df[1:])):
if intraday_df.iloc[i]["volume"] > 2*(candle_data[ticker].loc[date_orgnl,"avvol"])/75
and intraday_df.iloc[i]["high"] > hi_price
and open_price == '':
open_price = 0.8intraday_df.iloc[i+1]["open"] + 0.2intraday_df.iloc[i+1]["high"] #factoring in slippage
direction = 'long'
elif intraday_df.iloc[i]["volume"] > 2*(candle_data[ticker].loc[date_orgnl,"avvol"])/75
and intraday_df.iloc[i]["low"] < lo_price
and open_price == '':
open_price = 0.8intraday_df.iloc[i+1]["open"] + 0.2intraday_df.iloc[i+1]["low"] #factoring in slippage
direction = 'short'if open_price != '' and direction == 'long': if intraday_df.iloc[i]["high"] > hi_price*1.05: ticker_return = ((hi_price*1.05)/open_price)-1 date_stats[date][ticker] = ticker_return break elif intraday_df.iloc[i]["low"] < lo_price: ticker_return = (lo_price/open_price) - 1 date_stats[date][ticker] = ticker_return break else: ticker_return = (intraday_df.iloc[i]["close"]/open_price) - 1 date_stats[date][ticker] = ticker_return if open_price != '' and direction == 'short': if intraday_df.iloc[i]["low"] < lo_price*0.95: ticker_return = 1 - ((lo_price*0.95)/open_price) date_stats[date][ticker] = ticker_return break elif intraday_df.iloc[i]["high"] > hi_price: ticker_return = 1 - (hi_price/open_price) date_stats[date][ticker] = ticker_return break else: ticker_return = 1 - (intraday_df.iloc[i]["close"]/open_price) date_stats[date][ticker] = ticker_return except: print(ticker,date) return date_stats
date_stats = backtest(top_gap_by_date, candle_data)
###########################KPIs#####################################
def abs_return(date_stats):
df = pd.DataFrame(date_stats).T
df["ret"] = df.mean(axis=1)
df["ret"].fillna(0,inplace=True)
return (1+df["ret"]).cumprod().iloc[-1] - 1def win_rate(date_stats):
win_count = 0
lose_count = 0
for i in date_stats:
for ticker in date_stats[i]:
if date_stats[i][ticker] > 0:
win_count+=1
elif date_stats[i][ticker] < 0:
lose_count+=1
return (win_count/(win_count+lose_count))*100def mean_ret_winner(date_stats):
win_ret = []
for i in date_stats:
for ticker in date_stats[i]:
if date_stats[i][ticker] > 0:
win_ret.append(date_stats[i][ticker])
return sum(win_ret)/len(win_ret)def mean_ret_loser(date_stats):
los_ret = []
for i in date_stats:
for ticker in date_stats[i]:
if date_stats[i][ticker] < 0:
los_ret.append(date_stats[i][ticker])
return sum(los_ret)/len(los_ret)def equity_curve(date_stats):
df = pd.DataFrame(date_stats).T
df["ret"] = df.mean(axis=1)
df["ret"].fillna(0,inplace=True)
df["cum_ret"] = (1+df["ret"]).cumprod() - 1
df["cum_ret"].plot(title="return profile")print("*Strategy Performance Statistics")
print("total cumulative return = {}".format(round(abs_return(date_stats),4)))
print("total win rate = {}".format(round(win_rate(date_stats),2)))
print("mean return per win trade = {}".format(round(mean_ret_winner(date_stats),4)))
print("mean return per loss trade = {}".format(round(mean_ret_loser(date_stats),4)))
equity_curve(date_stats) -
Hello @swathi-priya
You are trying to get the data of a market holiday (2021-11-04).Due to this only the mentioned error happened.
Regards,
SmartAPI Team