多线程python的问题

多线程python的问题,python,multithreading,Python,Multithreading,在向该程序添加额外线程时遇到很多问题'。当它坐在那里时,它访问一个API并在一个帐户上执行交易。在我尝试将流式传输速率传递给止损订单之前,一切都很好。一旦我以这种方式运行它,代码就会运行一个循环,阻塞所有其他内容,并只打印最新的价格。我如何将这个东西设置为与其他两个线程同时运行 主要交易项目: import Queue import threading import time import json import streamer from execution import Executio

在向该程序添加额外线程时遇到很多问题'。当它坐在那里时,它访问一个API并在一个帐户上执行交易。在我尝试将流式传输速率传递给止损订单之前,一切都很好。一旦我以这种方式运行它,代码就会运行一个循环,阻塞所有其他内容,并只打印最新的价格。我如何将这个东西设置为与其他两个线程同时运行

主要交易项目:

import Queue
import threading
import time
import json
import streamer


from execution import Execution
from settings import STREAM_DOMAIN, API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID
from strategy import TestRandomStrategy
from streaming import StreamingForexPrices
from event import TickEvent
from streamer import demo

stop = demo(0)


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:
        try:
            event = events.get(False)
        except Queue.Empty:
            pass
        else:
            if event is 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 a second between polling
    events = Queue.Queue()





    # Trade 1000 unit of EUR/USD

    instrument = "EUR_USD"
    units = 1
    stopLoss = stop




    # Create the OANDA market price streaming class
    # making sure to provide authentication commands
    prices = StreamingForexPrices(
        STREAM_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID,
        instrument, events
    )
    #handle stopLoss price
    stop = demo(0)

    # 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 = TestRandomStrategy(instrument, units, events, stopLoss)

    # 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=[])
    stop_thread = threading.Thread(target=stop, args=[])


    # Start both threads
    trade_thread.start()
    price_thread.start()
    stop_thread.start()
非常感谢您的帮助

编辑: 好的,我已经深入研究了我认为的问题。这里的代码执行交易,当我尝试将价格发布到止损单时,脚本会给出错误信息。例如:

stopLoss = prices      NameError: name 'prices' is not defined 
另一方面,为了把问题简化到这里,我解决了另一个问题

代码:


你能把它简化成一个吗?让我看看我能做什么..tzaman,如果不添加所有其他模块,我想我做不到,我完全愿意这么做..重点是更具体地定位你的问题,而不是包括运行它所需的一切;就您的问题而言,您要求人们在能够提供帮助之前阅读并理解大量代码。你能至少在代码中包含指针/注释来指出问题的症结所在吗?好的,Tzaman,我已经尽可能地蒸发了,在这个过程中,我解决了大部分问题。我现在可以实时打印价格和报价数据,这是一件非常好的事情!
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 is 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 a second between polling
    events = Queue.Queue()

    # Trade 1000 unit of EUR/USD

    instrument = "EUR_USD"
    units = 1
    stopLoss = prices