Exemplo n.º 1
0
        public async Task GetVolumesAsync()
        {
            using (var client = new BinanceClient())
                using (var socketClient = new BinanceSocketClient())
                //using (var client = new BinanceClient())
                {
                    while (true)
                    {
                        foreach (DataRow pricer in CoinData.Rows)
                        {
                            var eighthourcandlesClient = await client.GetKlinesAsync(pricer[0].ToString(), KlineInterval.FiveMinutes, startTime : DateTime.UtcNow.AddHours(-8), endTime : DateTime.UtcNow, limit : 100);

                            if (eighthourcandlesClient.Success == false)
                            {
                                continue;
                            }

                            var eighthourcandles = eighthourcandlesClient.Data.ToList();
                            if (eighthourcandles.Count == 0)
                            {
                                continue;
                            }

                            IOrderedEnumerable <BinanceKline> candlearray = eighthourcandles.OrderByDescending(e => e.CloseTime);
                            decimal averagevolumeeighthours = candlearray.Average(candlestick => candlestick.Volume);
                            decimal fifteenminavg           = (candlearray.ElementAt(1).Volume + candlearray.ElementAt(2).Volume +
                                                               candlearray.ElementAt(0).Volume) / 3;


                            var orderBook = await client.GetOrderBookAsync(pricer[0].ToString(), 5);

                            //BinanceOrderBook orderBook = await client.GetOrderBookAsync(pricer[0].ToString(), 10);
                            IOrderedEnumerable <BinanceOrderBookEntry> orderAsks = orderBook.Data.Asks.OrderBy(e => e.Price);
                            IOrderedEnumerable <BinanceOrderBookEntry> orderBids = orderBook.Data.Bids.OrderBy(e => e.Price);


                            decimal totAsk        = orderAsks.AsEnumerable().Sum(o => o.Quantity);
                            decimal totBid        = orderBids.AsEnumerable().Sum(o => o.Quantity);
                            decimal rangeQuantity = 100 * (totBid - totAsk) / (totAsk + totBid);
                            decimal maxAsk        = orderAsks.First().Quantity;
                            decimal maxBid        = orderBids.First().Quantity;
                            decimal range         = 100 * (maxBid - maxAsk) / (maxAsk + maxBid);


                            var aggTrades = await client.GetAggregatedTradesAsync(pricer[0].ToString(), startTime : DateTime.UtcNow.AddMinutes(-2), endTime : DateTime.UtcNow, limit : 10);

                            if (aggTrades.Success == false)
                            {
                                continue;
                            }
                            //.Data.ToList()
                            DataRow row = CoinData.Rows.Cast <DataRow>().First(o => o[0].ToString() == pricer[0].ToString());
                            row[1] = DateTime.UtcNow;
                            row[2] = eighthourcandles.Last().Close;
                            row[3] = Decimal.Round(averagevolumeeighthours, MidpointRounding.AwayFromZero);
                            row[4] = PercentGive(candlearray.First().Volume, averagevolumeeighthours);
                            row[5] = PercentGive(fifteenminavg, averagevolumeeighthours);
                            row[7] = Math.Round(range, 5);
                            row[8] = Math.Round(rangeQuantity, 5);
                            int i = 0;
                            while (i != 20)
                            {
                                if (candlearray.ElementAt(i).Close < candlearray.ElementAt(i + 1).Close)
                                {
                                    break;
                                }
                                i++;
                            }
                            row[6] = i;

                            if (enableTrading)
                            {
                                IEnumerable <Candle> candls = candlearray.Select(p => new Candle(p.CloseTime, p.Open, p.High, p.Low, p.Close, p.Volume)).OrderBy(p => p.DateTime);
                                Trady.Analysis.Indicator.AverageDirectionalIndexRating avgDirIdx = new Trady.Analysis.Indicator.AverageDirectionalIndexRating(candls, 12, 12);
                                Trady.Analysis.Indicator.ExponentialMovingAverage      mavgFast  = new Trady.Analysis.Indicator.ExponentialMovingAverage(candls, 7);
                                //Trady.Analysis.Indicator.ExponentialMovingAverage mavgSlow = new Trady.Analysis.Indicator.ExponentialMovingAverage(candls, 50);
                                //Trady.Analysis.Indicator.IchimokuCloud ich = new Trady.Analysis.Indicator.IchimokuCloud(candls, 3, 9, 50);
                                Trady.Analysis.Indicator.SimpleMovingAverage ind_mavgFast = new Trady.Analysis.Indicator.SimpleMovingAverage(candls, 7);
                                Trady.Analysis.Indicator.SimpleMovingAverage ind_mavgSlow = new Trady.Analysis.Indicator.SimpleMovingAverage(candls, 50);
                                //var ind_mavgFast = candls.Sma(7).LastOrDefault().Tick;
                                //var ind_mavgSlow = candls.Sma(50).LastOrDefault().Tick;

                                /*
                                 * if (ind_mavgFast != null && ind_mavgSlow != null) {
                                 * if (ind_mavgFast > ind_mavgSlow)
                                 * {
                                 * var ind_adx = candls.SlowSto(7,14).LastOrDefault().Tick.Item1;
                                 * //Binance.Account.Orders.LimitOrder lmtOrder = new Binance.Account.Orders.LimitOrder(iuser);
                                 * //lmtOrder.Symbol = pricer[0].ToString();
                                 * //lmtOrder.Id = "1230";
                                 * //lmtOrder.Price = candlearray.First().Close;
                                 * //lmtOrder.Quantity = 100000;
                                 * //lmtOrder.IcebergQuantity = 12;
                                 * //api.PlaceAsync(lmtOrder, 0, CancellationToken.None);
                                 * //var testOrderResult = client.PlaceTestOrder(pricer[0].ToString(), OrderSide.Buy, OrderType.Market, 1, price: 1, timeInForce: TimeInForce.GoodTillCancel);
                                 * }
                                 * }*/
                            }

                            sql = "INSERT INTO ORDERS (Symbol, Scanned, Price, Volume, TakerBuyBaseAssetVolume, TakerBuyQuoteAssetVolume, NumberOfTrades, Hr8Av, NowPercent, Min15Percent, GreenCandles) VALUES ('" + pricer[0].ToString() + "','" + row[1].ToString() + "', '" + row[2].ToString() + "', '" + candlearray.First().Volume.ToString() + "', '" + candlearray.First().TakerBuyBaseAssetVolume.ToString() + "', '" + candlearray.First().TakerBuyQuoteAssetVolume.ToString() + "', '" + candlearray.First().TradeCount.ToString() + "', '" + row[3].ToString() + "', '" + row[4].ToString() + "', '" + row[5].ToString() + "', '" + row[6].ToString() + "');";
                            SQLiteCommand command = new SQLiteCommand(sql, _conn);
                            await command.ExecuteNonQueryAsync();

                            //var filtered = CoinData.Select().Where(o => Convert.ToDecimal(o[3].ToString()) > Convert.ToDecimal(o[4].ToString()));
                            if (this.WindowState != FormWindowState.Minimized)
                            {
                                await Task.Run(() =>
                                {
                                    backgroundWorkerView.RunWorkerAsync();
                                    //coindatagridview.DataSource = CoinData;
                                    //DataViewColours();

                                    //    //coindatagridview.Sort(coindatagridview.Columns[3], ListSortDirection.Descending);
                                });
                            }
                        }
                    }
                }
            //return CoinData;
        }
