Python and QuickFIX - Matlab, R project and Python | futures.io
futures.io futures trading


Python and QuickFIX
Started: by Branzol Views / Replies:8,339 / 34
Last Reply: Attachments:1

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.


You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike

Reply
 1  
 
Thread Tools Search this Thread
 

Python and QuickFIX

  #1 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received

Python and QuickFIX

Hi Everyone:

Just a bit of background, I've been slowly learning Python over the course of the last year, and putting together my own custom backtesting, and analysis system with a Django/HighCharts FrontEnd. Most of my inspiration for doing it came from https://futures.io/elite-automated-trading/29764-big-mike-s-custom-trading-platform.html thread.

I've somewhat hit a roadblock, and have been trying for about 3 weeks now to use the QuickFix Library to connect to a FIX Acceptor. Start the session, and send messages. The main one for me is a MarketData - Snapshot with Updates Request(35=w). I don't even really have any functional code to show for my 3 weeks as I can't get past the creating the session properly. The example on QuickFix's website are horrendous for Python and really don't provide much help. I was hoping maybe someone else had a snippet of code they would not mind sharing. I managed to get my IQfeed properly up and running in Python, but this one is just stumping me.

Thank you in advance.

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received


After searching what now feels like the entire web for a proper example, I was able to find a handful of examples that did not work and were riddle with errors. I tore a few of them apart, and would love to give credit to the original creators, but at this point I don't know what came from what.

The basic code I'm posting below will make a connection, and give you a option to place an order. You can switch out the order message for any type of FIX compliant message. At that point just take a FIX message format, and send the message in that manner. I've tried the Market-Data requests and they are working now. When I finish my final code of allowing you to enter a symbol list, and start streaming data to a database. And external input from my other scripts for automated ordering and closing of orders I'll post that.

I just wanted to ensure anyone else who's having issues with QuickFIX and Python has a good starting point.

 
Code
import sys
import argparse
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, sessionID, message):
        print "Sent the Admin following message: %s" % message.toString()
        return

    def fromAdmin(self, sessionID, message):
        print "Received the Admin following message: %s" % message.toString()
        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):
        print("Creating the following order: ")
        trade = fix.Message()
        trade.setField(fix.ClOrdID(self.genExecID())) #11=Unique order
        trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION))
        trade.setField(fix.Symbol('SMBL'))
        trade.setField(fix.Side(fix.Side_BUY))
        trade.setField(fix.OrdType(fix.OrdType_LIMIT))
        trade.setField(fix.OrderQty(100))
        trade.setField(fix.Price(10))
        print trade.toString()
        fix.Session.sendToTarget(trade, self.sessionID)

def main(config_file):
    try:
        settings = fix.SessionSettings("settings.cfg")
        application = Application()
        storeFactory = fix.FileStoreFactory(settings)
        logFactory = fix.FileLogFactory(settings)
        initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
        initiator.start()

        while 1:
                input = raw_input()
                if input == '1':
                    print "Putin Order"
                    application.put_order()
                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"
                    continue
    except (fix.ConfigError, fix.RuntimeError), e:
        print e

if __name__=='__main__':
    parser = argparse.ArgumentParser(description='FIX Client')
    parser.add_argument('file_name', type=str, help='Name of configuration file')
    args = parser.parse_args()
    main(args.file_name)
You'll also need the system "settings.cfg" file in the same directory as the script. Here's an example one I deleted out my information and put generic items.

 
Code
[DEFAULT]
ConnectionType=initiator
ReconnectInterval=60
FileStorePath=store
FileLogPath=log
StartTime=00:00:00
EndTime=00:00:00
UseDataDictionary=Y
DataDictionary=spec/FIX50SP2.xml
TransportDataDictionary=spec/FIXT11.xml
HttpAcceptPort=9911
ValidateUserDefinedFields=N
ResetOnLogout=Y
ResetOnLogon=Y
DefaultApplVerID=FIX.5.0SP2

# standard config elements

[SESSION]
# inherit ConnectionType, ReconnectInterval and SenderCompID from default
BeginString=FIXT.1.1
SenderCompID=YOUR ID
TargetCompID=YOUR PROVIDERS ID
SocketConnectHost=fix.test.com
SocketConnectPort=11000
HeartBtInt=30

Reply With Quote
The following 6 users say Thank You to Branzol for this post:
 
  #4 (permalink)
Trading Apprentice
Sao Paulo, Brazil
 
Futures Experience: Intermediate
Platform: Metastock
Favorite Futures: Forex
 
Posts: 4 since Apr 2015
Thanks: 3 given, 1 received

