/* IMPORTANT: always use your paper trading account. The code below will submit orders as part of the demonstration. */ /* IB will not be responsible for accidental executions on your live account. */ /* Any stock or option symbols displayed are for illustrative purposes only and are not intended to portray a recommendation. */ /* Before contacting our API support team please refer to the available documentation. */ public static int Main(string[] args) { EWrapperImpl testImpl = new EWrapperImpl(); EClientSocket clientSocket = testImpl.ClientSocket; EReaderSignal readerSignal = testImpl.Signal; //! [connect] clientSocket.eConnect("127.0.0.1", 7496, 0); //! [connect] //! [ereader] //Create a reader to consume messages from the TWS. The EReader will consume the incoming messages and put them in a queue var reader = new EReader(clientSocket, readerSignal); reader.Start(); //Once the messages are in the queue, an additional thread need to fetch them new Thread(() => { while (clientSocket.IsConnected()) { readerSignal.waitForSignal(); reader.processMsgs(); } }) { IsBackground = true }.Start(); //! [ereader] /*************************************************************************************************************************************************/ /* One (although primitive) way of knowing if we can proceed is by monitoring the order's nextValidId reception which comes down automatically after connecting. */ /*************************************************************************************************************************************************/ while (testImpl.NextOrderId <= 0) { } testIBMethods(clientSocket, testImpl.NextOrderId); Console.WriteLine("Disconnecting..."); clientSocket.eDisconnect(); return(0); }
/* IMPORTANT: always use your paper trading account. The code below will submit orders as part of the demonstration. */ /* IB will not be responsible for accidental executions on your live account. */ /* Any stock or option symbols displayed are for illustrative purposes only and are not intended to portray a recommendation. */ /* Before contacting our API support team please refer to the available documentation. */ public static int Main(string[] args) { EWrapperImpl testImpl = new EWrapperImpl(); EClientSocket clientSocket = testImpl.ClientSocket; EReaderSignal readerSignal = testImpl.Signal; //! [connect] clientSocket.eConnect("127.0.0.1", 7497, 0); //! [connect] //! [ereader] //Create a reader to consume messages from the TWS. The EReader will consume the incoming messages and put them in a queue var reader = new EReader(clientSocket, readerSignal); reader.Start(); //Once the messages are in the queue, an additional thread need to fetch them new Thread(() => { while (clientSocket.IsConnected()) { readerSignal.waitForSignal(); reader.processMsgs(); } }) { IsBackground = true }.Start(); int tickerId = 100; clientSocket.reqMarketDataType(2); getStockPrice(clientSocket, "AAPL", tickerId); Thread.Sleep(300000); Console.WriteLine("Disconnecting..."); clientSocket.eDisconnect(); return(0); }
public static int Main(string[] args) { EWrapperImpl testImpl = new EWrapperImpl(); testImpl.ClientSocket.eConnect("127.0.0.1", 7496, 222); while (testImpl.NextOrderId <= 0) { } //for (int i = 0; i < 2; i++) { testConnectDisconnect(testImpl); } Console.WriteLine("Disconnecting..."); testImpl.ClientSocket.eDisconnect(); return 0; }
public static int Main(string[] args) { EWrapperImpl testImpl = new EWrapperImpl(); testImpl.ClientSocket.eConnect("127.0.0.1", 7496, 0, false); /*************************************************************************************************************************************************/ /* One good way of knowing if we can proceed is by monitoring the order's nextValidId reception which comes down automatically after connecting. */ /*************************************************************************************************************************************************/ while (testImpl.NextOrderId <= 0) { } testIBMethods(testImpl); Console.WriteLine("Disconnecting..."); testImpl.ClientSocket.eDisconnect(); return(0); }
public static int Main(string[] args) { EWrapperImpl testImpl = new EWrapperImpl(); testImpl.ClientSocket.eConnect("127.0.0.1", 7496, 222); while (testImpl.NextOrderId <= 0) { } //for (int i = 0; i < 2; i++) { testConnectDisconnect(testImpl); } Console.WriteLine("Disconnecting..."); testImpl.ClientSocket.eDisconnect(); return(0); }
private static void testConnectDisconnect(EWrapperImpl wrapper) { //Order order = OrderSamples.LimitOrder(); //Contract contract = ContractSamples.getOption(); //wrapper.ClientSocket.reqMktData(1, ContractSamples.getOption(), "", false); //wrapper.ClientSocket.reqMktData(3, ContractSamples.getEuropeanStock(), "", false); //wrapper.ClientSocket.reqGlobalCancel(); //wrapper.ClientSocket.reqCurrentTime(); //wrapper.ClientSocket.reqMktData(1, ContractSamples.getForex(), "", false); //wrapper.ClientSocket.calculateImpliedVolatility(2, ContractSamples.getOption(), 10, 345); //wrapper.ClientSocket.calculateOptionPrice(3, ContractSamples.getOption(), 4.69, 345); //wrapper.ClientSocket.reqAccountSummary(1, "All", AccountSummaryTags.GetAllTags()); //wrapper.ClientSocket.reqAccountUpdates(true, "DU150462"); //wrapper.ClientSocket.reqAllOpenOrders(); //wrapper.ClientSocket.reqAutoOpenOrders(true); //wrapper.ClientSocket.reqOpenOrders(); //wrapper.ClientSocket.placeOrder(wrapper.NextOrderId, ContractSamples.getForex(), order); wrapper.ClientSocket.placeOrder(wrapper.NextOrderId, ContractSamples.getComboContract(), OrderSamples.LimitOrderForComboWithLegPrice()); //wrapper.ClientSocket.reqContractDetails(1, ContractSamples.getForex()); //wrapper.ClientSocket.reqExecutions(1, new ExecutionFilter()); //wrapper.ClientSocket.reqMktData(2, ContractSamples.getForex(), "", false); //wrapper.ClientSocket.reqFundamentalData(1, ContractSamples.getEuropeanStock(), "snapshot"); //wrapper.ClientSocket.reqHistoricalData(1, ContractSamples.getEurUsdForex(), "20130722 23:59:59", "1 D", "1 hour", "MIDPOINT", 1, 1); //wrapper.ClientSocket.cancelHistoricalData(1); //wrapper.ClientSocket.reqFundamentalData(2, ContractSamples.getEuropeanStock(), "snapshot"); //wrapper.ClientSocket.reqIds(-1); //wrapper.ClientSocket.reqManagedAccts(); //wrapper.ClientSocket.reqMarketDataType(1); //wrapper.ClientSocket.reqMarketDepth(1, ContractSamples.getForex(), 5); //wrapper.ClientSocket.reqNewsBulletins(true); //wrapper.ClientSocket.reqPositions(); //wrapper.ClientSocket.reqRealTimeBars(1, ContractSamples.getForex(), -1, "MIDPOINT", true); //wrapper.ClientSocket.reqScannerParameters(); //wrapper.ClientSocket.exerciseOptions(1, ContractSamples.getOption(), 1, 20, null, 1); //wrapper.ClientSocket.reqScannerSubscription(1, GetScannerSubscription()); //wrapper.ClientSocket.requestFA(Constants.FaProfiles); //NOT WORKING (ALSO CHECKED WITH JAVA 969) wrapper.ClientSocket.setServerLogLevel(1); Thread.Sleep(500000); }
private static void testConnectDisconnect(EWrapperImpl wrapper) { //Order order = OrderSamples.LimitOrder(); //Contract contract = ContractSamples.getOption(); //wrapper.ClientSocket.reqMktData(1, ContractSamples.getOption(), "", false); //wrapper.ClientSocket.reqMktData(3, ContractSamples.getEuropeanStock(), "", false); //wrapper.ClientSocket.reqGlobalCancel(); //wrapper.ClientSocket.reqCurrentTime(); //wrapper.ClientSocket.reqMktData(1, ContractSamples.getForex(), "", false); //wrapper.ClientSocket.calculateImpliedVolatility(2, ContractSamples.getOption(), 10, 345); //wrapper.ClientSocket.calculateOptionPrice(3, ContractSamples.getOption(), 4.69, 345); //wrapper.ClientSocket.reqAccountSummary(1, "All", AccountSummaryTags.GetAllTags()); //wrapper.ClientSocket.reqAccountUpdates(true, "DU150462"); //wrapper.ClientSocket.reqAllOpenOrders(); //wrapper.ClientSocket.reqAutoOpenOrders(true); //wrapper.ClientSocket.reqOpenOrders(); //wrapper.ClientSocket.placeOrder(wrapper.NextOrderId, ContractSamples.getForex(), order); wrapper.ClientSocket.placeOrder(wrapper.NextOrderId, ContractSamples.getComboContract(), OrderSamples.LimitOrderForComboWithLegPrice()); //wrapper.ClientSocket.reqContractDetails(1, ContractSamples.getForex()); //wrapper.ClientSocket.reqExecutions(1, new ExecutionFilter()); //wrapper.ClientSocket.reqMktData(2, ContractSamples.getForex(), "", false); //wrapper.ClientSocket.reqFundamentalData(1, ContractSamples.getEuropeanStock(), "snapshot"); //wrapper.ClientSocket.reqHistoricalData(1, ContractSamples.getEurUsdForex(), "20130722 23:59:59", "1 D", "1 hour", "MIDPOINT", 1, 1); //wrapper.ClientSocket.cancelHistoricalData(1); //wrapper.ClientSocket.reqFundamentalData(2, ContractSamples.getEuropeanStock(), "snapshot"); //wrapper.ClientSocket.reqIds(-1); //wrapper.ClientSocket.reqManagedAccts(); //wrapper.ClientSocket.reqMarketDataType(1); //wrapper.ClientSocket.reqMarketDepth(1, ContractSamples.getForex(), 5); //wrapper.ClientSocket.reqNewsBulletins(true); //wrapper.ClientSocket.reqPositions(); //wrapper.ClientSocket.reqRealTimeBars(1, ContractSamples.getForex(), -1, "MIDPOINT", true); //wrapper.ClientSocket.reqScannerParameters(); //wrapper.ClientSocket.exerciseOptions(1, ContractSamples.getOption(), 1, 20, null, 1); //wrapper.ClientSocket.reqScannerSubscription(1, GetScannerSubscription()); //wrapper.ClientSocket.requestFA(Constants.FaProfiles); //NOT WORKING (ALSO CHECKED WITH JAVA 969) wrapper.ClientSocket.setServerLogLevel(1); Thread.Sleep(500000); }
/*****************************************************************/ /* Below are few quick-to-test examples on the IB API functions. */ /*****************************************************************/ private static void testIBMethods(EWrapperImpl wrapper) { /***************************************************/ /*** Real time market data operations - Tickers ***/ /***************************************************/ /*** Requesting real time market data ***/ //wrapper.ClientSocket.reqMarketDataType(2); //wrapper.ClientSocket.reqMktData(1001, ContractSamples.getEurUsdForex(), "", false, GetFakeParameters(3)); //wrapper.ClientSocket.reqMktData(1002, ContractSamples.getOption(), "", false, GetFakeParameters(3)); //wrapper.ClientSocket.reqMktData(1003, ContractSamples.getEuropeanStock(), "", false); //Thread.Sleep(2000); /*** Canceling the market data subscription ***/ //wrapper.ClientSocket.cancelMktData(1001); //wrapper.ClientSocket.cancelMktData(1002); //wrapper.ClientSocket.cancelMktData(1003); /********************************************************/ /*** Real time market data operations - Market Depth ***/ /********************************************************/ /*** Requesting the Deep Book ***/ //wrapper.ClientSocket.reqMarketDepth(2001, ContractSamples.getEurGbpForex(), 5, GetFakeParameters(2)); //Thread.Sleep(2000); /*** Canceling the Deep Book request ***/ //wrapper.ClientSocket.cancelMktDepth(2001); /**********************************************************/ /*** Real time market data operations - Real Time Bars ***/ /**********************************************************/ /*** Requesting real time bars ***/ //wrapper.ClientSocket.reqRealTimeBars(3001, ContractSamples.getEurGbpForex(), -1, "MIDPOINT", true, GetFakeParameters(4)); //Thread.Sleep(2000); /*** Canceling real time bars ***/ //wrapper.ClientSocket.cancelRealTimeBars(3001); /**************************************************************/ /*** Real time market data operations - Streamed or Frozen ***/ /**************************************************************/ /*** Switch to frozen or streaming***/ //wrapper.ClientSocket.reqMarketDataType(1); /**********************************/ /*** Historical Data operations ***/ /**********************************/ /*** Requesting historical data ***/ //wrapper.ClientSocket.reqHistoricalData(4001, ContractSamples.getEurGbpForex(), "20130722 23:59:59", "1 D", "1 min", "MIDPOINT", 1, 1, GetFakeParameters(4)); //wrapper.ClientSocket.reqHistoricalData(4002, ContractSamples.getEuropeanStock(), "20131009 23:59:59", "10 D", "1 min", "TRADES", 1, 1); /*** Canceling historical data requests ***/ //wrapper.ClientSocket.cancelHistoricalData(4001); //wrapper.ClientSocket.cancelHistoricalData(4002); /*************************/ /*** Options Specifics ***/ /*************************/ /*** Calculating implied volatility ***/ //wrapper.ClientSocket.calculateImpliedVolatility(5001, ContractSamples.getOption(), 5, 85, GetFakeParameters(6)); /*** Canceling implied volatility ***/ //wrapper.ClientSocket.cancelCalculateImpliedVolatility(5001); /*** Calculating option's price ***/ //wrapper.ClientSocket.calculateOptionPrice(5002, ContractSamples.getOption(), 0.22, 85, GetFakeParameters(1)); /*** Canceling option's price calculation ***/ //wrapper.ClientSocket.cancelCalculateOptionPrice(5002); /*** Exercising options ***/ //wrapper.ClientSocket.exerciseOptions(5003, ContractSamples.GetSANTOption(), 1, 1, null, 1); /****************************/ /*** Contract information ***/ /****************************/ //wrapper.ClientSocket.reqContractDetails(6001, ContractSamples.GetbyIsin()); //wrapper.ClientSocket.reqContractDetails(210, ContractSamples.getOptionForQuery()); //wrapper.ClientSocket.reqContractDetails(211, ContractSamples.GetBondForQuery()); /***********************/ /*** Market Scanners ***/ /***********************/ /*** Requesting all available parameters which can be used to build a scanner request ***/ //wrapper.ClientSocket.reqScannerParameters(); /*** Triggering a scanner subscription ***/ //wrapper.ClientSocket.reqScannerSubscription(7001, ScannerSubscriptionSamples.GetScannerSubscription(), GetFakeParameters(5)); /*** Canceling the scanner subscription ***/ //wrapper.ClientSocket.cancelScannerSubscription(7001); /*****************************/ /*** Reuter's Fundamentals ***/ /*****************************/ /*** Requesting Fundamentals ***/ //wrapper.ClientSocket.reqFundamentalData(8001, ContractSamples.GetUSStock(), "snapshot", GetFakeParameters(4)); /*** Camceling fundamentals request ***/ //wrapper.ClientSocket.cancelFundamentalData(8001); /***********************/ /*** IB's Bulletins ***/ /***********************/ /*** Requesting Interactive Broker's news bulletins */ //wrapper.ClientSocket.reqNewsBulletins(true); /*** Canceling IB's news bulletins ***/ //wrapper.ClientSocket.cancelNewsBulletin(); /**************************/ /*** Account Management ***/ /**************************/ /*** Requesting managed accounts***/ //wrapper.ClientSocket.reqManagedAccts(); /*** Requesting accounts' summary ***/ //wrapper.ClientSocket.reqAccountSummary(9001, "All", AccountSummaryTags.GetAllTags()); /*** Subscribing to an account's information. Only one at a time! ***/ //wrapper.ClientSocket.reqAccountUpdates(true, "U150462"); /*** Requesting all accounts' positions. ***/ //wrapper.ClientSocket.reqPositions(); /**********************/ /*** Order handling ***/ /**********************/ /*** Requesting the next valid id ***/ //wrapper.ClientSocket.reqIds(-1); /*** Requesting all open orders ***/ //wrapper.ClientSocket.reqAllOpenOrders(); /*** Taking over orders to be submitted via TWS ***/ //wrapper.ClientSocket.reqAutoOpenOrders(true); /*** Requesting this API client's orders ***/ //wrapper.ClientSocket.reqOpenOrders(); /*** Placing/modifying an order - remember to ALWAYS increment the nextValidId after placing an order so it can be used for the next one! ***/ //Order order = OrderSamples.LimitOrder(); //order.OrderMiscOptions = GetFakeParameters(3); //wrapper.ClientSocket.placeOrder(wrapper.NextOrderId++, ContractSamples.getComboContract(), order); //wrapper.ClientSocket.placeOrder(wrapper.NextOrderId++, ContractSamples.getComboContract(), OrderSamples.LimitOrderForComboWithLegPrice()); //wrapper.ClientSocket.placeOrder(wrapper.NextOrderId++, ContractSamples.getVixComboContract(), OrderSamples.LimitOrder()); /*** Cancel all orders for all accounts ***/ //wrapper.ClientSocket.reqGlobalCancel(); /*** Request the day's executions ***/ //wrapper.ClientSocket.reqExecutions(10001, new ExecutionFilter()); /************************************/ /*** Financial Advisor Exclusive Operations ***/ /************************************/ /*** Requesting FA information ***/ //wrapper.ClientSocket.requestFA(Constants.FaAliases); //wrapper.ClientSocket.requestFA(Constants.FaGroups); //wrapper.ClientSocket.requestFA(Constants.FaProfiles); /*** Replacing FA information - Fill in with the appropriate XML string. ***/ //wrapper.ClientSocket.replaceFA(Constants.FaGroups, ""); /********************/ /*** Miscelaneous ***/ /********************/ /*** Request TWS' current time ***/ //wrapper.ClientSocket.reqCurrentTime(); /*** Setting TWS logging level ***/ //wrapper.ClientSocket.setServerLogLevel(1); /********************/ /*** Linking ***/ /********************/ //wrapper.ClientSocket.verifyRequest("a name", "9.71"); //wrapper.ClientSocket.verifyMessage("apiData"); //wrapper.ClientSocket.queryDisplayGroups(123); //wrapper.ClientSocket.subscribeToGroupEvents(124, 1); //wrapper.ClientSocket.updateDisplayGroup(125, "contract info"); //wrapper.ClientSocket.unsubscribeFromGroupEvents(124); //Thread.Sleep(3000); Console.WriteLine("Done"); Thread.Sleep(500000); }