Trouble with multithreading python -
having lot of trouble adding additional thread program'. sits accesses api , executes trades on account. fine , until try , pass streaming rate stoploss order. once run way code runs loop blocking out else , prints latest price. how can set thing run concurrently other 2 threads?
main trading program:
import queue import threading import time import json import streamer execution import execution settings import stream_domain, api_domain, access_token, account_id strategy import testrandomstrategy streaming import streamingforexprices event import tickevent streamer import demo stop = demo(0) def trade(events, strategy, execution): """ carries out infinite while loop polls events queue , directs each event either strategy component of execution handler. loop pause "heartbeat" seconds , continue. """ while true: try: event = events.get(false) except queue.empty: pass else: if event not none: if event.type == 'tick': strategy.calculate_signals(event) elif event.type == 'order': print "executing order!" execution.execute_order(event) elif event.type == 'stoploss': print "stop loss here!!!!" execution.execute_order(event) time.sleep(heartbeat) if __name__ == "__main__": heartbeat = 0 # half second between polling events = queue.queue() # trade 1000 unit of eur/usd instrument = "eur_usd" units = 1 stoploss = stop # create oanda market price streaming class # making sure provide authentication commands prices = streamingforexprices( stream_domain, access_token, account_id, instrument, events ) #handle stoploss price stop = demo(0) # create execution handler making sure # provide authentication commands execution = execution(api_domain, access_token, account_id) # create strategy/signal generator, passing # instrument, quantity of units , events queue strategy = testrandomstrategy(instrument, units, events, stoploss) # create 2 separate threads: 1 trading loop # , market price streaming class trade_thread = threading.thread(target=trade, args=(events, strategy, execution)) price_thread = threading.thread(target=prices.stream_to_queue, args=[]) stop_thread = threading.thread(target=stop, args=[]) # start both threads trade_thread.start() price_thread.start() stop_thread.start()
any appreciated!
edit: ok have drilled down problem think. code here executes trades , when try , post "prices" stoploss order script gives me error. example:
stoploss = prices nameerror: name 'prices' not defined
on other hand, in trying simplify problem post here solved 1 having!
code:
def trade(events, strategy, execution): while true: prices = demo(0) print prices while true: try: event = events.get(false) except queue.empty: pass else: if event not none: if event.type == 'tick': strategy.calculate_signals(event) elif event.type == 'order': print "executing order!" execution.execute_order(event) time.sleep(heartbeat) if __name__ == "__main__": heartbeat = 0 # half second between polling events = queue.queue() # trade 1000 unit of eur/usd instrument = "eur_usd" units = 1 stoploss = prices