private void btnFuturesAdd_Click(object sender, EventArgs e) { if (m_fd != null) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { var f = (from st in dc.Stocks where st.Ticker == m_fd.LocalSymbol select st).Count(); if (f > 0) { MessageBox.Show("Already added to the database."); return; } Stock s = new Stock(); s.Ticker = m_fd.LocalSymbol; s.Company = string.Format("{0} {1}", m_fd.LongName, m_fd.Expiry.ToString("MMMdd")); s.Exchange = m_fd.Exchange; s.SecType = "FUT"; s.FutureExpiry = m_fd.Expiry; dc.Stocks.InsertOnSubmit(s); try { dc.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
/************************************************************ * * Compute Theta Vega Ratio for the trade... not sure what this means * * *********************************************************/ //internal void ComputeThetaVegaRatio () //{ // double? theta = 0; // double? vega = 0; // foreach (var l in this.m_Legs) // { // if (l.OpenCloseStatus != OpenCloseValues.Close) // { // theta += l.Theta; // vega += l.Vega; // } // } // this.ThetaVegaRatio = theta / vega; //} internal void Persist(string DataGroupName) { if (this.Id != null) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { dc.UpdateTrade(Id, TradeType, Ticker, (int)OpenCloseStatus, Premium, Commissions, OpenDate, ClosedDate, TotalDelta, TotalTheta, (decimal?)TotalProfitLoss, (decimal?)ProfitThreshold, EmailNotifications, Notes, LastEmail); } } else { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { InsertTradeResult tr = dc.InsertTrade(Id, DataGroupName, TradeType, Ticker, (int)OpenCloseStatus, Premium, Commissions, OpenDate, ClosedDate, TotalDelta, TotalTheta, (decimal?)TotalProfitLoss, (decimal?)ProfitThreshold, EmailNotifications, Notes, LastEmail).Single(); Id = tr.TradeId; } } }
/****************************************************** * * Email notification * * ***************************************************/ internal void EmailBrian(string note, TimeSpan urgency) { if (LastEmail == null || (DateTime)LastEmail < DateTime.Now - urgency) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { var rec = (from r in dc.Trades where r.TradeId == Id select r).Single(); LastEmail = rec.LastEmail = DateTime.Now; dc.SubmitChanges(); } MailMessage msg = new MailMessage(); msg.To.Add("*****@*****.**"); msg.From = new MailAddress("*****@*****.**"); msg.Subject = note; msg.Body = note; SmtpClient smtp = new SmtpClient("smtp.telus.net"); NetworkCredential basicCredential = new NetworkCredential("*****@*****.**", "blackie0"); smtp.UseDefaultCredentials = false; smtp.Credentials = basicCredential; try { smtp.Send(msg); } catch (SmtpException se) { MessageBox.Show(se.Message, "SMTP email failed", MessageBoxButtons.OK); } } }
/*********************************************************** * * DatabasePrice (string ticker) * * ********************************************************/ static internal double?DatabasePrice(string ticker) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { var p = (from stock in dc.Stocks where stock.Ticker == ticker select stock.LastTrade).First(); return((double?)p); } }
private void EmailBrian(string note, TimeSpan urgency) { //OptionData od = m_Options[index]; //TimeSpan ts = new TimeSpan (0); //switch (n) //{ // case EmailNotify.ITM: // note = string.Format ("Option {0} {1} {2} strike {3} is ITM {4:F2}", od.Ticker, od.IfCall, od.IfSell, od.Strike, od.ProbITM); // ts = new TimeSpan (1, 0, 0); // 1 hour // break; // case EmailNotify.NEAR_ITM: // note = string.Format ("Option {0} {1} {2} strike {3} is approaching ITM {4:F2}", od.Ticker, od.IfCall, od.IfSell, od.Strike, od.ProbITM); // ts = new TimeSpan (3, 0, 0); // break; // case EmailNotify.PROFIT50: // note = string.Format ("Option {0} {1} {2} strike {3} is > 50% profitable {4:F2}", od.Ticker, od.IfCall, od.IfSell, od.Strike, od.PercentProfit); // ts = new TimeSpan (8, 0, 0); // break; // case EmailNotify.PROFIT75: // note = string.Format ("Option {0} {1} {2} strike {3} is > 75% profitable {4:F2}", od.Ticker, od.IfCall, od.IfSell, od.Strike, od.PercentProfit); // ts = new TimeSpan (4, 0, 0); // break; // default: // m_Log.Log (ErrorLevel.logERR, "Bug in NotifyBrian"); // break; //} if (LastEmail == null || (DateTime)LastEmail >= DateTime.Now + urgency) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { var rec = (from r in dc.Legs where r.Id == Id select r).Single(); LastEmail = rec.LastEmail = DateTime.Now; dc.SubmitChanges(); } MailMessage msg = new MailMessage(); msg.To.Add("*****@*****.**"); msg.From = new MailAddress("*****@*****.**"); msg.Subject = note; msg.Body = note; SmtpClient smtp = new SmtpClient("smtp.telus.net"); smtp.Send(msg); } }
/********************************************************************* * * FetchUnderlyingPrice * * ******************************************************************/ static internal Task <double> FetchUnderlyingPrice(int reqid, string ticker) { double price = 0.0; double close = 0.0; var tcs = new TaskCompletionSource <double> (); /* If the market is closed, get the price from somewhere else * ---------------------------------------------------------- */ if (!Utils.IfTradingNow()) { using (dbOptionsDataContext dc = new dbOptionsDataContext()) { var stockprice = (from s in dc.Stocks where s.Ticker == ticker select s.LastTrade ).SingleOrDefault(); if (stockprice == null) { tcs.SetException(new Exception(string.Format("Failed to fetch underlying price for {0}", ticker))); return(tcs.Task); } tcs.SetResult((double)stockprice); return(tcs.Task); } } //int reqid = m_random.Next (0xFFFF); /* * var errhandler = default (AxTWSLib._DTwsEvents_errMsgEventHandler); * var endhandler = default (AxTWSLib._DTwsEvents_tickSnapshotEndEventHandler); * var pricehandler = default (AxTWSLib._DTwsEvents_tickPriceEventHandler); * * errhandler = new AxTWSLib._DTwsEvents_errMsgEventHandler ((s, e) => * { * if (e.id == -1) * { * Log.Log (ErrorLevel.logERR, string.Format ("FetchPriceUnderlying: error {0}", e.errorMsg)); * return; * } * * if ((e.id & 0xFFFF0000) != Utils.ibPRICE) * { * return; * } * e.id &= 0xFFFF; * * Log.Log (ErrorLevel.logERR, string.Format ("FetchPriceUnderlying: error {0} ", e.errorMsg)); * * axTws.errMsg -= errhandler; * axTws.tickPrice -= pricehandler; * axTws.tickSnapshotEnd -= endhandler; * * axTws.cancelMktData (reqid); * * tcs.SetException (new Exception (e.errorMsg)); * }); * * pricehandler = new AxTWSLib._DTwsEvents_tickPriceEventHandler ((s, e) => * { * if ((e.id & 0xFFFF0000) != Utils.ibPRICE) * { * return; * } * e.id &= 0xFFFF; * if (reqid != e.id) * { * return; * } * * Log.Log (ErrorLevel.logERR, string.Format ("FetchPriceUnderlying: axTws_tickPrice for {0} tickType:{1} {2} value: {3}", ticker, e.tickType, TickType.Display (e.tickType), e.price)); * * * switch (e.tickType) * { * case TickType.LAST: * price = e.price; * break; * * case TickType.CLOSE: * close = e.price; * break; * * case TickType.BID: * //opt.Bid = e.price; * break; * * case TickType.ASK: * //opt.Ask = e.price; * break; * * default: * break; * } * }); * * endhandler = new AxTWSLib._DTwsEvents_tickSnapshotEndEventHandler ((s, e) => * { * if ((e.reqId & 0xFFFF0000) != Utils.ibPRICE) * { * return; * } * e.reqId &= 0xFFFF; * if (reqid != e.reqId) * { * return; * } * * Log.Log (ErrorLevel.logINF, string.Format ("FetchOneOptionChain: axTws_tickSnapshotEnd for {0}. price={1:N3} close={2:N3}", ticker, price, close)); * * axTws.errMsg -= errhandler; * axTws.tickPrice -= pricehandler; * axTws.tickSnapshotEnd -= endhandler; * * if (price != 0.0) * { * tcs.SetResult (price); * } * else * { * tcs.SetResult (close); * } * }); * * * axTws.errMsg += errhandler; * axTws.tickPrice += pricehandler; * axTws.tickSnapshotEnd += endhandler; * * TWSLib.IContract contract = axTws.createContract (); * * contract.symbol = ticker; * contract.currency = "USD"; * contract.secType = "STK"; * contract.exchange = "SMART"; * contract.localSymbol = ticker; * contract.includeExpired = 0; * * axTws.reqMktDataEx (Utils.ibPRICE | reqid, contract, "", 1, null); * return tcs.Task;*/ return(null); /* remove */ }