FIX-API-Sample github error

Created at 08 Feb 2017, 13:58
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!
mindbreaker's avatar

mindbreaker

Joined 19.07.2014

FIX-API-Sample github error
08 Feb 2017, 13:58


Hi when i test Fix-api-sample when hit trade logon button i show this:

8=FIX.4.49=11035=A49=fxpro.995196056=cSERVER57=TRADE50=TRADE34=152=20170208-11:55:0498=0108=30553=9951960554=360410=148

// Fix back message:
8=FIX.4.49=16035=534=149=cSERVER50=TRADE52=20170208-11:55:04.94956=fxpro.995196057=TRADE58=TargetSubID is assigned with the unexpected value 'TRADE', expected 'QUOTE'10=199

And error :


@mindbreaker
Replies

mindbreaker
08 Feb 2017, 15:08 ( Updated at: 21 Dec 2023, 09:20 )

Spotware

Hello Spotware

Why example from Spotware github FIX-API-Sample https://github.com/spotware/FIX-API-Sample does not work  on your own servers !!!!

 


@mindbreaker

mindbreaker
08 Feb 2017, 17:04

FIX DLL
// Decompiled with JetBrains decompiler
// Type: FIX_API_Library.MessageConstructor
// Assembly: FIX API Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Users\KazikOratugorator\Desktop\FIX-API-Sample-master\FIX API Library.dll

using System;
using System.Text;

namespace FIX_API_Library
{
  public class MessageConstructor
  {
    private string _host;
    private string _username;
    private string _password;
    private string _senderCompID;
    private string _senderSubID;
    private string _targetCompID;

    public MessageConstructor(string host, string username, string password, string senderCompID, string senderSubID, string targetCompID)
    {
      this._host = host;
      this._username = username;
      this._password = password;
      this._senderCompID = senderCompID;
      this._senderSubID = senderSubID;
      this._targetCompID = targetCompID;
    }

