static void RandomMethod(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 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
int upRange = (int)Math.Round((high - current) / Data.InstrProperties.Point);
int downRange = (int)Math.Round((current - low) / Data.InstrProperties.Point);
upRange = upRange < 0 ? 0 : upRange;
downRange = downRange < 0 ? 0 : downRange;
if (downRange + downRange == 0)
{
upRange = 1;
downRange = 1;
}
int iRandom = random.Next(upRange + downRange);
bool bHitHigh = false;
if (upRange > downRange)
bHitHigh = iRandom > upRange;
else
bHitHigh = iRandom < downRange;
if (bHitHigh)
{ // 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
int iUpRange = (int)Math.Round((thePrice - current) / Data.InstrProperties.Point);
int iDnRange = (int)Math.Round((current - low) / Data.InstrProperties.Point);
iUpRange = iUpRange < 0 ? 0 : iUpRange;
iDnRange = iDnRange < 0 ? 0 : iDnRange;
if (iDnRange + iDnRange == 0)
{
iUpRange = 1;
iDnRange = 1;
}
int iRandom = random.Next(iUpRange + iDnRange);
bool executeUpper = false;
if (iUpRange > iDnRange)
executeUpper = iRandom > iUpRange;
else
executeUpper = iRandom < iDnRange;
if (executeUpper)
{ // 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 Top
int upRange = (int)Math.Round((high - current) / Data.InstrProperties.Point);
int downRange = (int)Math.Round((current - thePrice) / Data.InstrProperties.Point);
upRange = upRange < 0 ? 0 : upRange;
downRange = downRange < 0 ? 0 : downRange;
if (downRange + downRange == 0)
{
upRange = 1;
downRange = 1;
}
int iRandom = random.Next(upRange + downRange);
bool executeUpper = false;
if (upRange > downRange)
executeUpper = iRandom > upRange;
else
executeUpper = iRandom < downRange;
if (executeUpper)
{ // 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 randomly chosen order
int upRange = (int)Math.Round((priceHigher - current) / Data.InstrProperties.Point);
int downRange = (int)Math.Round((current - priceLower) / Data.InstrProperties.Point);
upRange = upRange < 0 ? 0 : upRange;
downRange = downRange < 0 ? 0 : downRange;
if (downRange + downRange == 0)
{
upRange = 1;
downRange = 1;
}
int iRandom = random.Next(upRange + downRange);
bool executeUpper = false;
if (upRange > downRange)
executeUpper = iRandom > upRange;
else
executeUpper = iRandom < downRange;
Order theOrder;
double thePrice;
if (executeUpper)
{
theOrder = orderHigher;
thePrice = priceHigher;
}
else
{
theOrder = orderLower;
thePrice = priceLower;
}
current = thePrice;
ExecOrd(bar, theOrder, thePrice, eval);
}
else
{ // Execute or exit the bar
if (isHigherPrice)
{
int upRange = (int)Math.Round((priceHigher - current) / Data.InstrProperties.Point);
int downRange = (int)Math.Round((close - current) / Data.InstrProperties.Point);
upRange = upRange < 0 ? 0 : upRange;
downRange = downRange < 0 ? 0 : downRange;
if (downRange + downRange == 0)
{
upRange = 1;
downRange = 0;
}
int iRandom = random.Next(upRange + downRange);
if (iRandom > upRange)
{ // Execute the order
current = priceHigher;
ExecOrd(bar, orderHigher, priceHigher, eval);
}
else
{ // Exit the bar
current = close;
orderHigher.OrdStatus = OrderStatus.Cancelled;
session[bar].BacktestEval = BacktestEval.Ambiguous;
}
}
else if (isLowerPrice)
{ // The priceLower or Exit the bar
int upRange = (int)Math.Round((current - close) / Data.InstrProperties.Point);
int downRange = (int)Math.Round((current - priceLower) / Data.InstrProperties.Point);
upRange = upRange < 0 ? 0 : upRange;
downRange = downRange < 0 ? 0 : downRange;
if (downRange + downRange == 0)
{
upRange = 0;
downRange = 1;
}
int iRandom = random.Next(upRange + downRange);
if (iRandom > downRange)
{ // Execute the order
current = priceLower;
ExecOrd(bar, orderLower, priceLower, eval);
}
else
{ // Exit the bar
current = close;
orderLower.OrdStatus = OrderStatus.Cancelled;
session[bar].BacktestEval = BacktestEval.Ambiguous;
}
}
}
}
return;
}