Repository navigation
Expand file tree
/
Copy pathorders.py
More file actions
77 lines (65 loc) · 2.71 KB
/
Copy pathorders.py
File metadata and controls
77 lines (65 loc) · 2.71 KB
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
from smartapi import SmartConnect
import pandas as pd
from datetime import datetime
import credentials
import requests
import numpy as np
import pyotp
myotp = pyotp.TOTP(credentials.CD).now()
def place_order(token,symbol,qty,exch_seg,buy_sell,ordertype,price):
try:
orderparams = {
"variety": "NORMAL",
"tradingsymbol": symbol,
"symboltoken": token,
"transactiontype": buy_sell,
"exchange": exch_seg,
"ordertype": ordertype,
"producttype": "INTRADAY",
"duration": "DAY",
"price": price,
"squareoff": "0",
"stoploss": "0",
"quantity": qty
}
orderId=obj.placeOrder(orderparams)
print("The order id is: {}".format(orderId))
except Exception as e:
print("Order placement failed: {}".format(e.message))
def intializeSymbolTokenMap():
url = 'https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json'
d = requests.get(url).json()
global token_df
token_df = pd.DataFrame.from_dict(d)
token_df['expiry'] = pd.to_datetime(token_df['expiry'])
token_df = token_df.astype({'strike': float})
credentials.TOKEN_MAP = token_df
def getTokenInfo (exch_seg, instrumenttype,symbol,strike_price,pe_ce):
df = credentials.TOKEN_MAP
strike_price = strike_price*100
if exch_seg == 'NSE':
eq_df = df[(df['exch_seg'] == 'NSE') & (df['symbol'].str.contains('EQ')) ]
return eq_df[eq_df['name'] == symbol]
elif exch_seg == 'NFO' and ((instrumenttype == 'FUTSTK') or (instrumenttype == 'FUTIDX')):
return df[(df['exch_seg'] == 'NFO') & (df['instrumenttype'] == instrumenttype) & (df['name'] == symbol)].sort_values(by=['expiry'])
elif exch_seg == 'NFO' and (instrumenttype == 'OPTSTK' or instrumenttype == 'OPTIDX'):
return df[(df['exch_seg'] == 'NFO') & (df['instrumenttype'] == instrumenttype) & (df['name'] == symbol) & (df['strike'] == strike_price) & (df['symbol'].str.endswith(pe_ce))].sort_values(by=['expiry'])
if __name__ == '__main__':
intializeSymbolTokenMap()
obj=SmartConnect(api_key=credentials.API_KEY)
data = obj.generateSession(credentials.USER_NAME,credentials.PWD,myotp)
refreshToken= data['data']['refreshToken']
feedToken=obj.getfeedToken()
credentials.FEED_TOKEN = feedToken
def userin():
userProfile= obj.getProfile(refreshToken)
userProfile.place(x=280, y=250)
print(userProfile)
tokenInfo = getTokenInfo('NFO','OPTIDX','NIFTY',14900,'PE').iloc[0]
#print(tokenInfo)
symbol = tokenInfo['symbol']
token = tokenInfo['token']
lot = '100'
#lot = int(tokenInfo['lotsize'])
print(symbol, token, lot)
place_order(token,symbol,lot,'NFO','BUY','MARKET',200)