    public string LogonMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int heartBeatSeconds, bool resetSeqNum)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("98=0|");
      stringBuilder.Append("108=" + (object) heartBeatSeconds + "|");
      if (resetSeqNum)
        stringBuilder.Append("141=Y|");
      stringBuilder.Append("553=" + this._username + "|");
      stringBuilder.Append("554=" + this._password + "|");
      string str1 = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.Logon), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string HeartbeatMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber)
    {
      string message = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.Heartbeat), messageSequenceNumber, string.Empty);
      string str = this.ConstructTrailer(message);
      return (message + str).Replace("|", "\x0001");
    }

    public string TestRequestMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int testRequestID)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("112=" + (object) testRequestID + "|");
      string str1 = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.TestRequest), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string LogoutMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber)
    {
      string message = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.Logout), messageSequenceNumber, string.Empty);
      string str = this.ConstructTrailer(message);
      return (message + str).Replace("|", "\x0001");
    }

    public string ResendMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int endSequenceNo)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("16=" + (object) endSequenceNo + "|");
      string str1 = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.Resend), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string RejectMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int rejectSequenceNumber)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("45=" + (object) rejectSequenceNumber + "|");
      string str1 = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.Reject), messageSequenceNumber, string.Empty);
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string SequenceResetMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int rejectSequenceNumber)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("36=" + (object) rejectSequenceNumber + "|");
      string str1 = this.ConstructHeader(qualifier, this.SessionMessageCode(MessageConstructor.SessionMessageType.SequenceReset), messageSequenceNumber, string.Empty);
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string MarketDataRequestMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string marketDataRequestID, int subscriptionRequestType, int marketDepth, int marketDataEntryType, int noRelatedSymbol, long symbol = 0)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("262=" + marketDataRequestID + "|");
      stringBuilder.Append("263=" + (object) subscriptionRequestType + "|");
      stringBuilder.Append("264=" + (object) marketDepth + "|");
      stringBuilder.Append("265=1|");
      stringBuilder.Append("267=2|");
      stringBuilder.Append("269=0|269=1|");
      stringBuilder.Append("146=" + (object) noRelatedSymbol + "|");
      stringBuilder.Append("55=" + (object) symbol + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.MarketDataRequest), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string MarketDataSnapshotMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, long symbol, string noMarketDataEntries, int entryType, Decimal entryPrice, string marketDataRequestID = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("262=" + marketDataRequestID + "|");
      stringBuilder.Append("55=" + (object) symbol + "|");
      stringBuilder.Append("268=" + noMarketDataEntries + "|");
      stringBuilder.Append("269=" + (object) entryType + "|");
      stringBuilder.Append("270=" + (object) entryPrice + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.MarketDataRequest), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string MarketDataIncrementalRefreshMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string marketDataRequestID, int noMarketDataEntries, int marketDataUpdateAction, int marketDataEntryType, string marketDataEntryID, int noRelatedSymbol, Decimal marketDataEntryPrice = 0M, double marketDataEntrySize = 0.0, long symbol = 0)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("262=" + marketDataRequestID + "|");
      stringBuilder.Append("268=" + (object) noMarketDataEntries + "|");
      stringBuilder.Append("279=" + (object) marketDataUpdateAction + "|");
      stringBuilder.Append("269=" + (object) marketDataEntryType + "|");
      stringBuilder.Append("278=" + marketDataEntryID + "|");
      stringBuilder.Append("55=" + (object) symbol + "|");
      if (marketDataEntryPrice > Decimal.Zero)
        stringBuilder.Append("270=" + (object) marketDataEntryPrice + "|");
      if (marketDataEntrySize > 0.0)
        stringBuilder.Append("271=" + (object) marketDataEntrySize + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.MarketDataIncrementalRefresh), messageSequenceNumber, string.Empty);
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string NewOrderSingleMessage(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string orderID, long symbol, int side, string transactTime, int orderQuantity, int orderType, string timeInForce, Decimal price = 0M, Decimal stopPrice = 0M, string expireTime = "", string positionID = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("11=" + orderID + "|");
      stringBuilder.Append("55=" + (object) symbol + "|");
      stringBuilder.Append("54=" + (object) side + "|");
      stringBuilder.Append("60=" + transactTime + "|");
      stringBuilder.Append("38=" + (object) orderQuantity + "|");
      stringBuilder.Append("40=" + (object) orderType + "|");
      if (price != Decimal.Zero)
        stringBuilder.Append("44=" + (object) price + "|");
      if (stopPrice != Decimal.Zero)
        stringBuilder.Append("99=" + (object) stopPrice + "|");
      stringBuilder.Append("59=" + timeInForce + "|");
      if (expireTime != string.Empty)
        stringBuilder.Append("126=" + expireTime + "|");
      if (positionID != string.Empty)
        stringBuilder.Append("721=" + positionID + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.NewOrderSingle), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string OrderStatusRequest(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string orderID)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("11=" + orderID + "|");
      stringBuilder.Append("54=1|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.OrderStatusRequest), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string ExecutionReport(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string cTraderOrderID, string orderStatus, string transactTime, long symbol = 0, int side = 1, int averagePrice = 0, int orderQuantity = 0, int leavesQuantity = 0, int cumQuantity = 0, string orderID = "", string orderType = "", int price = 0, int stopPrice = 0, string timeInForce = "", string expireTime = "", string text = "", int orderRejectionReason = -1, string positionID = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("37=" + cTraderOrderID + "|");
      stringBuilder.Append("11=" + orderID + "|");
      stringBuilder.Append("150=F|");
      stringBuilder.Append("39=" + orderStatus + "|");
      stringBuilder.Append("55=" + (object) symbol + "|");
      stringBuilder.Append("54=" + (object) side + "|");
      stringBuilder.Append("60=" + transactTime + "|");
      if (averagePrice > 0)
        stringBuilder.Append("6=" + (object) averagePrice + "|");
      if (orderQuantity > 0)
        stringBuilder.Append("38=" + (object) orderQuantity + "|");
      if (leavesQuantity > 0)
        stringBuilder.Append("151=" + (object) leavesQuantity + "|");
      if (cumQuantity > 0)
        stringBuilder.Append("14=" + (object) cumQuantity + "|");
      if (orderType != string.Empty)
        stringBuilder.Append("40=" + orderType + "|");
      if (price > 0)
        stringBuilder.Append("44=" + (object) price + "|");
      if (stopPrice > 0)
        stringBuilder.Append("99=" + (object) stopPrice + "|");
      if (timeInForce != string.Empty)
        stringBuilder.Append("59=" + timeInForce + "|");
      if (expireTime != string.Empty)
        stringBuilder.Append("126=" + expireTime + "|");
      if (text != string.Empty)
        stringBuilder.Append("58=" + text + "|");
      if (orderRejectionReason != -1)
        stringBuilder.Append("103=" + (object) orderRejectionReason + "|");
      if (positionID != string.Empty)
        stringBuilder.Append("721=" + positionID + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.OrderStatusRequest), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string BusinessMessageReject(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, int referenceSequenceNum = 0, string referenceMessageType = "", string businessRejectRefID = "", int businessRejectReason = -1, string text = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      if ((uint) referenceSequenceNum > 0U)
        stringBuilder.Append("45=" + (object) referenceSequenceNum + "|");
      if (referenceMessageType != string.Empty)
        stringBuilder.Append("372=" + referenceMessageType + "|");
      if (businessRejectRefID != string.Empty)
        stringBuilder.Append("379=" + businessRejectRefID + "|");
      if (businessRejectReason != -1)
        stringBuilder.Append("380=" + (object) businessRejectReason + "|");
      if (text != string.Empty)
        stringBuilder.Append("58=" + text + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.BusinessMessageReject), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string RequestForPositions(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string requestID, string positionRequestID = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("710=" + requestID + "|");
      if (positionRequestID != string.Empty)
        stringBuilder.Append("721=" + positionRequestID + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.RequestForPosition), messageSequenceNumber, stringBuilder.ToString());
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    public string PositionReport(MessageConstructor.SessionQualifier qualifier, int messageSequenceNumber, string requestID, string totalNumberOfPositionReports, string positionRequestResult, string positionID = "", string symbol = "", string noOfPositions = "", string longQuantity = "", string shortQuantity = "", string settlementPrice = "")
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("710=" + requestID + "|");
      if (positionID != string.Empty)
        stringBuilder.Append("721=" + positionID + "|");
      stringBuilder.Append("727=" + totalNumberOfPositionReports + "|");
      stringBuilder.Append("728=" + positionRequestResult + "|");
      if (symbol != string.Empty)
        stringBuilder.Append("55=" + symbol + "|");
      if (noOfPositions != string.Empty)
        stringBuilder.Append("702=" + noOfPositions + "|");
      if (longQuantity != string.Empty)
        stringBuilder.Append("704=" + requestID + "|");
      if (shortQuantity != string.Empty)
        stringBuilder.Append("705=" + shortQuantity + "|");
      if (settlementPrice != string.Empty)
        stringBuilder.Append("730=" + settlementPrice + "|");
      string str1 = this.ConstructHeader(qualifier, this.ApplicationMessageCode(MessageConstructor.ApplicationMessageType.PositionReport), messageSequenceNumber, string.Empty);
      string str2 = this.ConstructTrailer(str1 + (object) stringBuilder);
      return (str1 + (object) stringBuilder + str2).Replace("|", "\x0001");
    }

    private string ConstructHeader(MessageConstructor.SessionQualifier qualifier, string type, int messageSequenceNumber, string bodyMessage)
    {
      StringBuilder stringBuilder1 = new StringBuilder();
      stringBuilder1.Append("8=FIX.4.4|");
      StringBuilder stringBuilder2 = new StringBuilder();
      stringBuilder2.Append("35=" + type + "|");
      stringBuilder2.Append("49=" + this._senderCompID + "|");
      stringBuilder2.Append("56=" + this._targetCompID + "|");
      stringBuilder2.Append("57=" + qualifier.ToString() + "|");
      stringBuilder2.Append("50=" + this._senderSubID + "|");
      stringBuilder2.Append("34=" + (object) messageSequenceNumber + "|");
      stringBuilder2.Append("52=" + DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss") + "|");
      int num = stringBuilder2.Length + bodyMessage.Length;
      stringBuilder1.Append("9=" + (object) num + "|");
      stringBuilder1.Append((object) stringBuilder2);
      return stringBuilder1.Replace("|", "\x0001").ToString();
    }

    private string ConstructTrailer(string message)
    {
      return ("10=" + (object) this.CalculateChecksum(message.Replace("|", "\x0001").ToString()) + "|").Replace("|", "\x0001");
    }

    private int CalculateChecksum(string dataToCalculate)
    {
      byte[] bytes = Encoding.ASCII.GetBytes(dataToCalculate);
      int num1 = 0;
      foreach (byte num2 in bytes)
        num1 += (int) num2;
      return num1 % 256;
    }

    private string SessionMessageCode(MessageConstructor.SessionMessageType type)
    {
      switch (type)
      {
        case MessageConstructor.SessionMessageType.Logon:
          return "A";
        case MessageConstructor.SessionMessageType.Logout:
          return "5";
        case MessageConstructor.SessionMessageType.Heartbeat:
          return "0";
        case MessageConstructor.SessionMessageType.TestRequest:
          return "1";
        case MessageConstructor.SessionMessageType.Resend:
          return "2";
        case MessageConstructor.SessionMessageType.Reject:
          return "3";
        case MessageConstructor.SessionMessageType.SequenceReset:
          return "4";
        default:
          return "0";
      }
    }

    private string ApplicationMessageCode(MessageConstructor.ApplicationMessageType type)
    {
      switch (type)
      {
        case MessageConstructor.ApplicationMessageType.MarketDataRequest:
          return "V";
        case MessageConstructor.ApplicationMessageType.MarketDataIncrementalRefresh:
          return "X";
        case MessageConstructor.ApplicationMessageType.NewOrderSingle:
          return "D";
        case MessageConstructor.ApplicationMessageType.OrderStatusRequest:
          return "H";
        case MessageConstructor.ApplicationMessageType.ExecutionReport:
          return "8";
        case MessageConstructor.ApplicationMessageType.BusinessMessageReject:
          return "j";
        case MessageConstructor.ApplicationMessageType.RequestForPosition:
          return "AN";
        case MessageConstructor.ApplicationMessageType.PositionReport:
          return "AP";
        default:
          return "0";
      }
    }

    public enum SessionMessageType
    {
      Logon,
      Logout,
      Heartbeat,
      TestRequest,
      Resend,
      Reject,
      SequenceReset,
    }

    public enum ApplicationMessageType
    {
      MarketDataRequest,
      MarketDataIncrementalRefresh,
      NewOrderSingle,
      OrderStatusRequest,
      ExecutionReport,
      BusinessMessageReject,
      RequestForPosition,
      PositionReport,
    }

    public enum SessionQualifier
    {
      QUOTE,
      TRADE,
    }
  }
}

 


@mindbreaker

mindbreaker
08 Feb 2017, 17:06

Send socket

using FIX_API_Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FIX_API_Sample
{
    public partial class frmFIXAPISample : Form
    {
    
        private int _pricePort = 5201;
        private int _tradePort = 5202;

        private string _host = "h20.p.ctrader.com";
        private string _username = "9951960";
        private string _password = "3604";
        private string _senderCompID = "fxpro.9951960";
        private string _senderSubID = "TRADE";

        private string _targetCompID = "cSERVER";

        private int _messageSequenceNumber = 1;
        private int _testRequestID = 1;
        TcpClient _priceClient;
        NetworkStream _priceStream;
        TcpClient _tradeClient;
        NetworkStream _tradeStream;
        MessageConstructor _messageConstructor;

        public frmFIXAPISample()
        {
            InitializeComponent();
            _priceClient = new TcpClient(_host, _pricePort);           
            _priceStream = _priceClient.GetStream();            
            _tradeClient = new TcpClient(_host, _tradePort);
            _tradeStream = _priceClient.GetStream();
            _messageConstructor = new MessageConstructor(_host, _username,
                _password, _senderCompID, _senderSubID, _targetCompID);
        }

        private void ClearText()
        {
            txtMessageSend.Text = "";
            txtMessageReceived.Text = "";
        }

        private string SendPriceMessage(string message)
        {            
            return SendMessage(message, _priceStream);
        }

        private string SendTradeMessage(string message)
        {           
            return SendMessage(message, _tradeStream);
        }

        private string SendMessage(string message, NetworkStream stream)
        {
            var byteArray = Encoding.ASCII.GetBytes(message);
            stream.Write(byteArray, 0, byteArray.Length);
            var buffer = new byte[1024];
            int i = 0;
            while (!stream.DataAvailable && i < 10)
            {
                Thread.Sleep(100);
                i++;
            }
            if(stream.DataAvailable)
                stream.Read(buffer, 0, 1024);
            _messageSequenceNumber++;
            var returnMessage = Encoding.ASCII.GetString(buffer);
            return returnMessage;
        }
      
        private void btnLogon_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, 30, false);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }       

        private void btnTestRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,_testRequestID);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
            txtMessageSend.Text = message;         
            txtMessageReceived.Text = SendPriceMessage(message);
            _messageSequenceNumber = 1;
        }

        private void btnMarketDataRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1,0,0,1,1);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnHeartbeat_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnResendRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,_messageSequenceNumber - 1);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            ClearText();
             var message = _messageConstructor.RejectMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,0);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnSequenceReset_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.SequenceResetMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,0);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnNewOrderSingle_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber,"1408471",1,1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,1, "3");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnOrderStatusRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.OrderStatusRequest(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private string Timestamp()
        {
            return (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
        }

        private void btnRequestForPositions_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.RequestForPositions(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "876316401");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLogonT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, 30, false);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnHeartbeatT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnTestRequestT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _testRequestID);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLogoutT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
            _messageSequenceNumber = 1;
        }

        private void btnResendRequestT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _messageSequenceNumber - 1);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnSpotMarketData_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1, 1, 0, 1, 1);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnStopOrder_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, 3, "3",0, (decimal)1.08);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLimitOrder_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,2, "3", (decimal)1.08);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }
    }
}

 


