Tuesday, 31 May 2016

Trading Platform in CSharp c# 4 Send Order

The videos are best viewed at the highest quality and full screen.
  • send a LMT = limit order
  • change the up down arrow in the limit box to move up 10 cents at a time
  • get the next valid id
  • create Buy and Sell buttons




Copy and paste the highlighted text and the text.

 public void DoOrder(ref int myShares, ref string orderType, ref string myAction)  
     {
       // myAction represent BUY or SELL  
       // orderType represents MKT, LMT etc...  
       // myShares is number of shares to buy or sell  
       int shares = Convert.ToInt32(this.numQuantity.Value);  
       double iShares = System.Math.Abs(shares);  
       // Create a new contract object  
       TWSLib.IContract ContractInfo = axTws1.createContract();  
       // Create a new Order object  
       TWSLib.IOrder OrderInfo = axTws1.createOrder();  
       // Fill the ContractInfo object with the necessary information   
       // Connection identifier  
       ContractInfo.conId = 0;  
       // Ticker symbol for the order  
       ContractInfo.symbol = this.cbSymbol.Text;  
       ContractInfo.secType = "STK";  
       ContractInfo.exchange = this.cbMarket.Text;  
       ContractInfo.primaryExchange = this.tbPrimaryExchange.Text;  
       ContractInfo.currency = "USD";  
       // Fill in the Order information:  
       // orderId is a *Unique* order identifier  
       OrderInfo.orderId = order_id;  
       // action will be BUY, SELL or SSHORT but I never used SSHORT  
       OrderInfo.action = myAction;  
       // totalQuantity is the total number of shares (or contracts)  
       OrderInfo.totalQuantity = myShares;  
       // orderType is one of MKT, LMT, STP, etc.  
       OrderInfo.orderType = orderType;  
       // auxPrice used for STP orders  
       // This is the STOP price for stop-limit orders, and the offset amount for relative orders.  
       // In all other cases, specify zero.  
       // OrderInfo.auxPrice = 0  
       // lmtPrice is the limit price for a limit order  
       // This is the LIMIT price, used for limit, stop-limit and relative orders.  
       // In all other cases specify zero. For relative orders with no limit price, also specify zero.  
       OrderInfo.lmtPrice = double.Parse(this.numLimitPrice.Text);  
       // timeInForce is either DAY, GTC, IOC, GTD etc.  
       OrderInfo.timeInForce = cbTIF.Text;  
       // triggerMethod  
       //    Specifies how Simulated Stop, Stop-Limit and Trailing Stop orders are triggered. Valid values are:  
       //0 - The default value. The "double bid/ask" function will be used for orders for OTC stocks and US options.
       // All other orders will used the "last" function.  
       //1 - use "double bid/ask" function, where stop orders are triggered based on two consecutive bid or ask prices.  
       //2 - "last" function, where stop orders are triggered based on the last price.  
       //3 double last function.  
       //4 bid/ask function.  
       //7 last or bid/ask function.  
       //8 mid-point function.  
       // displaySize   
       //OrderInfo.displaySize = int.Parse(this.tbVisible.Text);  
       // OrderInfo.outsideRth = myOutsideValue; // 1 = true and 0 = false  
       // There are about 40 other properties for different order types...  
       // We do not need to specify them in this case  
       // Submit the order. Parameters are  
       // OrderId  - A Unique identifier for each order  
       // Contract  - An object specifying the security to be ordered  
       // OrderInfo - An object specifying the type, quantity, side and other order parameters  
       this.axTws1.placeOrderEx(order_id, ContractInfo, OrderInfo);  
       // Changes the order type back to limit   
       cbOrderType.Text = "LMT";  
       order_id++;  
     }  






 private void numLimitPrice_KeyDown(object sender, KeyEventArgs e)  
     { 
       if (e.KeyCode == Keys.Control)  
         numLimitPrice.DecimalPlaces = 2;  
       numLimitPrice.Increment = 0.10M;  
     }  

 private void axTws1_nextValidId(object sender, AxTWSLib._DTwsEvents_nextValidIdEvent e)  
     {   
       // The next id will be held in e.id  
       order_id = e.id;  
     }  


private void numLimitPrice_KeyUp(object sender, KeyEventArgs e)
{
numLimitPrice.DecimalPlaces = 2;
numLimitPrice.Increment = 0.01M;
}

 private void btnSell_Click(object sender, EventArgs e)  
     {
       string myAction = "SELL";  
       int myShares = Convert.ToInt32(numQuantity.Text);  
       string orderType = cbOrderType.Text;  
       if (cbOrderType.Text == "LMT")  
       {  
         // Calls the methond DoOrder and passes on the variables   
         // myShares, orderType, and myAction values  
         DoOrder(ref myShares, ref orderType, ref myAction);  
       }  
     }  






Monday, 30 May 2016

Trading Platform in CSharp c# 3 - streaming data

