-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrading.py
74 lines (63 loc) · 2.36 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
import queue
import threading
import time
import decimal
from execution import Execution
from settings import STREAM_DOMAIN, API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID
from strategy import RSIStrategy
from streaming import StreamingForexPrices
def trade(events, strategy, execution):
"""
Carries out an infinite while loop that polls the
events queue and directs each event to either the
strategy component of the execution handler. The
loop will then pause for "heartbeat" seconds and
continue.
"""
while True:
decimal.getcontext().prec = 7
try:
event = events.get(False)
except queue.Empty:
pass
else:
if event is not None:
if event.type == 'TICK':
strategy.pricelist(event)
strategy.gainsandlosses(event)
strategy.print_signals(event)
strategy.calculate_signals(event)
elif event.type == 'ORDER':
print("Executing order!")
execution.execute_order(event)
time.sleep(heartbeat)
if __name__ == "__main__":
# heartbeat = 0.5 # Half a second between polling
heartbeat = 0.5 # Half a seconds between polling
events = queue.Queue()
# Trade 10000 units of EUR/USD
instrument = "EUR_USD"
units = 5
min_window = 15
persistence = 3
rsiupboundary = 80
rsilowboundary = 20
# Create the OANDA market price streaming class
# making sure to provide authentication commands
prices = StreamingForexPrices(
STREAM_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID,
instrument, events
)
# Create the execution handler making sure to
# provide authentication commands
execution = Execution(API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID)
# Create the strategy/signal generator, passing the
# instrument, quantity of units and the events queue
strategy = RSIStrategy(instrument, units, events, min_window, persistence, rsilowboundary, rsiupboundary)
# Create two separate threads: One for the trading loop
# and another for the market price streaming class
trade_thread = threading.Thread(target=trade, args=(events, strategy, execution))
price_thread = threading.Thread(target=prices.stream_to_queue, args=[])
# Start both threads
trade_thread.start()
price_thread.start()