Learn Algo Trading with Python | Codes | Youtube Series

Is it possible to run the scripts on cloud?

1 Like

Is this chatgpt integrated with VScode? Is there free extention?

I am Very Greatful to Imran Sir!, with your kind teaching and guidance I am able to successfully run option buying algo since two days, when market conditions are really not good for buying, but still able to close few test trades in green with automatic entry/exit, I am still testing it but you this is first success in algo trading so would like to share with all.
Initallly I was not able to run the algo for 6 hours but found the solution for the same.
I am copying example code here so anyone face API rate limit issues, this will help them to manage api rate limit and run algo for whole day till 3:20 PM.

pip install ratelimit

from ratelimit import limits, sleep_and_retry

ONE_MINUTE = 15
LIMIT_DATA_CALLS = 1
LIMIT_LTP_CALL = 3

@sleep_and_retry
@limits(calls=LIMIT_DATA_CALLS, period=ONE_MINUTE)
def limit_fetch_intraday_data(symbol):
    # Replace with your API call
    return tsl.get_intraday_data(tradingsymbol=symbol, exchange='INDEX', timeframe=2)

@sleep_and_retry
@limits(calls=LIMIT_DATA_CALLS, period=ONE_MINUTE)
def limit_fetch_historical_data(symbol):
    # Replace with your API call
    return tsl.get_historical_data(tradingsymbol=symbol, exchange='NFO', timeframe='5')

index_chart = limit_fetch_intraday_data('NIFTY')

index_future_chart = limit_fetch_historical_data('NIFTY DEC FUT')

This makes 4*2 = 8 api call per minute

2 Likes

chart = tsl.get_intraday_data('BANKNIFTY', 'INDEX', 5)
chart['RSI'] = talib.RSI(chart['close'], timeperiod=14) #pandas

Is it possible to find the RSI for the whole chart not for individual
candles like above.

@Tradehull_Imran sir I am getting same error ---- from yesterday Please look into this

Error processing GMRINFRA: No expiry date found for GMRINFRA

Waiting for Video on %change in Volume and %change in Open Interest

1 Like

thats great, is there any delay of scanning stocks etc?

Hi @vinay_kumaar

use below code

chart            = tsl.get_historical_data(tradingsymbol = 'ACC',exchange = 'NSE',timeframe="5")
chart['rsi']     = talib.RSI(chart['close'], timeperiod=14)
chart['rsi_sma'] = talib.SMA(chart['rsi'], timeperiod=30)

Hi @parveen_chugh

Use this updated file: Dhan_Tradehull_V2.py - Google Drive

corrections regarding to index data has been made

Hi @avinashkumar07412

we need to keep track if the orders have been placed or not

see ref video : https://youtu.be/wlQWSLzp_UI?si=-M_4avDm7RGcEHSJ&t=2618

Hi @SUKANTA_MONDAL

yes this is possible,
we can make buy entry conditions and sell entry conditions different.
and this algo can place order simultaneously

Hi @Kishore007

Try this code

import pdb
import time
import datetime
import traceback
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
from pprint import pprint
import talib
import pandas_ta as pta
import pandas_ta as ta
import warnings
warnings.filterwarnings("ignore")
# -----------------------------------------------------------------------------------
client_code = "11"
token_id = "11"
tsl = Tradehull(client_code, token_id)
# ----------------------------------------------------------------------------------
available_balance = tsl.get_balance()
max_loss = available_balance * -0.03  # max loss of 3%
# -----------------------------------------------------------------------------------------
watchlist = ['INFY', 'M&M', 'HINDALCO', 'DLF', 'TATASTEEL', 'NTPC', 'MARUTI', 'TATAMOTORS', 'ONGC', 'BPCL', 'WIPRO', 'SHRIRAMFIN', 'ADANIPORTS', 'JSWSTEEL', 'COALINDIA', 'ULTRACEMCO', 'BAJAJ-AUTO', 'LT', 'POWERGRID', 'ADANIENT', 'SBIN', 'HCLTECH', 'TCS', 'EICHERMOT', 'BAJAJFINSV', 'TECHM', 'LTIM', 'HINDUNILVR', 'BHARTIARTL', 'AXISBANK', 'GRASIM', 'DRREDDY', 'ICICIBANK', 'HDFCBANK', 'BAJFINANCE', 'SBILIFE', 'RELIANCE', 'KOTAKBANK', 'ITC', 'TITAN', 'SUNPHARMA', 'INDUSINDBK', 'APOLLOHOSP', 'BRITANNIA', 'NESTLEIND', 'HDFCLIFE', 'DIVISLAB', 'CIPLA', 'ASIANPAINT', 'TATACONSUM']
traded_wathclist = []
#-----------------------------------------------------------------------------------------------------

