Example #1
0
        /// <summary>
        /// Calculate single value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override RelativeStrengthIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null)
            {
                return(this);
            }

            var positives = new List <double>(Interval);
            var negatives = new List <double>(Interval);

            for (var i = 1; i <= Interval; i++)
            {
                var nextPrice     = collection.ElementAtOrDefault(collection.Count - i);
                var previousPrice = collection.ElementAtOrDefault(collection.Count - i - 1);

                if (nextPrice != null && previousPrice != null)
                {
                    positives.Add(Math.Max(nextPrice.Bar.Close.Value - previousPrice.Bar.Close.Value, 0.0));
                    negatives.Add(Math.Max(previousPrice.Bar.Close.Value - nextPrice.Bar.Close.Value, 0.0));
                }
            }

            var averagePositive    = CalculationManager.SimpleAverage(positives, positives.Count - 1, Interval);
            var averageNegative    = CalculationManager.SimpleAverage(negatives, negatives.Count - 1, Interval);
            var average            = ConversionManager.Compare(averageNegative, 0) ? 1.0 : averagePositive / averageNegative;
            var nextValue          = 100.0 - 100.0 / (1.0 + average);
            var nextIndicatorPoint = new PointModel
            {
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Last      = nextValue,
                Bar       = new PointBarModel
                {
                    Close = nextValue
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new RelativeStrengthIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = nextIndicatorPoint.Bar.Close;
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
Example #2
0
        /// <summary>
        /// Calculate indicator value
        /// </summary>
        /// <param name="currentPoint"></param>
        /// <returns></returns>
        public ImbalanceIndicator Calculate(IIndexCollection <IPointModel> collection, int direction = 0)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null)
            {
                return(this);
            }

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new ImbalanceIndicator();
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].TimeFrame = currentPoint.TimeFrame;
            currentPoint.Series[Name].ChartData = ChartData;

            var value = 0.0;

            switch (direction)
            {
            case 0: value = currentPoint.AskSize.Value - currentPoint.BidSize.Value; break;

            case 1: value = currentPoint.AskSize.Value; break;

            case -1: value = currentPoint.BidSize.Value; break;
            }

            currentPoint.Series[Name].Last      = value;
            currentPoint.Series[Name].Bar.Close = value;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            // Save values

            var nextIndicatorPoint = new PointModel
            {
                Last      = Last,
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Bar       = new PointBarModel
                {
                    Close = Last
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            return(this);
        }
Example #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        public InstrumentModel()
        {
            SwapLong     = 0.0;
            SwapShort    = 0.0;
            StepSize     = 0.01;
            StepValue    = 0.01;
            Commission   = 0.0;
            ContractSize = 1.0;

            ChartData   = new ChartDataModel();
            Points      = new TimeSpanCollection <IPointModel>();
            PointGroups = new TimeSpanCollection <IPointModel>();
        }
        /// <summary>
        /// Calculate single value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override MovingAverageIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null)
            {
                return(this);
            }

            var pointPrice = currentPoint.Bar.Close;

            switch (Mode)
            {
            case MovingAverageEnum.Bid: pointPrice = currentPoint.Bid; break;

            case MovingAverageEnum.Ask: pointPrice = currentPoint.Ask; break;
            }

            var nextIndicatorPoint = new PointModel
            {
                Last      = pointPrice,
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Bar       = new PointBarModel
                {
                    Close = pointPrice
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            var average = CalculationManager.LinearWeightAverage(Values.Select(o => o.Bar.Close.Value), Values.Count - 1, Interval);

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new MovingAverageIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = ConversionManager.Compare(average, 0) ? nextIndicatorPoint.Bar.Close : average;
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
        /// <summary>
        /// Calculate single value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override AverageTrueRangeIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint  = collection.ElementAtOrDefault(collection.Count - 1);
            var previousPoint = collection.ElementAtOrDefault(collection.Count - 2);

            if (currentPoint == null || previousPoint == null)
            {
                return(this);
            }

            var variance =
                Math.Max(currentPoint.Bar.High.Value, previousPoint.Bar.Close.Value) -
                Math.Min(currentPoint.Bar.Low.Value, previousPoint.Bar.Close.Value);

            var nextIndicatorPoint = new PointModel
            {
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Last      = variance,
                Bar       = new PointBarModel
                {
                    Close = variance
                }
            };

            if (Values.Count > Interval)
            {
                nextIndicatorPoint.Bar.Close = nextIndicatorPoint.Last = (Values.ElementAtOrDefault(Values.Count - 1).Bar.Close *Math.Max(Interval - 1, 0) + variance) / Interval;
            }

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel o) ? o : new AverageTrueRangeIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = nextIndicatorPoint.Bar.Close;
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
Example #6
0
        /// <summary>
        /// Calculate indicator value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override ScaleIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null || currentPoint.Series == null)
            {
                return(this);
            }

            var pointValue = currentPoint.Bar.Close ?? 0.0;

            _min = _min == null ? pointValue : Math.Min(_min.Value, pointValue);
            _max = _max == null ? pointValue : Math.Max(_max.Value, pointValue);

            var nextValue = ConversionManager.Compare(_min, _max) ? 0.0 : Min + (pointValue - _min.Value) * (Max - Min) / (_max.Value - _min.Value);

            var nextIndicatorPoint = new PointModel
            {
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Last      = nextValue,
                Bar       = new PointBarModel
                {
                    Close = nextValue
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new ScaleIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = CalculationManager.LinearWeightAverage(Values.Select(o => o.Bar.Close.Value), Values.Count - 1, Interval);
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
Example #7
0
        /// <summary>
        /// Calculate indicator value
        /// </summary>
        /// <param name="currentPoint"></param>
        /// <returns></returns>
        public PerformanceIndicator Calculate(IIndexCollection <IPointModel> collection, IEnumerable <IAccountModel> accounts)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null)
            {
                return(this);
            }

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new PerformanceIndicator();
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].TimeFrame = currentPoint.TimeFrame;
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = accounts.Sum(o => o.Balance + o.ActivePositions.Sum(v => v.GainLossAverageEstimate));
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
Example #8
0
 public EntityTable(IStorage storage, IIndexCollection indexes)
 {
     _storage = storage;
     _indexes = indexes;
 }
Example #9
0
 /// <summary>
 /// Constructor
 /// </summary>
 public ProcessorModel()
 {
     Charts   = new IndexCollection <IChartModel>();
     Gateways = new IndexCollection <IGatewayModel>();
 }