### Session Initialization and SSL Connection Source: https://context7.com/spotware/fix-api-sample/llms.txt This section details how to establish secure SSL connections to cTrader's price and trade servers, including certificate validation. ```APIDOC ## Session Initialization and SSL Connection Establishing secure SSL connections to cTrader price and trade servers with certificate validation. ### Method ```csharp // Server configuration string host = "h28.p.ctrader.com"; int pricePort = 5211; int tradePort = 5212; string username = "3006156"; string password = "sp0tw@re"; string senderCompID = "sales.3006156"; string senderSubID = "3006156"; string targetCompID = "CSERVER"; // Initialize price feed connection TcpClient priceClient = new TcpClient(host, pricePort); SslStream priceStreamSSL = new SslStream( priceClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); priceStreamSSL.AuthenticateAsClient(host); // Initialize trade connection TcpClient tradeClient = new TcpClient(host, tradePort); SslStream tradeStreamSSL = new SslStream( tradeClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); tradeStreamSSL.AuthenticateAsClient(host); // Certificate validation callback static bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { if (sslPolicyErrors == SslPolicyErrors.None) return true; Console.WriteLine("Certificate error: {0}", sslPolicyErrors); return false; } // Initialize message constructor MessageConstructor messageConstructor = new MessageConstructor( host, username, password, senderCompID, senderSubID, targetCompID ); ``` ### Parameters This section does not define specific API parameters as it focuses on code initialization. ``` -------------------------------- ### Initialize FIX API Sessions and SSL Connections in C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Initializes secure SSL connections to cTrader's price and trade servers. It configures server details, establishes TCP connections, authenticates using SSL, and includes a certificate validation callback. This is a prerequisite for any FIX communication. ```csharp using FIX_API_Library; using System.Net.Security; using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; // Server configuration string host = "h28.p.ctrader.com"; int pricePort = 5211; int tradePort = 5212; string username = "3006156"; string password = "sp0tw@re"; string senderCompID = "sales.3006156"; string senderSubID = "3006156"; string targetCompID = "CSERVER"; // Initialize price feed connection TcpClient priceClient = new TcpClient(host, pricePort); SslStream priceStreamSSL = new SslStream( priceClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); priceStreamSSL.AuthenticateAsClient(host); // Initialize trade connection TcpClient tradeClient = new TcpClient(host, tradePort); SslStream tradeStreamSSL = new SslStream( tradeClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); tradeStreamSSL.AuthenticateAsClient(host); // Certificate validation callback static bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { if (sslPolicyErrors == SslPolicyErrors.None) return true; Console.WriteLine("Certificate error: {0}", sslPolicyErrors); return false; } // Initialize message constructor MessageConstructor messageConstructor = new MessageConstructor( host, username, password, senderCompID, senderSubID, targetCompID ); ``` -------------------------------- ### Session Logon Source: https://context7.com/spotware/fix-api-sample/llms.txt This section explains how to authenticate with the FIX server and establish trading sessions, including sending logon messages and handling responses. ```APIDOC ## Session Logon Authenticate with the FIX server and establish a trading session with heartbeat interval. ### Method ```csharp int messageSequenceNumber = 1; // Create logon message for price feed session string logonMessage = messageConstructor.LogonMessage( MessageConstructor.SessionQualifier.QUOTE, // Session type messageSequenceNumber, // Sequence number 30, // Heartbeat interval (seconds) false // Reset sequence number ); // Send message and receive response byte[] byteArray = Encoding.ASCII.GetBytes(logonMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response format: // 8=FIX.4.4|9=XXX|35=A|49=CSERVER|56=sales.3006156|...98=0|108=30|10=XXX| // Logon for trade session string tradeLogonMessage = messageConstructor.LogonMessage( MessageConstructor.SessionQualifier.TRADE, // Trade session messageSequenceNumber, 30, false ); yteArray = Encoding.ASCII.GetBytes(tradeLogonMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); tradeStreamSSL.Read(buffer, 0, 1024); messageSequenceNumber++; ``` ### Parameters #### Query Parameters * **messageSequenceNumber** (integer) - Required - The sequence number for the FIX message. * **heartbeatInterval** (integer) - Required - The heartbeat interval in seconds. * **resetSequenceNumber** (boolean) - Required - Indicates whether to reset the sequence number. #### Request Body This endpoint does not use a request body. ### Response #### Success Response (200) * **response** (string) - The FIX protocol response message from the server. #### Response Example ``` 8=FIX.4.4|9=XXX|35=A|49=CSERVER|56=sales.3006156|...98=0|108=30|10=XXX| ``` ``` -------------------------------- ### Market Data Request - Spot Subscription Source: https://context7.com/spotware/fix-api-sample/llms.txt Subscribe to spot prices (top of book) for real-time pricing updates. ```APIDOC ## Market Data Request - Spot Subscription ### Description Subscribe to spot prices (top of book) for real-time pricing updates. ### Method POST (implied by sending data to a stream) ### Endpoint N/A (Stream-based connection) ### Parameters N/A ### Request Example ```csharp // Subscribe to spot market data for EURUSD string spotMarketDataRequest = messageConstructor.MarketDataRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, "EURUSD:WDqsoT", // Market data request ID 1, // 1 = Snapshot plus updates (subscribe) 1, // 1 = Spot subscription (top of book only) 0, // Market data entry type 1, // Number of symbols 1 // Symbol identifier ); byte[] byteArray = Encoding.ASCII.GetBytes(spotMarketDataRequest); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200 OK) Incremental refresh messages containing only the best bid and ask prices. #### Response Example ``` 35=X|262=EURUSD:WDqsoT|268=1|279=0|269=0|270=1.08500|271=1000000| ``` ``` -------------------------------- ### Test Request Source: https://context7.com/spotware/fix-api-sample/llms.txt Request a heartbeat response from the server to verify connection status. ```APIDOC ## Test Request ### Description Request a heartbeat response from the server to verify connection status. ### Method POST (implied by sending data to a stream) ### Endpoint N/A (Stream-based connection) ### Parameters N/A ### Request Example ```csharp int testRequestID = 1; // Create test request message string testRequestMessage = messageConstructor.TestRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, testRequestID ); testRequestID++; // Send and receive response byte[] byteArray = Encoding.ASCII.GetBytes(testRequestMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200 OK) A heartbeat message (35=0) is expected in response, containing the TestReqID (112=X). #### Response Example ``` 8=FIX.4.4|9=XXX|35=0|49=CSERVER|56=sales.3006156|57=QUOTE|50=3006156|34=X|52=YYYYMMDD-HH:mm:ss|112=1|10=XXX| ``` ``` -------------------------------- ### Initialize FIX Message Constructor Source: https://context7.com/spotware/fix-api-sample/llms.txt Creates and configures the MessageConstructor object with cTrader trading credentials. This object is used to generate FIX messages with proper formatting, including FIX version, body length, timestamps, sequence numbers, and checksums. ```csharp using FIX_API_Library; // Initialize with cTrader credentials MessageConstructor messageConstructor = new MessageConstructor( "h28.p.ctrader.com", // Host "3006156", // Username (trader login) "sp0tw@re", // Password "sales.3006156", // Sender CompID (broker.login) "3006156", // Sender SubID (trader login) "CSERVER" // Target CompID (always CSERVER) ); // All messages constructed will include these credentials in the header // Messages are automatically formatted with: // - FIX 4.4 protocol version // - Proper body length calculation // - UTC timestamps // - Sequence numbers // - Checksum validation ``` -------------------------------- ### Place Limit Order (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Constructs and sends a New Order Single message for a limit order. This order executes only at the specified price or better. It includes all parameters for a market order plus a limit price. The response is an ExecutionReport, which can indicate new, partially filled, filled, or cancelled status depending on market conditions and the time-in-force directive. ```csharp // Place limit buy order at 1.08000 string limitOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "10", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, // Order quantity 2, // Order type: 2=Limit "3", // Time in force: 3=IOC 1.08m // Limit price ); byte[] byteArray = Encoding.ASCII.GetBytes(limitOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: ExecutionReport with: // 39=0 (New) if order is accepted and pending // 39=1 (PartiallyFilled) if partially executed // 39=2 (Filled) if fully executed // 39=4 (Cancelled) if cannot be filled and IOC is used // 44=1.08 (echoed limit price) ``` -------------------------------- ### Request for Positions Source: https://context7.com/spotware/fix-api-sample/llms.txt Query all open positions or a specific position by its unique position ID. The response provides details about open positions, including position ID, symbol, quantity, and average price. ```APIDOC ## GET /positions ### Description Query all open positions or a specific position by position ID. ### Method GET ### Endpoint /positions ### Parameters #### Query Parameters - **requestID** (string) - Required - A unique identifier for the request. - **positionID** (string) - Optional - The unique identifier for a specific position. ### Request Example ```csharp // Request all open positions string positionsRequest = messageConstructor.RequestForPositions( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471" // Unique request ID ); // Request specific position by ID string specificPositionRequest = messageConstructor.RequestForPositions( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471", // Request ID "POS123456" // Specific position ID ); byte[] byteArray = Encoding.ASCII.GetBytes(positionsRequest); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200) - **requestID** (string) - The unique identifier for the request. - **positionID** (string) - The unique identifier for the position. - **symbol** (string) - The trading symbol. - **totalReports** (integer) - Total number of position reports. - **validRequest** (integer) - Indicates if the request was valid (0 for valid, 2 for no positions found). - **longQty** (number) - Volume for BUY positions. - **shortQty** (number) - Volume for SELL positions. - **avgPrice** (number) - The average price of the position. #### Response Example ```json { "requestID": "1408471", "positionID": "POS123456", "symbol": "EURUSD", "totalReports": 1, "validRequest": 0, "longQty": 1000, "shortQty": 0, "avgPrice": 1.12345 } ``` **Valid Request Indicators:** - 728=0 means valid request - 728=2 means no positions found ``` -------------------------------- ### Market Data Request - Depth Subscription Source: https://context7.com/spotware/fix-api-sample/llms.txt Subscribe to full order book depth for a trading instrument. ```APIDOC ## Market Data Request - Depth Subscription ### Description Subscribe to full order book depth for a trading instrument. ### Method POST (implied by sending data to a stream) ### Endpoint N/A (Stream-based connection) ### Parameters N/A ### Request Example ```csharp // Subscribe to market depth for EURUSD string marketDataRequest = messageConstructor.MarketDataRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, "EURUSD:WDqsoT", // Market data request ID (instrument symbol) 1, // Subscription type: 1=Subscribe, 2=Unsubscribe 0, // Market depth: 0=Full depth, 1=Spot only 0, // Market data entry type 1, // Number of related symbols 1 // Symbol ID ); // Send request byte[] byteArray = Encoding.ASCII.GetBytes(marketDataRequest); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200 OK) MarketDataIncrementalRefresh (35=X) messages containing order book depth information. #### Response Example ``` 35=X|262=EURUSD:WDqsoT|268=X|279=0|269=0|278=XXX|55=1|270=price|271=size| 269=0 is BID and 269=1 is OFFER ``` ``` -------------------------------- ### Send Test Request - C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Sends a test request message to the server to verify connection status and elicit a heartbeat response. The code creates the test request message, sends it, and then attempts to read a response from the price stream. It uses a small delay before reading the response. ```csharp int testRequestID = 1; // Create test request message string testRequestMessage = messageConstructor.TestRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, testRequestID ); testRequestID++; // Send and receive response byte[] byteArray = Encoding.ASCII.GetBytes(testRequestMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: Heartbeat message (35=0) with TestReqID (112=X) ``` -------------------------------- ### Subscribe to Market Data Spot - C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Subscribes to spot prices (top of book) for real-time pricing updates for a trading instrument like 'EURUSD:WDqsoT'. This code sends a market data request with subscription type 1 and market depth 1 (spot only). It then reads the incremental refresh messages containing the best bid and ask prices. ```csharp // Subscribe to spot market data for EURUSD string spotMarketDataRequest = messageConstructor.MarketDataRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, "EURUSD:WDqsoT", // Market data request ID 1, // 1 = Snapshot plus updates (subscribe) 1, // 1 = Spot subscription (top of book only) 0, // Market data entry type 1, // Number of symbols 1 // Symbol identifier ); byte[] byteArray = Encoding.ASCII.GetBytes(spotMarketDataRequest); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Incremental refresh messages will contain only best bid/ask: // 35=X|262=EURUSD:WDqsoT|268=1|279=0|269=0|270=1.08500|271=1000000| ``` -------------------------------- ### Place Market Order (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Constructs and sends a New Order Single message for a market order to the FIX API. This order is executed immediately at the best available price. It requires session qualifiers, message sequence number, client order ID, symbol ID, side, transaction time, order quantity, order type, and time in force. The response is an ExecutionReport indicating the order status. ```csharp // Place market buy order string newOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy, 2=Sell DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), // Transaction time 1000, // Order quantity (1000 = 0.01 lots) 1, // Order type: 1=Market "1" // Time in force: 1=GTC (ignored for market) ); // Send order byte[] byteArray = Encoding.ASCII.GetBytes(newOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: ExecutionReport (35=8) with: // 37=ServerOrderID|11=1408471|150=F|39=2|55=1|54=1|60=timestamp|6=avgPrice|38=1000|14=1000|721=positionID| // Where 39=2 indicates FILLED status ``` -------------------------------- ### Place Stop Order (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Constructs and sends a New Order Single message for a stop order. This order becomes a market order once the specified stop price is reached. The message includes parameters for a limit order but uses a stop price instead of a limit price. The initial response is an ExecutionReport with a 'New' status, followed by another report upon triggering. ```csharp // Place stop buy order at 1.08000 string stopOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "10", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, // Order quantity 3, // Order type: 3=Stop "3", // Time in force 0, // Price (not used for stop) 1.08m // Stop price ); byte[] byteArray = Encoding.ASCII.GetBytes(stopOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: ExecutionReport with: // 39=0 (New) when stop order is pending // 99=1.08 (stop price echoed back) // Once triggered, will receive another ExecutionReport with execution details ``` -------------------------------- ### Subscribe to Market Data Depth - C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Subscribes to full order book depth for a specified trading instrument, such as 'EURUSD:WDqsoT'. The code constructs a market data request message with subscription type 1 (subscribe) and market depth 0 (full depth). It then sends the request and reads the subsequent incremental refresh messages. ```csharp // Subscribe to market depth for EURUSD string marketDataRequest = messageConstructor.MarketDataRequestMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, "EURUSD:WDqsoT", // Market data request ID (instrument symbol) 1, // Subscription type: 1=Subscribe, 2=Unsubscribe 0, // Market depth: 0=Full depth, 1=Spot only 0, // Market data entry type 1, // Number of related symbols 1 // Symbol ID ); // Send request byte[] byteArray = Encoding.ASCII.GetBytes(marketDataRequest); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: MarketDataIncrementalRefresh (35=X) messages with: // 262=EURUSD:WDqsoT|268=X|279=0|269=0|278=XXX|55=1|270=price|271=size| // Where 269=0 is BID and 269=1 is OFFER ``` -------------------------------- ### Query Order Status by Client Order ID (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Requests the current status of a specific order using its client order ID. It sends an OrderStatusRequest message and parses the incoming ExecutionReport for the order's state, remaining quantity, and filled quantity. Dependencies include System.Text.Encoding and System.Threading.Thread. ```csharp // Request status for order with client ID "1408471" string orderStatusRequest = messageConstructor.OrderStatusRequest( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471" // Client order ID ); byte[] byteArray = Encoding.ASCII.GetBytes(orderStatusRequest); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: ExecutionReport (35=8) with current order state: // 11=1408471|39=X| // Where 39 values: // 0=New, 1=PartiallyFilled, 2=Filled, 4=Cancelled, 8=Rejected, C=Expired // 151=leavesQty (remaining quantity) // 14=cumQty (filled quantity) ``` -------------------------------- ### Send FIX Logon Messages for Trading Sessions in C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Constructs and sends FIX logon messages for both price and trade sessions. It utilizes a MessageConstructor to format the logon messages, including sequence numbers and heartbeat intervals, then sends them over the established SSL streams. Handles basic response reading and sequence number incrementing. ```csharp int messageSequenceNumber = 1; // Create logon message for price feed session string logonMessage = messageConstructor.LogonMessage( MessageConstructor.SessionQualifier.QUOTE, // Session type messageSequenceNumber, // Sequence number 30, // Heartbeat interval (seconds) false // Reset sequence number ); // Send message and receive response byte[] byteArray = Encoding.ASCII.GetBytes(logonMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response format: // 8=FIX.4.4|9=XXX|35=A|49=CSERVER|56=sales.3006156|...98=0|108=30|10=XXX| // Logon for trade session string tradeLogonMessage = messageConstructor.LogonMessage( MessageConstructor.SessionQualifier.TRADE, // Trade session messageSequenceNumber, 30, false ); byteArray = Encoding.ASCII.GetBytes(tradeLogonMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); tradeStreamSSL.Read(buffer, 0, 1024); messageSequenceNumber++; ``` -------------------------------- ### Request All Open Positions or Specific Position by ID (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Fetches all open positions or a specific position identified by its position ID. The code sends a RequestForPositions message and processes the incoming PositionReport messages, extracting details like position ID, symbol, quantity, and average price. It handles cases where no positions are found. ```csharp // Request all open positions string positionsRequest = messageConstructor.RequestForPositions( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471" // Unique request ID ); byte[] byteArray = Encoding.ASCII.GetBytes(positionsRequest); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Expected response: PositionReport (35=AP) messages: // 710=1408471|721=positionID|727=totalReports|728=0|55=symbolID|702=1|704=longQty|705=shortQty|730=avgPrice| // Where: // 728=0 means valid request (728=2 means no positions found) // 704=volume for BUY positions (705=0) // 705=volume for SELL positions (704=0) // Request specific position by ID string specificPositionRequest = messageConstructor.RequestForPositions( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471", // Request ID "POS123456" // Specific position ID ); ``` -------------------------------- ### Resend Request Source: https://context7.com/spotware/fix-api-sample/llms.txt Request retransmission of messages within a specified sequence number range. This is useful for recovering lost or out-of-order messages. ```APIDOC ## POST /messages/resend ### Description Request retransmission of messages within a specific sequence number range. ### Method POST ### Endpoint /messages/resend ### Parameters #### Request Body - **sessionQualifier** (string) - Required - The session qualifier (e.g., TRADE, QUOTE). - **messageSequenceNumber** (integer) - Required - The current message sequence number. - **endSequenceNumber** (integer) - Required - The sequence number up to which messages should be resent. ### Request Example ```csharp // Request resend of previous message string resendMessage = messageConstructor.ResendMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, messageSequenceNumber - 1 // End sequence number to resend ); byte[] byteArray = Encoding.ASCII.GetBytes(resendMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200) - **PossDupFlag** (string) - Indicates if the message is a duplicate ('Y'). - **messageType** (string) - The type of the resent message (e.g., 2 for heartbeat, 8 for execution report). - **endSeqNo** (integer) - The last sequence number in the resent range. #### Response Example ```json { "PossDupFlag": "Y", "messageType": "8", "endSeqNo": 123 } ``` ``` -------------------------------- ### Order Status Request Source: https://context7.com/spotware/fix-api-sample/llms.txt Query the current status of a specific order using its client order ID. The response includes the order's execution report with its current state, remaining quantity, and filled quantity. ```APIDOC ## GET /order/status ### Description Query the current status of a specific order by client order ID. ### Method GET ### Endpoint /order/status ### Parameters #### Query Parameters - **clientOrderID** (string) - Required - The unique identifier for the client's order. ### Request Example ```csharp // Request status for order with client ID "1408471" string orderStatusRequest = messageConstructor.OrderStatusRequest( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471" // Client order ID ); byte[] byteArray = Encoding.ASCII.GetBytes(orderStatusRequest); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); tradeStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200) - **clientOrderID** (string) - The client order ID. - **orderStatus** (string) - The current state of the order (e.g., New, PartiallyFilled, Filled, Cancelled, Rejected, Expired). - **leavesQty** (number) - The remaining quantity of the order. - **cumQty** (number) - The filled quantity of the order. #### Response Example ```json { "clientOrderID": "1408471", "orderStatus": "1", "leavesQty": 10.5, "cumQty": 5.0 } ``` **Status Codes:** - 0=New - 1=PartiallyFilled - 2=Filled - 4=Cancelled - 8=Rejected - C=Expired ``` -------------------------------- ### New Order Single - Limit Order Source: https://context7.com/spotware/fix-api-sample/llms.txt Places a limit order that executes only at the specified price or better. This order type is used when a specific entry or exit price is desired. ```APIDOC ## New Order Single - Limit Order ### Description Place a limit order that executes only at specified price or better. ### Method POST (Implied, as it sends a message) ### Endpoint Not explicitly defined, as this is a FIX message-based interaction. ### Parameters #### Request Body (FIX Message Fields) - **8** (BeginString) - Required - FIX protocol version. - **35** (MsgType) - Required - Message type, 'D' for New Order - Single. - **11** (ClOrdID) - Required - Client Order ID. - **55** (Symbol) - Required - Symbol ID. - **54** (Side) - Required - Order side: 1=Buy. - **59** (TimeInForce) - Required - Time in force: 3=IOC (Immediate Or Cancel). - **60** (TransactTime) - Required - Transaction time in UTC. - **38** (OrderQty) - Required - Order quantity. - **40** (OrdType) - Required - Order type: 2=Limit. - **44** (Price) - Required - Limit price. ### Request Example ```csharp // Place limit buy order at 1.08000 string limitOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "10", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, // Order quantity 2, // Order type: 2=Limit "3", // Time in force: 3=IOC 1.08m // Limit price ); byte[] byteArray = Encoding.ASCII.GetBytes(limitOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); ``` ### Response #### Success Response (ExecutionReport - 35=8) - **11** (ClOrdID) - Client Order ID. - **39** (ExecType) - Execution type: 0=New (pending), 1=PartiallyFilled, 2=Filled, 4=Cancelled (for IOC). - **44** (Price) - Echoed limit price. #### Response Example `39=0 (New) if order is accepted and pending` `39=1 (PartiallyFilled) if partially executed` `39=2 (Filled) if fully executed` `39=4 (Cancelled) if cannot be filled and IOC is used` `44=1.08 (echoed limit price)` ``` -------------------------------- ### Send Reject Message for Unprocessable Message (C#) Source: https://context7.com/spotware/fix-api-sample/llms.txt Sends a reject message when an invalid or unprocessable FIX message is received. This ensures proper error handling and communication with the trading partner. The reject message includes the reference sequence number of the message being rejected. ```csharp // Reject message with sequence number 0 string rejectMessage = messageConstructor.RejectMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, 0 // Reference sequence number being rejected ); byte[] byteArray = Encoding.ASCII.GetBytes(rejectMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; // Message format: 35=3|45=refSeqNum| ``` -------------------------------- ### New Order Single - Market Order Source: https://context7.com/spotware/fix-api-sample/llms.txt Places a market order for immediate execution at the best available price. This order type is suitable for urgent trades where price is less critical than speed of execution. ```APIDOC ## New Order Single - Market Order ### Description Place a market order for immediate execution at the best available price. ### Method POST (Implied, as it sends a message) ### Endpoint Not explicitly defined, as this is a FIX message-based interaction. ### Parameters #### Request Body (FIX Message Fields) - **8** (BeginString) - Required - FIX protocol version. - **35** (MsgType) - Required - Message type, 'D' for New Order - Single. - **11** (ClOrdID) - Required - Client Order ID. - **55** (Symbol) - Required - Symbol ID. - **54** (Side) - Required - Order side: 1=Buy, 2=Sell. - **59** (TimeInForce) - Required - Time in force: 1=GTC (ignored for market). - **60** (TransactTime) - Required - Transaction time in UTC. - **38** (OrderQty) - Required - Order quantity. - **40** (OrdType) - Required - Order type: 1=Market. ### Request Example ```csharp // Place market buy order string newOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "1408471", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy, 2=Sell DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), // Transaction time 1000, // Order quantity (1000 = 0.01 lots) 1, // Order type: 1=Market "1" // Time in force: 1=GTC (ignored for market) ); // Send order byte[] byteArray = Encoding.ASCII.GetBytes(newOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); ``` ### Response #### Success Response (ExecutionReport - 35=8) - **37** (ServerOrderID) - Server-assigned order ID. - **11** (ClOrdID) - Client Order ID. - **39** (ExecType) - Execution type: 2=FILLED. - **6** (AvgPx) - Average execution price. - **14** (LastQty) - Last executed quantity. - **38** (OrderQty) - Original order quantity. - **55** (Symbol) - Symbol. - **54** (Side) - Side. - **60** (TransactTime) - Transaction time. - **721** (PositionID) - Position ID. #### Response Example `35=8|37=ServerOrderID|11=1408471|150=F|39=2|55=1|54=1|60=timestamp|6=avgPrice|38=1000|14=1000|721=positionID|` ``` -------------------------------- ### Reject Message Source: https://context7.com/spotware/fix-api-sample/llms.txt Send a reject message to indicate that a received message is invalid or could not be processed. This includes the reference sequence number of the rejected message. ```APIDOC ## POST /messages/reject ### Description Send a reject message for an invalid or unprocessable message received. ### Method POST ### Endpoint /messages/reject ### Parameters #### Request Body - **sessionQualifier** (string) - Required - The session qualifier (e.g., TRADE, QUOTE). - **messageSequenceNumber** (integer) - Required - The current message sequence number. - **referenceSequenceNumber** (integer) - Required - The sequence number of the message being rejected. ### Request Example ```csharp // Reject message with sequence number 0 string rejectMessage = messageConstructor.RejectMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber, 0 // Reference sequence number being rejected ); byte[] byteArray = Encoding.ASCII.GetBytes(rejectMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); messageSequenceNumber++; ``` ### Response #### Success Response (200) - **messageType** (string) - Always '3' for a reject message. - **referenceSequenceNumber** (integer) - The sequence number of the rejected message. #### Response Example ```json { "messageType": "3", "referenceSequenceNumber": 0 } ``` ``` -------------------------------- ### New Order Single - Stop Order Source: https://context7.com/spotware/fix-api-sample/llms.txt Places a stop order that automatically converts into a market order once the stop price is reached. This is used to enter or exit a position once a certain price level is breached. ```APIDOC ## New Order Single - Stop Order ### Description Place a stop order that becomes market order when stop price is reached. ### Method POST (Implied, as it sends a message) ### Endpoint Not explicitly defined, as this is a FIX message-based interaction. ### Parameters #### Request Body (FIX Message Fields) - **8** (BeginString) - Required - FIX protocol version. - **35** (MsgType) - Required - Message type, 'D' for New Order - Single. - **11** (ClOrdID) - Required - Client Order ID. - **55** (Symbol) - Required - Symbol ID. - **54** (Side) - Required - Order side: 1=Buy. - **59** (TimeInForce) - Required - Time in force. - **60** (TransactTime) - Required - Transaction time in UTC. - **38** (OrderQty) - Required - Order quantity. - **40** (OrdType) - Required - Order type: 3=Stop. - **99** (StopPx) - Required - Stop price. ### Request Example ```csharp // Place stop buy order at 1.08000 string stopOrderMessage = messageConstructor.NewOrderSingleMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber, "10", // Client order ID 1, // Symbol ID 1, // Side: 1=Buy DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, // Order quantity 3, // Order type: 3=Stop "3", // Time in force 0, // Price (not used for stop) 1.08m // Stop price ); byte[] byteArray = Encoding.ASCII.GetBytes(stopOrderMessage); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); ``` ### Response #### Success Response (ExecutionReport - 35=8) - **39** (ExecType) - Execution type: 0=New (pending). - **99** (StopPx) - Stop price echoed back. #### Response Example `39=0 (New) when stop order is pending` `99=1.08 (stop price echoed back)` `Once triggered, will receive another ExecutionReport with execution details` ``` -------------------------------- ### Logout FIX Session and Close Connection Source: https://context7.com/spotware/fix-api-sample/llms.txt Gracefully terminates the FIX session for both price and trade streams, sends logout messages, and closes the associated network connections. It resets the sequence number after logout and expects a logout acknowledgment. ```csharp // Send logout message for price session string logoutMessage = messageConstructor.LogoutMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber ); byte[] byteArray = Encoding.ASCII.GetBytes(logoutMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); byte[] buffer = new byte[1024]; Thread.Sleep(100); priceStreamSSL.Read(buffer, 0, 1024); string response = Encoding.ASCII.GetString(buffer); // Reset sequence number after logout messageSequenceNumber = 1; // Expected response: Logout acknowledgment (35=5) // Message format: 8=FIX.4.4|...|35=5|... // Logout from trade session string tradeLogoutMessage = messageConstructor.LogoutMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber ); byteArray = Encoding.ASCII.GetBytes(tradeLogoutMessage); tradeStreamSSL.Write(byteArray, 0, tradeLogoutMessage.Length); tradeStreamSSL.Read(buffer, 0, 1024); // Close connections priceStreamSSL.Close(); priceClient.Close(); tradeStreamSSL.Close(); tradeClient.Close(); ``` -------------------------------- ### Send Heartbeat Messages - C# Source: https://context7.com/spotware/fix-api-sample/llms.txt Sends periodic heartbeat messages to maintain session connectivity for both price and trade sessions. The code constructs the heartbeat message using a message constructor and then sends it as a byte array over SSL streams. It increments the message sequence number after each send. ```csharp // Create heartbeat message for price session string heartbeatMessage = messageConstructor.HeartbeatMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber ); // Send without waiting for response byte[] byteArray = Encoding.ASCII.GetBytes(heartbeatMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); messageSequenceNumber++; // Expected message format: // 8=FIX.4.4|9=XXX|35=0|49=sales.3006156|56=CSERVER|57=QUOTE|50=3006156|34=X|52=YYYYMMDD-HH:mm:ss|10=XXX| // Heartbeat for trade session string tradeHeartbeat = messageConstructor.HeartbeatMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber ); byteArray = Encoding.ASCII.GetBytes(tradeHeartbeat); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); messageSequenceNumber++; ``` -------------------------------- ### Heartbeat Message Source: https://context7.com/spotware/fix-api-sample/llms.txt Send periodic heartbeat messages to maintain session connectivity for both price and trade sessions. ```APIDOC ## Heartbeat Message ### Description Send periodic heartbeat messages to maintain session connectivity. ### Method POST (implied by sending data to a stream) ### Endpoint N/A (Stream-based connection) ### Parameters N/A ### Request Example ```csharp // Create heartbeat message for price session string heartbeatMessage = messageConstructor.HeartbeatMessage( MessageConstructor.SessionQualifier.QUOTE, messageSequenceNumber ); // Send without waiting for response byte[] byteArray = Encoding.ASCII.GetBytes(heartbeatMessage); priceStreamSSL.Write(byteArray, 0, byteArray.Length); messageSequenceNumber++; // Heartbeat for trade session string tradeHeartbeat = messageConstructor.HeartbeatMessage( MessageConstructor.SessionQualifier.TRADE, messageSequenceNumber ); byteArray = Encoding.ASCII.GetBytes(tradeHeartbeat); tradeStreamSSL.Write(byteArray, 0, byteArray.Length); messageSequenceNumber++; ``` ### Response #### Success Response (200 OK) Heartbeat messages are typically acknowledged implicitly by the server maintaining the connection. No explicit success response body is detailed. #### Response Example ``` 8=FIX.4.4|9=XXX|35=0|49=sales.3006156|56=CSERVER|57=QUOTE|50=3006156|34=X|52=YYYYMMDD-HH:mm:ss|10=XXX| ``` ```