Learn Algo Trading with Python | Codes | Youtube Series

[Errno 2] No such file or directory

Hi @Kalpeshh_Patel

You have suggested a good method.
yes it can be done, we can use any common database/pickle/HDF5 and send our data frame to it.
both algo strategy can read from the same source.

Hi @Subhajitpanja
Yes it seems rate limits can create issues, specially if you are working with commodity.
I will take this point in next video on how to manage rate limits.

1 Like

Hi @rahulcse56
Added the same. :+1:

1 Like

Hi @Aijaz_Ahmad

Yes Backtesting it possible, however historical data seems to be a challenge as of now.

@Hardik @RahulDeshpande
Please add in roadmap.

1 Like

Hi @info_mail

Do check this link : Learn Algo Trading with Python | Codes | Youtube Series - #952 by Tradehull_Imran

Hi @Francis_Antony

It seems a network issue. PIP was not able to connect to download libraries required

Check if you are not behind a firewall or proxy.
If its a office laptop then it may have those restrictions.

1 Like

2 candle Theory code files : 2. 2 Candle Theory Algo.zip - Google Drive

Do post your question if you are having any confusion.

1 Like

Hi @Kalpeshh_Patel

use below pseudocode,

It will move SL to Entry price after options has moved up by 20%
we needed to make changes in trade_info also,

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")



# ---------------for dhan login ----------------
client_code = "client_code"
token_id    = "token_id"


tsl = Tradehull(client_code,token_id)

traded = "no"
trade_info = {"options_name":None, "qty":None, "sl":None, "CE_PE":None, "entry_price":None ,  "Trailed":None}