Hi Branzol,

Thank you for sharing your code. I spent a long time and I cannot run your code.

I got this error:

 
Code
$ python2 branzol.py settings.cfg 
1
Putin Order
Creating the following order: 
9=4111=121=338=10040=244=1054=155=SMBL10=068
Traceback (most recent call last):
  File "branzol.py", line 88, in <module>
    main(args.file_name)
  File "branzol.py", line 72, in main
    application.put_order()
  File "branzol.py", line 57, in put_order
    fix.Session.sendToTarget(trade, self.sessionID)
  File "/usr/lib/python2.7/site-packages/quickfix.py", line 34110, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, Application, name)
  File "/usr/lib/python2.7/site-packages/quickfix.py", line 34, in _swig_getattr
    raise AttributeError,name
AttributeError: sessionID
I believe that is incomplete, I saw in other sites that we need the Code to:

 
Code
acceptor = fix.SocketAcceptor(application, storeFactory, settings, logFactory)
initiator  =  fix.SocketInitiator(application, storeFactory, settings, logFactory)
And Settings to:
 
Code
ConnectionType=acceptor
ConnectionType=initiator
And also, I need add the folder "spec"

So, please, if possible, could you share the complete code? (or the repository or a zip file).

Thank you very much,
John

Reply With Quote
 
  #5 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received

Futures Edge on FIO

TraderFXUS View Post
Hi Branzol,

Thank you for sharing your code. I spent a long time and I cannot run your code.

I'll take a look for you when I get home. I cut and pasted the code from a functional file. The errors your posting are the ones I had when using other sections of code I found floating on the web. I'll take a look later tonight and link what I have that's functional. I'm using 2.7.9 as well.

Reply With Quote
 
  #6 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received


TraderFXUS View Post
Hi Branzol,
Thank you for sharing your code. I spent a long time and I cannot run your code.

I sat down this morning, and used this exact code connecting to a FIX Simulator server, it worked fine and had zero issues. My questions for you would be the following.
  1. How did you install Quickfix? Pip? Or Full Compile?
  2. What version of Python are you using and is it standard Python?
  3. Are you connecting to a development test bed server such as Fix Simulator? Or are you currently trying to connect to an exchange or broker?
  4. What version of FIX are you dealing with?

Will go by this piece by piece:

 
Code
$ python2 branzol.py settings.cfg
Your first error shows you telling it to use settings.cfg? If so that is wrong, the settings file was called in my script which is most likely why its throwing an error on the "SessionId". Unless you changed the code I posted and made it call in that method.

Your second question in regards to needing the Acceptor and Initiator lines of code.

 
Code
acceptor = fix.SocketAcceptor(application, storeFactory, settings, logFactory)
initiator  =  fix.SocketInitiator(application, storeFactory, settings, logFactory)
You do not need that. If you were using both an Acceptor and Initiator you would need two separate threads...etc. The easiest way to understand the Initiator / Acceptor concept is the Acceptor is the Exchange or Broker, and the Initiator is You. Yours might be different but on my current implementation with the Exchange I'm involved with it's just Initiator code.

The last question in regards to the spec folder. The only reason I called the spec folder in the code was to get to the dictionary files. In your settings.cfg you need to reference the dictionary files you will be using. You do not need the entire spec folder.

I could zip this example and send the directory structure for you, but you would have to ensure the exchange or broker is using the same version of FIX as my example is using. I can't zip my live code as its too heavily integrated into my solution for just that portion of code to work.

Please let me know if you need any additional help. Grasping FIX if you've never dealt with it before is most likely going to be one of the hardest things you've ever dealt with in programming. Once you do get a good understanding of it, its going to make a lot of sense.

Reply With Quote
The following user says Thank You to Branzol for this post:
 
  #7 (permalink)
Trading Apprentice
Sao Paulo, Brazil
 
Futures Experience: Intermediate
Platform: Metastock
Favorite Futures: Forex
 
Posts: 4 since Apr 2015
Thanks: 3 given, 1 received

How did you install Quickfix? Pip? Or Full Compile?

A: Full Compile from the git repository.

What version of Python are you using and is it standard Python?

A: My default version is $ python --version: Python 3.4.3
But I'm using $ python2 --version: Python 2.7.9

Are you connecting to a development test bed server such as Fix Simulator? Or are you currently trying to connect to an exchange or broker?

I'm trying to start in two terminals the Initiator and Acceptor to put then to communicate each other. I'm trying to develop a "Local Trading System"

What version of FIX are you dealing with?