@mindbreaker

mindbreaker
08 Feb 2017, 17:06

using FIX_API_Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FIX_API_Sample
{
    public partial class frmFIXAPISample : Form
    {
    
        private int _pricePort = 5201;
        private int _tradePort = 5202;

        private string _host = "h20.p.ctrader.com";
        private string _username = "9951960";
        private string _password = "3604";
        private string _senderCompID = "fxpro.9951960";
        private string _senderSubID = "TRADE";

        private string _targetCompID = "cSERVER";

        private int _messageSequenceNumber = 1;
        private int _testRequestID = 1;
        TcpClient _priceClient;
        NetworkStream _priceStream;
        TcpClient _tradeClient;
        NetworkStream _tradeStream;
        MessageConstructor _messageConstructor;

        public frmFIXAPISample()
        {
            InitializeComponent();
            _priceClient = new TcpClient(_host, _pricePort);           
            _priceStream = _priceClient.GetStream();            
            _tradeClient = new TcpClient(_host, _tradePort);
            _tradeStream = _priceClient.GetStream();
            _messageConstructor = new MessageConstructor(_host, _username,
                _password, _senderCompID, _senderSubID, _targetCompID);
        }

        private void ClearText()
        {
            txtMessageSend.Text = "";
            txtMessageReceived.Text = "";
        }

        private string SendPriceMessage(string message)
        {            
            return SendMessage(message, _priceStream);
        }

        private string SendTradeMessage(string message)
        {           
            return SendMessage(message, _tradeStream);
        }

        private string SendMessage(string message, NetworkStream stream)
        {
            var byteArray = Encoding.ASCII.GetBytes(message);
            stream.Write(byteArray, 0, byteArray.Length);
            var buffer = new byte[1024];
            int i = 0;
            while (!stream.DataAvailable && i < 10)
            {
                Thread.Sleep(100);
                i++;
            }
            if(stream.DataAvailable)
                stream.Read(buffer, 0, 1024);
            _messageSequenceNumber++;
            var returnMessage = Encoding.ASCII.GetString(buffer);
            return returnMessage;
        }
      
        private void btnLogon_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, 30, false);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }       

        private void btnTestRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,_testRequestID);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
            txtMessageSend.Text = message;         
            txtMessageReceived.Text = SendPriceMessage(message);
            _messageSequenceNumber = 1;
        }

        private void btnMarketDataRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1,0,0,1,1);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnHeartbeat_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnResendRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,_messageSequenceNumber - 1);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            ClearText();
             var message = _messageConstructor.RejectMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,0);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnSequenceReset_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.SequenceResetMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber,0);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnNewOrderSingle_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber,"1408471",1,1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,1, "3");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnOrderStatusRequest_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.OrderStatusRequest(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private string Timestamp()
        {
            return (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
        }

        private void btnRequestForPositions_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.RequestForPositions(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "876316401");
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLogonT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, 30, false);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnHeartbeatT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnTestRequestT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _testRequestID);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLogoutT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
            _messageSequenceNumber = 1;
        }

        private void btnResendRequestT_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _messageSequenceNumber - 1);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnSpotMarketData_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1, 1, 0, 1, 1);
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendPriceMessage(message);
        }

        private void btnStopOrder_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, 3, "3",0, (decimal)1.08);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }

        private void btnLimitOrder_Click(object sender, EventArgs e)
        {
            ClearText();
            var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,2, "3", (decimal)1.08);
            _testRequestID++;
            txtMessageSend.Text = message;
            txtMessageReceived.Text = SendTradeMessage(message);
        }
    }
}

 


