1require 'quickfix_ruby'
2
3class Application < Quickfix::Application
4
5	def initialize
6		super
7		@orderID = 0
8		@execID = 0
9	end
10
11	def onCreate(sessionID)
12	end
13
14	def onLogon(sessionID)
15	end
16
17	def onLogout(sessionID)
18	end
19
20	def toAdmin(sessionID, message)
21	end
22
23	def fromAdmin(sessionID, message)
24	end
25
26	def toApp(sessionID, message)
27	end
28
29	def fromApp(message, sessionID)
30
31		beginString = Quickfix::BeginString.new
32		msgType = Quickfix::MsgType.new
33		message.getHeader().getField( beginString )
34		message.getHeader().getField( msgType )
35
36		symbol = Quickfix::Symbol.new
37		side = Quickfix::Side.new
38		ordType = Quickfix::OrdType.new
39		orderQty = Quickfix::OrderQty.new
40		price = Quickfix::Price.new
41		clOrdID = Quickfix::ClOrdID.new
42		avgPx = Quickfix::AvgPx.new
43
44		message.getField( ordType )
45
46		if( ordType.getValue() != Quickfix.OrdType_LIMIT )
47			raise Quickfix::IncorrectTagValue.new( ordType.getField() )
48		end
49
50		message.getField( symbol )
51		message.getField( side )
52		message.getField( orderQty )
53		message.getField( price )
54		message.getField( clOrdID )
55
56		executionReport = Quickfix::Message.new
57		executionReport.getHeader().setField( beginString )
58		executionReport.getHeader().setField( Quickfix::MsgType.new(Quickfix.MsgType_ExecutionReport) )
59
60		executionReport.setField( Quickfix::OrderID.new(genOrderID()) )
61		executionReport.setField( Quickfix::ExecID.new(genExecID()) )
62		executionReport.setField( Quickfix::OrdStatus.new(Quickfix.OrdStatus_FILLED) )
63		executionReport.setField( symbol )
64		executionReport.setField( side )
65		executionReport.setField( Quickfix::CumQty.new(orderQty.getValue()) )
66		executionReport.setField( Quickfix::AvgPx.new(price.getValue()) )
67		executionReport.setField( Quickfix::LastShares.new(orderQty.getValue()) )
68		executionReport.setField( Quickfix::LastPx.new(price.getValue()) )
69		executionReport.setField( clOrdID )
70		executionReport.setField( orderQty )
71
72		if( beginString.getValue() == Quickfix.BeginString_FIX40 || beginString.getValue() == Quickfix.BeginString_FIX41 || beginString.getValue() == Quickfix.BeginString_FIX42 )
73			executionReport.setField( Quickfix::ExecTransType.new(Quickfix.ExecTransType_NEW) )
74		end
75
76		if( beginString.getValue() >= Quickfix.BeginString_FIX41 )
77			executionReport.setField( Quickfix::ExecType.new(Quickfix.ExecType_FILL) )
78			executionReport.setField( Quickfix::LeavesQty.new(0) )
79		end
80
81		begin
82			Quickfix::Session.sendToTarget( executionReport, sessionID )
83		rescue SessionNotFound
84			return
85		end
86	end
87
88	def genOrderID
89		@orderID = @orderID+1
90		return @orderID.to_s
91	end
92
93	def genExecID
94		@execID = @execID+1
95		return @execID.to_s
96	end
97end
98
99begin
100	file = ARGV[0]
101	settings = Quickfix::SessionSettings.new( file )
102	application = Application.new
103	storeFactory = Quickfix::FileStoreFactory.new( settings )
104	logFactory = Quickfix::ScreenLogFactory.new( settings )
105	acceptor = Quickfix::SocketAcceptor.new( application, storeFactory, settings, logFactory )
106        acceptor.start
107
108	while( true )
109		sleep(1)
110	end
111rescue Quickfix::ConfigError, Quickfix::RuntimeError => e
112	print e
113end
114