Exemplo n.º 1
0
        internal void ProcessSell(DateTime time, double bid, double ask, UnilateralTickCollection <DateTime> ticks)
        {
            KeyValuePair <DateTime, double>?whenTP = ticks.FirstEventWhenLessOrEqual(time, bid - m_tp);

            AllCount++;
            if (null == whenTP)
            {
                return;
            }
            Count++;
            KeyValuePair <DateTime, double> when = (KeyValuePair <DateTime, double>)whenTP;
            TimeSpan interval = when.Key - time;
            double   hours    = interval.TotalHours;

            Sum  += hours;
            Sum2 += hours * hours;
            if (time == when.Key)
            {
                return;
            }
            double maximum = ticks.FindMaximum(time, when.Key);

            maximum  = maximum - bid;
            Spread  += maximum;
            Spread2 += (maximum * maximum);
        }
Exemplo n.º 2
0
        private void FirstEventWhenLessOrEqualTest(DateTime start, double threshold)
        {
            KeyValuePair <DateTime, double>?entry = m_ticks.FirstEventWhenLessOrEqual(start, threshold);

            foreach (var element in m_items)
            {
                if (element.Key < start)
                {
                    continue;
                }
                if (element.Value > threshold)
                {
                    continue;
                }
                if (null == entry)
                {
                    Assert.Fail("FirstEventWhenLessOrEqual is failed - null return value");
                }
                KeyValuePair <DateTime, double> item = (KeyValuePair <DateTime, double>)entry;
                if (element.Key != item.Key)
                {
                    Assert.Fail("FirstEventWhenLessOrEqual is failed - invalid time");
                }
                if (element.Value != item.Value)
                {
                    Assert.Fail("FirstEventWhenLessOrEqual is failed - invalid value");
                }
                return;
            }
            if (null != entry)
            {
                Assert.Fail("FirstEventWhenLessOrEqual is failed - not null return value");
            }
        }
Exemplo n.º 3
0
        internal void ProcessSell(int time, double bid, double ask)
        {
            KeyValuePair <int, double>?whenTP = m_asks.FirstEventWhenLessOrEqual(time, bid - TakeProfit);

            m_allCount++;
            if (null == whenTP)
            {
                //AP it will be more fair
                whenTP = m_asks.LastTick;
                //return;
            }
            m_count++;
            KeyValuePair <int, double> when = (KeyValuePair <int, double>)whenTP;
            double interval = (when.Key - time) * this.TimeFactor;

            m_timeSum  += interval;
            m_timeSum2 += interval * interval;

            if (time < when.Key)
            {
                double maximum = m_asks.FindMaximum(time, when.Key);
                maximum     = maximum - bid;
                m_lossSum  += maximum;
                m_lossSum2 += (maximum * maximum);
            }
        }
Exemplo n.º 4
0
        public int CalculateNextInterruption(int step)
        {
            //return step + 1;

            int min = this.Ticks.Count - 1;

            if (step == min)
            {
                return(step + 1);
            }

            bool isEmpty = true;

            if (Orders.BuyLimits.Count > 0)
            {
                isEmpty = false;
                KeyValuePair <int, double>?entry = AsksIndexCollection.FirstEventWhenLessOrEqual(step + 1, Orders.BuyLimits.Keys[0]);
                if (entry != null)
                {
                    min = Math.Min(min, entry.Value.Key);
                }
            }
            if (Orders.BuyStops.Count > 0)
            {
                isEmpty = false;
                KeyValuePair <int, double>?entry = AsksIndexCollection.FirstEventWhenMoreOrEqual(step + 1, Orders.BuyStops.Keys[0]);
                if (entry != null)
                {
                    min = Math.Min(min, entry.Value.Key);
                }
            }
            if (Orders.SellLimits.Count > 0)
            {
                isEmpty = false;
                KeyValuePair <int, double>?entry = BidsIndexCollection.FirstEventWhenMoreOrEqual(step + 1, Orders.SellLimits.Keys[0]);
                if (entry != null)
                {
                    min = Math.Min(min, entry.Value.Key);
                }
            }
            if (Orders.SellStops.Count > 0)
            {
                isEmpty = false;
                KeyValuePair <int, double>?entry = BidsIndexCollection.FirstEventWhenLessOrEqual(step + 1, Orders.SellStops.Keys[0]);
                if (entry != null)
                {
                    min = Math.Min(min, entry.Value.Key);
                }
            }
            if (isEmpty)
            {
                return(step + 1);
            }

            return(min);
        }