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); //} }
// close this position, for reasons like stop loss, max holding periods public void closeThisPosition() { PairSignal selfSignal = new PairSignal(this.stkLeg.TickerID, this.etfLeg.TickerID); switch (this.thisPairStatus) { case PairType.openLong: selfSignal.TrSignal = PairType.closeLong; break; case PairType.openShort: selfSignal.TrSignal = PairType.closeShort; break; default: throw new Exception(String.Format("Cannot close my self! Status = {}", Convert.ToString(this.ThisPairStatus))); } // TODO EWrapperImpl.Instance.processSignal(selfSignal); }
public void processSignal(PairSignal oneSignal) { if (oneSignal.TrSignal == PairType.nullType) // do nothing return; // 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 Console.WriteLine("Please build the model first!, {0}, {1}", tmpPair.pairEtfLeg.Symbol, tmpPair.pairStkLeg.Symbol); Console.WriteLine("Press any key to start over..."); Console.ReadKey(); Environment.Exit(-2); } // create contract objects for both stk and etf Contract etfContract = new Contract(); etfContract.Symbol = tmpPair.pairEtfLeg.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.pairStkLeg.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: tmpPair.ThisPairStatus = PairType.openLong; tmpPair.pairEtfLeg.OpenOrderID= this.nextOrderId; tmpPair.pairStkLeg.OpenOrderID = this.nextOrderId + 1; break; case PairType.openShort: tmpPair.ThisPairStatus = PairType.openShort; tmpPair.pairEtfLeg.OpenOrderID= this.nextOrderId; tmpPair.pairStkLeg.OpenOrderID = this.nextOrderId + 1; break; case PairType.closeLong: tmpPair.ThisPairStatus = PairType.closeLong; tmpPair.pairEtfLeg.CloseOrderID = this.nextOrderId; tmpPair.pairStkLeg.CloseOrderID = this.nextOrderId + 1; break; case PairType.closeShort: tmpPair.ThisPairStatus = PairType.closeShort; tmpPair.pairEtfLeg.CloseOrderID = this.nextOrderId; tmpPair.pairStkLeg.CloseOrderID = this.nextOrderId + 1; break; default: Console.WriteLine("Wrong signal type. Press any key to start over..."); Console.ReadKey(); Environment.Exit(-2); break; } this.PairPosDict[oneSignal.StkTID] = tmpPair; switch (oneSignal.TrSignal) { case PairType.openLong: // long etf short stk stkOrder.Action = "SELL"; stkOrder.TotalQuantity = this.STK_SHARE; stkOrder.OrderType = "MKT"; etfOrder.Action = "BUY"; etfOrder.TotalQuantity = Convert.ToInt32(this.STK_SHARE * tmpPair.PairBeta); etfOrder.OrderType = "MKT"; break; case PairType.openShort: // short etf long stk stkOrder.Action = "BUY"; stkOrder.TotalQuantity = this.STK_SHARE; stkOrder.OrderType = "MKT"; etfOrder.Action = "SSHORT"; etfOrder.TotalQuantity = Convert.ToInt32(this.STK_SHARE * tmpPair.PairBeta); etfOrder.OrderType = "MKT"; break; case PairType.closeLong: // close, sell etf buy cover stk stkOrder.Action = "BUY"; stkOrder.TotalQuantity = this.STK_SHARE; stkOrder.OrderType = "MKT"; etfOrder.Action = "SELL"; etfOrder.TotalQuantity = Convert.ToInt32(this.STK_SHARE * tmpPair.PairBeta); etfOrder.OrderType = "MKT"; break; case PairType.closeShort: // close, buy cover etf sell stk stkOrder.Action = "SELL"; stkOrder.TotalQuantity = this.STK_SHARE; stkOrder.OrderType = "MKT"; etfOrder.Action = "BUY"; etfOrder.TotalQuantity = Convert.ToInt32(this.STK_SHARE * tmpPair.PairBeta); etfOrder.OrderType = "MKT"; break; default: // not gonna happen Console.WriteLine("Wrong signal! Press any key to stop..."); Console.ReadKey(); Environment.Exit(-1); break; } // TODO: add log file here this.ClientSocket.placeOrder(etfOrder.OrderId, etfContract, etfOrder); this.ClientSocket.placeOrder(stkOrder.OrderId, stkContract, stkOrder); this.ClientSocket.reqIds(1); }
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 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); //} }
// close this position, for reasons like stop loss, max holding periods public void closeThisPosition() { PairSignal selfSignal = new PairSignal(this.stkLeg.TickerID, this.etfLeg.TickerID); switch (this.thisPairStatus) { case PairType.openLong: selfSignal.TrSignal = PairType.closeLong; break; case PairType.openShort: selfSignal.TrSignal = PairType.closeShort; break; default: throw new Exception( String.Format("Cannot close my self! Status = {}", Convert.ToString(this.ThisPairStatus)) ); } // TODO EWrapperImpl.Instance.processSignal(selfSignal); }