Learn Algo Trading with Python | Codes | Youtube Series

Very Good Evening Sir, I would like to share my notes out of your Video series with all learners who are interested.

I need your verification of my ‘NOTES’ and permission for the same to share on this platform sir.

Herewith I am sending the abstract notes I have prepared from SESSION-10 and SESSION-11 sir. Please correct for any errors sir.

VBR Prasad

SESSION-10 - “RP-Tick by Tick data storage” Part-1

—Dt.03-04-2025—SESSION 10----

If you are a Scalper, you need Tick by Tick Data

Copy Files ‘Option_Selling_Algo’ and ‘Live_Trade_Data’ files from Session 8

Rename the file ‘Option_Selling_Algo’ as ‘Tick By Tick Data Storage’

import pdb
import time
import datetime
import traceback
from Dhan_Tradehull import Tradehull
import pandas as pd
from pprint import pprint
import talib
import pandas_ta as ta
import xlwings as xw
import winsound

client_code = “11111111”
token_id = “eeeeeeee”
tsl = Tradehull(client_code,token_id)

watchlist = [‘NIFTY’]

wb = xw.Book(‘Live Trade Data.xlsx’)
data_storage = wb.sheets[‘Data Storage’]

bot_token = “8059847390:AAECSnQK-yOaGJ-clJchb1cx8CDhx2VQq-M”

receiver_chat_id = “1918451082”

bot_token = “7570885801:AAGQoHl00ydQPCgldwXhu4Qn3A9HpnHG630”
receiver_chat_id = “6092170243”

Delete rest of all code lines of ‘Option_Selling_Algo’ file.

ce_strike, pe_strike, strike = tsl.ATM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0)

all_strike = [strike + number*50 for number in range(-20, 20)] # 2. finds all 40 strikes
all_ce_names = [“NIFTY 17 APR " + str(strike) + " CALL” for strike in all_strike]
all_pe_names = [“NIFTY 17 APR " + str(strike) + " PUT” for strike in all_strike]
equity_names = [“ACC”, “CIPLA”, “GAIL”, “NIFTY”]

all_names = all_ce_names + all_pe_names + equity_names

data_storage.range(‘B1’).value = all_names

all_ltp = tsl.get_ltp_data(names=all_names)

while True: # Now let’s replace the ‘while True’ to ‘for’ loop

for row_no in range(2, 1000000):

try:

	current_time = datetime.datetime.now()
	print(current_time)


	if current_time.second % 5 == 0:  # 5 stands for value in seconds and Here % stands for 'REMAINDER' of the unit 'second'
		tsl.send_telegram_alert(message=f"{current_time} data storage is working fine ",receiver_chat_id=receiver_chat_id,bot_token=bot_token)

	all_ltp = tsl.get_ltp_data(names=all_names) # Rest of the lines need not be cycled.
	
	temp_dict = {}
	temp_dict[current_time] = all_ltp

	temp_dict = pd.DataFrame(temp_dict)  # I am getting error as 'temp_dict' is not defined.
	
	ltp_sequence = temp_dict.T[all_names].iloc[0].to_list()

	data_storage.range('B' + str(row_no)).value = ltp_sequence
	
	data_storage.range('A' + str(row_no)).value = str(current_time)


except Exception as e:
	print(e)
	tsl.send_telegram_alert(message="error in storing data",receiver_chat_id=receiver_chat_id,bot_token=bot_token)
	continue







# temp_dict[all_names]
# (pdb++) temp_dict
# ['NIFTY 09 APR 21900 CALL', 'NIFTY 09 APR 21950 CALL', 'NIFTY 09 APR 22000 CALL', 'NIFTY 09 APR 22050 CALL', 'NIFTY 09 APR 22100 CALL', 'NIFTY 09 APR 22150 CALL', 'NIFTY 09 APR 22200 CALL', 'NIFTY 09 APR 22250 CALL', 'NIFTY 09 APR 22300 CALL', 'NIFTY 09 APR 22350 CALL', 'NIFTY 09 APR 22400 CALL', 'NIFTY 09 APR 22450 CALL', 'NIFTY 09 APR 22500 CALL', 'NIFTY 09 APR 22550 CALL', 'NIFTY 09 APR 22600 CALL', 'NIFTY 09 APR 22650 CALL', 'NIFTY 09 APR 22700 CALL', 'NIFTY 09 APR 22750 CALL', 'NIFTY 09 APR 22800 CALL', 'NIFTY 09 APR 22850 CALL', 'NIFTY 09 APR 22900 CALL', 'NIFTY 09 APR 22950 CALL', 'NIFTY 09 APR 23000 CALL', 'NIFTY 09 APR 23050 CALL', 'NIFTY 09 APR 23100 CALL', 'NIFTY 09 APR 23150 CALL', 'NIFTY 09 APR 23200 CALL', 'NIFTY 09 APR 23250 CALL', 'NIFTY 09 APR 23300 CALL', 'NIFTY 09 APR 23350 CALL', 'NIFTY 09 APR 23400 CALL', 'NIFTY 09 APR 23450 CALL', 'NIFTY 09 APR 23500 CALL', 'NIFTY 09 APR 23550 CALL', 'NIFTY 09 APR 23600 CALL', 'NIFTY 09 APR 23650 CALL', 'NIFTY 09 APR 23700 CALL', 'NIFTY 09 APR 23750 CALL', 'NIFTY 09 APR 23800 CALL', 'NIFTY 09 APR 23850 CALL', 'NIFTY 09 APR 21900 PUT', 'NIFTY 09 APR 21950 PUT', 'NIFTY 09 APR 22000 PUT', 'NIFTY 09 APR 22050 PUT', 'NIFTY 09 APR 22100 PUT', 'NIFTY 09 APR 22150 PUT', 'NIFTY 09 APR 22200 PUT', 'NIFTY 09 APR 22250 PUT', 'NIFTY 09 APR 22300 PUT', 'NIFTY 09 APR 22350 PUT', 'NIFTY 09 APR 22400 PUT', 'NIFTY 09 APR 22450 PUT', 'NIFTY 09 APR 22500 PUT', 'NIFTY 09 APR 22550 PUT', 'NIFTY 09 APR 22600 PUT', 'NIFTY 09 APR 22650 PUT', 'NIFTY 09 APR 22700 PUT', 'NIFTY 09 APR 22750 PUT', 'NIFTY 09 APR 22800 PUT', 'NIFTY 09 APR 22850 PUT', 'NIFTY 09 APR 22900 PUT', 'NIFTY 09 APR 22950 PUT', 'NIFTY 09 APR 23000 PUT', 'NIFTY 09 APR 23050 PUT', 'NIFTY 09 APR 23100 PUT', 'NIFTY 09 APR 23150 PUT', 'NIFTY 09 APR 23200 PUT', 'NIFTY 09 APR 23250 PUT', 'NIFTY 09 APR 23300 PUT', 'NIFTY 09 APR 23350 PUT', 'NIFTY 09 APR 23400 PUT', 'NIFTY 09 APR 23450 PUT', 'NIFTY 09 APR 23500 PUT', 'NIFTY 09 APR 23550 PUT', 'NIFTY 09 APR 23600 PUT', 'NIFTY 09 APR 23650 PUT', 'NIFTY 09 APR 23700 PUT', 'NIFTY 09 APR 23750 PUT', 'NIFTY 09 APR 23800 PUT', 'NIFTY 09 APR 23850 PUT', 'ACC', 'CIPLA', 'GAIL', 'NIFTY']  
# (pdb++) temp_dict.T  



