-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Trading.py
101 lines (92 loc) · 3.16 KB
/
_Trading.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
from _TradeBotHelper import *
# IB IMPORTS
from _Positions import get_positions
from _Orders import placeOrder, cancelTkrOrder
import _vars as v
def tradeBot(account, account_type, ticker, direction:str, quantity:int, price=None, unit= 'perc', orderType= 'limit', clientId=10, **kwargs):
"""
LONG Positions only ->> NO SHORTING (LONF ONLY)
:param ticker: Stock ticker
:param quantity: TradeSize
:param unit: {perc, qty, cash}
:param direction: (Buy or Sell or Close)
:return: Flask String to Print
"""
"""
pkg = {
'account':"Meesam",
'account_type':'Real',
'clientId':5,
'ticker': 'NKLA',
'direction': 'Buy',
'quantity': 80,
'price': 30,
'orderType':'DarkIce'
}
"""
direction= direction.title()
if price:
tkrprice = price
else:
tkrprice = None
# GET tickerPrice, quantity, cash
if unit=='cash':
qty, tkrprice = cashToQuantity(ticker, quantity, tkrprice)
# Change unit to qty
unit='qty'
cash = quantity
elif unit == 'qty':
cash, tkrprice = quantityToCash(ticker, quantity, tkrprice)
qty = quantity
elif unit == 'perc':
cash = percToCash(account, account_type, quantity)
qty, tkrprice = cashToQuantity(ticker,cash)
else:
qty = 0
return "Invalid PKG{...}"
if qty==0:
return f'Quantity: {qty} >> ERROR!'
# UPDATING PRICE
if price:
if round(price,1) != round(tkrprice,1) and tkrprice not in [0,'nan',None]:
price = tkrprice
else:
price = round(tkrprice,2)
# IF SIGNAL==CLOSE >> CLOSE ALL POSITION
# IF SIGNAL==SELL
# >> GIVEN QTY - CLOSE GIVEN QUANTITY - CLOSE TILL 0
# >> GIVEN PERC - CLOSE PERC OF THE POSITION
# CLOSE ALL OPEN POSITIONS FOR THE TICKER - >> Using Default ClientId=3
cancelTkrOrder(account, account_type, ticker)
if direction.lower() in ['close', 'sell']:
# GET POSITIONS
position_df = get_positions(account, account_type)
if ticker in position_df.Symbol.tolist():
positionSize = position_df[position_df.Symbol == ticker].Position.sum()
if direction == "Close":
# Update direction
direction = "Sell"
# Update quantity
qty = positionSize
# PLACE TRADE
return placeOrder(account, account_type, ticker, direction, qty, price, orderType, clientId)
elif direction == 'Sell':
if qty > positionSize:
qty = positionSize
return placeOrder(account, account_type, ticker, direction, qty, price, orderType, clientId)
else:
return f"Ticker: {ticker} Position Not Found ..."
else:
return placeOrder(account, account_type, ticker, direction, qty, price, orderType, clientId)
if __name__=="__main__":
tradeBot(
account=v.ACCOUNT_NAME,
account_type=v.ACCOUNT_TYPE,
ticker=v.ticker,
direction=v.direction,
price= v.price,
quantity=v.quantity,
unit=v.orderUnit,
orderType=v.orderType,
clientId=v.orderClientId
)