static bool TickScanning(int bar, BacktestEval eval,
ref double current, ref int reachedTick,
bool isTopReachable, bool isBottomReachable,
bool isHigherPrice, bool isLowerPrice,
double priceHigher, double priceLower,
Order orderHigher, Order orderLower,
bool isClosingAmbiguity)
{
double high = High[bar];
double low = Low[bar];
double close = Close[bar];
bool isScanningResult = false;
if (eval == BacktestEval.None)
{ // There isn't any orders
if (!session[bar].IsTopReached && !session[bar].IsBottomReached)
{ // Neither the top nor the bottom was reached
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > high)
{ // Top found
current = high;
session[bar].SetWayPoint(high, WayPointType.High);
session[bar].IsTopReached = true;
isScanningResult = true;
break;
}
else if (Data.TickData[bar][tick] - micron < low)
{ // Bottom found
current = low;
session[bar].SetWayPoint(low, WayPointType.Low);
session[bar].IsBottomReached = true;
isScanningResult = true;
break;
}
}
}
else if (!session[bar].IsTopReached)
{ // Whether hit the Top
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > high)
{ // Top found
current = high;
session[bar].SetWayPoint(high, WayPointType.High);
session[bar].IsTopReached = true;
isScanningResult = true;
break;
}
}
}
else if (!session[bar].IsBottomReached)
{ // Whether hit the Bottom
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] - micron < low)
{ // Bottom found
current = low;
session[bar].SetWayPoint(low, WayPointType.Low);
session[bar].IsBottomReached = true;
isScanningResult = true;
break;
}
}
}
}
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
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > thePrice)
{ // The order is reached
current = thePrice;
ExecOrd(bar, theOrder, thePrice, BacktestEval.Correct);
isScanningResult = true;
break;
}
else if (Data.TickData[bar][tick] - micron < low)
{ // Bottom is reached
current = low;
session[bar].SetWayPoint(low, WayPointType.Low);
session[bar].IsBottomReached = true;
isScanningResult = true;
break;
}
}
}
else if (!session[bar].IsTopReached && isTopReachable)
{ // The order or the Top
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > high)
{ // The Top is reached
current = high;
session[bar].SetWayPoint(high, WayPointType.High);
session[bar].IsTopReached = true;
isScanningResult = true;
break;
}
else if (Data.TickData[bar][tick] - micron < thePrice)
{ // The order is reached
current = thePrice;
ExecOrd(bar, theOrder, thePrice, BacktestEval.Correct);
isScanningResult = true;
break;
}
}
}
else
{ // Execute the order
double priceOld = Data.TickData[bar][reachedTick];
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (priceOld - micron < thePrice && Data.TickData[bar][tick] + micron > thePrice ||
priceOld + micron > thePrice && Data.TickData[bar][tick] - micron < thePrice)
{ // Order reached
current = thePrice;
ExecOrd(bar, theOrder, thePrice, BacktestEval.Correct);
isScanningResult = true;
break;
}
}
}
}
else if (eval == BacktestEval.Ambiguous)
{ // Ambiguous - two orders or order and bar closing
if (!isClosingAmbiguity)
{ // Execute the the first reached order
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > priceHigher)
{ // Upper order is reached
current = priceHigher;
ExecOrd(bar, orderHigher, priceHigher, BacktestEval.Correct);
isScanningResult = true;
break;
}
else if (Data.TickData[bar][tick] - micron < priceLower)
{ // Lower order is reached
current = priceLower;
ExecOrd(bar, orderLower, priceLower, BacktestEval.Correct);
isScanningResult = true;
break;
}
}
}
else
{ // Execute or exit the bar
Order theOrder = null;
double thePrice = 0.0;
if (isHigherPrice)
{
theOrder = orderHigher;
thePrice = priceHigher;
}
else if (isLowerPrice)
{
theOrder = orderLower;
thePrice = priceLower;
}
bool executeOrder = false;
if (isHigherPrice)
{
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] + micron > thePrice)
{ // The order is reached
executeOrder = true;
break;
}
}
}
else if (isLowerPrice)
{ // The priceLower or Exit the bar
int tickCount = Data.TickData[bar].Length;
for (int tick = reachedTick; tick < tickCount; tick++)
{
reachedTick = tick;
if (Data.TickData[bar][tick] - micron < thePrice)
{ // The order is reached
executeOrder = true;
break;
}
}
}
if (executeOrder)
{ // Execute the order
current = thePrice;
ExecOrd(bar, theOrder, thePrice, BacktestEval.Correct);
}
else
{ // Exit the bar
current = close;
theOrder.OrdStatus = OrderStatus.Cancelled;
session[bar].BacktestEval = BacktestEval.Correct;
}
isScanningResult = true;
}
}
return isScanningResult;
}