Exemplo n.º 1
0
 public static void storeEvergladesPrice(DateTime date, double price)
 {
     using (var context = new qpcptfaw())
     {
         //récupère l'id d'Everglades
         int id = Access.GetIdEverglades();
         var ev = from e in context.Prices
                  where e.AssetDBId == id && e.date == date
                  select e;
             // si la date existe déjà dans la table des prix on la remplace
         if (ev.Count()==1) //list_pair_db.Contains(new KeyValuePair<int, DateTime>(id, date)))
         {
             // on vérifie que la valeur de prix est différente
             // si identiques on return (rien à faire)
             if (ev.First().price == price && ev.First().priceEur == price)
             {
                 return;
             }
             // sinon on remplace
             ev.First().price = price;
             ev.First().priceEur = price;
             context.SaveChanges();
             return;
         }
         else if (ev.Count()==0)
         {
             // sinon on l'ajoute
             Price p = new Price { AssetDBId = id, date = date, price = price, priceEur =price };
             context.Prices.Add(p);
             context.SaveChanges();
             return;
         }
         else
         {
             throw new Exception("Problème dans la BD.");
         }
     }
 }
Exemplo n.º 2
0
        public static void storeInDB(string json, qpcptfaw context, string source, string code)
        {
            JObject jObj = JObject.Parse(json);
            JArray datas = (JArray)jObj["data"];

            //List<Currencies> list_currencies_db;
            //tous les currencies présents dans la BD
            /*try
            {
                list_currencies_db = Access.getAllCurrencies();
            }catch (Exception){
                list_currencies_db = new List<Currencies>();
            }*/

            Currencies current_currency = getCurrencyFromQuandlCode(code);

            //Si la BD ne contient pas ce currency, on le crée
            if (!Access.CurrenciesContains(current_currency))//!list_currencies_db.Contains(current_currency))
            {
                ForexDB fdb = new ForexDB{forex = current_currency, name = code, RateDBId = Access.getInterestRateIdFromIrate(getIrateFromCurrency(current_currency))};
                context.Assets.Add(fdb);
                context.SaveChanges();
            }

            //List<KeyValuePair<int, DateTime>> list_pair_db = Access.getAllForexRateKey(context);
            //KeyValuePair<int, DateTime> keyValue;
            List<Price> list_rates = new List<Price>();

            double rate = 0;

            //récupère l'id de la devise
            int id = Access.getForexIdFromCurrency(current_currency);
            if (id != -1)
            {
                foreach (var item in datas.Children())
                {
                    JToken[] data = item.ToArray();
                    DateTime date = DateTime.Parse(data[0].ToString());
                   // keyValue = new KeyValuePair<int, DateTime>(id, date);
                   if (!Access.ForexRateContainsKey(context, date, id)) //if (!list_pair_db.Contains(keyValue))
                    {
                        rate = double.Parse(data[1].ToString());
                        Price f = new Price {price = rate, priceEur = 1/rate, date = date, AssetDBId = id};
                        list_rates.Add(f);
                    }
                }
            }
            list_rates.ForEach(p => context.Prices.Add(p));
            context.SaveChanges();
        }
Exemplo n.º 3
0
        public static void storeInDB(string json, qpcptfaw context, string source, string code)
        {
            JObject jObj = JObject.Parse(json);
            JArray datas = (JArray)jObj["data"];

            //tous les symboles présents dans la BD
            //List<string> list_symbols_db = Access.getAllEquitiesSymbol(context);
            //récupère le symbole de l'action récupérée dans ce json
            //string symbol = (string)jObj["code"];
            string symbol = code;

            int aid;
            Currencies curr;

            //récupère tous les clés id-date de la BD (table Prices)
            //List<KeyValuePair<int, DateTime>> list_pair_db = Access.getAllPricesKey(context);

            //KeyValuePair<int, DateTime> keyValue;
            List<Price> list_prices = new List<Price>();

            double c = 0;
            double open = 0;
            double high = 0;
            double low = 0;
            double volume = 0;
            string name;

            //si la bd ne contient pas le symbole concerné, on crée une nouvelle action et on la stocke dans la BD (table asset)
            if (!Access.ContainsEquitySymbol(context, symbol))//!list_symbols_db.Contains(symbol))
            {
                name = (string)jObj["name"];
                EquityDB e = new EquityDB { name = name, symbol = symbol, PriceCurrency = CurrencyAsset.getCurrencyOf(name) };
                context.Assets.Add(e);
                context.SaveChanges();
                AssetDB.assetCounter();
            }

            //Yahoo ["Date","Open","High","Low","Close","Volume","Adjusted Close"]
            //Google ["Date","Open","High","Low","Close","Volume"

            //récupère l'id correspondant au symbole de l'action
            aid = Access.GetEquityIdFromSymbol(symbol);
            curr = Access.GetEquityCurrencyFromSymbol(symbol);
            //si l'id est -1 alors le symbol n'existe pas dans la bd, on ne stocke pas le prix
            if(aid!=-1){
                //on parse les données json
                 foreach (var item in datas.Children())
                {
                     //chaque item correspond aux données d'un jour
                    JToken[] data = item.ToArray();
                    DateTime date = DateTime.Parse(data[0].ToString());
                    //keyValue = new KeyValuePair<int, DateTime>(aid, date);
                     //si la bd ne contient pas les données pour cette action pour ce jour, on ajoute les données  
                    if (!Access.ContainsPricesKey(context, aid, date)){//!list_pair_db.Contains(keyValue)){
                        open = double.Parse(data[1].ToString());
                        high = double.Parse(data[2].ToString());
                        low = double.Parse(data[3].ToString());
                        volume = double.Parse(data[5].ToString());
                        if (source == "YAHOO")
                        {
                            c = double.Parse(data[6].ToString());
                        }
                        else if (source == "GOOG")
                        {
                            c = double.Parse(data[4].ToString());
                        }
           
                        Price p = new Price { AssetDBId = aid, price = c, date = date, high = high, low=low, open=open, volume=volume };
                        //conversion en euro
                        Dictionary<string, double> dic = new Dictionary<string, double>();
                        dic.Add("open", open);
                        dic.Add("high", high);
                        dic.Add("low", low);
                        dic.Add("price", c);
                        Dictionary<string, double> pricesEur = new Dictionary<string, double>();
                        if (!curr.Equals(Currencies.EUR))
                        {
                            pricesEur = CurrencyAsset.convertToEuro(dic, curr, date, context);
                            p.priceEur = pricesEur["price"];
                            p.highEur = pricesEur["high"];
                            p.lowEur = pricesEur["low"];
                            p.openEur = pricesEur["open"];
                            //p.priceEur = CurrencyAsset.convertToEuro(p.price, curr, date, context);
                        }
                        else
                        {
                            p.priceEur = p.price;
                            p.highEur = p.high;
                            p.lowEur = p.low;
                            p.openEur = p.open;
                        }
                        list_prices.Add(p);
                    }
                }
                list_prices.ForEach(p => context.Prices.Add(p));
                context.SaveChanges();
            }
        }