Build a Trading Platform in C# using Interactive Brokers API Part 7 - Watch List
# | Control | Name | Columns, Tabs | AllowUserToAddRows | RowHeadersVisible |
---|---|---|---|---|---|
1 | TabControl | tabControl2 | Watch List | ||
2 | DataGridView | dataGridView2 | Symbol, Last, Volume, Change, Close | False | False |
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
dataGridView1.Rows.Add();
}
for (int i = 0; i < 10; i++)
{
dataGridView2.Rows.Add();
}
dataGridView2.Columns[3].DefaultCellStyle.Format = "#.00\\%";
}
This code goes in the tickSize function in the EWrapperImpl.cs file
public virtual void tickSize(int tickerId, int field, int size)
{
string strData = "Tick Size. Ticker Id:" + tickerId +
", Field: " + field + ", Size: " + size;
//Console.WriteLine(strData);
myform.AddListBoxItem(strData);
myform.AddTickSizeItem(tickerId, field, size);
}
delegate void SetTextCallbackTickSize(int tickerId, int field, int size);
public void AddTickSizeItem(int tickerId,
int field,
int size)
{
if (this.tbLast.InvokeRequired)
{
SetTextCallbackTickSize d = new SetTextCallbackTickSize(AddTickSizeItem);
try
{
this.Invoke(d, new object[] { tickerId, field, size });
}
catch (Exception e)
{
Console.WriteLine("this is from _tickPrice ", e);
}
}
else
{
switch (tickerId)
{
case 10:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 0].Value = mySize.ToString();
break;
}
break;
case 11:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 1].Value = mySize.ToString();
break;
}
break;
case 12:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 2].Value = mySize.ToString();
break;
}
break;
case 13:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 3].Value = mySize.ToString();
break;
}
break;
case 14:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 4].Value = mySize.ToString();
break;
}
break;
case 15:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 5].Value = mySize.ToString();
break;
}
break;
case 16:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 6].Value = mySize.ToString();
break;
}
break;
case 17:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 7].Value = mySize.ToString();
break;
}
break;
case 18:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 8].Value = mySize.ToString();
break;
}
break;
case 19:
if (field == 8)
{
int mySize = size * 100;
dataGridView2[2, 9].Value = mySize.ToString();
break;
}
break;
}
}
}
case 10:
if (Convert.ToInt32(tickerPrice[1]) == 4) // last price = 4
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 0].Value = tick_price.ToString();
if (dataGridView2[4, 0].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 0].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 0].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9) // Close previous day
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 0].Value = tick_price.ToString();
}
break;
case 11:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 1].Value = tick_price.ToString();
if (dataGridView2[4, 1].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 1].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 1].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 1].Value = tick_price.ToString();
}
break;
case 12:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 2].Value = tick_price.ToString();
if (dataGridView2[4, 2].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 2].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 2].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 2].Value = tick_price.ToString();
}
break;
case 13:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 3].Value = tick_price.ToString();
if (dataGridView2[4, 3].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 3].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 3].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 3].Value = tick_price.ToString();
}
break;
case 14:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 4].Value = tick_price.ToString();
if (dataGridView2[4, 4].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 4].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 4].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 4].Value = tick_price.ToString();
}
break;
case 15:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 5].Value = tick_price.ToString();
if (dataGridView2[4, 5].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 5].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 5].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 5].Value = tick_price.ToString();
}
break;
case 16:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 6].Value = tick_price.ToString();
if (dataGridView2[4, 6].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 6].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 6].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 6].Value = tick_price.ToString();
}
break;
case 17:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 7].Value = tick_price.ToString();
if (dataGridView2[4, 7].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 7].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 7].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 7].Value = tick_price.ToString();
}
break;
case 18:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 8].Value = tick_price.ToString();
if (dataGridView2[4, 8].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 8].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 8].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 8].Value = tick_price.ToString();
}
break;
case 19:
if (Convert.ToInt32(tickerPrice[1]) == 4)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
tick_price = Math.Round(tick_price, 2);
dataGridView2[1, 9].Value = tick_price.ToString();
if (dataGridView2[4, 9].Value != null)
{
double previous_close = Convert.ToDouble(dataGridView2[4, 9].Value);
double change = Math.Round(((tick_price - previous_close) / previous_close) * 100, 2);
dataGridView2[3, 9].Value = change;
}
break;
}
if (Convert.ToInt32(tickerPrice[1]) == 9)
{
double tick_price = Convert.ToDouble(tickerPrice[2]);
dataGridView2[4, 9].Value = tick_price.ToString();
}
break;
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1) return; //check if row index is not selected (The index must not be negative)
if (dataGridView2.CurrentCell.ColumnIndex.Equals(0)) // indicates which column you get the info from 0 = first column
if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null)
cbSymbol.Text = Convert.ToString(dataGridView2.CurrentCell.Value);
getData(); // requests the data for the symbol in the order form
}
private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int loc = dataGridView2.Rows[e.RowIndex].Index;
dataGridView2[1, loc].Value = null;
dataGridView2[2, loc].Value = null;
dataGridView2[3, loc].Value = null;
dataGridView2[4, loc].Value = null;
if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null)
{
int rowIndex = dataGridView2.Rows[e.RowIndex].Index + 10;
ibClient.ClientSocket.cancelMktData(rowIndex);
string symbol = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// create a contract for streaming data
IBApi.Contract contract = new IBApi.Contract();
// Create a new TagValueList object (for API version 9.71 and later)
List<IBApi.TagValue> mktDataOptiones = new List<IBApi.TagValue>();
// Set stock symbol that the scanner found
contract.Symbol = symbol;
// 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";
ibClient.ClientSocket.reqMarketDataType(1);
ibClient.ClientSocket.reqMktData(rowIndex, contract, "", false, false, mktDataOptiones);
}
}
private void tabControl2_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLower(e.KeyChar))
{
e.KeyChar = char.ToUpper(e.KeyChar);
}
}
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView2.Rows[e.RowIndex].Cells[3].Value != null)
{
try
{
double change_percent = Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString().Trim());
change_percent = change_percent * 100;
string final_percent = change_percent.ToString("#.00\\%");
if (change_percent > 0)
{
dataGridView2.Rows[e.RowIndex].Cells[3].Style.ForeColor = System.Drawing.Color.Black;
}
else if (change_percent < 0)
{
dataGridView2.Rows[e.RowIndex].Cells[3].Style.ForeColor = System.Drawing.Color.Red;
}
}
catch
{
}
}
}
No comments:
Post a Comment