private void requestData()
 {
     //Define contract
     TWSLib.IContract Contract = Tws1.createContract();
     Contract.symbol   = "USD";
     Contract.secType  = "CASH";
     Contract.exchange = "IDEALPRO";
     Contract.currency = "JPY";
     //Make request
     Tws1.reqMktDataEx(1, Contract, "", 1);
 }
        private void btnReconnect_Click(object sender, EventArgs e)
        {
            axTws1.connect("", 7496, 0);

            /*
             * listOfPrices.Clear();
             * OurChart.Series["Price"].Points.Clear();
             * // Cancel the Initial Market data Connection when pressing connect
             * axTws1.cancelMktData(0);
             * // Create a new contract object
             * TWSLib.IContract ContractInfo = axTws1.createContract();
             * // Create a new TagValueList object (for API version 9.71)
             * TWSLib.ITagValueList mktDataOptions = axTws1.createTagValueList();
             * // Now fill the ContractInfo object with the necessary information
             * // Contract identifier (use a unique number for each security)
             * ContractInfo.conId = 0;
             * // Stock symbol
             * ContractInfo.symbol = tbSymbol.Text;
             * // Type of instrument: Stock=STK,Option=OPT,Future=FUT, etc.
             * ContractInfo.secType = "STK";
             * // The Options or Futures expiration data in the format YYYYMM
             * ContractInfo.expiry = "";
             * // The Options Strike Price
             * ContractInfo.strike = 0;
             * // The Options "PUT" or "CALL" rights
             * ContractInfo.right = "";
             * // The contract multiplier for Futures or Options
             * ContractInfo.multiplier = "";
             * // The destination of order or request. "SMART" =IB order router
             * ContractInfo.exchange = sMARTToolStripMenuItem.Text;
             * // The primary exchange where the instrument trades.
             * // NYSE,  NASDAQ, AMEX, BATS, ARCA, PHLX etc.
             * ContractInfo.primaryExchange = tbExchange.Text;
             * // The currency of the exchange USD or GBP or CAD or EUR, etc.
             * ContractInfo.currency = "USD";
             *
             * // Make the request for streaming market data
             * //   TickerID - use a unique number for each request
             * //   ContractInfo - the Contract object containing the financial instrument
             * //   genericTicks - Can be used to get different tick types (use "" for now)
             * //   Snapshot (0 for streaming, 1 to get a single data point)
             * axTws1.reqMktDataEx(0, ContractInfo, "", 0, mktDataOptions);
             * //   Note: If you receive an error asking for 5 parameters, try passing mktDataOptions
             * //   as the fifth parameter - this may have been fixed in newer version of the API
             * //   axTws1.reqMktDataEx(0, ContractInfo, "", 0, mktDataOptions);
             */


            listOfPrices.Clear();
            listOfHighs.Clear();
            listOfLows.Clear();
            histCloses.Clear();
            OurChart.Series["Price"].Points.Clear();
            OurChart.Series["SMA-20 D"].Points.Clear();
            OurChart.Series["EMA-20 D"].Points.Clear();
            chtStocks.Series["Closing Prices"].Points.Clear();

            // Cancel the Initial Market data Connection when pressing connect
            axTws1.cancelMktData(0);
            // Create a new TagValueList object (for API version 9.71)
            TWSLib.ITagValueList mktDataOptions = axTws1.createTagValueList();
            // Create a new contract object
            TWSLib.IContract ContractInfo = axTws1.createContract();
            // Now fill the ContractInfo object with the necessary information
            // Contract ID
            ContractInfo.conId = 0;
            // Stock symbol
            ContractInfo.symbol = tbSymbol.Text;
            // Type of instrument: Stock=STK,Option=OPT,Future=FUT, etc.
            ContractInfo.secType = "STK";
            // The destination of order or request. "SMART" =IB order router
            ContractInfo.exchange = sMARTToolStripMenuItem.Text;
            // The primary exchange for the stock.
            ContractInfo.primaryExchange = tbPrimaryExchange.Text;
            // The currency of the exchange USD or GBP or CAD or EUR, etc.
            ContractInfo.currency = "USD";
            // Make the request for Real Time Bars. Parameters are:
            //   TickerID       Use a unique number for each request
            //   ContractInfo   The Contract object specifying the financial instrument
            //   barSize        The size of a data bar - 5 seconds is the current setting
            //   whatToShow     Use "TRADES" to show OHLC trades. Can also use
            //                  "BID", "ASK" or "MID" for quotes
            //   useRTH         Use Regular Trading Hours only (set to 1)
            // Get open, high, low, close every ~ 5 sec.
            axTws1.reqRealTimeBarsEx(0, ContractInfo, 5, "TRADES", 1, mktDataOptions);
            // Get real time/streaming prices
            axTws1.reqMktDataEx(0, ContractInfo, "", 0, mktDataOptions);



            // Now call reqHistoricalDataEx with parameters:
            // tickerId, Contract, endDateTime, durationStr, barSize, WhatToShow,
            // useRTH, formatDate
            // for api version 9.71
            TWSLib.ITagValueList ChartOptions = axTws1.createTagValueList();

            axTws1.reqHistoricalDataEx(1, ContractInfo,
                                       this.tbDate.Text,
                                       this.tbDuration.Text,
                                       this.tbBars.Text,
                                       "TRADES", 1, 1, ChartOptions);

            listBox1.Items.Add("You have viewing a different Stock. CURRENTLY VIEWING: " + ContractInfo.symbol);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }// end Reconnect code segment.
        }// end comboStrategy_SelectedIndexChanged

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            // For Ta-Lib
            double[] priceArray = listOfPrices.ToArray();
            int      timeFrame  = Convert.ToInt32(nbTimeFrame.Value);

            // For IB
            TWSLib.IContract ContractInfo = axTws1.createContract();
            TWSLib.IOrder    OrderInfo    = axTws1.createOrder();
            ContractInfo.conId           = 0;
            ContractInfo.symbol          = tbSymbol.Text;
            ContractInfo.secType         = "STK";
            ContractInfo.exchange        = "SMART"; // Needs work
            ContractInfo.primaryExchange = this.tbPrimaryExchange.Text;
            ContractInfo.currency        = "USD";
            OrderInfo.orderId            = int.Parse(this.tbOrderId.Text);
            OrderInfo.action             = this.tbAction.Text;
            OrderInfo.totalQuantity      = int.Parse(this.tbQuantity.Text);
            OrderInfo.orderType          = this.tbOrderType.Text;
            OrderInfo.lmtPrice           = double.Parse(this.tbLimitPrice.Text);
            OrderInfo.timeInForce        = "DAY";

            int n = 0;

            while (true) // Keep looping
            {
                // Run if a new price has been appended to the list
                if (n != listOfPrices.Count())
                {
                    if (strategyToApply == "smaCrossover")
                    {
                        int      outBegIdxSma, outNbElementSma;
                        double[] outputSma = new double[priceArray.Length];

                        Core.Sma(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxSma, out outNbElementSma, outputSma);

                        if (outputSma.Last() < listOfPrices.Last())
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    else if (strategyToApply == "smaCrossunder")
                    {
                        int      outBegIdxSma, outNbElementSma;
                        double[] outputSma = new double[priceArray.Length];

                        Core.Sma(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxSma, out outNbElementSma, outputSma);

                        if (outputSma.Last() > listOfPrices.Last())
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    else if (strategyToApply == "emaCrossover")
                    {
                        int      outBegIdxEma, outNbElementEma;
                        double[] outputEma = new double[priceArray.Length];

                        Core.Ema(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxEma, out outNbElementEma, outputEma);

                        if (outputEma.Last() > listOfPrices.Last())
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    else if (strategyToApply == "emaCrossunder")
                    {
                        int      outBegIdxEma, outNbElementEma;
                        double[] outputEma = new double[priceArray.Length];

                        Core.Ema(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxEma, out outNbElementEma, outputEma);

                        if (outputEma.Last() < listOfPrices.Last())
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    else if (strategyToApply == "rsiOverSold")
                    {
                        int      outBegIdxRsi, outNbElementRsi;
                        double[] outputRsi = new double[priceArray.Length];

                        Core.Rsi(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxRsi, out outNbElementRsi, outputRsi);

                        if (outputRsi.Last() < 30)
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    else if (strategyToApply == "rsiOverBought")
                    {
                        int      outBegIdxRsi, outNbElementRsi;
                        double[] outputRsi = new double[priceArray.Length];

                        Core.Rsi(0, listOfPrices.Count - 1, priceArray, timeFrame, out outBegIdxRsi, out outNbElementRsi, outputRsi);

                        if (outputRsi.Last() > 70)
                        {
                            this.axTws1.placeOrderEx(int.Parse(this.tbOrderId.Text), ContractInfo, OrderInfo);
                        }
                    }

                    n = listOfPrices.Count();
                } // end: if (n != listOfPrices.Count())
            }     // end: while (true)
        }         // end btnSubmit_Click