# pdb.set_trace()

If I want to store Tick by Tick Data for 40 strikes, Also Tick by Tick data for ACC, CIPLAand GAIL

1. Find ATM =

2. find_40_strikes =

3. get_call_and_put_names =

4. all_ltp_data =

5. store_ltp =

CE_symbol_name, PE_symbol_name, strike = tsl.ATM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0)

(pdb++) CE_symbol_name, PE_symbol_name, strike_price = tsl.ATM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0)

(pdb++) strike # 1. Finds the ATM

[x for x in range(-20, 20)]

(pdb++) [x for x in range(-20, 20)]

In Python: The Following Code line is called ‘LIST COMPREHENSION’, which offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you

[strike + x*50 for x in range(-20, 20)] # strike stands for strike, 50 stands for Nigty Lot size, ‘x’ stands for first number: -20

(pdb++) [strike + x*50 for x in range(-20, 20)]

(pdb++) strike + -20*50 # Gives the value at -20.

Now I want to rename the above line as below:

all_strike = [strike + number*50 for number in range(-20, 20)] # 2. finds all 40 strikes

all_ce_names = [strike for strike in all_strike]

(pdb++) all_ce_names = [strike for strike in all_strike]

(pdb++) all_ce_names

[21900, 21950, 22000, 22050, 22100, 22150, 22200, 22250, 22300, 22350, 22400, 22450, 22500, 22550, 22600, 22650, 22700, 22750, 22800, 22850, 22900, 22950, 23000, 23050, 23100, 23150, 23200, 23250, 23300, 23350, 23400, 23450, 23500, 23550, 23600, 23650, 23700, 23750, 23800, 23850]

all_ce_names = [str(strike) for strike in all_strike] # Now the above line is converted to get the sring values.

(pdb++) all_ce_names = [str(strike) for strike in all_strike]

(pdb++) all_ce_names

[‘21900’, ‘21950’, ‘22000’, ‘22050’, ‘22100’, ‘22150’, ‘22200’, ‘22250’, ‘22300’, ‘22350’, ‘22400’, ‘22450’, ‘22500’, ‘22550’, ‘22600’, ‘22650’, ‘22700’, ‘22750’, ‘22800’, ‘22850’, ‘22900’, ‘22950’, ‘23000’, ‘23050’, ‘23100’, ‘23150’, ‘23200’, ‘23250’, ‘23300’, ‘23350’, ‘23400’, ‘23450’, ‘23500’, ‘23550’, ‘23600’, ‘23650’, ‘23700’, ‘23750’, ‘23800’, ‘23850’]

all_ce_names = ["NIFTY 09 APR " + str(strike) for strike in all_strike] # Now I have added another string “NIFTY 10 APR” to the above line of code.

(pdb++) all_ce_names = [“NIFTY 09 APR” + str(strike) for strike in all_strike]

(pdb++) all_ce_names

[‘NIFTY 09 APR 21900’, ‘NIFTY 09 APR 21950’, ‘NIFTY 09 APR 22000’, ‘NIFTY 09 APR 22050’, ‘NIFTY 09 APR 22100’, ‘NIFTY 09 APR 22150’, ‘NIFTY 09 APR 22200’, ‘NIFTY 09 APR 22250’, ‘NIFTY 09 APR 22300’, ‘NIFTY 09 APR 22350’, ‘NIFTY 09 APR 22400’, ‘NIFTY 09 APR 22450’, ‘NIFTY 09 APR 22500’, ‘NIFTY 09 APR 22550’, ‘NIFTY 09 APR 22600’, ‘NIFTY 09 APR 22650’, ‘NIFTY 09 APR 22700’, ‘NIFTY 09 APR 22750’, ‘NIFTY 09 APR 22800’, ‘NIFTY 09 APR 22850’, ‘NIFTY 09 APR 22900’, ‘NIFTY 09 APR 22950’, ‘NIFTY 09 APR 23000’, ‘NIFTY 09 APR 23050’, ‘NIFTY 09 APR 23100’, ‘NIFTY 09 APR 23150’, ‘NIFTY 09 APR 23200’, ‘NIFTY 09 APR 23250’, ‘NIFTY 09 APR 23300’, ‘NIFTY 09 APR 23350’, ‘NIFTY 09 APR 23400’, ‘NIFTY 09 APR 23450’, ‘NIFTY 09 APR 23500’, ‘NIFTY 09 APR 23550’, ‘NIFTY 09 APR 23600’, ‘NIFTY 09 APR 23650’, ‘NIFTY 09 APR 23700’, ‘NIFTY 09 APR 23750’, ‘NIFTY 09 APR 23800’, ‘NIFTY 09 APR 23850’]