# -----------------------------------------------------------------------------------------------------
while True:
	live_pnl = tsl.get_live_pnl()
	current_time = datetime.datetime.now().time()

	if current_time < datetime.time(00, 15):
		print("Wait for market to start", current_time)
		time.sleep(1)
		continue

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

	all_ltp = tsl.get_ltp_data(stock_name)[stock_name]

	for stock_name in watchlist:
		time.sleep(0.1)
		current_time = datetime.datetime.now().time()
		print(f"Scanning {stock_name}, Time: {current_time}")

		# Conditions on 1-minute timeframe-----------------------------------------------------------------
		chart_1 = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="1")
		ltp     = all_ltp[stock_name]
		pervious_candle = chart_1.iloc[-2]
		no_repeat_order = stock_name not in traded_wathclist

		# 1) RSI    ------------------------ apply indicators--------------------------------------
		chart_1['rsi'] = talib.RSI(chart_1['close'], timeperiod=14)
		cc_1 		= chart_1.iloc[-2]
		cc_2 		= chart_1.iloc[-3]
		ut_1 		= cc_1['rsi'] > 60
		dt_1 		= cc_1['rsi'] < 40

		# 2) Linear Regression Slope -------------------------- apply indicators-----------------------------------
		chart_1['LINEARREG_SLOPE']     = talib.LINEARREG_SLOPE(chart_1['close'], timeperiod=9)
		cc_1            = chart_1.iloc[-2]  
		cc_2            = chart_1.iloc[-3]
		ut_2			= cc_1['LINEARREG_SLOPE'] >= 0
		dt_2			= cc_1['LINEARREG_SLOPE'] <= 0

		# 3) ADX -------------------------- apply indicators-----------------------------------
		chart_1['adx']   = talib.ADX(chart_1['high'], chart_1['low'], chart_1['close'], timeperiod=14)
		cc_1             = chart_1.iloc[-2]  #pandas  completed candle of 1 min timeframe
		cc_2             = chart_1.iloc[-3]
		adx  		     = cc_1['adx'] >= 20

		

		# BUY conditions ------------------------------------------------------------------------------
		if ut_1 and ut_2 and adx and no_repeat_order:
			print(stock_name, "is in uptrend, Buy this script")
			#print(f"BUY {stock_name}\t{current_time}\t{ut_1}\t{ut_2}\t{adx}\t{ut_3}\t{ut_4}")
			qty = 1
			ltp = tsl.get_ltp_data(stock_name)[stock_name]
			entry_price = ltp
			entry_orderid = tsl.order_placement(stock_name, 'NSE', qty, 0, 0, 'MARKET', 'BUY', 'MIS')
			traded_wathclist.append({'symbol': stock_name, 'entry_price': entry_price, 'direction': 'BUY'})

		# SELL conditions -----------------------------------------------------------------------------
		if dt_1 and dt_2 and adx and no_repeat_order:
			print(stock_name, "is in downtrend, Sell this script")
			#print(f"SELL {stock_name}\t{current_time}\t{dt_1}\t{dt_2}\t{adx}\t{dt_3}\t{dt_4}")
			qty = 1
			ltp = tsl.get_ltp_data(stock_name)[stock_name]
			entry_price = ltp
			entry_orderid 	   = tsl.order_placement(stock_name, 'NSE', qty, 0, 0, 'MARKET', 'SELL', 'MIS')
			traded_wathclist.append({'symbol': stock_name, 'entry_price': entry_price, 'direction': 'SELL'})
1 Like

Hi @Aijaz_Ahmad

did you run any testing code previous day…
if yes, It may be such that the previous day, hourly rate limits were hit.

and after 27th only the main code was running so no rate limits were hit.

Hi @Samis

Upload the zip of your complete code and share the google drive link to tradehull_mentorship@tradehull.com

Sir, what is hourly rate limits ?

Hi @babji3

use this file : Dhan_Tradehull_V2.py - Google Drive
rate limits are managed here

Hi @Subhajitpanja @Samis

This means no expiry date was found for GMRINFRA in instrument file

this is a managed error message, so we can ignore it safely

Hi @thakurmhn
I am using github copilot there,
see : GitHub Copilot · Your AI pair programmer · GitHub

Although chatgpt is better than it

Hi @thakurmhn
Congrats on your first Algo

Also you have implemented a good solution for rate-limit issues. :+1:

Hi @Aijaz_Ahmad
I will check on this one