static void BarInterpolation(int bar)
{
BacktestEval eval = BacktestEval.Error;
double open = Open[bar];
double high = High[bar];
double low = Low[bar];
double close = Close[bar];
double current = open;
int reachedIntrabar = 0;
int tradedIntrabar = -1;
int reachedTick = 0;
do
{
Order orderHigher = null;
Order orderLower = null;
double priceHigher = high;
double priceLower = low;
bool isHigherPrice = false;
bool isLowerPrice = false;
bool isTopReachable = true;
bool isBottomReachable = true;
bool isClosingAmbiguity = false;
bool isScanningResult = false;
// Setting the parameters
for (int ord = 0; ord < session[bar].Orders; ord++)
{
if (!CheckOrd(bar, ord)) continue;
Order order = session[bar].Order[ord];
double price0 = order.OrdPrice;
double price1 = order.OrdPrice2;
if (high + micron > price0 && price0 > low - micron)
{
if (isTopReachable)
isTopReachable = current > price0 + micron;
if (isBottomReachable)
isBottomReachable = current < price0 - micron;
if (price0 > current - micron && price0 < priceHigher + micron)
{ // New nearer Upper price
isHigherPrice = true;
priceHigher = price0;
orderHigher = order;
isTopReachable = false;
}
else if (price0 < current && price0 > priceLower - micron)
{ // New nearer Lower price
isLowerPrice = true;
priceLower = price0;
orderLower = order;
isBottomReachable = false;
}
}
if (high + micron > price1 && price1 > low - micron)
{
if (isTopReachable)
isTopReachable = current > price1 + micron;
if (isBottomReachable)
isBottomReachable = current < price1 - micron;
if (price1 > current - micron && price1 < priceHigher + micron)
{ // New nearer Upper price
isHigherPrice = true;
priceHigher = price1;
orderHigher = order;
}
else if (price1 < current && price1 > priceLower - micron)
{ // New nearer Lower price
isLowerPrice = true;
priceLower = price1;
orderLower = order;
}
}
}
// Evaluate the bar
if (!isLowerPrice && !isHigherPrice)
{
eval = BacktestEval.None;
}
else if (isLowerPrice && isHigherPrice)
{
eval = BacktestEval.Ambiguous;
}
else
{
// Check for a Closing Ambiguity
if (session[bar].IsBottomReached && session[bar].IsTopReached &&
((current > close - micron && close > priceLower) ||
(current < close + micron && close < priceHigher)))
{
isClosingAmbiguity = true;
eval = BacktestEval.Ambiguous;
}
else
{
eval = BacktestEval.Correct;
}
}
//if (session[bar].IsTopReached && isHigherPrice && current > close - micron)
//{
// Console.WriteLine("Ambiguous - " + bar);
//}
//else if (session[bar].IsBottomReached && isLowerPrice && current < close + micron)
//{
// Console.WriteLine("Ambiguous - " + bar);
//}
if (isScanning && Configs.UseTickData && Data.IsTickData && Data.TickData[bar] != null)
{
isScanningResult = TickScanning(bar, eval,
ref current, ref reachedTick,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
}
if (isScanning && !isScanningResult && Data.IntraBarsPeriods[bar] != Data.Period)
{
isScanningResult = IntrabarScanning(bar, eval, ref current,
ref reachedIntrabar, ref tradedIntrabar,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
}
// Calls a method
if (!isScanningResult)
{
switch (interpolationMethod)
{
case InterpolationMethod.Optimistic:
case InterpolationMethod.Pessimistic:
OptimisticPessimisticMethod(bar, eval, ref current,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
break;
case InterpolationMethod.Shortest:
ShortestMethod(bar, eval, ref current,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
break;
case InterpolationMethod.Nearest:
NearestMethod(bar, eval, ref current,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
break;
case InterpolationMethod.Random:
RandomMethod(bar, eval, ref current,
isTopReachable, isBottomReachable,
isHigherPrice, isLowerPrice,
priceHigher, priceLower,
orderHigher, orderLower,
isClosingAmbiguity);
break;
default:
break;
}
}
} while (!(eval == BacktestEval.None && session[bar].IsTopReached && session[bar].IsBottomReached));
return;
}