Exemplo n.º 1
0
 private async Task OnVoluming(PullerSession session)
 {
     using (var db = DatabaseService.CreateContext())
     {
         db.Database.ExecuteSqlCommand(
             "UPDATE borsa_currency_t A SET Volume24Hour = (SELECT LEAST(SUM(B.Volume / GREATEST(B.VolumePeriod,1)), 99999999.99999999) FROM history_t B WHERE B.BorsaCurrencyId = A.Id AND B.EntryDate > DATE_SUB(NOW(), INTERVAL 1 DAY)) WHERE NOT Disabled = 1 AND NOT AutoGenerated = 1 AND Volume > 0");
     }
 }
Exemplo n.º 2
0
 private async Task OnClearHistory(PullerSession session)
 {
     using (var db = DatabaseService.CreateContext())
     {
         //clear old history
         var date = DateTime.Now.AddDays(-2);
         db.Database.ExecuteSqlCommand("DELETE FROM history_t WHERE EntryDate < '" +
                                       date.ToString("yyyy-MM-dd HH:mm:ss") + "'");
     }
 }
Exemplo n.º 3
0
        public static PullerSession Create()
        {
            PullerSession session = new PullerSession();

            foreach (var socket in SocketManager.Borsa.Sockets)
            {
                try
                {
                    CoreSocketClient client = new CoreSocketClient()
                    {
                        GetTimeout = 30000
                    };
                    client.ConnectAndListen("wss://" + socket + "/ws");
                    session.Clients.Add(client);
                }
                catch (Exception e)
                {
                }
            }

            return(session);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a timer with given interval that will work to pull data from exchange markets
        /// </summary>
        /// <param name="name">Name of the puller</param>
        /// <param name="seconds">interval in seconds</param>
        /// <param name="delaySeconds">Wait before starting puller</param>
        /// <param name="onPullingHandler">Handler to be run while pulling</param>
        protected void CreatePuller(string name, int seconds, int delaySeconds, Func <PullerSession, Task> onPullingHandler, bool needsPuller = true)
        {
#if DEBUG
            if (!Tester.IsServiceTesting(this, name))
            {
                return;
            }
#endif
            Task.Factory.StartNew(() =>
            {
                if (delaySeconds > 0)
                {
                    Thread.Sleep(delaySeconds * 1000);
                }

                var timer  = new Puller(seconds * 1000);
                timer.Name = name;
                ElapsedEventHandler onTimerElapsed = (s, a) =>
                {
                    var puller = (Puller)s;
                    try
                    {
                        if (puller.IsPulling)
                        {
                            Console.WriteLine(Name + " " + puller.Name + ": Still running, skipping...");
                        }
                        else
                        {
                            puller.IsPulling = true;
                            if (SocketManager.Maintenence)
                            {
                                Console.WriteLine(Name + " " + puller.Name + ": Waiting maintenance...");
                            }
                            SocketManager.MaintenenceEvent.WaitOne();
                            //var t = new System.Timers.Timer(60000 * 2);
                            //t.Elapsed += (sender, args) =>
                            //{
                            //    puller.IsPulling = false;
                            //    try
                            //    {
                            //        t.Stop();
                            //        t.Dispose();
                            //    }
                            //    catch (Exception e)
                            //    {
                            //    }
                            //};
                            //t.Start();
                            Console.WriteLine(Name + " " + puller.Name + ": Running...");
                            if (needsPuller)
                            {
                                using (var session = PullerSession.Create())
                                {
                                    onPullingHandler(session).Wait();
                                }
                            }
                            else
                            {
                                onPullingHandler(null).Wait();
                            }
                            //try
                            //{
                            //    t.Stop();
                            //    t.Dispose();
                            //}
                            //catch (Exception e)
                            //{
                            //}
                            puller.IsPulling = false;
                            Console.WriteLine(Name + " " + puller.Name + ": Done");
                        }
                    }
                    catch (Exception e)
                    {
                        puller.IsPulling = false;
                        Console.WriteLine(e);
                    }
                };
                timer.Elapsed += onTimerElapsed;
                lock (Pullers)
                {
                    Pullers[name] = timer;
                }
                timer.Start();
                onTimerElapsed.Invoke(timer, null);
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sends a GET request to the given url and returns the json result as object
        /// </summary>
        /// <typeparam name="T">Return object type</typeparam>
        /// <param name="url">Url to request</param>
        /// <param name="configurationFactory">Can be used to configure client before sending request</param>
        /// <returns></returns>
        protected T Pull <T>(string url, PullerSession session)
        {
            WaitForLimit();
            try
            {
                int trial = 0;
                //url = $"http://zzproxy{counter}.eu-gb.mybluemix.net/index.php?q=" + Uri.EscapeDataString(url);
                while (trial++ < 6)
                {
                    //lock (locker)
                    //{
                    //    counter = SocketManager.NextIndex(ExchangeId, counter);
                    //}
                    //var response = SocketManager.Pull(url, headers, ExchangeId, counter, Name);
                    try
                    {
                        var cl = session.GetNextClient();
                        //Console.WriteLine($"{Name} pulling on #{cl.Url}");
                        return(JsonConvert.DeserializeObject <T>(cl.Get <string>(url)));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Url: " + url + " " + e.Message);
                    }

                    //if (response.IsSuccess)
                    //{
                    //    try
                    //    {
                    //        var r = JsonConvert.DeserializeObject<T>(response.Response);
                    //        if (r == null)
                    //        {
                    //            throw new Exception("Null error: " + url);
                    //        }

                    //        return r;
                    //    }
                    //    catch (Exception e)
                    //    {
                    //        Console.WriteLine("Url: " + url + " " + e);
                    //        if (trial == 6)
                    //            throw;
                    //    }
                    //}

                    Thread.Sleep(500);
                    //using (var response = await Client.GetAsync(url).ConfigureAwait(false))
                    //{
                    //    var bytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                    //    var content = Encoding.UTF8.GetString(bytes);
                    //    try
                    //    {
                    //        var r = JsonConvert.DeserializeObject<T>(content);
                    //        if (r == null)
                    //        {
                    //            throw new Exception("Null error");
                    //        }

                    //        return r;
                    //    }
                    //    catch (Exception e)
                    //    {
                    //        Console.WriteLine("Url: " + url + " " + e);
                    //        if (trial == 6)
                    //            throw;
                    //        Thread.Sleep(500);
                    //    }
                    //}
                    //Console.WriteLine("Retrying...");
                }
                throw new Exception("Null error: " + url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(default(T));
            }
        }
Exemplo n.º 6
0
        private async Task OnCalculateArbitrage(PullerSession session)
        {
            using (var db = DatabaseService.CreateContext())
            {
                var lastDate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:mm:ss");
                var sql      = $@"SELECT * FROM (SELECT T1.*, T2.*, FC.ShortName FromCoin, TC.ShortName ToCoin, ((T1.BuyArbitrageWeight - T2.SellArbitrageWeight) / T2.SellArbitrageWeight) * 100 AS ChangeRate FROM 
(SELECT BC.Id, BC.CanWithdraw, BC.CanDeposit, BC.Price, BC.BuyArbitrageWeight, BC.BuyArbitrageVolume, BC.SellArbitrageVolume, BC.BorsaId, BC.FromCurrencyId, BC.ToCurrencyId, B.Name BorsaName, BC.Volume24Hour FROM borsa_currency_t BC 
INNER JOIN borsa_t B ON BC.BorsaId = B.Id
WHERE (NOT BC.Disabled = TRUE) AND (NOT BC.AutoGenerated = TRUE) AND (BC.LastUpdate > '{lastDate}')
) T1 
INNER JOIN 
(SELECT BC.Id Id1, BC.CanWithdraw CanWithdraw1, BC.CanDeposit CanDeposit1, BC.Price Price1, BC.BuyArbitrageVolume BuyArbitrageVolume1, BC.SellArbitrageVolume SellArbitrageVolume1, BC.SellArbitrageWeight, BC.BorsaId BorsaId1, BC.FromCurrencyId FromCurrencyId1, BC.ToCurrencyId ToCurrencyId1, B.Name BorsaName1, BC.Volume24Hour Volume24Hour1 FROM borsa_currency_t BC 
INNER JOIN borsa_t B ON BC.BorsaId = B.Id
WHERE (NOT BC.Disabled = TRUE) AND (NOT BC.AutoGenerated = TRUE) AND (BC.LastUpdate > '{lastDate}')
) T2 ON
T1.ToCurrencyId = T2.ToCurrencyId1 AND T1.FromCurrencyId = T2.FromCurrencyId1 AND NOT T1.BorsaId = T2.BorsaId1
INNER JOIN currency_t TC ON T1.ToCurrencyId = TC.Id 
INNER JOIN currency_t FC ON T1.FromCurrencyId = FC.Id 
) TT
WHERE ChangeRate > 0";

                db.ArbitrageT.Load();
                var dic = db.ArbitrageT.Local.ToDictionary(a => Tuple.Create((int?)a.BuyerBcid, (int?)a.SellerBcid), a => a);
                using (var cmd = db.Database.GetDbConnection().CreateCommand())
                {
                    cmd.CommandText = sql;
                    try
                    {
                        db.Database.OpenConnection();
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var key    = Tuple.Create(reader["Id1"] as int?, reader["Id"] as int?);
                                var entity = dic.ContainsKey(key)
                                    ? dic[key]
                                    : db.ArbitrageT.Add(new ArbitrageT()
                                {
                                    FromBorsaId    = (int)reader["BorsaId1"],
                                    ToBorsaId      = (int)reader["BorsaId"],
                                    FromCurrencyId = (int)reader["FromCurrencyId1"],
                                    ToCurrencyId   = (int)reader["ToCurrencyId1"],
                                }).Entity;

                                if (entity.Id > 0)
                                {
                                    db.ArbitrageHistoryT.Add(new ArbitrageHistoryT()
                                    {
                                        BuyerBcid                 = entity.BuyerBcid,
                                        SellerBcid                = entity.SellerBcid,
                                        FromCurrencyId            = entity.FromCurrencyId,
                                        ToBorsaId                 = entity.ToBorsaId,
                                        ToCurrencyId              = entity.ToCurrencyId,
                                        BuyerPrice                = entity.BuyerPrice,
                                        BuyerBuyArbitrageVolume   = entity.BuyerBuyArbitrageVolume,
                                        BuyerSellArbitrageVolume  = entity.BuyerSellArbitrageVolume,
                                        BuyerSellArbitrageWeight  = entity.BuyerSellArbitrageWeight,
                                        BuyerVolume24Hour         = entity.BuyerVolume24Hour,
                                        ChangeRate                = entity.ChangeRate,
                                        EntryDate                 = entity.EntryDate,
                                        FromBorsaId               = entity.FromBorsaId,
                                        SellerBuyArbitrageVolume  = entity.SellerBuyArbitrageVolume,
                                        SellerBuyArbitrageWeight  = entity.SellerBuyArbitrageWeight,
                                        SellerSellArbitrageVolume = entity.SellerSellArbitrageVolume,
                                        SellerPrice               = entity.SellerPrice,
                                        SellerVolume24Hour        = entity.SellerVolume24Hour,
                                        isTradable                = entity.isTradable
                                    });
                                }
                                entity.BuyerBcid  = (int)reader["Id1"];
                                entity.BuyerPrice = (reader["Price1"] as decimal?).GetValueOrDefault(0);
                                entity.BuyerSellArbitrageWeight = (reader["SellArbitrageWeight"] as decimal?).GetValueOrDefault(0);
                                entity.BuyerBuyArbitrageVolume  = (reader["BuyArbitrageVolume1"] as decimal?).GetValueOrDefault(0);
                                entity.BuyerSellArbitrageVolume = (reader["SellArbitrageVolume1"] as decimal?).GetValueOrDefault(0);
                                entity.BuyerVolume24Hour        = reader["Volume24Hour1"] as decimal?;
                                entity.SellerBcid  = (int)reader["Id"];
                                entity.SellerPrice = (reader["Price"] as decimal?).GetValueOrDefault(0);
                                entity.SellerBuyArbitrageWeight  = (reader["BuyArbitrageWeight"] as decimal?).GetValueOrDefault(0);
                                entity.SellerBuyArbitrageVolume  = (reader["BuyArbitrageVolume"] as decimal?).GetValueOrDefault(0);
                                entity.SellerSellArbitrageVolume = (reader["SellArbitrageVolume"] as decimal?).GetValueOrDefault(0);
                                entity.SellerVolume24Hour        = reader["Volume24Hour"] as decimal?;
                                entity.ChangeRate = Math.Min(Math.Round((reader["ChangeRate"] as decimal?).GetValueOrDefault(0), 4), 999999.9999m);
                                entity.EntryDate  = DateTime.Now;
                                entity.isTradable =
                                    !"0".Equals(reader["CanWithdraw1"].ToString()) && !"0".Equals(reader["CanDeposit1"].ToString()) &&
                                    !"0".Equals(reader["CanWithdraw"].ToString()) && !"0".Equals(reader["CanWithdraw"].ToString());
                                entity.FromCoinName  = reader["FromCoin"].ToString();
                                entity.ToCoinName    = reader["ToCoin"].ToString();
                                entity.FromBorsaName = reader["BorsaName1"].ToString();
                                entity.ToBorsaName   = reader["BorsaName"].ToString();
                            }
                        }
                        db.Database.CloseConnection();
                    }
                    catch
                    {
                    }
                }
                db.SaveChanges();
            }
        }