-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_app.py
181 lines (150 loc) · 5.4 KB
/
main_app.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
from initialize import initialize_connection as con
from ohlcv import interval, ohlcv
from Signal import signal
# Basic Imports
import datetime
import time
import sqlite3
# =====================imput parameters=====================
# Trading pairs
# tickers = {"currency":{"EUR/USD",'USD/CAD'},
# "index":{"US30"},
# "commodity":{"USOIL"}}
# 'XAU/USD':10,
# 'US30':20,
# 'AUD/USD':30,
#'GBP/USD':30,
tickers = {
'EUR/USD':30,
'GBP/USD':30,
'USD/CAD':30
}
# trading times
start = input("Enter a start time HH:MM (24h.): ")
end = input("Enter an end time HH:MM (24h.): ")
s = start.split(":")
e = end.split(":")
if len(s)==2:
s_time = datetime.time(int(s[0]),int(s[1]))
else:
s_time = datetime.time(7, 30)
if len(e)==2:
e_time = datetime.time(int(e[0]),int(e[1]))
else:
e_time = datetime.time(14, 30)
now = datetime.datetime.now().time()
# Trading interval
trade_interval = interval.fifteenMinute
trade_interval_refresh = 60 * 15
# Activating the algorithm for the active time range
activation_condition = ((s_time < now) and (now < e_time))
# ===========================================================
# RECORDING SESSION PNL
db_name = 'orders_pnl.db'
connection = sqlite3.connect(db_name)
cursor = connection.cursor()
while activation_condition==True:
account = con.get_accounts()
balance = account.balance
positions = con.get_open_positions()
perc_total_balance = 0.6
for ticker in tickers:
# ======= Getting data ===========
df = ohlcv(ticker,trade_interval)
# ======== Position sizing ========
con.subscribe_market_data(ticker)
price = con.get_last_price(ticker).mean()
con.unsubscribe_market_data(ticker)
total_borrow_amount = ((perc_total_balance*balance.iloc[0]/len(tickers))*tickers[ticker])
# Order amount
order_amount = round(total_borrow_amount/price,0)
if (len(ticker)==7) and (ticker!="XAU/USD"):
order_amount = order_amount/1000
if order_amount==0:
order_amount += 1
# =================================
# ___TP and SL in pips___
take_profit = 80
stop_loss = -20
# ======== Signal ==========
s_ticker = signal(df)
is_Buy = None
try:
is_Buy = positions.loc[positions.currency==ticker,"isBuy"].iloc[0]
except:
print(f"No open position for {ticker}")
# BUYING
if s_ticker == 1 and ((is_Buy is None) or (is_Buy==False)):
# Closing the short position
if is_Buy == False:
con.close_all_for_symbol(ticker)
print(f"CLOSING POSITION for {ticker}")
# buy order
try:
con.open_trade(symbol=ticker, is_buy=True,
is_in_pips=True,
amount=order_amount,
order_type='AtMarket',
time_in_force='GTC',
limit=take_profit,
stop=stop_loss,
trailing_step =True
)
print(f"Created BUY order for {ticker}: {order_amount}")
except:
print(f"Buy Order failed for {ticker}")
# SHORTING
elif s_ticker == -1 and ((is_Buy is None) or (is_Buy == True)):
# Closing the long position
if is_Buy == True:
con.close_all_for_symbol(ticker)
print(f"CLOSING POSITION for {ticker}")
# sell order
try:
con.open_trade(symbol=ticker, is_buy=False,
is_in_pips=True,
amount=order_amount,
order_type='AtMarket',
time_in_force='GTC',
limit=take_profit,
stop=stop_loss,
trailing_step =True
)
print(f"Created SELL order for {ticker}: {order_amount}")
except:
print(f"Sell Order failed for {ticker}")
# CLOSING
elif s_ticker == 0 and (is_Buy is not None):
# close the position
con.close_all_for_symbol(ticker)
print(f"CLOSING POSITION for {ticker}")
else:
print(f"No new positions open for {ticker}...")
now = datetime.datetime.now().time()
activation_condition = ((s_time < now) and (now < e_time))
if activation_condition:
time_stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
pnl = round(con.get_accounts().dayPL.iloc[0],0)
insert_into = f"INSERT INTO pnl (time,PnL) VALUES (?,?)"
cursor.execute(insert_into,(time_stamp,pnl))
connection.commit()
print()
print(f"Current PnL: {pnl}")
time.sleep(trade_interval_refresh)
# Saving and Closing DB
connection.close()
print("Trading session over....")
print("_________________________")
print("Closing All Trades")
active_pos = con.get_open_positions()
if len(active_pos)>0:
for ticker in tickers:
try:
con.close_all_for_symbol(ticker)
print(f"Closed trade for {ticker}...")
except:
print(f"No position available for {ticker}")
else:
print('No Positions to Close')
con.close()
print("Connection Closed...")