@mindbreaker

Spotware
08 Feb 2017, 17:11

Hi mindbreaker,

Thanks for your post. First of all, please verify that you have the latest version of the code. We fixed a similar issue yesterday. We have just tried the latest version on a demo server locally and works ok.

The FIX API sample has been uploaded recently and has taken into consideration the latest Rules of Engagement which have not been deployed on live servers yet. Full deployment is scheduled over the weekend so we would recommend to have a look at it next week. 

Regarding the error, we aknowledge that the application is not bullet proof so some errors might occur. It serves moslty an educational purpose to demonstrate users how to construct, send and receive a FIX API message. If you need a production ready FIX engine, you will need develop the code sample further.

You can read more here https://help.spotware.com/FIX/FIXSample 

Let us know if your issue is resolved or if it persists after Monday.

       

@Spotware

Spotware
08 Feb 2017, 17:15

Hi mindbreaker,

Just seen the code. You are indeed using an older version. Please get the latest one. Also

  private string _senderSubID = "TRADE";

You need to assign your account ID to _senderSubID. Please see latest Rules of Engagement https://www.spotware.com/pdf/cTraderFixApi_v2.9.1.pdf?v2.9.1


@Spotware

mindbreaker
11 Feb 2017, 18:06

FIX cTrader

Hi and thanks works fine.

