Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The following code is getting successful LOGON messages, but when i send quote-request in the 2nd message, its not replying & sending me a heartbeat after 30 seconds.Someone told me i'm not correctly calculating checksum, so that's why Fix-server ignoring the price-request message.Can you give me any clue why it's not working for the 2nd message?

What I have tried:

public static int total;

time = DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss.fff");

                                body = "35=V|49=" + FixID + "|56=CSERVER|34="+msgSeqenceNum+"|52=" + time + "|50=QUOTE|57=QUOTE|262=13203481x|263=1|264=1|265=1|146=1|55=1|267=2|269=0|269=1|";

                                // body length
                                bodylen = body.Length;
                                string fix = "8=FIX.4.4|9=" + bodylen + "|";

                                // message
                                message = fix + body;

                                message = message.Replace('|', '\u0001');
                            int total2 = 0;
                                messageBytes = Encoding.ASCII.GetBytes(message);//converted to 0's &1s
                                for (int i = 0; i < message.Length; i++)
                                    total2 += messageBytes[i];

                                checksum = total2 % 256;
                                checksumStr = checksum.ToString().PadLeft(3, '0');

                                sum = "10=" + checksumStr + "|";
                                m = fix + body + sum;
                                m = m.Replace('|', '\u0001');
Posted
Updated 27-Mar-17 22:03pm
v2
Comments
Graeme_Grant 28-Mar-17 2:56am    
What does the host service documentation say regarding the calculation?
Viswash Manik 28-Mar-17 3:49am    
Hi Graeme, the checksum value was calculated as per standard FIX-guidelines.

1 solution

Quote:
Can you give me any clue why it's not working for the 2nd message?


look where you have 'total' defined .. do you reset the value of total before you get the checksum for your second message ?

I would break out this code, to get the checksum

C#
message = message.Replace('|', '\u0001');
 byte[] messageBytes = Encoding.ASCII.GetBytes(message);
 for (int i = 0; i < message.Length; i++)
 total += messageBytes[i];

 int checksum = 0;
 foreach (byte chData in messageBytes)
 {
     checksum += chData;
 }

 checksum = total % 256;
 string checksumStr = checksum.ToString().PadLeft(3, '0');


into a separate function/procedure/method - you repeat the code, real bad idea - if you break that code out of each location you can then go

C#
checksumStr = getChecksum(message);


for example .. then you can remove this disaster
Quote:
public static int total;
from your client

While I'm here, I'll throw in my 2 cents worth on building messages like that by concatenating strings - its barely ok for a test - I'd expect someone working for me to make a message builder class, for each type of message (so it starts as an abstract class), that you pass the values to it and it builds the message nicely - your messages as they are are hard to extend, debug etc - I guess you're lucky you're not working for me :)
 
Share this answer
 
v3
Comments
Jochen Arndt 28-Mar-17 3:22am    
My 5.

I just miss a note about the useless foreach loop.
Viswash Manik 28-Mar-17 3:48am    
@garth ... hahahahahah, sorry for the mistakes sir. I didnt notice that. I started learning c# 2 weeks ago :P
Garth J Lancaster 28-Mar-17 4:52am    
no worries - we all have to learn sometime - if you had been using the debugger you might have looked at the second checksum calc and seen 'total' already had a value

I'd point out that you're also re-inventing the wheel - there's a library out there (well, a few), but "QuickFix' may make you're life easier http://www.quickfixengine.org/documentation.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900