all_ce_names = [“NIFTY 09 APR " + str(strike) + " CALL” for strike in all_strike] # Now I have added another string “CALL” to the above line of code.

(pdb++) all_ce_names = [“NIFTY 10 APR” + str(strike) for strike in all_strike]

(pdb++) all_ce_names

[‘NIFTY 09 APR 21900 CALL’, ‘NIFTY 09 APR 21950 CALL’, ‘NIFTY 09 APR 22000 CALL’, ‘NIFTY 09 APR 22050 CALL’, ‘NIFTY 09 APR 22100 CALL’, ‘NIFTY 09 APR 22150 CALL’, ‘NIFTY 09 APR 22200 CALL’, ‘NIFTY 09 APR 22250 CALL’, ‘NIFTY 09 APR 22300 CALL’, ‘NIFTY 09 APR 22350 CALL’, ‘NIFTY 09 APR 22400 CALL’, ‘NIFTY 09 APR 22450 CALL’, ‘NIFTY 09 APR 22500 CALL’, ‘NIFTY 09 APR 22550 CALL’, ‘NIFTY 09 APR 22600 CALL’, ‘NIFTY 09 APR 22650 CALL’, ‘NIFTY 09 APR 22700 CALL’, ‘NIFTY 09 APR 22750 CALL’, ‘NIFTY 09 APR 22800 CALL’, ‘NIFTY 09 APR 22850 CALL’, ‘NIFTY 09 APR 22900 CALL’, ‘NIFTY 09 APR 22950 CALL’, ‘NIFTY 09 APR 23000 CALL’, ‘NIFTY 09 APR 23050 CALL’, ‘NIFTY 09 APR 23100 CALL’, ‘NIFTY 09 APR 23150 CALL’, ‘NIFTY 09 APR 23200 CALL’, ‘NIFTY 09 APR 23250 CALL’, ‘NIFTY 09 APR 23300 CALL’, ‘NIFTY 09 APR 23350 CALL’, ‘NIFTY 09 APR 23400 CALL’, ‘NIFTY 09 APR 23450 CALL’, ‘NIFTY 09 APR 23500 CALL’, ‘NIFTY 09 APR 23550 CALL’, ‘NIFTY 09 APR 23600 CALL’, ‘NIFTY 09 APR 23650 CALL’, ‘NIFTY 09 APR 23700 CALL’, ‘NIFTY 09 APR 23750 CALL’, ‘NIFTY 09 APR 23800 CALL’, ‘NIFTY 09 APR 23850 CALL’]

NOW LET’S GET THE LTP OF ALL CE NAMES

ltp = tsl.get_ltp_data(names=all_ce_names)

(pdb++) ltp = tsl.get_ltp_data(names=all_ce_names)

(pdb++) ltp

{‘NIFTY 09 APR 21900 CALL’: 986.1, ‘NIFTY 09 APR 21950 CALL’: 928.5, ‘NIFTY 09 APR 22000 CALL’: 898, ‘NIFTY 09 APR 22050 CALL’: 838.05, ‘NIFTY 09 APR 22100 CALL’: 795, ‘NIFTY 09 APR 22150 CALL’: 717.6, ‘NIFTY 09 APR 22200 CALL’: 705.7, ‘NIFTY 09 APR 22250 CALL’: 651.45, ‘NIFTY 09 APR 22300 CALL’: 607.1, ‘NIFTY 09 APR 22350 CALL’: 555, ‘NIFTY 09 APR 22400 CALL’: 508, ‘NIFTY 09 APR 22450 CALL’: 463.5, ‘NIFTY 09 APR 22500 CALL’: 407.45, ‘NIFTY 09 APR 22550 CALL’: 371.55, ‘NIFTY 09 APR 22600 CALL’: 328.45, ‘NIFTY 09 APR 22650 CALL’: 286.25, ‘NIFTY 09 APR 22700 CALL’: 247, ‘NIFTY 09 APR 22750 CALL’: 211.4, ‘NIFTY 09 APR 22800 CALL’: 175, ‘NIFTY 09 APR 22850 CALL’: 144, ‘NIFTY 09 APR 22900 CALL’: 115.7, ‘NIFTY 09 APR 22950 CALL’: 92.45, ‘NIFTY 09 APR 23000 CALL’: 71.55, ‘NIFTY 09 APR 23050 CALL’: 55.85, ‘NIFTY 09 APR 23100 CALL’: 43.15, ‘NIFTY 09 APR 23150 CALL’: 32.95, ‘NIFTY 09 APR 23200 CALL’: 25.05, ‘NIFTY 09 APR 23250 CALL’: 19.1, ‘NIFTY 09 APR 23300 CALL’: 15.15, ‘NIFTY 09 APR 23350 CALL’: 11.7, ‘NIFTY 09 APR 23400 CALL’: 9.15, ‘NIFTY 09 APR 23450 CALL’: 7.55, ‘NIFTY 09 APR 23500 CALL’: 6.3, ‘NIFTY 09 APR 23550 CALL’: 5.05, ‘NIFTY 09 APR 23600 CALL’: 4.15, ‘NIFTY 09 APR 23650 CALL’: 3.6, ‘NIFTY 09 APR 23700 CALL’: 3, ‘NIFTY 09 APR 23750 CALL’: 2.75, ‘NIFTY 09 APR 23800 CALL’: 2.4, ‘NIFTY 09 APR 23850 CALL’: 2.1}

NOW SIMILARLY LET’S GET THE LTP OF ALL PE NAMES

all_pe_names = [“NIFTY 09 APR " + str(strike) + " PUT” for strike in all_strike]

(pdb++) ltp = tsl.get_ltp_data(names=all_pe_names)

(pdb++) ltp

