Need help in quickfix, python

Created at 31 Dec 2016, 07:17
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SM

smuttynutty

Joined 21.03.2016

Need help in quickfix, python
31 Dec 2016, 07:17


Hi,

Grabbed this code from futures.io
I have no idea however how to make it send the Username tag on Logon.
Setting Message Type (A) Fields in FIX44.xml doesn't do a thing, 
so where it's at?

Output is
8=FIX.4.4 9=81 35=A 34=1 49=**.** 52=20161231-
04:02:16.063 56=cServer 98=0 108=30 141=Y 10=215

So it's lacking 553,554, and I think 57, how do one add them?

Thanks a million

 

import sys
import time
import thread
import argparse
from datetime import datetime
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):
        return

    def fromAdmin(self, sessionID, message):
        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.getHeader().setField(fix.BeginString(fix.BeginString_FIX44)) #
        trade.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle)) #39=D
        trade.setField(fix.ClOrdID(self.genExecID())) #11=Unique order

        trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) #21=3 (Manual order, best executiona)
        trade.setField(fix.Symbol('SMBL')) #55=SMBL
        trade.setField(fix.Side(fix.Side_BUY)) #43=1 Buy
        trade.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit order
        trade.setField(fix.OrderQty(100)) #38=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)

 


@smuttynutty
Replies

cyfer
01 Jan 2017, 02:09

I havn't coded in Python for years now so I can't really remember how to set this, but what I do in C# "should be the same in Python" is I send the User/Pass in the 

ToAdmin message , Like you set the Parameters of a message .. you set User/Pass in the "ToAdmin" message. 

public void ToAdmin(QuickFix.Message msg, SessionID sessionID)
        {
            if (msg.ToString().Contains("35=A"))// Use Message Cracker instead of this
            {               
                msg.SetField(new QuickFix.Fields.TargetSubID("Quote"));
                msg.SetField(new QuickFix.Fields.Username("*********"));
                msg.SetField(new QuickFix.Fields.Password("*****"));
            }
        }

TargetSubID is another Field that no connections worked without forcing that field in ToAdmin message .. setting this field in the settings file didn't work .. woked only like that. 

ToAdmin message is  Admin level messages so any message from Client side (You) will go through this message . 

FromAdmin is the opposite .. any level from admin(Auth messages mainly) will be received through this message (you need to crack it.)

 


@cyfer

smuttynutty
01 Jan 2017, 05:38

Yes, great. I was strugling with it for two days.
Awfully grateful

I wish you so much money in New Year that IMF will cry their eyes out.

 

 

Python transcript

    def toAdmin(self, message, sessionID):
     msgType = fix.MsgType ()
     message.getHeader ().getField (msgType)
     if (msgType.getValue () == fix.MsgType_Logon):
        message.setField(fix.Username('***'))
        message.setField(fix.Password('***'))
        message.setField(fix.TargetSubID('Quote'))
        return

 


@smuttynutty

cyfer
01 Jan 2017, 18:34

Ha ha :) 

Happy New Year 

 


@cyfer

beneditobvn
26 Mar 2018, 21:39 ( Updated at: 23 Jan 2024, 13:16 )

Hey [smuttynutty], do you have a Python script that works fine with this FIX API?


@beneditobvn