Friday, 15 January 2021

Updated Trading Platform in C# Streaming Data

 
 
 
here is a link to all of the the streaming data that you can request
 
 
 
 

 


# Control Name Text or Value Increment Decimal
1 Combobox
cbSymbol MSFT - -
2 numericupdown numQuantity
100 100 0
3 numericupdown
numPrice 0 0.01 2
4 Combobox
cbMarket
SMART
- -
5 Combobox
cbOrderType
LMT
- -
6 Text box
tbVisible
100
- -
7 Text Box
tbPrimaryEx
NASDAQ
- -
8 Combobox
cbTif
DAY
- -
9 Text Box
tbBid
0.00
- -
10 Text Box
tbAsk
0.00
- -
11 Text Box
tbLast
0.00
- -



The code goes in the tickPrice method in the EWrapperImpl.cs file

                         string _tickPrice = tickerId + "," + field +
                      "," + price + "," + attribs.CanAutoExecute;
            
	    // sends the string to Form1
            myform.AddTextBoxItemTickPrice(_tickPrice);          
            

 

 

C# additions are highlighted in yellow for the Form1.cs file.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using IBApi;

namespace IB_TradingPlatform
{
    public partial class Form1 : Form
    {

        // This delegate enables asynchronous calls for setting
        // the text property on a ListBox control.
        delegate void SetTextCallback(string text);
        delegate void SetTextCallbackTickPrice(string _tickPrice);

        public void AddListBoxItem(string text)
        {
            // See if a new invocation is required form a different thread          
            if (this.lbData.InvokeRequired) 
            {
                SetTextCallback d = new SetTextCallback(AddListBoxItem);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                // Add the text string to the list box
                this.lbData.Items.Add(text);
            }
        }


        // Create the ibClient object to represent the connection
        IB_TradingPlatform.EWrapperImpl ibClient;


        public Form1()
        {
            InitializeComponent();

            // instantiate the ibClient
            ibClient = new IB_TradingPlatform.EWrapperImpl();
        }

        
        private void btnConnect_Click(object sender, EventArgs e)
        {
            // Parameters to connect to TWS are:
            // host       - IP address or host name of the host running TWS
            // port       - listening port 7496 or 7497
            // clientId   - client application identifier can be any number
            ibClient.ClientSocket.eConnect("", 7497, 0);

            var reader = new EReader(ibClient.ClientSocket, ibClient.Signal);
            reader.Start();
            new Thread(() => {
                while (ibClient.ClientSocket.IsConnected())
                {
                    ibClient.Signal.waitForSignal();
                    reader.processMsgs();
                }
            })
            { IsBackground = true }.Start();
            // Wait until the connection is completed
            while (ibClient.NextOrderId <= 0) { }

            // Set up the form object in the EWrapper
            ibClient.myform = (Form1)Application.OpenForms[0];

            getData();
        }

        public void AddTextBoxItemTickPrice(string _tickPrice)
        {
            if (this.tbLast.InvokeRequired)            {
                SetTextCallbackTickPrice d = new SetTextCallbackTickPrice(AddTextBoxItemTickPrice);
                try
                {
                    this.Invoke(d, new object[] { _tickPrice });
                }
                catch (Exception e)
                {
                    Console.WriteLine("This is from tickPrice", e);                }
            }
            else
            {

                string[] tickerPrice = new string[] { _tickPrice };
                tickerPrice = _tickPrice.Split(',');

                if (Convert.ToInt32(tickerPrice[0]) == 1)                {
                    if (Convert.ToInt32(tickerPrice[1]) == 4)// Delayed Last quote 68, if you want realtime use tickerPrice == 4
                    {
                        // Add the text string to the list box
                        
                        this.tbLast.Text = tickerPrice[2];
                        
                    }
                    else if (Convert.ToInt32(tickerPrice[1]) == 2)  // Delayed Ask quote 67, if you want realtime use tickerPrice == 2
                    {
                        // Add the text string to the list box
                        
                        this.tbAsk.Text = tickerPrice[2];

                    }
                    else if (Convert.ToInt32(tickerPrice[1]) == 1)  // Delayed Bid quote 66, if you want realtime use tickerPrice == 1
                    {
                        // Add the text string to the list box
                        
                        this.tbBid.Text = tickerPrice[2];

                    }
                }
            }
        }