[‘NIFTY 09 APR 21900 PUT’, ‘NIFTY 09 APR 21950 PUT’, ‘NIFTY 09 APR 22000 PUT’, ‘NIFTY 09 APR 22050 PUT’, ‘NIFTY 09 APR 22100 PUT’, ‘NIFTY 09 APR 22150 PUT’, ‘NIFTY 09 APR 22200 PUT’, ‘NIFTY 09 APR 22250 PUT’, ‘NIFTY 09 APR 22300 PUT’, ‘NIFTY 09 APR 22350 PUT’, ‘NIFTY 09 APR 22400 PUT’, ‘NIFTY 09 APR 22450 PUT’, ‘NIFTY 09 APR 22500 PUT’, ‘NIFTY 09 APR 22550 PUT’, ‘NIFTY 09 APR 22600 PUT’, ‘NIFTY 09 APR 22650 PUT’, ‘NIFTY 09 APR 22700 PUT’, ‘NIFTY 09 APR 22750 PUT’, ‘NIFTY 09 APR 22800 PUT’, ‘NIFTY 09 APR 22850 PUT’, ‘NIFTY 09 APR 22900 PUT’, ‘NIFTY 09 APR 22950 PUT’, ‘NIFTY 09 APR 23000 PUT’, ‘NIFTY 09 APR 23050 PUT’, ‘NIFTY 09 APR 23100 PUT’, ‘NIFTY 09 APR 23150 PUT’, ‘NIFTY 09 APR 23200 PUT’, ‘NIFTY 09 APR 23250 PUT’, ‘NIFTY 09 APR 23300 PUT’, ‘NIFTY 09 APR 23350 PUT’, ‘NIFTY 09 APR 23400 PUT’, ‘NIFTY 09 APR 23450 PUT’, ‘NIFTY 09 APR 23500 PUT’, ‘NIFTY 09 APR 23550 PUT’, ‘NIFTY 09 APR 23600 PUT’, ‘NIFTY 09 APR 23650 PUT’, ‘NIFTY 09 APR 23700 PUT’, ‘NIFTY 09 APR 23750 PUT’, ‘NIFTY 09 APR 23800 PUT’, ‘NIFTY 09 APR 23850 PUT’]

NOW LET’S GET THE LTP OF ALL PE NAMES

ltp = tsl.get_ltp_data(names=all_pe_names)

(pdb++) ltp = tsl.get_ltp_data(names=all_pe_names)

(pdb++) ltp

{‘NIFTY 09 APR 21900 PUT’: 3.4, ‘NIFTY 09 APR 21950 PUT’: 3.7, ‘NIFTY 09 APR 22000 PUT’: 4.05, ‘NIFTY 09 APR 22050 PUT’: 4.55, ‘NIFTY 09 APR 22100 PUT’: 5.1, ‘NIFTY 09 APR 22150 PUT’: 5.95, ‘NIFTY 09 APR 22200 PUT’: 6.7, ‘NIFTY 09 APR 22250 PUT’: 7.55, ‘NIFTY 09 APR 22300 PUT’: 9.4, ‘NIFTY 09 APR 22350 PUT’: 11, ‘NIFTY 09 APR 22400 PUT’: 13.2, ‘NIFTY 09 APR 22450 PUT’: 16.7, ‘NIFTY 09 APR 22500 PUT’: 20.65, ‘NIFTY 09 APR 22550 PUT’: 25.9, ‘NIFTY 09 APR 22600 PUT’: 32.6, ‘NIFTY 09 APR 22650 PUT’: 40.6, ‘NIFTY 09 APR 22700 PUT’: 51.1, ‘NIFTY 09 APR 22750 PUT’: 64.75, ‘NIFTY 09 APR 22800 PUT’: 80, ‘NIFTY 09 APR 22850 PUT’: 99.2, ‘NIFTY 09 APR 22900 PUT’: 121.85, ‘NIFTY 09 APR 22950 PUT’: 147.15, ‘NIFTY 09 APR 23000 PUT’: 177.15, ‘NIFTY 09 APR 23050 PUT’: 211.25, ‘NIFTY 09 APR 23100 PUT’: 245.25, ‘NIFTY 09 APR 23150 PUT’: 285.85, ‘NIFTY 09 APR 23200 PUT’: 329, ‘NIFTY 09 APR 23250 PUT’: 372.65, ‘NIFTY 09 APR 23300 PUT’: 416.95, ‘NIFTY 09 APR 23350 PUT’: 467.35, ‘NIFTY 09 APR 23400 PUT’: 513, ‘NIFTY 09 APR 23450 PUT’: 560.75, ‘NIFTY 09 APR 23500 PUT’: 610.2, ‘NIFTY 09 APR 23550 PUT’: 656.2, ‘NIFTY 09 APR 23600 PUT’: 704.7, ‘NIFTY 09 APR 23650 PUT’: 757, ‘NIFTY 09 APR 23700 PUT’: 806.5, ‘NIFTY 09 APR 23750 PUT’: 858.15, ‘NIFTY 09 APR 23800 PUT’: 902.5, ‘NIFTY 09 APR 23850 PUT’: 962.1}

NOW LET’S COMBINE(MERGE) BOTH CE AND PE NAMES AS WELL AS EQUITY NAMES

all_ce_names = [“NIFTY 09 APR " + str(strike) + " CALL” for strike in all_strike]

all_pe_names = [“NIFTY 09 APR " + str(strike) + " PUT” for strike in all_strike]

equity_names = [“ACC”, “CIPLA”, “GAIL”, “NIFTY”]

all_names = all_ce_names + all_pe_names + equity_names

(pdb++) all_names = all_ce_names + all_pe_names + equity_names

(pdb++) all_names

