Example #1
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         try
         {
             this._strategy      = StrategyManager.Load(this.openFileDialog1.FileName);
             this._strategy.Log += (_1, _2) => this.ShowLog(_1.ToString() + " " + _2);
             this.ShowLog($"策略“{this._strategy.Name}”成功加载");
             this.label5.Text = "当前策略:" + this._strategy.GetType().Name;
         }
         catch (Exception ex)
         {
             this.ShowLog(ex.GetBaseException().Message);
         }
     }
 }
Example #2
0
        private void _loadBACK()
        {
            BootstrapForm bootstrap = new BootstrapForm(this);

            bootstrap.Show();


            /*
             * GoogleFinance.GoogleFinance googleFinance = new GoogleFinance.GoogleFinance();
             * List<Candle> test = googleFinance.GetAllFrom("BVMF", "ABEV3", 3600, "1d");
             *
             * Clipboard.SetText("empty");
             * foreach (Candle candle in test)
             * {
             *  bootstrap.Debug(candle.DateTime + "\n", true);
             * }
             *
             *
             * return;
             */

            /*
             *          bootstrap.Close();
             *
             *          loadDone = true;
             *          return;
             */

            //criando o objeto de banco de dados
            DB = new DBConnection(
                ConfigurationManager.AppSettings["databaseHost"],
                ConfigurationManager.AppSettings["databaseName"],
                ConfigurationManager.AppSettings["databaseUid"],
                ConfigurationManager.AppSettings["databasePassword"]
                );

            try
            {
                DB.testConnection();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Application.Exit();
            };


            //criando o cliente de stream.
            HttpClient client     = new HttpClient();
            WebClient  downloader = new WebClient();
            string     tmpUrl;
            string     logoDir = config.AppSettings.Settings["logoDir"].Value;

            string[]      files    = Directory.GetFiles(logoDir);
            List <string> logoList = new List <string>();

            foreach (string file in files)
            {
                tmpUrl = file.Split('.')[0];
                tmpUrl = tmpUrl.Split('\\')[1];
                logoList.Add(Regex.Replace(tmpUrl, "[0-9]", ""));
            }

            rtdStreamListener = new RTDStream.RtdStreamerListener();
            //carregando a lista de ações.
            stockManager = new StockManager(DB);
            List <string> tickers = new List <string>();

            //stockIdByTicker = new Dictionary<string, int>();
            List <string>[] results = DB.Select("select * from stock where id <= 300");
            Stock           tmpStock;

            //string tmpUrl;
            bootstrap.MainLabel = "Atualizando Logos de empresas...";
            float totalData;
            float currentData;

            bootstrap.SubProgressMax = results[1].Count;
            currentData           = 0;
            bootstrap.SubProgress = 0;
            for (int i = 0; i < results[1].Count; i++)
            {
                // Console.WriteLine("Adicionando {0}", results[1][i]);
                tickers.Add(results[1][i]);
                //stockIdByTicker.Add(results[1][i], int.Parse(results[0][i]));
                tmpStock = new Stock(int.Parse(results[0][i]), results[2][i], results[1][i]);
                stockManager.Add(tmpStock);

                //bootstrap.SubProgress = (int)((currentData / totalData) * 100);
                if (logoList.Contains(Regex.Replace(tmpStock.Ticker, "[0-9]", "")) == false) //precisa autualizar logo.
                {
                    bootstrap.SubLabel = "Obtendo logo de " + tmpStock.Name;
                    tmpUrl             = "http://br.advfn.com/common/images/companies/BOV_" + tmpStock.Ticker + ".png";

                    downloader.DownloadFile(tmpUrl, logoDir + "/" + Regex.Replace(tmpStock.Ticker, "[0-9]", "") + ".png");
                }
                bootstrap.SubProgress = (int)++currentData;
            }
            streamerClient           = new RTDStream.StreamerClient(rtdStreamListener, tickers);
            bootstrap.SubProgress    = 0;
            bootstrap.SubProgressMax = 100;
            //inicializando o gerenciador de estratégias.
            strategyManager = new StrategyManager();
            strategyManager.Load();


//inicializando as bases históricas.


            //candleManager = new CandleManager(DB);
            historicalDataResolutions  = new Dictionary <string, bool>();
            historicalResolutionSource = new Dictionary <int, string>();
            //config.AppSettings.Settings
            foreach (KeyValueConfigurationElement configValue in config.AppSettings.Settings)
            {
                //bootstrap.Debug(configValue.Key + "\n", true);
                if (configValue.Key.Contains("HistoricalDataResolution_"))
                {
                    historicalDataResolutions.Add(configValue.Key.Split('_')[1], configValue.Value == "1" ? true : false);
                }
            }
            List <int> resolutions = new List <int>();

            foreach (KeyValuePair <string, bool> entry in historicalDataResolutions)
            {
                // bootstrap.Debug(entry.Key + "\n", true);
                if (entry.Value)
                {
                    int tmp = HistoricalDataResolutionStringToSeconds(entry.Key);
                    //bootstrap.Debug(tmp + ":" + entry.Key + "\n", true);
                    historicalResolutionSource.Add(tmp, entry.Key);
                    resolutions.Add(tmp);
                }
            }



            //int currentPeriodLength;
            long currentTimestamp = 0;
            //string currentTicker;
            string stockDataParams;
            string googleFinaceBaseUrl = "http://www.google.com/finance/getprices?x=BVMF&f=d,c,v,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg&p=100Y";

            string[] lines;
            string[] lineData;
            string   resultData;
            //Candle currentCandle;
            // List<Candle> currentCandleList;
            DateTime currentDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            DateTime candleDateTime;
            //Dictionary<string, bool> updatesNeeded = new Dictionary<string, bool>();
            Dictionary <string, StringBuilder> updateQueries = new Dictionary <string, StringBuilder>();
            StringBuilder updateQuery;
            bool          updateNeeded;

            totalData             = tickers.Count * resolutions.Count;
            currentData           = 0;
            bootstrap.MainLabel   = "Obtendo dados históricos...";
            bootstrap.SubProgress = 0;
            foreach (string currentTicker in tickers)
            {
                foreach (int currentPeriodLength in resolutions)
                {
                    Stock currentStock = stockManager.Get(currentTicker);
                    currentStock.CreateHistoricalDataList(currentPeriodLength);
                    //currentStock.CreateHistoricalData()
                    //candleManager.CreateList(currentStock.Id, currentPeriodLength);

                    resultData   = "SELECT timestamp FROM period WHERE stock_id = ";
                    resultData  += currentStock.Id + " AND periodLength = " + currentPeriodLength;
                    resultData  += " ORDER BY timestamp DESC LIMIT 1";
                    updateNeeded = false;
                    results      = DB.Select(resultData);


                    if (results[0].Count == 0)  //se o ultimo periodo do db for de um dia anterior a ontem, pegar do google e atualizar.
                    {
                        updateNeeded = true;
                    }
                    else
                    {
                        DateTime lastUpdateDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                        lastUpdateDateTime = lastUpdateDateTime.AddSeconds(long.Parse(results[0][0]));
                        // bootstrap.Debug("NOW:" + DateTime.Now.ToString() + "\n", true);
                        //  bootstrap.Debug(lastUpdateDateTime.ToString() + "\n", true);
                        //   bootstrap.Debug((DateTime.Now - lastUpdateDateTime).TotalDays + "\n" + results[0][0] + "\n", true);
                        if ((DateTime.Now - lastUpdateDateTime).TotalDays >= 1)
                        {
                            updateNeeded = true;
                        }
                    }


                    //updatesNeeded.Add(currentTicker + "_" +currentPeriodLength, updateNeeded);

                    int value = (int)((currentData / totalData) * 100);
                    value = value > 100 ? 100 : value;
                    value = value < 0 ? 0 : value;
                    bootstrap.SubProgress = value;
                    //bootstrap.Debug(value+"");
                    currentData++;



                    historicalResolutionSource.TryGetValue(currentPeriodLength, out resultData);
                    if (!updateNeeded)
                    {
                        // bootstrap.Debug("No Update Nedded @@@@@@\n", true);
                        bootstrap.SubLabel = currentTicker + ":" + resultData + " de Base Local...";
                    }
                    else
                    {
                        // bootstrap.Debug("Updating...\n", true);

                        bootstrap.SubLabel = currentTicker + ":" + resultData + " de Google...";
                        //Clipboard.SetText(bootstrap.SubLabel);



                        stockDataParams = "&q=" + currentTicker + "&i=" + currentPeriodLength;

                        HttpResponseMessage response = client.GetAsync(googleFinaceBaseUrl + stockDataParams).Result;  // Blocking call!
                        if (response.IsSuccessStatusCode)
                        {
                            resultData = response.Content.ReadAsStringAsync().Result;
                            resultData = resultData.Replace(",", "@");
                            resultData = resultData.Replace(".", ",");
                            lines      = resultData.Split('\n');
                            //8 linhas de cabeçalho, no min
                            if (lines.Length > 7)
                            {
                                updateQuery = new StringBuilder();
                                updateQuery.Append("INSERT INTO period VALUES ");



                                //resultData = "INSERT INTO period VALUES ";
                                // resultData = "";
                                for (int i = 7; i < lines.Length; i++)
                                {
                                    lineData = lines[i].Split('@');
                                    if (lineData.Length != 6)
                                    {
                                        continue;
                                    }
                                    //ignorar linhas q começam com a
                                    if (lineData[0].Contains("a")) //nova timestamp;
                                    {
                                        currentTimestamp = long.Parse(lineData[0].Replace("a", ""));
                                        //resultData += "Setando timestamp para: " + currentTimestamp + "\n";
                                        currentDateTime  = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                                        currentDateTime  = currentDateTime.AddSeconds(currentTimestamp).ToLocalTime();
                                        currentTimestamp = AppData.ConvertToUnixTime(currentDateTime);
                                        continue;
                                    }
                                    candleDateTime = currentDateTime.AddSeconds(currentPeriodLength * long.Parse(lineData[0]));

                                    updateQuery.Append(
                                        "(" +
                                        currentStock.Id + ";" +
                                        AppData.ConvertToUnixTime(candleDateTime) + ";" +
                                        currentPeriodLength + ";" +
                                        lineData[4] + ";" +
                                        lineData[1] + ";" +
                                        lineData[2] + ";" +
                                        lineData[3] + ";" +
                                        lineData[5] + ");"
                                        );
                                }

                                updateQueries.Add(currentTicker + "_" + currentPeriodLength, updateQuery);
                            }
                            else
                            {
                                //Clipboard.SetText(googleFinaceBaseUrl + stockDataParams);
                                bootstrap.Debug("Resposta inválida\n" + resultData + "\n" + googleFinaceBaseUrl + stockDataParams + "\n");
                            }
                        }
                        else
                        {
                            bootstrap.Debug(response.RequestMessage.RequestUri + " : Faiô!: " + (int)response.StatusCode + " : " + response.ReasonPhrase);
                        }
                    }
                }
            }
            totalData   = updateQueries.Count;
            currentData = 0;

            //bootstrap.SubLabel = "Salvando novos dados obtidos...";

            foreach (KeyValuePair <string, StringBuilder> sb in updateQueries)
            {
                //int stockId;
                string periodLength;
                string ticker = sb.Key.Split('_')[0];
                // stockIdByTicker.TryGetValue(ticker, out stockId);
                historicalResolutionSource.TryGetValue(int.Parse(sb.Key.Split('_')[1]), out periodLength);

                bootstrap.SubLabel = "Atualizando dados históricos de " + ticker + ":" + periodLength + "...";

                string query = sb.Value.ToString();
                query  = query.Replace(",", ".");
                query  = query.Replace(";", ",");
                query += "@@@";
                query  = query.Replace(",@@@", " ON DUPLICATE KEY UPDATE stock_id=stock_id");
                //Clipboard.SetText(query);
                try
                {
                    DB.Query(query);
                }
                catch (Exception) { }

                query = null;
                bootstrap.SubProgress = (int)((currentData / totalData) * 100);
            }
            updateQueries.Clear();


            //TODO: Adicionar dados RTD ao banco.
            //somente os dados que o google não possui, q geralmente são os de aftermarkert

            //recarregar tudo do db.

            totalData   = stockManager.All.Count;
            currentData = 0;
            //string temp;

            foreach (Stock stock in stockManager.All)
            {
                bootstrap.SubLabel = "Carregando dados históricos de " + stock.Name + "...";
                stock.HistoricalDataManager.ReloadAllFromDb();
                bootstrap.SubProgress = (int)((currentData / totalData) * 100);
                currentData++;
            }


            loadDone = true;
        }
Example #3
0
 private void LoadStrategyManager(BootstrapForm bootstrap)
 {
     bootstrap.MainLabel = "Iniciando Gerenciador de Estratégias...";
     strategyManager     = new StrategyManager();
     strategyManager.Load();
 }