1) How close positions?

2) How add Stop loss and Take profit to opened positions ( or modify stop loss and take profit )

3) Is it possible create FIX without login, logout and heartbeat messages ( Login and Password send with single message, then send and receive data is much easier and users don't need to worry about session, heartbeats and connections [something like rest api in ajax] ) ?

 

private string SendMessage(string message, NetworkStream stream)
{
    var byteArray = Encoding.ASCII.GetBytes(message);
    stream.Write(byteArray, 0, byteArray.Length);
    //set buffer
    var buffer = new byte[4096];
    int i = 0;
    while (!stream.DataAvailable && i < 10)
    {
        Thread.Sleep(100);
        i++;
    }
    if (stream.DataAvailable)
        // set buffer
        stream.Read(buffer, 0, 4096);
    _messageSequenceNumber++;
    var returnMessage = Encoding.ASCII.GetString(buffer);
    return returnMessage;
}


// Add multiline to message input field !!!!

Bye


@mindbreaker

Spotware
13 Feb 2017, 09:53

Hi mindbreaker,

1) In order to close a position, you need to send a NewOrderSingle request with the reverse direction and the PostionID as a paramater. See below

var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471", 1,2, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,1, "1",0,0,"", "438169");
          

2) This is not supported by FIX API. You will need to develop this logic yourself.

