Learn Algo Trading with Python | Codes | Youtube Series

Hi Sir, @Tradehull_Imran

Now the Algo is working with the following Erros, but the order has been placed.
Microsoft Windows [Version 10.0.22631.4391]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Rejith Koroth\Desktop\REJITH KOROTH\DHAN_APP\DHAN ALGO\8.Session8\8. Session8- 2nd Live Algo_2\2nd live Algo>py “Multi timeframe Algo.py”
-----Logged into Dhan-----
reading existing file all_instrument 2024-11-12.csv
Got the instrument file
Traceback (most recent call last):
File “C:\Users\Rejith Koroth\Desktop\REJITH KOROTH\DHAN_APP\DHAN ALGO\8.Session8\8. Session8- 2nd Live Algo_2\2nd live Algo\Dhan_Tradehull_V2.py”, line 178, in get_live_pnl
closePrice = ltp_data[underlying]
KeyError: ‘NIFTY 14 NOV 24050 CALL’
MOTHERSON
OFSS
OFSS is in downtrend, Sell this script
MANAPPURAM
MANAPPURAM is in downtrend, Sell this script
BSOFT
CHAMBLFERT
DIXON
NATIONALUM
DLF
IDEA
ADANIPORTS
ADANIPORTS is in downtrend, Sell this script
SAIL
HINDCOPPER
HINDCOPPER is in downtrend, Sell this script
INDIGO
RECLTD
PNB
HINDALCO
RBLBANK
GNFC
ALKEM
CONCOR
PFC
GODREJPROP
MARUTI
ADANIENT
ONGC
CANBK
OBEROIRLTY
BANDHANBNK
SBIN
HINDPETRO
CANFINHOME
TATAMOTORS
LALPATHLAB
MCX
TATACHEM
BHARTIARTL
INDIAMART
LUPIN
INDUSTOWER
VEDL
SHRIRAMFIN
POLYCAB
WIPRO
UBL
SRF
BHARATFORG
GRASIM
IEX
BATAINDIA
AARTIIND
TATASTEEL
UPL
HDFCBANK
LTF
TVSMOTOR
GMRINFRA
IOC
ABCAPITAL
ACC
IDFCFIRSTB
ABFRL
ZYDUSLIFE
GLENMARK
TATAPOWER
PEL
IDFC
single positional indexer is out-of-bounds
Traceback (most recent call last):
File “C:\Users\Rejith Koroth\Desktop\REJITH KOROTH\DHAN_APP\DHAN ALGO\8.Session8\8. Session8- 2nd Live Algo_2\2nd live Algo\Dhan_Tradehull_V2.py”, line 244, in get_historical_data
security_id = self.instrument_df[((self.instrument_df[‘SEM_TRADING_SYMBOL’]==tradingsymbol)|(self.instrument_df[‘SEM_CUSTOM_SYMBOL’]==tradingsymbol))&(self.instrument_df[‘SEM_EXM_EXCH_ID’]==instrument_exchange[exchange])].iloc[-1][‘SEM_SMST_SECURITY_ID’]
File “C:\Users\Rejith Koroth\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py”, line 1103, in getitem
return self._getitem_axis(maybe_callable, axis=axis)
File “C:\Users\Rejith Koroth\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py”, line 1656, in _getitem_axis
self._validate_integer(key, axis)
File “C:\Users\Rejith Koroth\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py”, line 1589, in _validate_integer
raise IndexError(“single positional indexer is out-of-bounds”)
IndexError: single positional indexer is out-of-bounds
Traceback (most recent call last):
File “Multi timeframe Algo.py”, line 57, in
chart_1[‘rsi’] = talib.RSI(chart_1[‘close’], timeperiod=14) #pandas
TypeError: ‘NoneType’ object is not subscriptable

C:\Users\Rejith Koroth\Desktop\REJITH KOROTH\DHAN_APP\DHAN ALGO\8.Session8\8. Session8- 2nd Live Algo_2\2nd live Algo>

C:\Users\Rejith Koroth\Desktop\REJITH KOROTH\DHAN_APP\DHAN ALGO\8.Session8\8. Session8- 2nd Live Algo_2\2nd live Algo>


