static bool IntrabarScanning(int bar, BacktestEval eval, ref double current,
ref int reachedIntrabar, ref int tradedIntrabar,
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 is no more orders
if (!session[bar].IsTopReached && !session[bar].IsBottomReached)
{ // Neither the top nor the bottom was reached
bool goUpward = false;
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > high)
{ // Top found
goUpward = true;
isScanningResult = true;
}
if (Data.IntraBarData[bar][intraBar].Low - micron < low)
{ // Bottom found
if (isScanningResult)
{ // Top and Bottom into the same intrabar
isScanningResult = false;
break;
}
goUpward = false;
isScanningResult = true;
}
if (isScanningResult)
break;
}
if (isScanningResult)
{
if (goUpward)
{ // 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)
{ // Whether hit the Top
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + 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
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].Low - 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
bool goUpward = false;
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > thePrice)
{ // The order is reached
goUpward = true;
isScanningResult = true;
}
if (Data.IntraBarData[bar][intraBar].Low - micron < low)
{ // Bottom is reached
if (isScanningResult)
{ // The Order and Bottom into the same intrabar
isScanningResult = false;
break;
}
goUpward = false;
isScanningResult = true;
}
if (isScanningResult)
break;
}
if (isScanningResult)
{
if (goUpward)
{ // Execute
if (tradedIntrabar == reachedIntrabar)
{
isScanningResult = false;
return isScanningResult;
}
current = thePrice;
ExecOrd(bar, theOrder, thePrice, eval);
tradedIntrabar = reachedIntrabar;
}
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 Top
bool goUpward = false;
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > high)
{ // The Top is reached
goUpward = true;
isScanningResult = true;
}
if (Data.IntraBarData[bar][intraBar].Low - micron < thePrice)
{ // The order is reached
if (isScanningResult)
{ // The Top and the order are into the same intrabar
isScanningResult = false;
break;
}
// The order is reachable downwards
goUpward = false;
isScanningResult = true;
}
if (isScanningResult)
break;
}
if (isScanningResult)
{
if (goUpward)
{ // Hit the Top
current = high;
session[bar].SetWayPoint(high, WayPointType.High);
session[bar].IsTopReached = true;
}
else
{ // Execute
if (tradedIntrabar == reachedIntrabar)
{
isScanningResult = false;
return isScanningResult;
}
current = thePrice;
ExecOrd(bar, theOrder, thePrice, eval);
tradedIntrabar = reachedIntrabar;
}
}
}
else
{ // Execute the order
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > thePrice &&
Data.IntraBarData[bar][intraBar].Low - micron < thePrice)
{ // Order reached
if (tradedIntrabar == reachedIntrabar)
{
isScanningResult = false;
return isScanningResult;
}
current = thePrice;
ExecOrd(bar, theOrder, thePrice, eval);
isScanningResult = true;
tradedIntrabar = reachedIntrabar;
break;
}
}
}
}
else if (eval == BacktestEval.Ambiguous)
{ // Ambiguous - two orders or order and bar closing
if (!isClosingAmbiguity)
{ // Execute the the first reached order
bool executeUpper = false;
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > priceHigher)
{ // Upper order is reached
executeUpper = true;
isScanningResult = true;
}
if (Data.IntraBarData[bar][intraBar].Low - micron < priceLower)
{ // Lower order is reached
if (isScanningResult)
{ // Top and Bottom into the same intrabar
isScanningResult = false;
break;
}
executeUpper = false;
isScanningResult = true;
}
if (isScanningResult)
break;
}
if (isScanningResult)
{
Order theOrder;
double thePrice;
if (executeUpper)
{
theOrder = orderHigher;
thePrice = priceHigher;
}
else
{
theOrder = orderLower;
thePrice = priceLower;
}
if (tradedIntrabar == reachedIntrabar)
{
isScanningResult = false;
return isScanningResult;
}
eval = BacktestEval.Correct;
current = thePrice;
ExecOrd(bar, theOrder, thePrice, eval);
tradedIntrabar = reachedIntrabar;
}
}
else
{ // Execute or exit the bar
Order theOrder = null;
double thePrice = 0;
if (isHigherPrice)
{
theOrder = orderHigher;
thePrice = priceHigher;
}
else if (isLowerPrice)
{
theOrder = orderLower;
thePrice = priceLower;
}
bool executeOrder = false;
if (isHigherPrice)
{
for (int intraBar = reachedIntrabar; intraBar < Data.IntraBarBars[bar]; intraBar++)
{
reachedIntrabar = intraBar;
if (Data.IntraBarData[bar][intraBar].High + micron > thePrice)
{ // The order is reached
executeOrder = true;
break;
}
}
}
else if (isLowerPrice)
{ // The priceLower or Exit the bar
for (int b = reachedIntrabar; b < Data.IntraBarBars[bar]; b++)
{
reachedIntrabar = b;
if (Data.IntraBarData[bar][b].Low - micron < thePrice)
{ // The order is reached
executeOrder = true;
break;
}
}
}
if (executeOrder)
{ // Execute the order
if (tradedIntrabar == reachedIntrabar)
{
isScanningResult = false;
return isScanningResult;
}
current = thePrice;
eval = BacktestEval.Correct;
ExecOrd(bar, theOrder, thePrice, eval);
tradedIntrabar = reachedIntrabar;
}
else
{ // Exit the bar
current = close;
theOrder.OrdStatus = OrderStatus.Cancelled;
session[bar].BacktestEval = BacktestEval.Correct;
}
isScanningResult = true;
}
}
return isScanningResult;
}