3) It is not possible now and we don't think that this is going to be added as it defeats the purpose of FIX, which is to reduce the necessary information transmitted to minimum for performance reasons.

If points 2 and 3 are important for you you might consider using Trading API instead of FIX


@Spotware

mindbreaker
13 Feb 2017, 11:44

RE:

Spotware said:

Hi mindbreaker,

1) In order to close a position, you need to send a NewOrderSingle request with the reverse direction and the PostionID as a paramater. See below

var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471", 1,2, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000,1, "1",0,0,"", "438169");
          

2) This is not supported by FIX API. You will need to develop this logic yourself.

3) It is not possible now and we don't think that this is going to be added as it defeats the purpose of FIX, which is to reduce the necessary information transmitted to minimum for performance reasons.

If points 2 and 3 are important for you you might consider using Trading API instead of FIX

Hi Spotware,

Thank you for your answer.

Hmm. I will login and logout after send one message if 3) is not possible.

Thanks again and bye.


@mindbreaker

#EOL
13 Feb 2017, 17:00

RE: RE:

mindbreaker said:

Hmm. I will login and logout after send one message if 3) is not possible.

I beleive, some FIX messaging engine like QuickFIX can handle all the heartbits and session management for you


@#EOL

mindbreaker
15 Feb 2017, 14:07

RE: RE: RE:

Thanks

But is it possible to create position label in FIX ???


@mindbreaker

#EOL
16 Feb 2017, 13:15

RE: RE: RE: RE:

Don't think so.

There is nothing like this in the specification

Try to vote for it, maybe they will add this in the future http://vote.spotware.com/


@#EOL

aimerdoux
04 Mar 2017, 23:20

Hi Mind Breaker

Hi How are you MindBreaker I hope you're doing ok, I'd like to talk to you about business sutff, if interested please contact me at andrew@infinitechtrading.com thanks for your time.


@aimerdoux