[‘NIFTY 09 APR 21900 CALL’, ‘NIFTY 09 APR 21950 CALL’, ‘NIFTY 09 APR 22000 CALL’, ‘NIFTY 09 APR 22050 CALL’, ‘NIFTY 09 APR 22100 CALL’, ‘NIFTY 09 APR 22150 CALL’, ‘NIFTY 09 APR 22200 CALL’, ‘NIFTY 09 APR 22250 CALL’, ‘NIFTY 09 APR 22300 CALL’, ‘NIFTY 09 APR 22350 CALL’, ‘NIFTY 09 APR 22400 CALL’, ‘NIFTY 09 APR 22450 CALL’, ‘NIFTY 09 APR 22500 CALL’, ‘NIFTY 09 APR 22550 CALL’, ‘NIFTY 09 APR 22600 CALL’, ‘NIFTY 09 APR 22650 CALL’, ‘NIFTY 09 APR 22700 CALL’, ‘NIFTY 09 APR 22750 CALL’, ‘NIFTY 09 APR 22800 CALL’, ‘NIFTY 09 APR 22850 CALL’, ‘NIFTY 09 APR 22900 CALL’, ‘NIFTY 09 APR 22950 CALL’, ‘NIFTY 09 APR 23000 CALL’, ‘NIFTY 09 APR 23050 CALL’, ‘NIFTY 09 APR 23100 CALL’, ‘NIFTY 09 APR 23150 CALL’, ‘NIFTY 09 APR 23200 CALL’, ‘NIFTY 09 APR 23250 CALL’, ‘NIFTY 09 APR 23300 CALL’, ‘NIFTY 09 APR 23350 CALL’, ‘NIFTY 09 APR 23400 CALL’, ‘NIFTY 09 APR 23450 CALL’, ‘NIFTY 09 APR 23500 CALL’, ‘NIFTY 09 APR 23550 CALL’, ‘NIFTY 09 APR 23600 CALL’, ‘NIFTY 09 APR 23650 CALL’, ‘NIFTY 09 APR 23700 CALL’, ‘NIFTY 09 APR 23750 CALL’, ‘NIFTY 09 APR 23800 CALL’, ‘NIFTY 09 APR 23850 CALL’, ‘NIFTY 09 APR 21900 PUT’, ‘NIFTY 09 APR 21950 PUT’, ‘NIFTY 09 APR 22000 PUT’, ‘NIFTY 09 APR 22050 PUT’, ‘NIFTY 09 APR 22100 PUT’, ‘NIFTY 09 APR 22150 PUT’, ‘NIFTY 09 APR 22200 PUT’, ‘NIFTY 09 APR 22250 PUT’, ‘NIFTY 09 APR 22300 PUT’, ‘NIFTY 09 APR 22350 PUT’, ‘NIFTY 09 APR 22400 PUT’, ‘NIFTY 09 APR 22450 PUT’, ‘NIFTY 09 APR 22500 PUT’, ‘NIFTY 09 APR 22550 PUT’, ‘NIFTY 09 APR 22600 PUT’, ‘NIFTY 09 APR 22650 PUT’, ‘NIFTY 09 APR 22700 PUT’, ‘NIFTY 09 APR 22750 PUT’, ‘NIFTY 09 APR 22800 PUT’, ‘NIFTY 09 APR 22850 PUT’, ‘NIFTY 09 APR 22900 PUT’, ‘NIFTY 09 APR 22950 PUT’, ‘NIFTY 09 APR 23000 PUT’, ‘NIFTY 09 APR 23050 PUT’, ‘NIFTY 09 APR 23100 PUT’, ‘NIFTY 09 APR 23150 PUT’, ‘NIFTY 09 APR 23200 PUT’, ‘NIFTY 09 APR 23250 PUT’, ‘NIFTY 09 APR 23300 PUT’, ‘NIFTY 09 APR 23350 PUT’, ‘NIFTY 09 APR 23400 PUT’, ‘NIFTY 09 APR 23450 PUT’, ‘NIFTY 09 APR 23500 PUT’, ‘NIFTY 09 APR 23550 PUT’, ‘NIFTY 09 APR 23600 PUT’, ‘NIFTY 09 APR 23650 PUT’, ‘NIFTY 09 APR 23700 PUT’, ‘NIFTY 09 APR 23750 PUT’, ‘NIFTY 09 APR 23800 PUT’, ‘NIFTY 09 APR 23850 PUT’, ‘ACC’, ‘CIPLA’, ‘GAIL’]

NOW LET’S GET THE LTP OF ALL NAMES

all_ltp = tsl.get_ltp_data(names=all_names)

(pdb++) all_ltp = tsl.get_ltp_data(names=all_names)

(pdb++) all_ltp

