Ejemplo n.º 1
0
        public TitleCommand ReadGmail()
        {
            var Url     = @"https://mail.google.com/mail/feed/atom";
            var encoded = TextToBase64(m_strGmail + ":" + m_strPassword);
            var request = HttpWebRequest.Create(Url);

            request.Method        = "POST";
            request.ContentLength = 0;
            request.Headers.Add("Authorization", "Basic" + encoded);
            try
            {
                var         respone = request.GetResponse();
                var         stream  = respone.GetResponseStream();
                XmlReader   reader  = XmlReader.Create(stream);
                XmlDocument doc     = new XmlDocument();
                doc.Load(reader);
                XmlNodeList emailList = doc.GetElementsByTagName("entry");

                for (int i = 0; i < emailList.Count; i++)
                {
                    string emailTitle = emailList[i].ChildNodes.Item(0).InnerText.Trim();
                    if (emailTitle.Contains("TradingView Alert:"))
                    {
                        string[] spearator    = { ":", "_" };
                        string[] strlist      = emailTitle.Split(spearator, StringSplitOptions.RemoveEmptyEntries);
                        string   strOperation = strlist[1].Trim();
                        if (strOperation == "BUY" || strOperation == "SELL")
                        {
                            TitleCommand Command = new TitleCommand();
                            Command.Operation   = strOperation;
                            Command.Period      = strlist[2];
                            Command.Symbol      = strlist[3];
                            Command.OriginTitle = Command.Operation + "_" + Command.Period + "_" + Command.Symbol;
                            string strModifiedTime = emailList[i].ChildNodes.Item(3).InnerText.Trim();
                            if (m_lastModifiedTime == strModifiedTime)
                            {
                                return(null);
                            }
                            else
                            {
                                m_lastModifiedTime = strModifiedTime;
                                return(Command);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(null);
        }
Ejemplo n.º 2
0
        private void m_Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                TitleCommand Command = m_GmailReader.ReadGmail();
                if (Command == null)
                {
                    return;
                }
                if (!m_Dic.ContainsKey(Command.OriginTitle))
                {
                    m_Dic.Add(Command.OriginTitle, 1);
                }
                else
                {
                    m_Dic[Command.OriginTitle]++;
                }

                string strLog = "================= EMAIL RECEIVED! =================\n";
                strLog += "Title : " + Command.OriginTitle +
                          "\nCount: " + m_Dic[Command.OriginTitle].ToString();
                Log(strLog);

                ShowEmailTitleCount();

                for (int i = 0; i < 3; i++)
                {
                    OrderParameter Param = m_XLSReader.ReadOrderParameters(Command.OriginTitle, m_Dic[Command.OriginTitle], i);
                    if (Param == null)
                    {
                        Log("################## Can't Search Information in Xls File! #####################");
                        return;
                    }

                    strLog  = "====================== INFORMATION FROM XLS FILE! ==========================\n";
                    strLog += "Price: " + Param.strPrice +
                              "\nLot: " + Param.dLot.ToString() +
                              "\nStopLoss: " + Param.dStopLoss.ToString() +
                              "\nTakeProfit: " + Param.dTakeProfit.ToString() +
                              "\nX: " + Param.dX.ToString() +
                              "\nExpireTime: " + Param.expireTime.ToString();
                    Log(strLog);

                    CandleStick Stick = m_MT4Controller.GetLastCandleStick(Command.Symbol, Command.Period);
                    if (Stick == null)
                    {
                        return;
                    }

                    strLog  = "=================== LASTEST CANDLE VALUES =====================\n";
                    strLog += "Low: " + Stick.Low.ToString() +
                              "\nHigh: " + Stick.High.ToString() +
                              "\nOpen: " + Stick.Open.ToString() +
                              "\nClose: " + Stick.Close.ToString();
                    Log(strLog);

                    double dPrice      = 0;
                    double dTakeProfit = 0;
                    double dStopLoss   = 0;
                    if (Param.strPrice == "Low")
                    {
                        dPrice = Stick.Low;
                    }
                    if (Param.strPrice == "High")
                    {
                        dPrice = Stick.High;
                    }
                    if (Param.strPrice == "Open")
                    {
                        dPrice = Stick.Open;
                    }
                    if (Param.strPrice == "Close")
                    {
                        dPrice = Stick.Close;
                    }

                    dPrice += Param.dX;

                    if (Command.Operation == "BUY")
                    {
                        dTakeProfit = dPrice + Param.dTakeProfit;
                        dStopLoss   = dPrice - Param.dStopLoss;
                    }
                    else if (Command.Operation == "SELL")
                    {
                        dTakeProfit = dPrice - Param.dTakeProfit;
                        dStopLoss   = dPrice + Param.dStopLoss;
                    }

                    DateTime expireTime = m_MT4Controller.GetCurrentMT4Time();
                    expireTime = expireTime.AddHours(Param.expireTime);

                    strLog  = "=================== ORDER PARAMETERS =====================\n";
                    strLog += "Symbol: " + Command.Symbol +
                              "\nOperation: " + Command.Operation +
                              "\nLot: " + Param.dLot.ToString() +
                              "\nPrice: " + dPrice.ToString() +
                              "\nExpireTime: " + expireTime.ToString() +
                              "\nTakeProfit: " + dTakeProfit.ToString() +
                              "\nStopLoss: " + dStopLoss.ToString() +
                              "\nSlippage: 20" +
                              "\nAskPrice: " + m_MT4Controller.GetAskPrice(Command.Symbol).ToString() +
                              "\nBidPrice: " + m_MT4Controller.GetBidPrice(Command.Symbol).ToString();
                    Log(strLog);

                    m_MT4Controller.SendOrder(Command.Symbol, Command.Operation, Param.dLot, dPrice, expireTime, dTakeProfit, dStopLoss);
                }
            }
            catch (Exception ex)
            {
                Log("############## SOME ERROR HAPPEND! ##############");
                Log(ex.ToString());
            }
        }