public string RunProcedures() { // Request Data from IB Console.WriteLine("{0}: Obtaining position info ...", DateTime.Now.ToShortTimeString()); string holdingsFile = UControl.HoldingsFolder + runDate.ToString("yyyyMMdd") + ".txt"; myPortfolio = ibClient.reqPositions(); myPortfolio = myPortfolio.Where(pos => (pos.contract.SecType == "OPT" && pos.account == AccountNumber)).ToList(); // Get prices of contracts List <CompleteTick_Struct> ContractPrices = IBPrice2DT(); // Filter invalid contracts for (int i = 0; i < myPortfolio.Count; i++) { Position_Struct ps = myPortfolio[i]; if (ContractPrices[i].getTick(tickPreference.LastPreference) > ContractPriceMin) { try { PositionCnt.Add(ps.contract.Symbol, 0); } catch (ArgumentException) { } PositionCnt[ps.contract.Symbol] -= (int)ps.pos; } } // Output position file using (StreamWriter sw = new StreamWriter(holdingsFile, false)) { foreach (string symbol in PositionCnt.Keys) { sw.WriteLine("{0},{1}", symbol, PositionCnt[symbol]); } } return(holdingsFile); }
public void ClosePositionsAuto() { // Get all positions myPortfolio = ibClient.reqPositions(); myPortfolio = myPortfolio.Where(pos => (pos.contract.SecType == "OPT" && pos.account == OControl.defAccount)).ToList(); List <CompleteTick_Struct> ContractPrices = IBPrice2DT(); // Close positions for (int i = 0; i < myPortfolio.Count; i++) { Position_Struct ps = myPortfolio[i]; DateTime lastTradingDate = DateTime.ParseExact(ps.contract.LastTradeDateOrContractMonth, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); if (ContractPrices[i].getTick(tickPreference.LastPreference) < ContractPriceMin && (lastTradingDate - DateTime.Today).Days > ContractTimeSpan && ps.pos != 0) { Contract myContract = ps.contract; myContract.Exchange = "SMART"; // Construct order Order myOrder = new Order(); myOrder.Account = OControl.defAccount; myOrder.Action = (ps.pos > 0) ? "SELL" : "BUY"; myOrder.TotalQuantity = (int)(-ps.pos); myOrder.OrderType = "LMT"; myOrder.LmtPrice = 0.01; myOrder.Tif = "DAY"; myOrder.Transmit = true; // Place order nextID++; ibClient.placeOrder(nextID, ps.contract, myOrder); // Acknowledgement Console.WriteLine(string.Format("Order {0} has been placed.", ps.contract.LocalSymbol)); } } }