{‘ACC’: 1966.3, ‘GAIL’: 176.61, ‘CIPLA’: 1415.25, ‘NIFTY 09 APR 21900 CALL’: 986.1, ‘NIFTY 09 APR 21900 PUT’: 3.4, ‘NIFTY 09 APR 21950 CALL’: 928.5, ‘NIFTY 09 APR 21950 PUT’: 3.7, ‘NIFTY 09 APR 22000 CALL’: 898, ‘NIFTY 09 APR 22000 PUT’: 4.05, ‘NIFTY 09 APR 22050 CALL’: 838.05, ‘NIFTY 09 APR 22050 PUT’: 4.55, ‘NIFTY 09 APR 22100 CALL’: 795, ‘NIFTY 09 APR 22100 PUT’: 5.1, ‘NIFTY 09 APR 22150 CALL’: 717.6, ‘NIFTY 09 APR 22150 PUT’: 5.95, ‘NIFTY 09 APR 22200 CALL’: 705.7, ‘NIFTY 09 APR 22200 PUT’: 6.7, ‘NIFTY 09 APR 22250 CALL’: 651.45, ‘NIFTY 09 APR 22250 PUT’: 7.55, ‘NIFTY 09 APR 22300 CALL’: 607.1, ‘NIFTY 09 APR 22300 PUT’: 9.4, ‘NIFTY 09 APR 22350 CALL’: 555, ‘NIFTY 09 APR 22350 PUT’: 11, ‘NIFTY 09 APR 22400 CALL’: 508, ‘NIFTY 09 APR 22400 PUT’: 13.2, ‘NIFTY 09 APR 22450 CALL’: 463.5, ‘NIFTY 09 APR 22450 PUT’: 16.7, ‘NIFTY 09 APR 22500 CALL’: 407.45, ‘NIFTY 09 APR 22500 PUT’: 20.65, ‘NIFTY 09 APR 22550 CALL’: 371.55, ‘NIFTY 09 APR 22550 PUT’: 25.9, ‘NIFTY 09 APR 22600 CALL’: 328.45, ‘NIFTY 09 APR 22600 PUT’: 32.6, ‘NIFTY 09 APR 22650 CALL’: 286.25, ‘NIFTY 09 APR 22650 PUT’: 40.6, ‘NIFTY 09 APR 22700 CALL’: 247, ‘NIFTY 09 APR 22700 PUT’: 51.1, ‘NIFTY 09 APR 22750 CALL’: 211.4, ‘NIFTY 09 APR 22750 PUT’: 64.75, ‘NIFTY 09 APR 22800 CALL’: 175, ‘NIFTY 09 APR 22800 PUT’: 80, ‘NIFTY 09 APR 22850 CALL’: 144, ‘NIFTY 09 APR 22850 PUT’: 99.2, ‘NIFTY 09 APR 22900 CALL’: 115.7, ‘NIFTY 09 APR 22900 PUT’: 121.85, ‘NIFTY 09 APR 22950 CALL’: 92.45, ‘NIFTY 09 APR 22950 PUT’: 147.15, ‘NIFTY 09 APR 23000 CALL’: 71.55, ‘NIFTY 09 APR 23000 PUT’: 177.15, ‘NIFTY 09 APR 23050 CALL’: 55.85, ‘NIFTY 09 APR 23050 PUT’: 211.25, ‘NIFTY 09 APR 23100 CALL’: 43.15, ‘NIFTY 09 APR 23100 PUT’: 245.25, ‘NIFTY 09 APR 23150 CALL’: 32.95, ‘NIFTY 09 APR 23150 PUT’: 285.85, ‘NIFTY 09 APR 23200 CALL’: 25.05, ‘NIFTY 09 APR 23200 PUT’: 329, ‘NIFTY 09 APR 23250 CALL’: 19.1, ‘NIFTY 09 APR 23250 PUT’: 372.65, ‘NIFTY 09 APR 23300 CALL’: 15.15, ‘NIFTY 09 APR 23300 PUT’: 416.95, ‘NIFTY 09 APR 23350 CALL’: 11.7, ‘NIFTY 09 APR 23350 PUT’: 467.35, ‘NIFTY 09 APR 23400 CALL’: 9.15, ‘NIFTY 09 APR 23400 PUT’: 513, ‘NIFTY 09 APR 23450 CALL’: 7.55, ‘NIFTY 09 APR 23450 PUT’: 560.75, ‘NIFTY 09 APR 23500 CALL’: 6.3, ‘NIFTY 09 APR 23500 PUT’: 610.2, ‘NIFTY 09 APR 23550 CALL’: 5.05, ‘NIFTY 09 APR 23550 PUT’: 656.2, ‘NIFTY 09 APR 23600 CALL’: 4.15, ‘NIFTY 09 APR 23600 PUT’: 704.7, ‘NIFTY 09 APR 23650 CALL’: 3.6, ‘NIFTY 09 APR 23650 PUT’: 757, ‘NIFTY 09 APR 23700 CALL’: 3, ‘NIFTY 09 APR 23700 PUT’: 806.5, ‘NIFTY 09 APR 23750 CALL’: 2.75, ‘NIFTY 09 APR 23750 PUT’: 858.15, ‘NIFTY 09 APR 23800 CALL’: 2.4, ‘NIFTY 09 APR 23800 PUT’: 902.5, ‘NIFTY 09 APR 23850 CALL’: 2.1, ‘NIFTY 09 APR 23850 PUT’: 962.1}

ltp_temp_dict.T[all_names].iloc[0].to_list()

ltp = tsl.get_ltp_data(names=[‘NIFTY 09 APR 22900 CALL’, ‘NIFTY 09 APR 22900 PUT’])

SESSION-10 - PART-2 - ‘RP-How to deploy Algo on the Server’

—Session 10 — Part 2 ------

Deployment of Algo on server

  1. Overheat of Laptop
  2. Fluctuations in Internet Broad Band
  3. You need peace after deploying Algo without any hurdles.

What’s a Server?
(Kampstaal)

There will be 7 passes to reach Server
Bnaks, Colleges, Hospitals and several companies use Server

Dhan also uses remote Server, located in Data Centres.

You also call it as Cloud.

Several Companies like AWS by Amazon, Google, Microsoft, Azure are all exmaples of Cloud Computing Platforms.

Now let’s start using AWS beloging to Amazon here.

Sign into the console.amazon

Search for lightstall

Browser window will open for lightstall

Click on ‘create instance’ button, means a computer / server.amazon

Select your platform as Windows

Click on ‘Windows Server 2019’

Now select the size of Server you require.

Positively Select 2GB Memory , 60GB SSD Storage (90 days free), which costs $22/- per month.

Then Click on ‘Create instance’ button at the bottom

For the first time it takes few minutes to get your ‘Server’ ready.

Wait until it indicates the stage of ‘Running’.

Now click on the ‘Server’ window.

At the bottom of the window,
You will be provided with ID & Address, in the left side box.
You will be provide with Userbane & Password in the rigt side box.

Also don't forget to retrieve the default password and save it for further usage.

Next Step is to Connect Remote Desktop

Which gives you a box in which you have to fill all the credentials,
Save the connection settings.

Once again get the Remote Desktop, and click on ‘Connect’.

It will ask for the password, which you have to fill in and get connected.

And click on ‘Remember me’ , to get automatically get connected.

Now your server is ready to use.

Downlod Google Chrome on your laptop and paste in the server

ChromeSetup box will appear on the Server desk

Right click and ‘Rin it as Administrator’

Now copy and install all files to run the algo

