Tuesday, 7 June 2016

Trading Platform in CSharp c# 5 account update

In this video tutorial:
  • add the different order types to the order type combo box.
  • SelectAll() in the Symbol combo box so we don't have to click in the box and delete what is there,
    you just type a new symbol after you hit enter.
  • Add a count down timer to wait 5 tenths of a second before it adds the price of the bid text box into the limit price number box.
  • Add 2 text box for the current position size and the average price in the average price text box by requesting account updates.
Copy and paste the highlighted code into your project
  1. tip if you want the timer to tick every second you can set the interval in the properties window to 1000, but for this program make sure it is set to 100
private void timer1_Tick(object sender, EventArgs e)
{
     try
     {
          if (timer1_counter == 0) // checks to see if the timer counter is at zero
         {
              timer1.Stop(); // Stop the timer
              // add the bid price to the limit box
              numLimitPrice.Value = Convert.ToDecimal(tbBid.Text);
              timer1_counter = 5;  // reset timer counter back to 5
         }
         timer1_counter--; // subtract 1 every time there is tick
      }
      catch
      {
         timer1_counter = 5;
         timer1.Stop();
      }
}




make this a global variable

int timer1_counter = 5;

Add this to the bottom of the request for market data of the key down event of the symbol combobox (cbSymbol)

timer1.Start();
cbSymbol.SelectAll();
axTws1.reqAccountUpdates(1, myAccount); // requests account updates

Add this to the top of the request market data of the key down event

axTws1.reqAccountUpdates(0, myAccount); // cancels the account updates

updatePortfolioEx copy the highlighted text

 private void axTws1_updatePortfolioEx(object sender, AxTWSLib._DTwsEvents_updatePortfolioExEvent e)  
     {
       int idCon = e.contract.conId;  
       string theSymbol1 = Convert.ToString(e.contract.symbol);  
       // e.averageCost  
       // e.marketValue   
       // e.realizedPNL    
       // e.unrealizedPNL  
       // e.position  
       // e.marketValue  
       // e.contract.symbol = stock symbol  
       // if the symbol in the combo box is the same as the e.contract.symbol  
       // then add the Position size and the average price to the text boxes  
       if (theSymbol1 == cbSymbol.Text)  
       {  
         // Gets the average price of the current symbol  
         double averagePrice = e.averageCost;  
         //Rounds off the current price adds it to the text box  
         tbAvgPrice.Text = Convert.ToString(Math.Round(averagePrice, 2));  
         // puts the position size in the text box  
         tbPosition.Text = Convert.ToString(e.position);  
       }
     }  








No comments:

Post a Comment