In this video:
  • you will add some textboxes, comboboxes, labels, numberupdown, to your form
  • request and receive live streaming data bid, ask, and last prices
    The videos are best viewed at the highest quality and full screen.






This KeyDown event requests the market data from the server.
click on the link below.

 private void cbSymbol_KeyDown(object sender, KeyEventArgs e)  
     { 
       if (e.KeyCode == Keys.Enter)  
       { 
         e.SuppressKeyPress = true;  
         // still works with fake account number  
         string myAccount = "U132458";  
         axTws1.cancelMktData(2);  
         axTws1.reqAccountUpdates(0, myAccount); 
         // Create a TagValueList object (for API version 9.71 users) 
         TWSLib.ITagValueList mktDataOptions = axTws1.createTagValueList();
         // Create a new contract object  
         TWSLib.IContract ContractInfo = axTws1.createContract();  
         // Connection identifier  
         ContractInfo.conId = 0;  
         // Stock symbol  
         ContractInfo.symbol = cbSymbol.Text;  
         // Type Stock=STK,Option=OPT,Future=FUT, etc.  
         ContractInfo.secType = "STK";  
         // The Options or Futures expiration data in the format YYYYMMDD  
         ContractInfo.expiry = "";  
         // The Options Strike Price   
         ContractInfo.strike = 0;  
         // used for Options PUT or CALL  
         ContractInfo.right = "";  
         // The contract multiplier for Futures or Options not used for Stocks  
         ContractInfo.multiplier = "";  
         // The destination of order or request. "SMART" = IB order router  
         ContractInfo.exchange = "SMART";  
         // The primary exchange where the instrument trades.   
         // NYSE, NASDAQ, AMEX, BATS, ARCA, PHLX etc.  
         ContractInfo.primaryExchange = "NASDAQ";  
         // The currency of the exchange USD or GBP or CAD, etc.  
         ContractInfo.currency = "USD";  
         // Request the streaming market data for symbol in cbSymbol  
         //  TickerID, ContractInfo, genericTicks and Snapshot (0 for streaming)  
         axTws1.reqMktDataEx(2, ContractInfo, "165, 233", 0);  
         // if you are using API version 9.71 you need a fifth parameter
         // axTws1.reqMktDataEx(2, ContractInfo, "165, 233", 0, mktDataOptions); 
         //timer1.Start();  
         cbSymbol.SelectAll();  
         axTws1.reqAccountUpdates(1, myAccount); // requests account updates  
         //AddSymbol();  
                } 
     }  




This event gets the bid ask and last price from the server

 private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)  
     {
       if ((e.tickType) == 2)  // 2 = Ask  
       {  
         // Gets the Ask price and formats it to 2 decimals  
         // sends the result to the Ask textbox  
         double theAsk = e.price;  
         int decimalPlaces = 2;  
         theAsk = theAsk * Math.Pow(100, decimalPlaces);  
         theAsk = Math.Truncate(theAsk);  
         theAsk = theAsk / Math.Pow(100, decimalPlaces);  
         string askPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theAsk);  
         tbAsk.Text = askPrice;  
       }  
       else if ((e.tickType) == 1)  // 1 = Bid  
       {  
         // Gets the Bid price and formats it to 2 decimals  
         // sends the result to the Bid textbox  
         double theBid = e.price;  
         int decimalPlaces = 2;  
         theBid = theBid * Math.Pow(100, decimalPlaces);  
         theBid = Math.Truncate(theBid);  
         theBid = theBid / Math.Pow(100, decimalPlaces);  
         string bidPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theBid);  
         tbBid.Text = bidPrice;   
       }  
       if ((e.tickType) == 4)  // 4 = Last  
       {  
         // Gets the Last price and formats it to 2 decimals  
         // sends the result to the Last text box
         double theLast = e.price;  
         int decimalPlaces = 2;  
         theLast = theLast * Math.Pow(100, decimalPlaces);  
         theLast = Math.Truncate(theLast);  
         theLast = theLast / Math.Pow(100, decimalPlaces);  
         string lastPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theLast);  
         tbLast.Text = lastPrice;  
       }  
     }  


This event changes the character from lowercase to uppercase
 private void cbSymbol_KeyPress(object sender, KeyPressEventArgs e)  
     {
      // changes lowercase to uppercase 
      if (char.IsLower(e.KeyChar))  
       {  
         e.KeyChar = char.ToUpper(e.KeyChar);  
       } 
     }  











Save

Trading Platform Tutorial in CSharp C# 2

Trading Platform Tutorial

This will be a series of video tutorials called Create a trading platform. The tutorial that will show you how to create a working trading platform.
The program will be coded in csharp c# using Visual Studio 2012
The program will connect to Trader Workstation from Interactive Brokers.
You need to download and install the trading platform and the API from Interactive Brokers and install it.

I am using an earlier version of the API_Version=9.70. I think if you use this version you will have to force it to install the 32 bit version.




 private void btnConnect_Click(object sender, EventArgs e)  
 {
    // The last variable can be any number  
    axTws1.connect("", 7496, 1);  
 }