Install Python and required libraries, including sublime text

Copy and paste the required Session file to run your algo

You can procede to run your algo by using ‘cmd’ propt

After starting the algo, you can close your server, but remember your algo keeps running

NOTE: REMEMBER THE ALGO IN THE CLOSED SERVER WILL KEEP RUNNING, EVEN AFTER YOU CLOSE THE SERVER

if you want to open the server again at your convenient time, you can open it with your credentials

NOW LET US TAK ABOUT BILLING

Remember that as long as your Server is in the state of ‘Running’ you will be billed

AWS will charge around Rs.3,500/- per month for a 4GB Memory Server

Say , if you want to save on the Monthly Flat charges of Rs.3,500, You can do it

Say if you can start and stop the Sever as per the trading hours:

Hourly Cost = Monthly Cost / Total Hours in a month = Rs.3500 / 720 Hours = Rs.4.86/- per Hours

Now if you calculate for actual usage hours:

Final Cost = Hourly Cost x Hours used = Rs.4.86 x 132 Hours = Rs.642/- per month  # 22 Days x 6 hours per day = 132 Hours

Remember instead of paying RS.3,500/-, you can only pay Rs.642/- per month, provided you start and stop the server every day after market hours

This matters a lot to every small trader investing small capital initially

Remember to take a snapshot after stopping the server, which will be utilized to start on next days

EXAMPLE:

    This is a one time process
    
        Monday - Install --- Complete algo.. (Store ID and Password)

            Monday : 3:30 pm :  |  stop the server and take snapshot

            Choose 'Snapshots' from the available Menu and click on 'Create snapshot' and Name it

            Monday : 4:00 pm :  | Delete your server by Clicking on the top right hand corner, you can choose the delete option

    Further Daily process: 

        Create instance from your snapshot
        Now the server will start by filling our credentials
        Use the server during the market hours and delete the srver 

    REMEMBER THAT THERE IS NO NEED TO TAKE SNAPSHOT EVERYDAY, THE SAME SNPSHOT WILL BE USEFUL EVERY DAY

    THIS PROCEDURE SHOULD CONTINUE ON ALL TRADING DAYS, AS PER YOUR WISH

DO REMEMBER THAT YOU CAN CHOOSE A BETTER SUBSCRIPTION OF 8GB WILL BE BETTER, AS YOU CAN SAVE ON THE ABOVE METHOS


NOW LET US KNOW ABOUT HYPERCARE

  1. develop the Algo
  2. pdb level testing
  3. Test the algo atleast for 14 consecutive days
  4. If any error occurs during the testing period, resolve it start Re-Testing process

1 Like

D:\stocks\session_3 codebase\Dhan codebase>py Dhan_websocket.py
Reading existing file all_instrument 2025-04-14.csv
Watchlist changed. Reconnecting the feed…
Disconnected from WebSocket feed.
Authorizing…
Authorization successful!

what is missing here?
please help…

Dhan_websocket no more required
Watch latest Algo series videos

Hi,

I tried executing multiple times the libraries it was installed successfully. also installed separately to see if it works, but i still don’t see in my list of library even after successful installation. is there any thing i am missing

Regards,
narayan

Hi @SEBI.RA.Samir.Sir ,

Do update to our latest codebase version 3.0.6:

  • Open Command Prompt: Press Win, type cmd, and press Enter.
  • Install Dhan-Tradehull: Run pip install Dhan-Tradehull
  • Confirm the installation by running pip show Dhan-Tradehull
  1. Sometimes the API would not work when the Market is closed.

  2. Guide to use the updated codebase:
    Refer the below pypi link for more details:
    Client Challenge

Video reference :

Hi @Rajashekhar_Rangappa ,

On high level ideally you can call option chain thrice in a min.

Hi @Msk92 ,

Do update to our latest codebase version 3.0.6:

  • Open Command Prompt: Press Win, type cmd, and press Enter.
  • Install Dhan-Tradehull: Run pip install Dhan-Tradehull
  • Confirm the installation by running pip show Dhan-Tradehull

Guide to use the updated codebase:
Refer the below pypi link for more details:
https://pypi.org/project/Dhan-Tradehull/

Video reference :

Hi @Vasili_Prasad ,

Make sure you have loaded the csv file and the below condition is true.

