Ejemplo n.º 1
0
        //Candle stuff
        public void PlaceCandleInChart(MainService.Candle can)
        {
            //First adjust the candle high and low if the open and close are the same
            if (can.high == can.low)
            {
                can.high = can.high + MainService.double_epsilon; //Allow us to create a range
                can.low  = can.low - MainService.double_epsilon;
                if (can.low < 0)
                {
                    can.low = 0;
                }
            }

            //And it will add it to the list
            lock (MainService.ChartLastPrice)
            {
                if (MainService.VisibleCandles.Count >= 100)
                {
                    MainService.VisibleCandles.RemoveAt(0); //Remove the first / oldest candle
                }
                MainService.VisibleCandles.Add(can);
            }
            //Don't adjust candle positions until manually forced
            last_candle_time = MainService.UTCTime();
        }
Ejemplo n.º 2
0
        public void UpdateLastCandle(double val)
        {
            //This will update the last candle in the view, must be ran from UI thread
            if (MainService.VisibleCandles.Count == 0)
            {
                //Make a new candle, how history exists
                MainService.Candle can = new MainService.Candle();
                can.open  = val;
                can.close = val;
                can.high  = val;
                can.low   = val;
                PlaceCandleInChart(can);
            }
            else
            {
                //This will update the value for the last candle
                MainService.Candle can = MainService.VisibleCandles[MainService.VisibleCandles.Count - 1]; //Get last candle
                if (val > can.high)
                {
                    can.high = val;
                }
                if (val < can.low)
                {
                    can.low = val;
                }

                can.close = val;

                //Look at the last chartlastprice to find the close price for the candle
                int timeline = chart_timeline;
                lock (MainService.ChartLastPrice)
                {
                    for (int i = MainService.ChartLastPrice[timeline].Count - 1; i >= 0; i--)
                    {
                        if (MainService.ChartLastPrice[timeline][i].market == MainService.exchange_market)
                        {
                            can.close = Convert.ToDouble(MainService.ChartLastPrice[timeline][i].price);
                            break;
                        }
                    }
                }
            }
            AdjustCandlePositions();
        }
Ejemplo n.º 3
0
        public void AddCurrentCandle()
        {
            if (MainService.ChartLastPrice[chart_timeline].Count == 0)
            {
                return;
            }
            //This will add a new candle based on current last chart prices
            //Then Load the current candle into the chart. This candle is not stored in database and based soley and chartlastprice
            double open = -1, close = -1, high = -1, low = -1;

            for (int pos = 0; pos < MainService.ChartLastPrice[chart_timeline].Count; pos++)
            {
                if (MainService.ChartLastPrice[chart_timeline][pos].market == MainService.exchange_market)
                {
                    double price = Convert.ToDouble(MainService.ChartLastPrice[chart_timeline][pos].price);
                    if (open < 0)
                    {
                        open = price;
                    }
                    if (price > high)
                    {
                        high = price;
                    }
                    if (low < 0 || price < low)
                    {
                        low = price;
                    }
                    close = price; //The last price will be the close
                }
            }
            if (open > 0)
            {
                //May not have any candles for this market
                MainService.Candle new_can = new MainService.Candle();
                new_can.open  = open;
                new_can.close = close;
                new_can.high  = high;
                new_can.low   = low;
                PlaceCandleInChart(new_can);
            }
        }
Ejemplo n.º 4
0
        public void UpdateCandles()
        {
            //Do the off main thread stuff first, then mainthread stuff
            SqliteConnection mycon = new SqliteConnection("Data Source=\"" + MainService.App_Path + "/neblidex.db\";Version=3;");

            mycon.Open();

            //Set our busy timeout, so we wait if there are locks present
            SqliteCommand statement = new SqliteCommand("PRAGMA busy_timeout = 5000", mycon); //Create a transaction to make inserts faster

            statement.ExecuteNonQuery();
            statement.Dispose();

            string myquery  = "";
            int    backtime = 0;

            if (chart_timeline == 0)
            { //24 hr
                backtime = MainService.UTCTime() - 60 * 60 * 25;
                myquery  = "Select highprice, lowprice, open, close From CANDLESTICKS24H Where market = @mark And utctime > @time Order By utctime ASC";
            }
            else if (chart_timeline == 1)
            {                                                                                  //7 day
                backtime = MainService.UTCTime() - (int)Math.Round(60.0 * 60.0 * 24.0 * 6.25); //Closer to actual time of 100 candles
                myquery  = "Select highprice, lowprice, open, close From CANDLESTICKS7D Where market = @mark And utctime > @time Order By utctime ASC";
            }

            statement = new SqliteCommand(myquery, mycon);
            statement.Parameters.AddWithValue("@time", backtime);
            statement.Parameters.AddWithValue("@mark", MainService.exchange_market);
            SqliteDataReader statement_reader = statement.ExecuteReader();
            DataTable        table            = new DataTable();

            table.Load(statement_reader); //Loads all the data in the table
            statement_reader.Close();
            statement.Dispose();
            mycon.Close();

            //Do most the work on the background thread
            //Clear the Visible Candles and reload the charts for the appropriate timescale and market
            lock (MainService.ChartLastPrice)
            {
                MainService.VisibleCandles.Clear();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    //Go Candle by Candle to get results
                    MainService.Candle can = new MainService.Candle();
                    //Must use cultureinfo as some countries see . as ,
                    can.open  = Convert.ToDouble(table.Rows[i]["open"], CultureInfo.InvariantCulture);
                    can.close = Convert.ToDouble(table.Rows[i]["close"], CultureInfo.InvariantCulture);
                    can.low   = Convert.ToDouble(table.Rows[i]["lowprice"], CultureInfo.InvariantCulture);
                    can.high  = Convert.ToDouble(table.Rows[i]["highprice"], CultureInfo.InvariantCulture);
                    PlaceCandleInChart(can);
                }

                //Add a recent candle based on the last trade
                AddCurrentCandle();
            }

            //The following will be ran on the UI thread
            //Now work on the data on the main thread
            MainService.NebliDex_Activity.RunOnUiThread(() => {
                Market_Percent.TextColor = App.grey_color;
                Market_Percent.Text      = "00.00%";
                Chart_Last_Price.Text    = "0.00000000";

                AdjustCandlePositions();
            });
        }