        private void getData()
        {
            ibClient.ClientSocket.cancelMktData(1); // cancel market data

            // Create a new contract to specify the security we are searching for
            IBApi.Contract contract = new IBApi.Contract();
            // Create a new TagValueList object (for API version 9.71 and later) 
            List mktDataOptions = new List();

            // Set the underlying stock symbol from the cbSymbol combobox
            contract.Symbol = cbSymbol.Text;
            // Set the Security type to STK for a Stock
            contract.SecType = "STK";
            // Use "SMART" as the general exchange
            contract.Exchange = "SMART";
            // Set the primary exchange (sometimes called Listing exchange)
            // Use either NYSE or ISLAND
            contract.PrimaryExch = "ISLAND";
            // Set the currency to USD
            contract.Currency = "USD";

            // If using delayed market data subscription
            // in the line below to make sure parameter is 3 not 1 
            ibClient.ClientSocket.reqMarketDataType(1);  // delayed data = 3 live = 1

            // Kick off the subscription for real-time data (add the mktDataOptions list for API v9.71)
            //ibClient.ClientSocket.reqMktData(1, contract, "", false, mktDataOptions);
            // For API v9.72 and higher, add one more parameter for regulatory snapshot
            ibClient.ClientSocket.reqMktData(1, contract, "", false, false, mktDataOptions);

        }


        private void cbSymbol_SelectedIndexChanged(object sender, EventArgs e)
        {
            getData();
        }

        private void cbSymbol_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLower(e.KeyChar))
            {
                e.KeyChar = char.ToUpper(e.KeyChar);
            }
        }

        private void cbSymbol_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbSymbol.SelectionStart = 0;
                cbSymbol.SelectionLength = cbSymbol.Text.Length;
                
                e.SuppressKeyPress = true;                

                string name = cbSymbol.Text;
                

                // adds the security symbol to the dropdown list in the symbol combobox
                if (!cbSymbol.Items.Contains(name))                {
                    cbSymbol.Items.Add(name);
                }
                cbSymbol.SelectAll();

                getData();  
            }
        }
    }
}


26 comments:

  1. Hi BingaZingas,

    Thank you for your scripts it is very useful!

    I got integer in bid, ask, last field. I found that the price split not correct in From1.cs. I made a litle change.
    EwrapperImpl,cs
    //! [tickprice] --> change , to ;
    string _tickPrice = tickerId + ";" + field +
    ";" + price + ";" + attribs.CanAutoExecute;
    //! [tickprice]

    Form1,cs
    AddTextBoxItemTickPrice(string _tickPrice)
    Channge , to ;

    tickerPrice = _tickPrice.Split(';');

    With this change I get decimals in the bid, ask, last fields.

    ReplyDelete
  2. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. privacidadenlared

    ReplyDelete
  3. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. افضل شركة تداول اسهم أمريكية

    ReplyDelete
  4. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. Quotex Review

    ReplyDelete
  5. Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. invite code of okex

    ReplyDelete
  6. Free Mt4 copier I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.

    ReplyDelete
  7. This is a brilliant blog! I'm very happy with the comments!.. https://putlocker-live.org

    ReplyDelete
  8. This article is an appealing wealth of informative data that is interesting and well-written. I commend your hard work on this and thank you for this information. You’ve got what it takes to get attention. https://soap2dayy.co

    ReplyDelete
  9. I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. https://moviesyify.org

    ReplyDelete
  10. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. https://gomovies-sc.com

    ReplyDelete
  11. I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site. https://zmoviess.co

    ReplyDelete
  12. i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. https://primewire.today

    ReplyDelete
  13. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://projectfreetv.space/

    ReplyDelete
  14. Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as  https://thecouchtuner.one

    ReplyDelete
  15. Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. https://currentputlocker.site

    ReplyDelete
  16. It is included in my habit that I often visit blogs in my free time, so after landing on your blog. I have thoroughly impressed with it and decided to take out some precious time to visit it again and again. Thanks. https://the123movies.site

    ReplyDelete
  17. Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. https://soap2daymovies.website

    ReplyDelete
  18. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... https://la-123movies.one

    ReplyDelete
  19. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://123putlocker.website

    ReplyDelete
  20. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious https://soap2day.shop

    ReplyDelete
  21. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive https://soap2day.monster

    ReplyDelete
    Replies
    1. Hi there! Nice stuff, do keep me posted when you post again something like this! https://putlockertv.one

      Delete
  22. Wonderful blog! Do you have any tips and hints for aspiring writers? Because I’m going to start my website soon, but I’m a little lost on everything. Many thanks! https://movies123.win

    ReplyDelete
  23. Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking. https://the123movies.top

    ReplyDelete
  24. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.
    https://watchcouchtuner.website

    ReplyDelete
  25. Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive https://theprimewire.site/

    ReplyDelete