ATM_time_data = index_data[index_data['timestamp']==start_time]
if not ATM_time_data.empty:
 ATM close = ATM_time_data.iloc[-1]['close]

Hi @Vindhyawasini ,

tsl.get_intraday_data was a old method to get historical data… it gives data only for today, which makes problem if we want to create indicators on it.

Dhanhq was upgraded and now we can use get_historical_data which gives data for last 5 working days.

Retrieve historical or intraday data:

Get Historical Data

  • tsl.get_historical_data(tradingsymbol: str, exchange: str, timeframe: str, debug: str = “NO”)
    • Arguments:
      • tradingsymbol (str): The trading symbol for the instrument you want to fetch data for (e.g., ‘NIFTY’, ‘ACC’).
      • exchange (str): The exchange where the instrument is traded (e.g., ‘NSE’, ‘INDEX’).
      • timeframe (str): The timeframe for the data. It can be:
        • ‘1’ for 1-minute candles
        • ‘5’ for 5-minute candles
        • ‘15’ for 15-minute candles
        • ‘25’ for 25-minute candles
        • ‘60’ for 60-minute candles
        • ‘DAY’ for daily candles
      • debug (optional, str): Set to “YES” to enable detailed API response logging. Default is “NO”.
    • Sample Code:
data = tsl.get_historical_data(tradingsymbol='NIFTY', exchange='INDEX', timeframe="DAY") 
data = tsl.get_historical_data(tradingsymbol='ACC', exchange='NSE', timeframe="1")

Also for expiry parameter you have to select 0 for current expiry, 1 for next expiry and so on.

ce_name, pe_name, strike=  tsl.ATM_Strike_Selection(‘NIFTY’,‘1)

Do update to our latest codebase version 3.0.6:

  • Open Command Prompt: Press Win, type cmd, and press Enter.
  • Install Dhan-Tradehull: Run pip install Dhan-Tradehull
  • Confirm the installation by running pip show Dhan-Tradehull

Guide to use the updated codebase:
Refer the below pypi link for more details:
https://pypi.org/project/Dhan-Tradehull/

Video reference :

Hi @Vasili_Prasad ,

Thanks for sharing the notes to students. Onething to note is that, do use the ‘Preformatted Text’ option to send code in a proper format.

Hi @Shashiprakash_Maurya ,

Websocket is no longer required. We have switched our calls to Rest API calls. You can follow the below steps:
Do update to our latest codebase version 3.0.6:

  • Open Command Prompt: Press Win, type cmd, and press Enter.
  • Install Dhan-Tradehull: Run pip install Dhan-Tradehull
  • Confirm the installation by running pip show Dhan-Tradehull

Guide to use the updated codebase:
Refer the below pypi link for more details:
https://pypi.org/project/Dhan-Tradehull/
Video reference :

Hi @narayan_uppu ,

Cross check the version in which you are installing the packages. In cmd enter the below

where python

If there is more then 1 version of python installed in your system, then uninstall other versions and try to install .

Hi Imran,

I could see only one version present in the system.

Regards,
Narayan

Thank you very much for your correction. I will check and report to you sir.

VBR Prasad

Sure sir, I will try to follow. In my old laptop I am not getting that </> symbol to follow your suggestion sir.

Is there any way to post the ‘Sublime Text’ file in this group sir. If so please suggest.
VBR Prasad

Sir, Still I am getting error as I have enclosed the ‘cmd’ file sir.C:\Users\LENOVO PC\Desktop\RP\Dhan Algo\DHAN Advanced Algo Trading Series\Session 9\Session 9\Dhan Option Chain\Dhan Option Chain>py "1. Download_Historical_data.py" Codebase Version 3 -----Logged into Dhan----- reading existing file all_instrument 2025-04-16.csv Got the instrument file Exception in Getting OHLC data as {'status': 'failure', 'remarks': {'error_code': 'DH-905', 'error_type': 'Input_Exception', 'error_message': 'System is unable to fetch data due to incorrect parameters or no data present'}, 'data': {'errorType': 'Input_Exception', 'errorCode': 'DH-905', 'errorMessage': 'System is unable to fetch data due to incorrect parameters or no data present'}} Traceback (most recent call last): File "1. Download_Historical_data.py", line 49, in <module> option_df.to_csv(f'data/{index_name}/{today_date}/{strike}.csv',index=False) AttributeError: 'NoneType' object has no attribute 'to_csv'

And my Code is :

from Dhan_Tradehull import Tradehull
import datetime
import pdb
import os



client_code = "1101092420"
token_id = "eeeeeeee"
tsl = Tradehull(client_code, token_id)

index_name = 'NIFTY'

no_strikes_to_replay = 20

expiry_date = "17 APR"

start_time ='2025-04-16 09:15:00+05:30'



today_date = datetime.datetime.now().date()

index_data = tsl.get_historical_data(tradingsymbol=index_name, exchange='INDEX', timeframe="1")

ATM_time_data = index_data[index_data['timestamp']==start_time]

if not ATM_time_data.empty:
    ATM_close = ATM_time_data.iloc[-1]['close']

step = tsl.index_step_dict[index_name]

ATM_Strike = round(ATM_close/step)*step

all_strikes = [ATM_Strike+(step*i) for i in range(1,no_strikes_to_replay+1)] + [ATM_Strike-(step*i) for i in range(1,no_strikes_to_replay+1)] + [ATM_Strike]

call_and_put_Strikes = [f"{index_name} {expiry_date} {strike} CALL" for strike in all_strikes] + [f"{index_name} {expiry_date} {strike} PUT" for strike in all_strikes]


file_path = f'data/{index_name}/{today_date}/'

directory = os.path.dirname(file_path)

if not os.path.exists(directory):
    os.makedirs(directory)

for strike in call_and_put_Strikes:
    option_df = tsl.get_historical_data(tradingsymbol=strike, exchange='NFO', timeframe="1")
    option_df.to_csv(f'data/{index_name}/{today_date}/{strike}.csv',index=False)
1 Like

the codes written by Imram,
Will first complete the calculation of indicators (4 or more) and all for watchlist stock for 1st name in the list and then move to next one ?

What if i have 50 stocks in the watch list, and when it is calculating indicators of say 37th stock, the buy condition for 1st stock was met.
will that trade have to wait for the loop to finish ?

isnt this an inefficient way ?

Hi,

I think he has shown a way how it can be done. Now, it depends on the number of stocks in your basket/watchlist.

Definitely, as you said, one will miss the opportunity to buy. The loop will check calculate for all the indicators and based on ones buy/sell conditions execute the orders, but if the number of stocks are more, it will take time, so best idea would to reduce the number of stocks in the basket/watchlist.

Though this scenario have ways to be solved. But, every one has its own pros and cons.
Note:- I haven’t tried any. But these solutions are logical and are do-able.

  1. Using multi- threading / multi-processing
    cons-but this approach can face rate-limit problem and heavy system resources
  2. batch/segmented scanning/processing (say a batch of 10 stks is processed)
    cons - complex architecture

HTH
Reagrds

1 Like

It doesn’t make sense to call option_call three times within a single minute, especially since it’s being used for both entry and exit logic.

If I’m trading across three indices, that means I can only trigger it once per index and then have to wait — which introduces unnecessary delays. In fast markets, that delay can cost me the trade. It’s a loss waiting to happen… :man_shrugging:
Any thoughts welcome
@Dhan @RahulDeshpande
FYI…
Getting Error at Option Chain as {'status': 'failure', 'remarks': {'error_code': None, 'error_type': None, 'error_message': None}, 'data': {'data': {'805': 'Too many requests. Further requests may result in the user being blocked.'}, 'status': 'failed'}}

1 Like