使用多种Python策略的Quickfix

时间:2016-11-29 00:14:21

标签: python-2.7 quickfix

我很难交易用Python编写的几种交易策略。 我已经通过Quickfix建立了FIX连接,但是如果策略的脚本在Quickfix连接脚本中,我只能发送命令。由于我有几个策略,我真的不知道如何从单独的脚本发送订单。有人可以给我一些解决方案吗?

import sys
import datetime
import time
import quickfix as fix


class Application(fix.Application):
    orderID = 0
    execID = 0
    def gen_ord_id(self):
        global orderID
        orderID+=1
        return orderID

    def onCreate(self, sessionID):
        return

    def onLogon(self, sessionID):
        self.sessionID = sessionID
        print ("Successful Logon to session '%s'." % sessionID.toString())
        return

    def onLogout(self, sessionID):
        return

    def toAdmin(self, message, sessionID):
        username = fix.Username("username")
        mypass = fix.Password("password")
        mycompid = fix.TargetSubID("targetsubid")
        message.setField(username)
        message.setField(mypass)
        message.setField(mycompid)

    def fromAdmin(self, message, sessionID):         
        TradeID = fix.TradingSessionID
        message.getField(TradeID)
        return

    def toApp(self, sessionID, message):
        print "Sent the following message: %s" % message.toString()
        return

    def fromApp(self, message, sessionID):
        print "Received the following message: %s" % message.toString()
        return

    def genOrderID(self):
        self.orderID = self.orderID + 1
        return `self.orderID`

    def genExecID(self):
        self.execID = self.execID + 1
        return `self.execID`

    def put_order(self, sessionID, myinstrument, myquantity):
        self.myinstrument = myinstrument
        self.myquantity = myquantity
        print("Creating the following order: ")
        today = datetime.datetime.now()
        nextID = today.strftime("%m%d%Y%H%M%S%f")
        trade = fix.Message()
        trade.getHeader().setField(fix.StringField(8, "FIX.4.4"))
        trade.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle))
        trade.setField(fix.ClOrdID(nextID)) #11=Unique order
        trade.getHeader().setField(fix.Account("account"))
        trade.getHeader().setField(fix.TargetSubID("targetsubid"))
        trade.setField(fix.Symbol(myinstrument)) #55=SMBL ?
        trade.setField(fix.TransactTime())
        trade.setField(fix.CharField(54, fix.Side_BUY))
        trade.setField(fix.OrdType(fix.OrdType_MARKET))  # 40=2 Limit order
        trade.setField(fix.OrderQty(myquantity))  # 38=100
        print trade.toString()
        fix.Session.sendToTarget(trade, self.sessionID)


try:
     file = sys.argv[1]
     settings = fix.SessionSettings(file)
     application = Application()
     storeFactory = fix.FileStoreFactory(settings)
     logFactory = fix.ScreenLogFactory(settings)
     initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
     initiator.start()

   while 1:
       time.sleep(1)

       if input == '1':
           print "Putin Order"
           application.put_order(fix.Application)          
       if input == '2':
           sys.exit(0)
       if input == 'd':
           import pdb
           pdb.set_trace()
       else:
           print "Valid input is 1 for order, 2 for exit"

except (fix.ConfigError, fix.RuntimeError) as e:
  print e

这是我的发起人应用。我的问题是我可以从另一个python脚本更新以下值: trade.setField(fix.Symbol(myinstrument)) trade.setField(fix.OrderQty(myquantity))

所以我想从另一个python脚本更改myinstrument和myquantity并强制启动器使用新值执行以下命令application.put_order(fix.Application)。我的问题是这有可能吗?

1 个答案:

答案 0 :(得分:0)

听起来你需要一个QuickFIX订阅的内部消息传递层,以及你的单独Python脚本发布订单。它涉及工作流程设计。尝试使用VertX之类的内容,因为可以使用Python进行设置。