private static void accountOperations(EClientSocket client) { /*** Requesting managed accounts***/ //! [reqmanagedaccts] client.reqManagedAccts(); //! [reqmanagedaccts] /*** Requesting accounts' summary ***/ Thread.Sleep(2000); //! [reqaaccountsummary] client.reqAccountSummary(9001, "All", AccountSummaryTags.GetAllTags()); //! [reqaaccountsummary] //! [reqaaccountsummaryledger] client.reqAccountSummary(9002, "All", "$LEDGER"); //! [reqaaccountsummaryledger] Thread.Sleep(2000); //! [reqaaccountsummaryledgercurrency] client.reqAccountSummary(9003, "All", "$LEDGER:EUR"); //! [reqaaccountsummaryledgercurrency] Thread.Sleep(2000); //! [reqaaccountsummaryledgerall] client.reqAccountSummary(9004, "All", "$LEDGER:ALL"); //! [reqaaccountsummaryledgerall] //! [cancelaaccountsummary] client.cancelAccountSummary(9001); client.cancelAccountSummary(9002); client.cancelAccountSummary(9003); client.cancelAccountSummary(9004); //! [cancelaaccountsummary] /*** Subscribing to an account's information. Only one at a time! ***/ Thread.Sleep(2000); //! [reqaaccountupdates] client.reqAccountUpdates(true, "U150462"); //! [reqaaccountupdates] Thread.Sleep(2000); //! [cancelaaccountupdates] client.reqAccountUpdates(false, "U150462"); //! [cancelaaccountupdates] //! [reqaaccountupdatesmulti] client.reqAccountUpdatesMulti(9002, "U150462", "EUstocks", true); //! [reqaaccountupdatesmulti] Thread.Sleep(2000); /*** Requesting all accounts' positions. ***/ //! [reqpositions] client.reqPositions(); //! [reqpositions] Thread.Sleep(2000); //! [cancelpositions] client.cancelPositions(); //! [cancelpositions] //! [reqpositionsmulti] client.reqPositionsMulti(9003, "DU74649", "EUstocks"); //! [reqpositionsmulti] }
private void Request() { ClientSocket.reqManagedAccts(); ClientSocket.reqAllOpenOrders(); ClientSocket.reqAutoOpenOrders(true); ClientSocket.reqPositions(); // notify to swich reqAccountUpdates() to update portfolio ClientSocket.reqAccountSummary(1, "All", AccountSummaryTags.GetAllTags()); //ClientSocket.reqAccountUpdates(); }
public async Task <List <AccountSummaryMessage> > GetAccountSummary() { var accountSummary = await this.Dispatch <AccountSummaryMessage>((requestId) => { this.Log("Requesting Account Summary..."); _ibClient.ClientSocket.reqAccountSummary(requestId, "All", AccountSummaryTags.GetAllTags()); return(true); }); //we don't want a subscription if (accountSummary.Count > 0) { _ibClient.ClientSocket.cancelAccountSummary(accountSummary[0].RequestId); } return(accountSummary); }
public static int Main(string[] args) { // specify account and strategy string IBaccount = TradeBooks.accountInit(); string tradingModel = "es_growth"; string tradingInstrument = "ES"; int multipler = 50; int tryCNT = 1; int tryGap = (int)openBook.gap * 20; int bestCNT = 0; double[] Returns = new double[2]; double[] Premium = new double[tryCNT]; int[] LegB_ID = new int[tryCNT]; int[] LegB = new int[tryCNT]; int legS; int legS_ID; openBook.positionCNT = 1; for (int n = 0; n < openBook.positionCNT; n++) { openBook.symbol[n] = tradingInstrument; openBook.multipler[n] = multipler; } //! start the connection int[] channel = new int[2]; channel = TradeBooks.channelSetup(IBaccount, tradingModel); EClientSocket clientSocket = Allture.ClientSocket; EReaderSignal readerSignal = Allture.Signal; //! [connect] clientSocket.eConnect("127.0.0.1", channel[0], channel[1]); //! [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 can be created to fetch them new Thread(() => { while (clientSocket.IsConnected()) { readerSignal.waitForSignal(); reader.processMsgs(); } }) { IsBackground = true }.Start(); //! [ereader] //**** current time ***** string date_time, last_time; date_time = DateTime.Now.ToString("hh:mm:ss tt"); //************************** //*** Account Info *** //************************** clientSocket.reqAccountSummary(9001, "All", AccountSummaryTags.GetAllTags()); Thread.Sleep(1000); Console.WriteLine("account ID: " + Allture.account_ID); Console.WriteLine("account Value: " + Allture.account_value); Console.WriteLine("account_BuyingPower: " + Allture.account_BuyingPower); Console.WriteLine("account_InitMarginReq: " + Allture.account_InitMarginReq); Console.WriteLine("account_MaintMarginReq: " + Allture.account_MaintMarginReq); Console.WriteLine("account_ExcessLiquidity: " + Allture.account_ExcessLiquidity); Console.WriteLine("account_AvailableFunds: " + Allture.account_AvailableFunds); Console.WriteLine("\n"); Allture.remainingOrderSize = 100000; // set to maxium order size clientSocket.reqMarketDataType(1); // clientSocket.reqGlobalCancel(); Thread.Sleep(sleep1); while ((DateTime.Now > Convert.ToDateTime("09:30:00 AM")) && (DateTime.Now < Convert.ToDateTime("16:00:00 PM"))) { last_time = date_time; for (int n = 0; n < openBook.positionCNT; n++) { if (openBook.status[n] == "complete") { continue; } if (openBook.status[n] == "submit") { Allture.currOrderId = openBook.currOrderID[n]; Allture.remainingOrderSize = 0; Allture.checkOrderEnd = false; clientSocket.reqOpenOrders(); while (!Allture.checkOrderEnd) { Thread.Sleep(200); } if (Allture.remainingOrderSize == 0) { openBook.status[n] = "complete"; openBook.capital[n] = 0; openBook.size[n] = 0; continue; // if remaining size = 0, means this order has completely filled. then go to the next position } } legS = 2700; legS_ID = getContractID(clientSocket, Allture, openBook, n, legS, "FOP", "GLOBEX"); LegB[0] = legS - 100; LegB_ID[0] = getContractID(clientSocket, Allture, openBook, n, LegB[0], "FOP", "GLOBEX"); //******* check the order status again. if still exiting, cancel the order order, update the order size and capital if (openBook.status[n] == "submit") { Allture.currOrderId = openBook.currOrderID[n]; Allture.remainingOrderSize = 0; Allture.checkOrderEnd = false; clientSocket.reqOpenOrders(); while (!Allture.checkOrderEnd) { Thread.Sleep(200); } if (Allture.remainingOrderSize == 0) { openBook.status[n] = "complete"; openBook.capital[n] = 0; openBook.size[n] = 0; continue; } else { clientSocket.cancelOrder(openBook.currOrderID[n]); //cancel existing order Thread.Sleep(sleep1); openBook.status[n] = "cancel"; if (Allture.remainingOrderSize > openBook.size[n]) { Allture.remainingOrderSize = openBook.size[n]; } openBook.capital[n] = openBook.capital[n] - (openBook.size[n] - Allture.remainingOrderSize) * openBook.margin[n]; openBook.size[n] = (int)(openBook.usingCapital * openBook.capital[n] / 10000); } } else { openBook.size[n] = (int)(100); } //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], LegB_ID[0], legS_ID, openBook.multipler[n].ToString(), "GLOBEX"), OrderSamples.ComboLimitOrder("BUY", openBook.size[n], -1.0, false)); Thread.Sleep(sleep1); //********* update remaining of the trading book ************** openBook.status[n] = "submit"; openBook.currOrderID[n] = Allture.NextOrderId; openBook.legS[n] = legS; openBook.legB[n] = LegB[0]; openBook.premium[n] = 1.0; } } clientSocket.reqGlobalCancel(); Thread.Sleep(sleep1); Console.WriteLine("today's trading is done...time: " + DateTime.Now); clientSocket.eDisconnect(); return(0); }
public static int Main(string[] args) { // specify account and strategy string IBaccount = TradeBooks.accountInit(); string tradingModel = "monitor"; double indexPrice = 0, lastPrice = 0, vixPrice = 0, vix1Price = 0, vix2Price = 0; int legB_ID, legS_ID; double spread_premium; //*** set the monitoring parameters ********** bool autoExit = false; double SPXexitPoint = 30; double ESexitPoint = 30; double CLexitPoint = 3; double GCexitPoint = 20; double exitPoint = 100; int Alert1 = 0; int Alert2 = 0; bool[] alert1 = new bool[Constants.max_position]; bool[] alert2 = new bool[Constants.max_position]; bool[] alert3 = new bool[Constants.max_position]; bool alert_vix = false; // vix/vix1 backwardation bool alert_vix1 = false; // vix1/vix2 backwardation List <double> putSPXlegS = new List <double>(); List <double> callSPXlegS = new List <double>(); List <double> putESlegS = new List <double>(); List <double> callESlegS = new List <double>(); List <double> putCLlegS = new List <double>(); List <double> callCLlegS = new List <double>(); List <double> putGClegS = new List <double>(); List <double> callGClegS = new List <double>(); //**** current date and time ***** string date_time, today; date_time = DateTime.Now.ToString("h:mm:ss tt"); today = DateTime.Now.ToString("MMdd"); // today = "0928"; // if today is not the trading close day //***** set up excel ************** _Excel.Application excel = new _Excel.Application(); excel.DisplayAlerts = false; _Excel.Workbook monitor_wb = excel.Workbooks.Open("C:\\Users\\Jack\\Documents\\Companies\\allture\\Trading\\option_spread\\Accounts\\All_Account_Oct_2018.xlsm"); _Excel.Worksheet csheet = monitor_wb.Worksheets[today]; // string path = TradeBooks.xlsPathSetup(IBaccount); // _Excel.Workbook monitor_wb = excel.Workbooks.Open(path + IBaccount + "\\account_" + IBaccount + ".xlsm"); // _Excel.Worksheet csheet = monitor_wb.Worksheets["Positions"]; Console.WriteLine("\n start calculating, time: " + DateTime.Now + " \n"); //******* read position data to openBook position_xls_readin(csheet, openBook); monitor_wb.Close(0); excel.Quit(); for (int n = 0; n < openBook.positionCNT; n++) { if (openBook.symbol[n] == "SPX") { if (openBook.CallPut[n] == "Put") { putSPXlegS.Add(openBook.legS[n]); } else { callSPXlegS.Add(openBook.legS[n]); } } if (openBook.symbol[n] == "ES") { if (openBook.CallPut[n] == "Put") { putESlegS.Add(openBook.legS[n]); } else { callESlegS.Add(openBook.legS[n]); } } if (openBook.symbol[n] == "CL") { if (openBook.CallPut[n] == "Put") { putCLlegS.Add(openBook.legS[n]); } else { callCLlegS.Add(openBook.legS[n]); } } if (openBook.symbol[n] == "GC") { if (openBook.CallPut[n] == "Put") { putGClegS.Add(openBook.legS[n]); } else { callGClegS.Add(openBook.legS[n]); } } } callSPXlegS.Sort((s1, s2) => s1.CompareTo(s2)); callESlegS.Sort((s1, s2) => s1.CompareTo(s2)); callCLlegS.Sort((s1, s2) => s1.CompareTo(s2)); callGClegS.Sort((s1, s2) => s1.CompareTo(s2)); putSPXlegS.Sort((s1, s2) => s2.CompareTo(s1)); putESlegS.Sort((s1, s2) => s2.CompareTo(s1)); putCLlegS.Sort((s1, s2) => s2.CompareTo(s1)); putGClegS.Sort((s1, s2) => s2.CompareTo(s1)); //! start the connection ********************************// int[] channel = new int[2]; channel = TradeBooks.channelSetup(IBaccount, tradingModel); EClientSocket clientSocket = Allture.ClientSocket; EReaderSignal readerSignal = Allture.Signal; //! [connect] clientSocket.eConnect("127.0.0.1", channel[0], channel[1]); //! [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 can be created to fetch them new Thread(() => { while (clientSocket.IsConnected()) { readerSignal.waitForSignal(); reader.processMsgs(); } }) { IsBackground = true }.Start(); //! [ereader] //************************** //*** Account Info *** //************************** clientSocket.reqAccountSummary(9001, "All", AccountSummaryTags.GetAllTags()); Thread.Sleep(1000); Console.WriteLine("account ID: " + Allture.account_ID); Console.WriteLine("account Value: " + Allture.account_value); Console.WriteLine("account_BuyingPower: " + Allture.account_BuyingPower); Console.WriteLine("account_ExcessLiquidity: " + Allture.account_ExcessLiquidity); Console.WriteLine("account_AvailableFunds: " + Allture.account_AvailableFunds); Console.WriteLine("\n"); //******* calculate the adj std, return with spx, vix, vxx at order execuation in xls // runSPXstrategyCurr(trading_new, csheet, clientSocket); for (int n = 0; n < openBook.positionCNT; n++) { alert1[n] = false; alert2[n] = false; alert3[n] = false; } clientSocket.reqMarketDataType(1); Thread.Sleep(sleep1); while ((DateTime.Now > Convert.ToDateTime("09:30:00 AM")) && (DateTime.Now < Convert.ToDateTime("16:01:00 PM"))) { do { indexPrice = getMktData(clientSocket, Allture, "SPX", "IND", "", "CBOE"); }while (indexPrice == lastPrice); lastPrice = indexPrice; // to ensure vix, vix1, vix2 not consecutive, since their prices aren't chaning very much each time, can't be consecutive for lastPrice. do { vixPrice = getMktData(clientSocket, Allture, "VIX", "IND", "", "CBOE"); }while (vixPrice == lastPrice); lastPrice = vixPrice; do { indexPrice = getMktData(clientSocket, Allture, "SPX", "IND", "", "CBOE"); }while (indexPrice == lastPrice); lastPrice = indexPrice; // to ensure vix, vix1, vix2 not consecutive, since their prices aren't chaning very much each time, can't be consecutive for lastPrice. do { vix1Price = getMktData(clientSocket, Allture, "VIX", "FUT", openBook.vx1Exp, "CFE"); }while (vix1Price == lastPrice); lastPrice = vix1Price; do { indexPrice = getMktData(clientSocket, Allture, "SPX", "IND", "", "CBOE"); }while (indexPrice == lastPrice); lastPrice = indexPrice; // to ensure vix, vix1, vix2 not consecutive, since their prices aren't chaning very much each time, can't be consecutive for lastPrice. do { vix2Price = getMktData(clientSocket, Allture, "VIX", "FUT", openBook.vx2Exp, "CFE"); }while (vix2Price == lastPrice); lastPrice = vix2Price; Console.WriteLine("\n UX2 price: " + vix2Price + "\n"); if (!alert_vix && (vix1Price < vixPrice)) { SendMail("*****@*****.**", "Alert vix backwardation ! ", "VIX:" + vixPrice + " UX1:" + vix1Price); // SendMail("*****@*****.**", "Alert vix backwardation ! ", "VIX:" + vixPrice + " UX1:" + vix1Price); alert_vix = true; } if (!alert_vix1 && (vix2Price < vix1Price)) { SendMail("*****@*****.**", "Alert UX1 backwardation ! ", "UX1:" + vix1Price + " UX2:" + vix2Price); // SendMail("*****@*****.**", "Alert UX1 backwardation ! ", "UX1:" + vix1Price + " UX2:" + vix2Price); alert_vix1 = true; } for (int n = 0; n < openBook.positionCNT; n++) { if (openBook.size[n] == 0) { continue; } //********************************************************** //*** Real time market price - Real Time Index bid/ask/mid** //********************************************************** if (openBook.symbol[n] == "SPX") { if (openBook.CallPut[n] == "Put") { if (openBook.legS[n] < putSPXlegS[0]) { continue; } } do { indexPrice = getMktData(clientSocket, Allture, "SPX", "IND", "", "CBOE"); }while (indexPrice == lastPrice); lastPrice = indexPrice; Console.WriteLine("\n spx price: " + indexPrice + "\n"); exitPoint = SPXexitPoint; } else if (openBook.symbol[n] == "ES") { if (openBook.CallPut[n] == "Put") { if (openBook.legS[n] < putESlegS[0]) { continue; } } do { indexPrice = getMktData(clientSocket, Allture, "ES", "FUT", openBook.underlyingExp[n], "GLOBEX"); }while (indexPrice == lastPrice); lastPrice = indexPrice; Console.WriteLine("\n es price: " + indexPrice + "\n"); exitPoint = ESexitPoint; } else if (openBook.symbol[n] == "CL") { if (openBook.CallPut[n] == "Put") { if (openBook.legS[n] < putCLlegS[0]) { continue; } } do { indexPrice = getMktData(clientSocket, Allture, "CL", "FUT", openBook.underlyingExp[n], "NYMEX"); }while (indexPrice == lastPrice); lastPrice = indexPrice; Console.WriteLine("\n cl price: " + indexPrice + "\n"); exitPoint = CLexitPoint; } else if (openBook.symbol[n] == "GC") { if (openBook.CallPut[n] == "Put") { if (openBook.legS[n] < putGClegS[0]) { continue; } } do { indexPrice = getMktData(clientSocket, Allture, "GC", "FUT", openBook.underlyingExp[n], "NYMEX"); }while (indexPrice == lastPrice); lastPrice = indexPrice; Console.WriteLine("\n GC price: " + indexPrice + "\n"); exitPoint = GCexitPoint; } else { continue; } if (openBook.CallPut[n] == "Put") { if (!alert1[n] && (indexPrice <= (openBook.legS[n] + exitPoint + Alert1))) { // if alert1, sending email; SendMail("*****@*****.**", "Alert1 !! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); // SendMail("*****@*****.**", "Alert1 ! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); alert1[n] = true; } /* if (!alert2[n] && (indexPrice <= (openBook.legS[n] + exitPoint + Alert2))) * // { * // if alert2, sending email; * // SendMail("*****@*****.**", "Alert2 !! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.legS[n] + ", " + openBook.size[n] + ", " + openBook.accountID[n]); * // SendMail("*****@*****.**", "Alert2 ! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); * // alert2[n] = true; * // } * if (!alert3[n] && (indexPrice <= (openBook.legS[n] + exitPoint))) * { * // if alert3, sending email; * SendMail("*****@*****.**", "Alert3 !! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); * // SendMail("*****@*****.**", "Alert3 ! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + ", " + openBook.legS[n] + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); * alert3[n] = true; * } */ // very complicated for auto exit and high risk, need more time to think through !!! // seems if we don't directly get position info from IB, then should not do any auto exits if (autoExit) { if (alert3[n]) { //*** estimate the current spread's price and contract ID for combo orders if (openBook.symbol[n] == "SPX") { spread_premium = 0; // spread_premium = 0.05 + getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "OPT", "SMART"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "OPT", "SMART"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "OPT", "SMART"); //********* Place order *************************** clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "100", "SMART"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == putSPXlegS[0]) { putSPXlegS.RemoveAt(0); } // else if (openBook.legS[n] == putSPXlegS[1]) putSPXlegS.RemoveAt(1); // else if (openBook.legS[n] == putSPXlegS[2]) putSPXlegS.RemoveAt(2); } else if (openBook.symbol[n] == "ES") { spread_premium = 0; // spread_premium = 0.05 + getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "GLOBEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "GLOBEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "GLOBEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "50", "GLOBEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == putESlegS[0]) { putESlegS.RemoveAt(0); } // else if (openBook.legS[n] == putESlegS[1]) putESlegS.RemoveAt(1); // else if (openBook.legS[n] == putESlegS[2]) putESlegS.RemoveAt(2); } else if (openBook.symbol[n] == "CL") { spread_premium = 0; // spread_premium = getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "NYMEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "NYMEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "NYMEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "1000", "NYMEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == putESlegS[0]) { putCLlegS.RemoveAt(0); } // else if (openBook.legS[n] == putESlegS[1]) putESlegS.RemoveAt(1); // else if (openBook.legS[n] == putESlegS[2]) putESlegS.RemoveAt(2); } else if (openBook.symbol[n] == "GC") { spread_premium = 0; // spread_premium = getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "NYMEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "NYMEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "NYMEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "100", "NYMEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == putESlegS[0]) { putGClegS.RemoveAt(0); } // else if (openBook.legS[n] == putESlegS[1]) putESlegS.RemoveAt(1); // else if (openBook.legS[n] == putESlegS[2]) putESlegS.RemoveAt(2); } else { continue; } // very complicated, need more time to think through !!! one thought - just simply place order, don't cancel it, until it filled // seems if we don't directly get position info from IB, then should not do any auto exits } } } else if (openBook.CallPut[n] == "Call") { if (!alert3[n] && (indexPrice >= (openBook.legS[n] - exitPoint))) { // if alert3, sending email; SendMail("*****@*****.**", "call Alert3 !! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + openBook.legS[n] + ", " + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); SendMail("*****@*****.**", "call Alert3 ! " + openBook.model[n], openBook.symbol[n] + " " + indexPrice + ", " + ", " + openBook.legS[n] + openBook.expiration[n].Substring(4) + ", " + openBook.size[n] + ", " + openBook.accountID[n]); alert3[n] = true; } // very complicated for auto exit and high risk, need more time to think through !!! // seems if we don't directly get position info from IB, then should not do any auto exits // must have the auto exit. otherwise, you can't handle that many position mannually. and sychological/emotion is another issue. if (autoExit) { if (alert3[n]) { //*** estimate the current spread's price and contract ID for combo orders if (openBook.symbol[n] == "SPX") { // spread_premium = 0; spread_premium = 0.05 + getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "OPT", "SMART"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "OPT", "SMART"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "OPT", "SMART"); //********* Place order *************************** clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "100", "SMART"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == callSPXlegS[0]) { callSPXlegS.RemoveAt(0); } // else if (openBook.legS[n] == callSPXlegS[1]) callSPXlegS.RemoveAt(1); // else if (openBook.legS[n] == callSPXlegS[2]) callSPXlegS.RemoveAt(2); } else if (openBook.symbol[n] == "ES") { // spread_premium = 0; spread_premium = 0.05 + getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "GLOBEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "GLOBEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "GLOBEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "50", "GLOBEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == callESlegS[0]) { callESlegS.RemoveAt(0); } // else if (openBook.legS[n] == callESlegS[1]) callESlegS.RemoveAt(1); // else if (openBook.legS[n] == callESlegS[2]) callESlegS.RemoveAt(2); else if (openBook.symbol[n] == "CL") { spread_premium = 0; // spread_premium = getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "NYMEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "NYMEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "NYMEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "1000", "NYMEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == callCLlegS[0]) { callCLlegS.RemoveAt(0); } // else if (openBook.legS[n] == callCLlegS[1]) callCLlegS.RemoveAt(1); // else if (openBook.legS[n] == callCLlegS[2]) callCLlegS.RemoveAt(2); } else if (openBook.symbol[n] == "GC") { spread_premium = 0; // spread_premium = getSpreadPremium(clientSocket, Allture, openBook, n, openBook.legS[n], openBook.legB[n], "FOP", "NYMEX"); legS_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legS[n], "FOP", "NYMEX"); legB_ID = getContractID(clientSocket, Allture, openBook, n, openBook.legB[n], "FOP", "NYMEX"); //********* Place order ************************ clientSocket.reqIds(-1); Thread.Sleep(sleep1); clientSocket.placeOrder(Allture.NextOrderId, ContractSamples.Contract_Combo(openBook.symbol[n], "BAG", openBook.expiration[n], openBook.CallPut[n], legB_ID, legS_ID, "100", "NYMEX"), OrderSamples.ComboLimitOrder("SELL", openBook.size[n], -spread_premium, false)); Thread.Sleep(sleep2); openBook.size[n] = 0; if (openBook.legS[n] == callCLlegS[0]) { callGClegS.RemoveAt(0); } // else if (openBook.legS[n] == callCLlegS[1]) callCLlegS.RemoveAt(1); // else if (openBook.legS[n] == callCLlegS[2]) callCLlegS.RemoveAt(2); } // very complicated, need more time to think through !!! one thought - just simply place order, don't cancel it, until it filled // seems if we don't directly get position info from IB, then should not do any auto exits } } } } } } Console.WriteLine("\n start finish, time: " + DateTime.Now + " \n"); return(0); }