Creating a trading system from scratch

How many lines of code you are comfortable with


  • Total voters
    61

ncube

Well-Known Member
Ya i m using windows 10 n have schedule it for tommorrow 9.12...Hope it works fingerscrossed...:hungover:
I believe this scheduling is only for generating the zerodha_order.html page, Once it is generated, one need to manually open it and submit the orders to zerodha kite or even the order submission is automated?
 

UberMachine

Well-Known Member
This might reduce some effort of downloading csv and changing dates
Edit-Forgot Holidays now must work fine
still needs to consider if two holiday are together except SAT,SUN maybe creating local file having holiday dates was less messy
Python:
import os.path
import requests,zipfile,io
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 10)
PREVDAY=yesterday.strftime("%a")

if PREVDAY=='Sun':
    yesterday -= datetime.timedelta(days = 2)

DATE=yesterday.strftime("%d%b%Y").upper()
YEAR=yesterday.strftime("%Y")
MON=yesterday.strftime('%b').upper()

LINK= f"https://www.nseindia.com/content/historical/EQUITIES/{YEAR}/{MON}/cm{DATE}bhav.csv.zip"

print(f'Downloading -{DATE} csv')

r = requests.get(LINK)

if len(r.content)<300:
    print('Yesterday was holiday')
    yesterday -= datetime.timedelta(days = 1)
    DATE=yesterday.strftime("%d%b%Y").upper()
    YEAR=yesterday.strftime("%Y")
    MON=yesterday.strftime('%b').upper()
    LINK=f"https://www.nseindia.com/content/historical/EQUITIES/{YEAR}/{MON}/cm{DATE}bhav.csv.zip"
    r = requests.get(LINK)
    z = zipfile.ZipFile(io.BytesIO(r.content))
    z.extractall(path='C:\\tradingsystem\\data')
    print (f"Check if ORDERFILE {DATE} date is correct, File downloaded")

else:
    z = zipfile.ZipFile(io.BytesIO(r.content))
    z.extractall(path='C:\\tradingsystem\\data')
    print ("File downloaded")

ORDER_FILENAME = f'data/cm{DATE}bhav.csv'
Good work. :up:
My code is something similar for extracting the bhav file
Python:
def get_bhav_copy(date):
    """
    Get bhav copy for NSE for a given date and
    convert it into a dataframe
    TO DO: Add BadFile Exception
    """
    from zipfile import ZipFile
    from io import BytesIO
   
    base_url = 'https://www.nseindia.com/content/historical/EQUITIES/2018/AUG/cm'
    headers = {'Referer': 'https://www.nseindia.com/products/content/all_daily_reports.htm'}
    d = Timestamp(date)
    bhavfile = base_url + d.strftime('%d%b%Y').upper() + 'bhav.csv.zip'
    bhavfile = base_url + '21AUG2018bhav.csv.zip' # Uncomment for custom date
    req = requests.get(bhavfile, headers=headers)
    z = ZipFile(BytesIO(req.content))
    fn = z.namelist()[0]
    with z.open(fn) as f:
        db = read_csv(f, index_col=0, parse_dates=True, usecols=range(12))
    return db.query('SERIES == "EQ"').reset_index()
And I use pandas bdate_range function for dates. I do manual adjustments every month and for holidays
Python:
d, _ = pd.bdate_range(end=datetime.datetime.now(), periods=2)
You could include this in utils.py file and use it in the notebook.
 

Similar threads