-
Notifications
You must be signed in to change notification settings - Fork 19
/
920algo.py
207 lines (160 loc) · 7.29 KB
/
920algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Author = RustAlgo
Basic Code Created in an Hour purely for Education, Not for Trading Using Real Money
You are Solely and wholly Responsible for Your Loss, Author is Not Responsible for Any of Your Losses
For Morea details refer README.md
"""
import datetime
import time
import requests
import pandas as pd
from dhanhq import dhanhq
import os
from nsepython import *
import dhancred
import json
dhan= dhanhq(client_id=dhancred.client_id(),access_token=dhancred.access_token())
print(f"Session to Broker Dhan Established at {datetime.datetime.now()}")
## Lot Size and SL Points ##########
Qty= 50
SL= 30
############################################################################################
underlying = "NIFTY"
nearestexpiry= expiry_list(underlying)
expiry= nearestexpiry[0]
print(f'Selected Expiry is {expiry}')
##############################################################################################
# Downloading Scrip Master of Dhan
security_idsource = "https://images.dhan.co/api-data/api-scrip-master.csv"
todays_date= datetime.datetime.now().strftime("%Y-%m-%d")
file_name= f'api-scrip-master.csv'
response = requests.get(security_idsource)
with open(file_name, "wb") as f:
f.write(response.content)
print(f"Scrip Master '{file_name}' downloaded successfully.")
#Created a Pandas Data Frame for Placing Orders
required_cols = ['SEM_EXM_EXCH_ID','SEM_SMST_SECURITY_ID','SEM_INSTRUMENT_NAME','SEM_TRADING_SYMBOL','SEM_CUSTOM_SYMBOL','SEM_EXPIRY_DATE','SEM_EXPIRY_FLAG']
master= pd.read_csv("api-scrip-master.csv",usecols=required_cols)
#############################################################################################
while True:
current_time = datetime.datetime.now().time()
if current_time >= datetime.time(9, 20, 00): #9,20,30
print(f"It is now {datetime.datetime.now()}. Continuing...")
break
else:
print(f"It is currently {current_time}. Waiting for 9:20:30 AM...")
time.sleep(1) # sleep for 1 second before checking at 9:20:30 AM
###############################################################################
ltp= (nse_quote_ltp(underlying))
print(f'LTP of {underlying} is {ltp}')
ATM = round(ltp/50)*50
print(f'At The Money Strike Of Underlying {underlying} is {ATM}')
expiry_date_string = expiry
expiry_date = datetime.datetime.strptime(expiry_date_string, '%d-%b-%Y')
year = expiry_date.year
month = expiry_date.strftime('%b').upper()
day = expiry_date.day
atmCE= f'NIFTY {day} {month} {ATM} CALL'
atmPE= f'NIFTY {day} {month} {ATM} PUT'
print(f"At The Money Call Strike is {atmCE}, At The Money Put Strike is {atmPE}")
LTPofCE= float(nse_quote_ltp(underlying,"latest","CE",ATM))
print(f'Call is Now Trading at {LTPofCE}')
LTPofPE= float(nse_quote_ltp(underlying,"latest","PE",ATM))
print(f'Put is Now Trading at {LTPofPE}')
# Saving SL as LTP of CE and PE Minus SL as defined earlier Before placing order
ce_entry_price= float(LTPofCE)
pe_entry_price= float(LTPofPE)
#SL in Points
ceSL= float(round(LTPofCE+SL,1)) #SL in Points
peSL= float(round(LTPofPE+SL,1)) #SL in Points
#SL in Percentage
# ceSL = float(round(LTPofCE * (1 + SL), 1)) #SL in Percentage
# peSL = float(round(LTPofPE * (1 + SL), 1)) #SL in Percentage
#Getting Security ID for ATM CE and PE
securityidCE = master.loc[master['SEM_CUSTOM_SYMBOL'] == atmCE, 'SEM_SMST_SECURITY_ID'].iloc[0]
securityidPE = master.loc[master['SEM_CUSTOM_SYMBOL'] == atmPE, 'SEM_SMST_SECURITY_ID'].iloc[0]
print(f'Retrieved Security Id of Boker for CE is {securityidCE}')
print(f'Retrieved Security Id of Boker for PE is {securityidPE}')
print(f'placing Market Orders for {atmCE} and {atmPE}')
########################################################################################
dhan.place_order(security_id=str(securityidCE),
exchange_segment=dhan.FNO,
transaction_type=dhan.SELL,
quantity=Qty,
order_type=dhan.MARKET,
product_type=dhan.INTRA,
price=0)
print(f'Market Order Placed for {atmCE}')
dhan.place_order(security_id=str(securityidPE),
exchange_segment=dhan.FNO,
transaction_type=dhan.SELL,
quantity=Qty,
order_type=dhan.MARKET,
product_type=dhan.INTRA,
price=0)
print(f'Market Order Placed for {atmPE}')
################################################################################
print(f"CEentryprice is {ce_entry_price}")
print(f"PEentryprice is {pe_entry_price}")
print(f"CESL is now {ceSL}")
print(f"PESL is now {peSL}")
###############################################################################
print("All Positions are Punched, Wating for SL")
traded = "No"
while traded == "No":
dt=datetime.datetime.now()
try:
LTPofCE=nse_quote_ltp(underlying,"latest","CE",ATM)
LTPofPE=nse_quote_ltp(underlying,"latest","PE",ATM)
print("Inside Exit: Checking CE:",LTPofCE," and PE:",LTPofPE)
if ((LTPofCE > ceSL) or (dt.hour >= 15 and dt.minute >= 15)):
exitCE= dhan.place_order(security_id=str(securityidCE), exchange_segment=dhan.FNO,transaction_type=dhan.BUY,quantity=Qty,order_type=dhan.MARKET,product_type=dhan.INTRA,price=0)
traded = "CE"
print("EXiting CE Leg")
time.sleep(5)
elif((LTPofPE > peSL) or (dt.hour >= 15 and dt.minute >= 15)):
exitPE= dhan.place_order(security_id=str(securityidPE), exchange_segment=dhan.FNO,transaction_type=dhan.BUY,quantity=Qty,order_type=dhan.MARKET,product_type=dhan.INTRA,price=0)
traded = "PE"
print("Exiting PE Leg")
time.sleep(5)
else:
print("No SL is Hit")
time.sleep(5)
except:
print("Couldn't find LTP , RETRYING !!")
time.sleep(5)
if(traded=="CE"):
peSL = pe_entry_price
while traded == "CE":
dt= datetime.datetime.now()
try:
LTPofPE=nse_quote_ltp(underlying,"latest","PE",ATM)
if ((ltp > peSL) or (dt.hour >= 15 and dt.minute >= 15)):
exitPE= dhan.place_order(security_id=str(securityidPE), exchange_segment=dhan.FNO,transaction_type=dhan.BUY,quantity=Qty,order_type=dhan.MARKET,product_type=dhan.INTRA,price=0)
traded = "Close"
print("PE SL Hit or Market is About to be closed")
time.sleep(5)
else:
print("PE SL not hit")
time.sleep(1)
except:
print("Couldn't find LTP , RETRYING !!")
time.sleep(1)
elif(traded == "PE"):
ceSL = ce_entry_price
while traded == "PE":
dt = datetime.datetime.now()
try:
LTPofCE=nse_quote_ltp(underlying,"latest","CE",ATM)
if ((ltp > ceSL) or (dt.hour >= 15 and dt.minute >= 15)):
exitCE= dhan.place_order(security_id=str(securityidCE), exchange_segment=dhan.FNO,transaction_type=dhan.BUY,quantity=Qty,order_type=dhan.MARKET,product_type=dhan.INTRA,price=0)
traded = "Close"
time.sleep(5)
else:
print("CE SL not hit")
time.sleep(5)
except:
print("Couldn't find LTP , RETRYING !!")
time.sleep(1)
elif (traded == "Close"):
print("All trades done. Exiting Code")