Exemplo n.º 2
0
        public async Task <DataTable> GetVolumes()
        {
            await SetupCoinData();

            using (var client = new BinanceClient())
            {
                while (true)
                {
                    foreach (DataRow pricer in CoinData.Rows)
                    {
                        IEnumerable <BinanceKline>        eighthourcandles = client.GetKlines(pricer[0].ToString(), KlineInterval.FiveMinutes, startTime: DateTime.UtcNow.AddHours(-8), endTime: DateTime.UtcNow, limit: 100).Data.ToList();
                        IOrderedEnumerable <BinanceKline> candlearray      = eighthourcandles.OrderByDescending(e => e.CloseTime);
                        decimal averagevolumeeighthours = candlearray.Average(candlestick => candlestick.Volume);
                        decimal fifteenminavg           = (candlearray.ElementAt(1).Volume + candlearray.ElementAt(2).Volume +
                                                           candlearray.ElementAt(0).Volume) / 3;
                        BinanceOrderBook orderBook = client.GetOrderBook(pricer[0].ToString(), 10).Data;
                        IOrderedEnumerable <BinanceOrderBookEntry> orderAsks = orderBook.Asks.OrderByDescending(e => e.Quantity);
                        IOrderedEnumerable <BinanceOrderBookEntry> orderBids = orderBook.Bids.OrderByDescending(e => e.Quantity);
                        decimal maxAsk = orderAsks.First().Price;
                        decimal maxBid = orderBids.First().Price;
                        IEnumerable <BinanceAggregatedTrades> aggTrades = client.GetAggregatedTrades(pricer[0].ToString(), startTime: DateTime.UtcNow.AddMinutes(-2), endTime: DateTime.UtcNow, limit: 10).Data.ToList();
                        DataRow row = CoinData.Rows.Cast <DataRow>().First(o => o[0].ToString() == pricer[0].ToString());
                        row[1] = DateTime.UtcNow;
                        row[2] = eighthourcandles.Last().Close;
                        row[3] = Decimal.Round(averagevolumeeighthours, MidpointRounding.AwayFromZero);
                        row[4] = PercentGive(candlearray.First().Volume, averagevolumeeighthours);
                        row[5] = PercentGive(fifteenminavg, averagevolumeeighthours);
                        int i = 0;
                        while (i != 20)
                        {
                            if (candlearray.ElementAt(i).Close < candlearray.ElementAt(i + 1).Close)
                            {
                                break;
                            }
                            i++;
                        }
                        row[6] = i;
                        //if (enableTrading)
                        {
                            IEnumerable <Candle> candls = candlearray.Select(p => new Candle(p.CloseTime, p.Open, p.High, p.Low, p.Close, p.Volume));
                            Trady.Analysis.Indicator.AverageDirectionalIndexRating avgDirIdx = new Trady.Analysis.Indicator.AverageDirectionalIndexRating(candls, 12, 12);
                            Trady.Analysis.Indicator.ModifiedMovingAverage         mavgFast  = new Trady.Analysis.Indicator.ModifiedMovingAverage(candls, 9);
                            Trady.Analysis.Indicator.ModifiedMovingAverage         mavgSlow  = new Trady.Analysis.Indicator.ModifiedMovingAverage(candls, 50);
                            Trady.Analysis.Indicator.IchimokuCloud ich = new Trady.Analysis.Indicator.IchimokuCloud(candls, 3, 9, 50);

                            /*if (ich.ShortPeriodCount > ich.MiddlePeriodCount)
                             * {
                             *  Binance.Account.Orders.LimitOrder lmtOrder = new Binance.Account.Orders.LimitOrder(iuser);
                             *  lmtOrder.Symbol = pricer[0].ToString();
                             *  lmtOrder.Id = "1230";
                             *  lmtOrder.Price = candlearray.First().Close;
                             *  lmtOrder.Quantity = 100000;
                             *  lmtOrder.IcebergQuantity = 12;
                             *  api.PlaceAsync(lmtOrder, 0, CancellationToken.None);
                             * }*/
                        }
                        sql = "INSERT INTO ORDERS (Symbol, Scanned, Price, Volume, TakerBuyBaseAssetVolume, TakerBuyQuoteAssetVolume, NumberOfTrades, Hr8Av, NowPercent, Min15Percent, GreenCandles) VALUES ('" + pricer[0].ToString() + "','" + row[1].ToString() + "', '" + row[2].ToString() + "', '" + candlearray.First().Volume.ToString() + "', '" + candlearray.First().TakerBuyBaseAssetVolume.ToString() + "', '" + candlearray.First().TakerBuyQuoteAssetVolume.ToString() + "', '" + candlearray.First().Trades.ToString() + "', '" + row[3].ToString() + "', '" + row[4].ToString() + "', '" + row[5].ToString() + "', '" + row[6].ToString() + "');";
                        SQLiteCommand command = new SQLiteCommand(sql, _conn);
                        command.ExecuteNonQuery();
                        if (chkShowNowPercent.Checked)
                        {
                            CoinData.DefaultView.RowFilter = "NowPercent > 0";
                        }
                        //var filtered = CoinData.Select().Where(o => Convert.ToDecimal(o[3].ToString()) > Convert.ToDecimal(o[4].ToString()));
                        if (this.WindowState != FormWindowState.Minimized)
                        {
                            DataViewColours();
                            coindatagridview.DataSource = CoinData;
                            //coindatagridview.Sort(coindatagridview.Columns[3], ListSortDirection.Descending);
                        }
                    }
                }
            }

            return(CoinData);
        }