Forex_Strategy_Builder.Backtester.NearestMethod C# (CSharp) Method

NearestMethod() static private method

Nearest order first Method
static private NearestMethod ( int bar, BacktestEval eval, double &current, bool isTopReachable, bool isBottomReachable, bool isHigherPrice, bool isLowerPrice, double priceHigher, double priceLower, Order orderHigher, Order orderLower, bool isClosingAmbiguity ) : void
bar int
eval BacktestEval
current double
isTopReachable bool
isBottomReachable bool
isHigherPrice bool
isLowerPrice bool
priceHigher double
priceLower double
orderHigher Order
orderLower Order
isClosingAmbiguity bool
return void
        static void NearestMethod(int bar, BacktestEval eval, ref double current,
            bool isTopReachable, bool isBottomReachable,
            bool isHigherPrice, bool isLowerPrice,
            double priceHigher, double priceLower,
            Order orderHigher, Order orderLower,
            bool isClosingAmbiguity)
        {
            double open  = Open[bar];
            double high  = High[bar];
            double low   = Low[bar];
            double close = Close[bar];

            if (eval == BacktestEval.None)
            {   // There is no more orders
                if (!session[bar].IsTopReached && !session[bar].IsBottomReached)
                {   // Neither the top nor the bottom was reached
                    if (close < open)
                    {   // Hit the Top
                        current = high;
                        session[bar].SetWayPoint(high, WayPointType.High);
                        session[bar].IsTopReached = true;
                    }
                    else
                    {   // Hit the Bottom
                        current = low;
                        session[bar].SetWayPoint(low, WayPointType.Low);
                        session[bar].IsBottomReached = true;
                    }
                }
                else if (!session[bar].IsTopReached)
                {   // Hit the Top
                    current = high;
                    session[bar].SetWayPoint(high, WayPointType.High);
                    session[bar].IsTopReached = true;
                }
                else if (!session[bar].IsBottomReached)
                {   // Hit the Bottom
                    current = low;
                    session[bar].SetWayPoint(low, WayPointType.Low);
                    session[bar].IsBottomReached = true;
                }
            }
            if (eval == BacktestEval.Correct)
            {   // Hit the order or the top/bottom
                Order theOrder = null;
                double thePrice = 0;
                if (isHigherPrice)
                {
                    theOrder = orderHigher;
                    thePrice = priceHigher;
                }
                else if (isLowerPrice)
                {
                    theOrder = orderLower;
                    thePrice = priceLower;
                }

                if (!session[bar].IsBottomReached && isBottomReachable)
                {   // The order or the bottom
                    double upRange   = thePrice - current;
                    double downRange = current - low;

                    if (upRange < downRange)
                    {   // Execute
                        current = thePrice;
                        ExecOrd(bar, theOrder, thePrice, eval);
                    }
                    else
                    {   // Hit the Bottom
                        current = low;
                        session[bar].SetWayPoint(low, WayPointType.Low);
                        session[bar].IsBottomReached = true;
                    }
                }
                else if (!session[bar].IsTopReached && isTopReachable)
                {   // The order or the bottom
                    double upRange   = high - current;
                    double downRange = current - thePrice;

                    if (upRange < downRange)
                    {   // Hit the Top
                        current = high;
                        session[bar].SetWayPoint(high, WayPointType.High);
                        session[bar].IsTopReached = true;
                    }
                    else
                    {   // Execute
                        current = thePrice;
                        ExecOrd(bar, theOrder, thePrice, eval);
                    }
                }
                else
                {   // Execute the order
                    current = thePrice;
                    ExecOrd(bar, theOrder, thePrice, eval);
                }
            }
            else if (eval == BacktestEval.Ambiguous)
            {   // Ambiguous - two orders or order and bar closing
                if (!isClosingAmbiguity)
                {   // Execute the nearest order
                    double upRange   = priceHigher - current;
                    double downRange = current - priceLower;

                    Order  theOrder;
                    double thePrice;
                    if (upRange < downRange)
                    {
                        theOrder = orderHigher;
                        thePrice = priceHigher;
                    }
                    else
                    {
                        theOrder = orderLower;
                        thePrice = priceLower;
                    }
                    current = thePrice;
                    ExecOrd(bar, theOrder, thePrice, eval);
                }
                else
                {   // Exit the bar
                    current = close;
                    session[bar].BacktestEval = BacktestEval.Ambiguous;
                    if (isHigherPrice)
                        orderHigher.OrdStatus = OrderStatus.Cancelled;
                    else if (isLowerPrice)
                        orderLower.OrdStatus = OrderStatus.Cancelled;
                }
            }

            return;
        }