FIXT.1.1 or FIX44.xml or FIX50.xml

I put:
SenderCompID=tw
TargetCompID=cpuid

I'll do more testings, but I'm trying to code both, the Initiator and Activator.

Thank you for helping.

Reply With Quote
 
  #8 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received

I'll sit down in the am and help you with an acceptor code. Now it makes more sense. This code was written to originally talk to a fixsim.com acceptor and finally transferred to Nadexs acceptor.

Reply With Quote
The following user says Thank You to Branzol for this post:
 
  #9 (permalink)
Trading Apprentice
Sao Paulo, Brazil
 
Futures Experience: Intermediate
Platform: Metastock
Favorite Futures: Forex
 
Posts: 4 since Apr 2015
Thanks: 3 given, 1 received

I was trying translate these examples from (C#, C++, etc) to Python, but sounds that the QuickFIX commands change and I cannot find useful information in the QuickFIX site.

mprabhat.com/2012/07/02/creating-a-fix-initiator-using-quickfixj/
codeproject.com/Articles/332405/Retrieve-MarketPrice-from-FixServer-using-QuickFix
codeproject.com/Articles/429147/The-FIX-client-and-server-implementation-using-Qui
codeproject.com/Articles/145174/Fix-Message-Implementation-using-QuickFix

Reply With Quote
 
  #10 (permalink)
Elite Member
Baltimore Maryland/United States
 
Futures Experience: Intermediate
Platform: Sierrachart/Custom Python Solution
Broker/Data: IB/Nadex/IQFeed/RealTick
Favorite Futures: NK, YM, CL, NG
 
Branzol's Avatar
 
Posts: 36 since May 2014
Thanks: 42 given, 54 received



TraderFXUS View Post
I was trying translate these examples from (C#, C++, etc) to Python, but sounds that the QuickFIX commands change and I cannot find useful information in the QuickFIX site.

mprabhat.com/2012/07/02/creating-a-fix-initiator-using-quickfixj/
codeproject.com/Articles/332405/Retrieve-MarketPrice-from-FixServer-using-QuickFix
codeproject.com/Articles/429147/The-FIX-client-and-server-implementation-using-Qui
codeproject.com/Articles/145174/Fix-Message-Implementation-using-QuickFix

The commands you find in any of the Quickfix implementations will work. You just have to format them for use in Python. Below you'll find a very, I repeat very rough implementation of an initiator and Acceptor. The Acceptor is just the example executor code from the Quickfix website. The executor was written for 4/4.1/4.2 FIX so I had to switch the initiator to 4.2 for now as I don't have the time this morning to switch out the syntax to 5.0SP2.

Steps are as follows:
  1. Unzip the folder.
  2. Run acceptor.py with no arguments after it.
  3. Run initiator.py with the argument initiatorsettings.cfg
  4. If you check the logs you will see there are communicating and connecting. The Acceptor also will show the messages flowing in.

Please note currently it's throwing a message about the order being in the wrong format. I'm sure that is due to the previous order was in FIX 5.0SP2 format and it needs to be converted to FIX 4.2 or the executor needs to be formatted for FIX 5.0. I can take a look at that but it's not going to be until tomorrow evening. I just wanted to give you enough code to play around with today and somewhat see how this all functions.

Is there anything your really struggling to understand when it comes to Quickfix and FIX?

Attached Files
Register to download File Type: zip QuickFix Tests.zip (266.7 KB, 79 views)
Reply With Quote
The following user says Thank You to Branzol for this post:

Reply



futures.io > > > > Python and QuickFIX

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

2016 True Edge Awards: Voting is open until Jan 21!

Now
 

An Afternoon with FIO member Massive l

Elite only
 

Anthony Drager: Pulling the trigger with confidence

Elite only
 

Leo Murphy: The Art and Science of Technical Analysis

Elite only
 

FuturesTrader71: Ask Me Anything

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
CQG API and Python Branzol Elite Automated Trading 2 December 6th, 2016 09:37 PM
IB with Python jamesico Matlab, R project and Python 2 September 9th, 2014 08:19 AM
Tradier with R and or Python Keith Leesus Matlab, R project and Python 2 September 5th, 2014 01:23 AM
Python Help Wanted $ wlblount Hire a Consultant or Programmer 0 December 25th, 2013 10:14 AM
QuickFIX "layer" for Ninja? heech NinjaTrader Programming 11 April 8th, 2010 09:44 PM


All times are GMT -4. The time now is 12:49 AM.

no new posts
Page generated 2017-01-18 in 0.17 seconds with 20 queries on phoenix via your IP 54.163.149.151