while True:

	current_time = datetime.datetime.now()

	index_chart  = tsl.get_historical_data(tradingsymbol='NIFTY NOV FUT', exchange='NFO', timeframe="1")
	time.sleep(3)
	index_ltp    = tsl.get_ltp_data(names = ['NIFTY NOV FUT'])['NIFTY NOV FUT']

	if (index_chart == None):
		time.sleep(60)
		continue


	# rsi    ------------------------ apply indicators
	index_chart['rsi'] = talib.RSI(index_chart['close'], timeperiod=14)

	# vwap
	index_chart.set_index(pd.DatetimeIndex(index_chart['timestamp']), inplace=True)
	index_chart['vwap'] = pta.vwap(index_chart['high'] , index_chart['low'], index_chart['close'] , index_chart['volume'])

	# Supertrend
	indi = ta.supertrend(index_chart['high'], index_chart['low'], index_chart['close'], 10, 2)
	index_chart = pd.concat([index_chart, indi], axis=1, join='inner')


	# vwma
	index_chart['pv'] = index_chart['close'] * index_chart['volume']
	index_chart['vwma'] = index_chart['pv'].rolling(20).mean() / index_chart['volume'].rolling(20).mean()

	# volume
	volume = 50000

	first_candle   = index_chart.iloc[-3]
	second_candle  = index_chart.iloc[-2]
	running_candle = index_chart.iloc[-1]


	# ---------------------------- BUY ENTRY CONDITIONS ----------------------------
	bc1 = first_candle['close'] > first_candle['vwap']                # First Candle close is above VWAP
	bc2 = first_candle['close'] > first_candle['SUPERT_10_2.0']       # First Candle close is above Supertrend 
	bc3 = first_candle['close'] > first_candle['vwma']                # First Candle close is above VWMA
	bc4 = first_candle['rsi'] < 80                                    # First candle RSI < 80	
	bc5 = second_candle['volume'] > 50000                             # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
	bc6 = traded == "no"
	bc7 = index_ltp > first_candle['low']
	print(f"BUY \t {current_time} \t {bc1} \t {bc2} \t {bc3} \t {bc4} \t {bc5} \t {bc7} \t first_candle {str(first_candle['timestamp'].time())}")


	# ---------------------------- SELL ENTRY CONDITIONS ----------------------------
	sc1 = first_candle['close'] < first_candle['vwap']                # First Candle close is below VWAP
	sc2 = first_candle['close'] < first_candle['SUPERT_10_2.0']       # First Candle close is below Supertrend 
	sc3 = first_candle['close'] < first_candle['vwma']                # First Candle close is below VWMA
	sc4 = first_candle['rsi'] > 20                                    # First candle RSI < 80	
	sc5 = second_candle['volume'] > 50000                             # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
	sc6 = traded == "no"
	sc7 = index_ltp < first_candle['high']
	print(f"SELL \t {current_time} \t {sc1} \t {sc2} \t {sc3} \t {sc4} \t {sc5} \t {sc7} \t first_candle {str(first_candle['timestamp'].time())} \n")


	if bc1 and bc2 and bc3 and bc4 and bc5 and bc6 and bc7:
		print("Sell Signal Formed")

		ce_name, pe_name, strike   = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
		lot_size                   = tsl.get_lot_size(ce_name)*1
		entry_orderid              = tsl.order_placement(ce_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
		traded                     = "yes"
		trade_info['options_name'] = ce_name
		trade_info['qty']          = lot_size
		trade_info['sl']           = first_candle['low']
		trade_info['CE_PE']        = "CE"

		time.sleep(1)
		trade_info['entry_price'] = tsl.get_executed_price(orderid=trade_info['entry_orderid'])



	if sc1 and sc2 and sc3 and sc4 and sc5 and sc6 and sc7:
		print("Sell Signal Formed")

		ce_name, pe_name, strike   = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
		lot_size                   = tsl.get_lot_size(pe_name)*1
		entry_orderid              = tsl.order_placement(pe_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
		traded                     = "yes"
		trade_info['options_name'] = pe_name
		trade_info['qty']          = lot_size
		trade_info['sl']           = first_candle['high']
		trade_info['CE_PE']        = "PE"



	# ---------------------------- check for exit SL/TG

	if traded == "yes":

		long_position  = trade_info['CE_PE'] == "CE"
		short_position = trade_info['CE_PE'] == "PE"


		if long_position:

			price_has_moved_20_pct        = ltp > (trade_info['entry_price'])*1.2
			position_has_not_been_trailed = trade_info['Trailed'] is None

			if price_has_moved_20_pct and position_has_not_been_trailed:
				trade_info['sl']           = trade_info['entry_price']
				trade_info['Trailed']      = "yes_I_have_trailed"


			sl_hit = index_ltp < trade_info['sl']
			tg_hit = index_ltp < running_candle['SUPERT_10_2.0']




			if sl_hit or tg_hit:
				print("Order Exited", trade_info)
				exit_orderid        = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
				pdb.set_trace()





		if short_position:
			sl_hit = index_ltp > trade_info['sl']
			tg_hit = index_ltp > running_candle['SUPERT_10_2.0']

			if sl_hit or tg_hit:
				print("Order Exited", trade_info)
				exit_orderid        = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
				pdb.set_trace()
















# trade_info = {
# 				'CE_PE': 'PE',
# 				'options_name': 'NIFTY 21 NOV 23350 PUT',
# 				'qty': 25,
# 				'sl': 23357.95
# 			}



1 Like

I am trying to install Ta-Lib on macOS, but I am encountering the following error:

Hi @Naga_Rajesh_K
Sure will take it up on Monday.

Hi @thakurmhn

Link : Learn Algo Trading with Python | Codes | Youtube Series - #1041 by Tradehull_Imran

Hi @Prasun_Chakraborty

Do send complete error screenshot.

Hi, @Tradehull_Imran ,
Good Day!
I wanted to check if you have setup available for macOS?

Hi @vivek4pandey

do check this link : TA-Lib
and this one : How do I install TA-lib in python 3 in Mac OS High Sierra? - Stack Overflow

Thanks Imran.

I have tried again to execute that Library and it worked.

1 Like

Hi @Tradehull_Imran,

Can you please share the backtest code in the meantime.

Hi @Tradehull_Imran,

When I am running the algo on weekend/holidays or after market hours, chart = tsl.get_intraday_data(stock_name, U_Exge, 3) function is throwing error so is there any possibility to get this data from the last traded day’s pricing data with this function.

Hi
@Tradehull_Imran

How to Solve error

if (index_chart == None):
    time.sleep(60)
    continue

C:\Algo Practice\Session 8>py “2 candle theory Algo - Both Side Pseudocode V2.py”
File “2 candle theory Algo - Both Side Pseudocode V2.py”, line 37
if (index_chart == None):
^
SyntaxError: invalid syntax

Hi
@Tradehull_Imran
I applied the code
Trade is execute but sl error

C:\Algo Practice\Session 8>py “RSI Option V2.py”
Codebase Version 2.1
-----Logged into Dhan-----
reading existing file all_instrument 2024-11-25.csv
Got the instrument file
available_balance 275667.47
BUY 09:55:23.121570 True False False cc_1 09:45:00
SELL 09:55:23.121570 True True True cc_1 09:45:00

NIFTY NOV FUT Buy PUT
Traceback (most recent call last):
File “RSI Option V2.py”, line 146, in
price_has_moved_20_pct = ltp > (trade_info[‘entry_price’])*1.2
NameError: name ‘ltp’ is not defined


Please do correction