// quoteDict has been moved to PairPos Dict // CSV writer for checking quotes // public void CSVWriter(int tickerID) // { // // create filename // string fileName = ""; // try // { // fileName = string.Format("{0}{1}_{2}.csv", QUOTE_FOLDER_DIR, tickerID, TickerSymbolDict[tickerID]); // } // catch (Exception) // { // Console.WriteLine("No ticker ID: {0}", tickerID); // return; // } // // // // delete the file if exists // if (File.Exists(fileName)) // { // File.Delete(fileName); // } // // try // { // using (FileStream fs = File.Create(fileName)) // { // // write title // Byte[] title = new UTF8Encoding(true).GetBytes("Time, Price, Size\n"); // fs.Write(title, 0, title.Length); // // // write contents // for (int i = 0; i < QuoteDict[tickerID].Count; i++) // { // QuoteTick tmpQtItm = QuoteDict[tickerID][i]; // string tmpStr = String.Format("{0},{1},{2}\n", tmpQtItm.QtTime, tmpQtItm.QtLastPrice, tmpQtItm.QtLastSize); // Byte[] line = new UTF8Encoding(true).GetBytes(tmpStr); // fs.Write(line, 0, line.Length); // } // } // } // catch (Exception) // { // throw; // } // } // go through the symbol dictionary, and fill out the PairPosition dictionary public void CreatePairObjs() { this.PairPosDict.Clear(); foreach (var KeyValuePair in this.TickerSymbolDict) { // skip etf if ((KeyValuePair.Key & 1) == 1) { continue; } PairPos newPair = new PairPos(); newPair.PairBeta = 0; // beta initialize int tmpEtfTID = (KeyValuePair.Key & 1024) + 1; // clear all digits in 1 to 9. Aka, clear stk infomation newPair.pairEtfLeg.TickerID = tmpEtfTID; try { newPair.pairEtfLeg.Symbol = this.TickerSymbolDict[tmpEtfTID]; } catch (Exception) { throw; } newPair.pairStkLeg.TickerID = KeyValuePair.Key; newPair.pairStkLeg.Symbol = KeyValuePair.Value; this.PairPosDict.Add(KeyValuePair.Key, newPair); } }
public PairPos createPair(PairSignal oneSignal) { Equities etfLeg = this.equityDict[oneSignal.EtfTID]; Equities stkLeg = this.equityDict[oneSignal.StkTID]; PairPos newPair = new PairPos(etfLeg, stkLeg); return(newPair); // blah blah //foreach (var KeyValuePair in this.TickerSymbolDict) //{ // // skip etf // if ((KeyValuePair.Key & 1) == 1) // continue; // PairPos newPair = new PairPos(); // newPair.PairBeta = 0; // beta initialize // int tmpEtfTID = (KeyValuePair.Key & 1024) + 1; // clear all digits in 1 to 9. Aka, clear stk infomation // newPair.pairEtfLeg.TickerID = tmpEtfTID; // try // { // newPair.pairEtfLeg.Symbol = this.TickerSymbolDict[tmpEtfTID]; // } // catch (Exception) // { // throw; // } // newPair.pairStkLeg.TickerID = KeyValuePair.Key; // newPair.pairStkLeg.Symbol = KeyValuePair.Value; // this.PairPosDict.Add(KeyValuePair.Key, newPair); //} }
// quoteDict has been moved to PairPos Dict // CSV writer for checking quotes // public void CSVWriter(int tickerID) // { // // create filename // string fileName = ""; // try // { // fileName = string.Format("{0}{1}_{2}.csv", QUOTE_FOLDER_DIR, tickerID, TickerSymbolDict[tickerID]); // } // catch (Exception) // { // Console.WriteLine("No ticker ID: {0}", tickerID); // return; // } // // // // delete the file if exists // if (File.Exists(fileName)) // { // File.Delete(fileName); // } // // try // { // using (FileStream fs = File.Create(fileName)) // { // // write title // Byte[] title = new UTF8Encoding(true).GetBytes("Time, Price, Size\n"); // fs.Write(title, 0, title.Length); // // // write contents // for (int i = 0; i < QuoteDict[tickerID].Count; i++) // { // QuoteTick tmpQtItm = QuoteDict[tickerID][i]; // string tmpStr = String.Format("{0},{1},{2}\n", tmpQtItm.QtTime, tmpQtItm.QtLastPrice, tmpQtItm.QtLastSize); // Byte[] line = new UTF8Encoding(true).GetBytes(tmpStr); // fs.Write(line, 0, line.Length); // } // } // } // catch (Exception) // { // throw; // } // } // go through the symbol dictionary, and fill out the PairPosition dictionary public void CreatePairObjs() { this.PairPosDict.Clear(); foreach (var KeyValuePair in this.TickerSymbolDict) { // skip etf if ((KeyValuePair.Key & 1) == 1) continue; PairPos newPair = new PairPos(); newPair.PairBeta = 0; // beta initialize int tmpEtfTID = (KeyValuePair.Key & 1024) + 1; // clear all digits in 1 to 9. Aka, clear stk infomation newPair.pairEtfLeg.TickerID = tmpEtfTID; try { newPair.pairEtfLeg.Symbol = this.TickerSymbolDict[tmpEtfTID]; } catch (Exception) { throw; } newPair.pairStkLeg.TickerID = KeyValuePair.Key; newPair.pairStkLeg.Symbol = KeyValuePair.Value; this.PairPosDict.Add(KeyValuePair.Key, newPair); } }
static void Main(string[] args) { //Connect EWrapperImpl.Instance.ClientSocket.eConnect("127.0.0.1", 7496, 0); Thread.Sleep(2000); #region test // Contract contract = new Contract(); // contract.Symbol = "DIA"; // contract.SecType = "STK"; // contract.Currency = "USD"; // contract.Exchange = "SMART"; // // Order tmpOrder = new Order(); // //tmpOrder.ClientId = 0; // tmpOrder.OrderId = EWrapperImpl.Instance.NextOrderId; // tmpOrder.Action = "SELL"; // tmpOrder.TotalQuantity = 20; // tmpOrder.OrderType = "MKT"; // // EWrapperImpl.Instance.ClientSocket.placeOrder(EWrapperImpl.Instance.NextOrderId, contract, tmpOrder); // Console.ReadKey(); #endregion //EWrapperImpl.Instance.getAllQuote(); // PairSignal tmpSignal = new PairSignal(); // tmpSignal.StkTID = 8; // CSCO // tmpSignal.EtfTID = 1; // tmpSignal.TrSignal = PairType.openLong; // // EWrapperImpl.Instance.processSignal(tmpSignal); // Console.ReadKey(); // // //EWrapperImpl.Instance.getAllQuote(); // // tmpSignal.TrSignal = PairType.closeLong; // EWrapperImpl.Instance.processSignal(tmpSignal); // // Console.ReadKey(); // self close PairSignal tmpSignal = new PairSignal(); tmpSignal.StkTID = 8; // CSCO tmpSignal.EtfTID = 1; tmpSignal.TrSignal = PairType.openLong; EWrapperImpl.Instance.processSignal(tmpSignal); Console.ReadKey(); PairPos tmpPair = EWrapperImpl.Instance.PairPosDict[tmpSignal.StkTID]; tmpPair.closeThisPosition(); #region request market data IB //Create and define a contract to fetch data for // Contract contract = new Contract(); // contract.Symbol = "EUR"; // contract.SecType = "CASH"; // contract.Currency = "GBP"; // contract.Exchange = "IDEALPRO"; // // //Invoke IB's ClientSocket's data request // ibClient.ClientSocket.reqMktData(1, contract, "", false, null); #endregion //Stay alive for a little while Console.ReadKey(); Console.WriteLine("The End."); }
public void processSignal(PairSignal oneSignal) { if (oneSignal.TrSignal == PairType.nullType) { // do nothing return; } if (!PairPosDict.ContainsKey(oneSignal.StkTID)) { PairPos newPair = this.createPair(oneSignal); PairPosDict.Add(oneSignal.StkTID, newPair); } // retrieve Pair information from pair dict, based on this signal PairPos tmpPair = this.PairPosDict[oneSignal.StkTID]; //tmpPair.ThisPairStatus = oneSignal.TrSignal; // TODO: remove this line later, this is just for test tmpPair.PairBeta = 1; if (tmpPair.PairBeta == 0) { // the beta of this pair is zero, this pair hasn't be completely set up. exit application throw new Exception("Please build your model first!"); } // create contract objects for both stk and etf Contract etfContract = new Contract(); etfContract.Symbol = tmpPair.EtfLeg.Symbol; etfContract.SecType = "STK"; // Both etf and stk are using the same security type, required by IB etfContract.Currency = "USD"; etfContract.Exchange = "SMART"; Contract stkContract = new Contract(); stkContract.Symbol = tmpPair.StkLeg.Symbol; stkContract.SecType = "STK"; stkContract.Currency = "USD"; stkContract.Exchange = "SMART"; // create order objects for both stk and etf Order etfOrder = new Order(); etfOrder.OrderId = this.NextOrderId; etfOrder.OrderType = "MKT"; Order stkOrder = new Order(); stkOrder.OrderId = this.NextOrderId + 1; stkOrder.OrderType = "MKT"; // pass order ID to Pair Dictionary switch (oneSignal.TrSignal) { case PairType.openLong: if (tmpPair.ThisPairStatus == PairType.openLong | tmpPair.ThisPairStatus == PairType.openShort) { throw new Exception(string.Format("Pair {0} and {1} already open, cannot open again!", tmpPair.EtfLeg.Symbol, tmpPair.StkLeg.Symbol)); } tmpPair.ThisPairStatus = PairType.openLong; tmpPair.EtfLeg.OpenOrderID = this.nextOrderId; tmpPair.StkLeg.OpenOrderID = this.nextOrderId + 1; tmpPair.CurrHoldingPeriod = 0; // open, holding period = 0 break; case PairType.openShort: if (tmpPair.ThisPairStatus == PairType.openLong | tmpPair.ThisPairStatus == PairType.openShort) { throw new Exception(string.Format("Pair {0} and {1} already open, cannot open again!", tmpPair.EtfLeg.Symbol, tmpPair.StkLeg.Symbol)); } tmpPair.ThisPairStatus = PairType.openShort; tmpPair.EtfLeg.OpenOrderID = this.nextOrderId; tmpPair.StkLeg.OpenOrderID = this.nextOrderId + 1; tmpPair.CurrHoldingPeriod = 0; break; case PairType.closeLong: if (tmpPair.ThisPairStatus == PairType.closeLong | tmpPair.ThisPairStatus == PairType.closeShort | tmpPair.ThisPairStatus == PairType.nullType) { throw new Exception(string.Format("Pair {0} and {1} not opened, cannot close!", tmpPair.EtfLeg.Symbol, tmpPair.StkLeg.Symbol)); } tmpPair.ThisPairStatus = PairType.closeLong; tmpPair.EtfLeg.CloseOrderID = this.nextOrderId; tmpPair.StkLeg.CloseOrderID = this.nextOrderId + 1; tmpPair.CurrHoldingPeriod = -1; // close, holding period = -1 break; case PairType.closeShort: if (tmpPair.ThisPairStatus == PairType.closeLong | tmpPair.ThisPairStatus == PairType.closeShort | tmpPair.ThisPairStatus == PairType.nullType) { throw new Exception(string.Format("Pair {0} and {1} not opened, cannot close!", tmpPair.EtfLeg.Symbol, tmpPair.StkLeg.Symbol)); } tmpPair.ThisPairStatus = PairType.closeShort; tmpPair.EtfLeg.CloseOrderID = this.nextOrderId; tmpPair.StkLeg.CloseOrderID = this.nextOrderId + 1; tmpPair.CurrHoldingPeriod = -1; break; default: throw new Exception("Wrong signal type. Press any key to start over..."); } this.PairPosDict[oneSignal.StkTID] = tmpPair; switch (oneSignal.TrSignal) { case PairType.openLong: // long etf short stk stkOrder.Action = "SELL"; stkOrder.TotalQuantity = Convert.ToInt32(tmpPair.InitEtfShare * tmpPair.PairBeta); stkOrder.OrderType = "MKT"; etfOrder.Action = "BUY"; etfOrder.TotalQuantity = tmpPair.InitEtfShare; etfOrder.OrderType = "MKT"; break; case PairType.openShort: // short etf long stk stkOrder.Action = "BUY"; stkOrder.TotalQuantity = Convert.ToInt32(tmpPair.InitEtfShare * tmpPair.PairBeta); stkOrder.OrderType = "MKT"; etfOrder.Action = "SELL"; etfOrder.TotalQuantity = tmpPair.InitEtfShare; etfOrder.OrderType = "MKT"; break; case PairType.closeLong: // close, sell etf buy cover stk stkOrder.Action = "BUY"; stkOrder.TotalQuantity = Convert.ToInt32(tmpPair.InitEtfShare * tmpPair.PairBeta); stkOrder.OrderType = "MKT"; etfOrder.Action = "SELL"; etfOrder.TotalQuantity = tmpPair.InitEtfShare; etfOrder.OrderType = "MKT"; break; case PairType.closeShort: // close, buy cover etf sell stk stkOrder.Action = "SELL"; stkOrder.TotalQuantity = Convert.ToInt32(tmpPair.InitEtfShare * tmpPair.PairBeta); stkOrder.OrderType = "MKT"; etfOrder.Action = "BUY"; etfOrder.TotalQuantity = tmpPair.InitEtfShare; etfOrder.OrderType = "MKT"; break; default: // not gonna happen throw new Exception("Wrong signal!"); break; } // TODO: add log file here this.ClientSocket.placeOrder(etfOrder.OrderId, etfContract, etfOrder); this.ClientSocket.placeOrder(stkOrder.OrderId, stkContract, stkOrder); this.ClientSocket.reqIds(1); }
public PairPos createPair(PairSignal oneSignal) { Equities etfLeg = this.equityDict[oneSignal.EtfTID]; Equities stkLeg = this.equityDict[oneSignal.StkTID]; PairPos newPair = new PairPos(etfLeg, stkLeg); return newPair; // blah blah //foreach (var KeyValuePair in this.TickerSymbolDict) //{ // // skip etf // if ((KeyValuePair.Key & 1) == 1) // continue; // PairPos newPair = new PairPos(); // newPair.PairBeta = 0; // beta initialize // int tmpEtfTID = (KeyValuePair.Key & 1024) + 1; // clear all digits in 1 to 9. Aka, clear stk infomation // newPair.pairEtfLeg.TickerID = tmpEtfTID; // try // { // newPair.pairEtfLeg.Symbol = this.TickerSymbolDict[tmpEtfTID]; // } // catch (Exception) // { // throw; // } // newPair.pairStkLeg.TickerID = KeyValuePair.Key; // newPair.pairStkLeg.Symbol = KeyValuePair.Value; // this.PairPosDict.Add(KeyValuePair.Key, newPair); //} }