Пример #1
0
            public bool Set(PriceFluctuation fluctuation)
            {
                if (Grid == null)
                {
                    Debug.LogError("Сначала задайте Grid для свечки");
                }
                float bodyHeight   = Grid.Scale * ((float)(fluctuation.Close - fluctuation.Open));
                float shadowHeight = (float)(Grid.Scale * (fluctuation.High - fluctuation.Low));

                body.size    = new Vector2(body.size.x, bodyHeight);
                borders.size = body.size + new Vector2(borderWidth, bodyHeight > 0? borderWidth : -borderWidth);
                shadow.size  = new Vector2(shadow.size.x, shadowHeight);

                if (fluctuation.Open > fluctuation.Close)
                {
                    body.color = downColor;
                }
                else
                {
                    body.color = upColor;
                }
                shadow.color = shadowColor;

                transform.position        = new Vector2(Grid.FromDateToXAxis(fluctuation.PeriodBegin), bodyHeight / 2 + Grid.Scale * (float)fluctuation.Open);
                shadow.transform.position = new Vector2(shadow.transform.position.x, shadowHeight / 2 + Grid.Scale * (float)fluctuation.Low);
                PeriodBegin = fluctuation.PeriodBegin;
                return(true);
            }
Пример #2
0
        public void CalculateMovingAverage(int id, int length)
        {
            if (length <= 0)
            {
                Debug.LogError("length должен быть >0");
                return;
            }

            IEnumerable <PriceFluctuation> fluctuations = chartDataManager.GetPriceFluctuationsByTimeFrame(chartDataManager.DataBeginTime, chartDataManager.WorkEndTime);
            PriceFluctuation startFluct = fluctuations.OrderBy(f => f.PeriodBegin).ElementAtOrDefault(length - 1);

            if (startFluct != null)
            {
                foreach (var fluct in fluctuations.Where(f => f.PeriodBegin >= chartDataManager.WorkBeginTime && f.PeriodBegin >= startFluct.PeriodBegin))
                {
                    double ma = 0;
                    int    i  = 0;

                    foreach (var prev_fluct in fluctuations.Where(f => f.PeriodBegin <= fluct.PeriodBegin && f.PeriodBegin + (length - 1) * chartDataManager.TFrame >= fluct.PeriodBegin))
                    {
                        ma += prev_fluct.Close;
                        i++;
                    }


                    if (i == length)
                    {
                        ma /= length;
                        fluct.ExtraData[id] = (float)ma;
                    }
                }
            }
        }
 void ChangeText(PriceFluctuation fluct)
 {
     if (fluct != null)
     {
         openText.text   = fluct.Open.ToString();
         highText.text   = fluct.High.ToString();
         lowText.text    = fluct.Low.ToString();
         closText.text   = fluct.Close.ToString();
         volumeText.text = fluct.Volume.ToString();
     }
 }
Пример #4
0
            //Trying to execute. Returning true if success.
            internal bool TryToExecute(PriceFluctuation fluctuation)
            {
                switch (type)
                {
                case Type.Limit:
                {
                    if (!IsAmountCorrect(PlayerManager.Instance, Price))
                    {
                        state = State.Canceled;
                        Debug.Log("Недостаточно средств, для выполнения ордера");
                        return(false);
                    }

                    if (Amount > 0)
                    {
                        if (Price >= (decimal)fluctuation.Open)
                        {
                            ExecutionPrice = (decimal)fluctuation.Open;
                        }
                        else if (Price <= (decimal)fluctuation.Low)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (Price <= (decimal)fluctuation.Open)
                        {
                            ExecutionPrice = (decimal)fluctuation.Open;
                        }
                        else if (Price >= (decimal)fluctuation.High)
                        {
                            return(false);
                        }
                    }
                }
                break;

                case Type.Market:
                {
                    ExecutionPrice = (decimal)fluctuation.Open;
                    Amount         = AmountFilter(PlayerManager.Instance);
                }
                break;

                default: { throw new ArgumentOutOfRangeException("Действие для этого типа ордера не описано"); }
                }
                state = State.Filled;
                return(true);
            }
        public IEnumerable <double?> SimpleMovingAverage(IEnumerable <PriceFluctuation> fluctuations, TimeFrame TFrame, int length)
        {
            if (length <= 0)
            {
                Debug.LogError("length должен быть >0");
                return(null);
            }


            double?[] ma_values = new double?[fluctuations.Count()];

            PriceFluctuation pf = fluctuations.OrderBy(t => t.PeriodBegin).ElementAt(length - 1);

            int j = 0;
            int i;

            foreach (var fluct in fluctuations.Where(f => f.PeriodBegin >= pf.PeriodBegin))
            {
                double ma = 0;
                i = 0;

                foreach (var prev_fluct in fluctuations.Where(f => f.PeriodBegin <= fluct.PeriodBegin && f.PeriodBegin + (length - 1) * TFrame >= fluct.PeriodBegin))
                {
                    ma += prev_fluct.Close;
                    i++;
                }


                if (i == length)
                {
                    ma          /= length;
                    ma_values[j] = ma;
                }
                else
                {
                    ma_values[j] = null;
                }
                j++;
            }
            return(ma_values);
        }
Пример #6
0
    IEnumerable <PriceFluctuation> GetPriceFluctationInDESC(int framesCount)
    {
        if (!IsSettingsSet())
        {
            return(null);
        }

        PriceFluctuation[] candles = new PriceFluctuation[framesCount];
        double             high, low, open, close;
        DateTime           periodBegin; //periodEnd;
        double             volume;

        int jd = trades.Length - 1;

        for (int id = candles.Length - 1; id != -1; id--)
        {
            periodBegin = trades[jd].DTime.FloorToTimeFrame(TFrame);
            //periodEnd = trades[jd].DTime.UpToNextFrame(TFrame);

            high   = low = open = close = trades[jd].Price;
            volume = trades[jd].Volume;

            for (jd = jd - 1; jd != -1 && trades[jd].DTime > periodBegin; jd--)
            {
                if (trades[jd].Price > high)
                {
                    high = trades[id].Price;
                }
                if (trades[jd].Price < low)
                {
                    low = trades[id].Price;
                }
                open    = trades[jd].Price;
                volume += trades[jd].Volume;
            }
            candles[id] = new PriceFluctuation(periodBegin, volume, close, open, low, high);
        }
        current = jd;
        return(candles);
    }