<`# TA-Lib

Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

import pdb
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
import talib
import time
import datetime

client_code = “1104068813”
token_id = “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzMzODkxMTg1LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwNDA2ODgxMyJ9.ng5EkX1O_ZLx1CMvMyvUCpA6PFos9jKb90Nd8ZK99_YBhOCk2gnJn18CcLh8FoIswozr5K9oE2uSOoFMRr”

tsl = Tradehull(client_code,token_id)

available_balance = tsl.get_balance()
leveraged_margin = available_balance5
max_trades = 3
per_trade_margin = (leveraged_margin/max_trades)
max_loss = (available_balance
1)/100*-1

watchlist = [‘MOTHERSON’, ‘OFSS’, ‘MANAPPURAM’, ‘BSOFT’, ‘CHAMBLFERT’, ‘DIXON’, ‘NATIONALUM’, ‘DLF’, ‘IDEA’, ‘ADANIPORTS’, ‘SAIL’, ‘HINDCOPPER’, ‘INDIGO’, ‘RECLTD’, ‘PNB’, ‘HINDALCO’, ‘RBLBANK’, ‘GNFC’, ‘ALKEM’, ‘CONCOR’, ‘PFC’, ‘GODREJPROP’, ‘MARUTI’, ‘ADANIENT’, ‘ONGC’, ‘CANBK’, ‘OBEROIRLTY’, ‘BANDHANBNK’, ‘SBIN’, ‘HINDPETRO’, ‘CANFINHOME’, ‘TATAMOTORS’, ‘LALPATHLAB’, ‘MCX’, ‘TATACHEM’, ‘BHARTIARTL’, ‘INDIAMART’, ‘LUPIN’, ‘INDUSTOWER’, ‘VEDL’, ‘SHRIRAMFIN’, ‘POLYCAB’, ‘WIPRO’, ‘UBL’, ‘SRF’, ‘BHARATFORG’, ‘GRASIM’, ‘IEX’, ‘BATAINDIA’, ‘AARTIIND’, ‘TATASTEEL’, ‘UPL’, ‘HDFCBANK’, ‘LTF’, ‘TVSMOTOR’, ‘GMRINFRA’, ‘IOC’, ‘ABCAPITAL’, ‘ACC’, ‘IDFCFIRSTB’, ‘ABFRL’, ‘ZYDUSLIFE’, ‘GLENMARK’, ‘TATAPOWER’, ‘PEL’, ‘IDFC’, ‘LAURUSLABS’, ‘BANKBARODA’, ‘KOTAKBANK’, ‘CUB’, ‘GAIL’, ‘DABUR’, ‘TECHM’, ‘CHOLAFIN’, ‘BEL’, ‘SYNGENE’, ‘FEDERALBNK’, ‘NAVINFLUOR’, ‘AXISBANK’, ‘LT’, ‘ICICIGI’, ‘EXIDEIND’, ‘TATACOMM’, ‘RELIANCE’, ‘ICICIPRULI’, ‘IPCALAB’, ‘AUBANK’, ‘INDIACEM’, ‘GRANULES’, ‘HDFCAMC’, ‘COFORGE’, ‘LICHSGFIN’, ‘BAJAJFINSV’, ‘INFY’, ‘BRITANNIA’, ‘M&MFIN’, ‘BAJFINANCE’, ‘PIIND’, ‘DEEPAKNTR’, ‘SHREECEM’, ‘INDUSINDBK’, ‘DRREDDY’, ‘TCS’, ‘BPCL’, ‘PETRONET’, ‘NAUKRI’, ‘JSWSTEEL’, ‘MUTHOOTFIN’, ‘CUMMINSIND’, ‘CROMPTON’, ‘M&M’, ‘GODREJCP’, ‘IGL’, ‘BAJAJ-AUTO’, ‘HEROMOTOCO’, ‘AMBUJACEM’, ‘BIOCON’, ‘ULTRACEMCO’, ‘VOLTAS’, ‘BALRAMCHIN’, ‘SUNPHARMA’, ‘ASIANPAINT’, ‘COALINDIA’, ‘SUNTV’, ‘EICHERMOT’, ‘ESCORTS’, ‘HAL’, ‘ASTRAL’, ‘NMDC’, ‘ICICIBANK’, ‘TORNTPHARM’, ‘JUBLFOOD’, ‘METROPOLIS’, ‘RAMCOCEM’, ‘INDHOTEL’, ‘HINDUNILVR’, ‘TRENT’, ‘TITAN’, ‘JKCEMENT’, ‘ASHOKLEY’, ‘SBICARD’, ‘BERGEPAINT’, ‘JINDALSTEL’, ‘MFSL’, ‘BHEL’, ‘NESTLEIND’, ‘HDFCLIFE’, ‘COROMANDEL’, ‘DIVISLAB’, ‘ITC’, ‘TATACONSUM’, ‘APOLLOTYRE’, ‘AUROPHARMA’, ‘HCLTECH’, ‘LTTS’, ‘BALKRISIND’, ‘DALBHARAT’, ‘APOLLOHOSP’, ‘ABBOTINDIA’, ‘ATUL’, ‘UNITDSPR’, ‘PVRINOX’, ‘SIEMENS’, ‘SBILIFE’, ‘IRCTC’, ‘GUJGASLTD’, ‘BOSCHLTD’, ‘NTPC’, ‘POWERGRID’, ‘MARICO’, ‘HAVELLS’, ‘MPHASIS’, ‘COLPAL’, ‘CIPLA’, ‘MGL’, ‘ABB’, ‘PIDILITIND’, ‘MRF’, ‘LTIM’, ‘PAGEIND’, ‘PERSISTENT’]
traded_wathclist =

while True:

live_pnl = tsl.get_live_pnl()
current_time = datetime.datetime.now().time()

if current_time < datetime.time(9, 30):
	print("wait for market to start", current_time)
	continue


if (current_time > datetime.time(15, 15)) or (live_pnl < max_loss):
	I_want_to_trade_no_more = tsl.kill_switch('ON')
	order_details = tsl.cancel_all_orders()
	print("Market is over, Bye Bye see you tomorrow", current_time)
	break



for stock_name in watchlist:
	time.sleep(0.7)
	print(stock_name)



	# Conditions that are on 1 minute timeframe
	# chart_1        = tsl.get_intraday_data(stock_name, 'NSE', 1)       # 1 minute chart   # this call has been updated to get_historical_data call, 
	chart_1          = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'NSE',timeframe="1")


	chart_1['rsi'] = talib.RSI(chart_1['close'], timeperiod=14) #pandas
	cc_1           = chart_1.iloc[-2]  #pandas  completed candle of 1 min timeframe
	uptrend        = cc_1['rsi'] > 50
	downtrend      = cc_1['rsi'] < 49


	# Conditions that are on 5 minute timeframe
	# chart_5        = tsl.get_intraday_data(stock_name, 'NSE', 5)       # 5 minute chart
	chart_5          = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'NSE',timeframe="5") # this call has been updated to get_historical_data call, 
	chart_5['upperband'], chart_5['middleband'], chart_5['lowerband'] = talib.BBANDS(chart_5['close'], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
	cc_5           = chart_5.iloc[-1]   # pandas
	ub_breakout    = cc_5['high'] > cc_5['upperband']
	lb_breakout    = cc_5['low'] < cc_5['lowerband']

	no_repeat_order = stock_name not in traded_wathclist
	max_order_limit = len(traded_wathclist) <= max_trades


	if uptrend and ub_breakout and no_repeat_order and max_order_limit:
		print(stock_name, "is in uptrend, Buy this script")

		sl_price          = round((cc_1['close']*0.98),1)
		qty               = int(per_trade_margin/cc_1['close'])

		buy_entry_orderid = tsl.order_placement(stock_name,'NSE', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
		sl_orderid        = tsl.order_placement(stock_name,'NSE', 1, 0, sl_price, 'STOPMARKET', 'SELL', 'MIS')
		traded_wathclist.append(stock_name)

	if downtrend and lb_breakout and no_repeat_order and max_order_limit:
		print(stock_name, "is in downtrend, Sell this script")

		sl_price          = round((cc_1['close']*1.02),1)
		qty               = int(per_trade_margin/cc_1['close'])

		buy_entry_orderid = tsl.order_placement(stock_name,'NSE', 1, 0, 0, 'MARKET', 'SELL', 'MIS')
		sl_orderid        = tsl.order_placement(stock_name,'NSE', 1, 0, sl_price, 'STOPMARKET', 'BUY', 'MIS')
		traded_wathclist.append(stock_name)

`

1 Like