mirror of
https://github.com/rithsila/MT5-EA-Sniper-Strategy.git
synced 2026-08-15 03:38:11 +00:00
Market order and pending orders handles. Some pending orders issues to fix
This commit is contained in:
Binary file not shown.
@@ -46,29 +46,24 @@ void OnTick()
|
||||
|
||||
if(InpActivateDCAHedging)
|
||||
{
|
||||
Print("DCA Hedging is activated");
|
||||
ArrayResize(instruments,2);
|
||||
Print("DCA Hedging is activated");
|
||||
ArrayResize(instruments,2);
|
||||
instruments[0] = InpInstrument1;
|
||||
instruments[1] = InpInstrument2;
|
||||
}
|
||||
else
|
||||
{
|
||||
Print("DCA Hedging is not activated");
|
||||
ArrayResize(instruments,1);
|
||||
Print("DCA Hedging is not activated");
|
||||
ArrayResize(instruments,1);
|
||||
instruments[0] = InpInstrument1;
|
||||
}
|
||||
|
||||
Print("Instrument size", ArraySize(instruments));
|
||||
for(int i=0; i<ArraySize(instruments); i++)
|
||||
{
|
||||
Spread = SymbolInfoInteger(instruments[i], SYMBOL_SPREAD);
|
||||
SymbolInfoTick(instruments[i],last_tick);
|
||||
Print("last_tick ask ", last_tick.ask, " instruments ", instruments[i]);
|
||||
gSymbol = instruments[i];
|
||||
point = SymbolInfoDouble(gSymbol, SYMBOL_POINT);
|
||||
|
||||
Print("Point ", InpDefaultTakeProfit);
|
||||
|
||||
|
||||
CheckOperationHours();
|
||||
CheckPreChecks();
|
||||
|
||||
@@ -25,10 +25,10 @@ void DcaManager()
|
||||
case BUY_PENDING_ORDERS :
|
||||
signal = PENDING_ORDERS;
|
||||
break;
|
||||
case NO_UPPER_BUY_ORDERS :
|
||||
case NO_UPPER_BUY_ORDER :
|
||||
signal = BUY_STOP_SIGNAL;
|
||||
break;
|
||||
case NO_LOWER_BUY_ORDERS :
|
||||
case NO_LOWER_BUY_ORDER :
|
||||
signal = BUY_LIMIT_SIGNAL;
|
||||
break;
|
||||
case BUY_POSITION_EXISTS :
|
||||
@@ -50,72 +50,76 @@ ENUM_DCA_STATUS DcaWatcher()
|
||||
{
|
||||
if(gTotalBuyPositions > 0)
|
||||
{
|
||||
//Get last opened position id
|
||||
int lastTicketId = gTotalBuyPositions;
|
||||
|
||||
bool hasOrderAbove = false, hasOrderBelow = false;
|
||||
|
||||
if(PositionGetTicket(lastTicketId) == 0)
|
||||
{
|
||||
int Error=GetLastError();
|
||||
Print("ERROR - Unable to select the order - ",Error," - ",Error);
|
||||
Print("ERROR - Unable to select the position - ",Error," - ",Error);
|
||||
return NO_DEALS;
|
||||
}
|
||||
|
||||
if(PositionGetSymbol(lastTicketId)==gSymbol && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
|
||||
if(PositionSelect(gSymbol))
|
||||
{
|
||||
//Compute above and below orders price
|
||||
|
||||
Print("Last ticket id ", lastTicketId, " last ticket price ", PositionGetDouble(POSITION_PRICE_OPEN));
|
||||
gUpOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN) + (InpBuyCallBack * point);
|
||||
gDownOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN) - (InpBuyCallBack * point);
|
||||
|
||||
//Check orders around the last opened position
|
||||
for(int i=0; i<gTotalOrders; i++)
|
||||
if(PositionGetSymbol(lastTicketId)==gSymbol && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
|
||||
{
|
||||
//If there is a problem reading the order print the error, exit the function and return false
|
||||
if(OrderGetTicket(i) == 0)
|
||||
//Compute above and below orders price
|
||||
Print("Last ticket id ", lastTicketId, " last ticket price ", PositionGetDouble(POSITION_PRICE_OPEN));
|
||||
gUpOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN) + (InpBuyCallBack * point);
|
||||
gDownOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN) - (InpBuyCallBack * point);
|
||||
|
||||
Print("gUpOpenPrice ", gUpOpenPrice, " gDownOpenPrice ", gDownOpenPrice);
|
||||
|
||||
//Check orders around the last opened position
|
||||
for(int i=0; i<gTotalOrders; i++)
|
||||
{
|
||||
int Error=GetLastError();
|
||||
Print("ERROR - Unable to select the order - ",Error," - ",Error);
|
||||
//If there is a problem reading the order print the error, exit the function and return false
|
||||
if(OrderGetTicket(i) == 0)
|
||||
{
|
||||
int Error=GetLastError();
|
||||
Print("ERROR - Unable to select the order - ",Error," - ",Error);
|
||||
return NO_DEALS;
|
||||
}
|
||||
|
||||
//Check existence of above and below orders to last opened position
|
||||
if(OrderGetString(ORDER_SYMBOL)==gSymbol && OrderGetInteger(ORDER_MAGIC)==InpMagicNumber)
|
||||
{
|
||||
if(gUpOpenPrice == OrderGetDouble(ORDER_PRICE_OPEN))
|
||||
{
|
||||
hasOrderAbove = true;
|
||||
}
|
||||
if(gDownOpenPrice == OrderGetDouble(ORDER_PRICE_OPEN))
|
||||
{
|
||||
hasOrderBelow = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Do nothing if both orders exist
|
||||
if(hasOrderAbove && hasOrderBelow)
|
||||
{
|
||||
Print("hasOrderAbove && hasOrderBelow");
|
||||
return NO_DEALS;
|
||||
}
|
||||
|
||||
//Check existence of above and below orders to last opened position
|
||||
if(OrderGetString(ORDER_SYMBOL)==gSymbol && OrderGetInteger(ORDER_MAGIC)==InpMagicNumber)
|
||||
//Report non existence of one or both orders
|
||||
if(!hasOrderAbove && hasOrderBelow)
|
||||
{
|
||||
if(gUpOpenPrice == OrderGetDouble(ORDER_PRICE_OPEN))
|
||||
{
|
||||
hasOrderAbove = true;
|
||||
}
|
||||
if(gDownOpenPrice == OrderGetDouble(ORDER_PRICE_OPEN))
|
||||
{
|
||||
hasOrderBelow = true;
|
||||
}
|
||||
Print("hasOrderAbove && hasOrderBelow");
|
||||
return NO_UPPER_BUY_ORDER;
|
||||
}
|
||||
if(hasOrderAbove && !hasOrderBelow)
|
||||
{
|
||||
Print("hasOrderAbove && !hasOrderBelow");
|
||||
return NO_LOWER_BUY_ORDER;
|
||||
}
|
||||
if(!hasOrderAbove && !hasOrderBelow)
|
||||
{
|
||||
Print("!hasOrderAbove && !hasOrderBelow");
|
||||
return BUY_PENDING_ORDERS;
|
||||
}
|
||||
}
|
||||
|
||||
//Do nothing if both orders exist
|
||||
if(hasOrderAbove && hasOrderBelow)
|
||||
{
|
||||
return NO_DEALS;
|
||||
return BUY_POSITION_EXISTS;
|
||||
}
|
||||
|
||||
//Report non existence of one or both orders
|
||||
if(!hasOrderAbove && hasOrderBelow)
|
||||
{
|
||||
return NO_UPPER_BUY_ORDERS;
|
||||
}
|
||||
if(hasOrderAbove && !hasOrderBelow)
|
||||
{
|
||||
return NO_LOWER_BUY_ORDERS;
|
||||
}
|
||||
if(!hasOrderAbove && !hasOrderBelow)
|
||||
{
|
||||
return BUY_PENDING_ORDERS;
|
||||
}
|
||||
|
||||
return BUY_POSITION_EXISTS;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
Binary file not shown.
@@ -66,8 +66,8 @@ enum ENUM_DCA_STATUS
|
||||
{
|
||||
NO_DEALS=0,
|
||||
NO_BUY_POSITIONS=1,
|
||||
NO_UPPER_BUY_ORDERS=2,
|
||||
NO_LOWER_BUY_ORDERS=3,
|
||||
NO_UPPER_BUY_ORDER=2,
|
||||
NO_LOWER_BUY_ORDER=3,
|
||||
BUY_POSITION_EXISTS=4,
|
||||
UPPER_BUY_ORDERS=5,
|
||||
LOWER_BUY_ORDERS=6,
|
||||
@@ -135,7 +135,7 @@ double gUpOpenPrice, gDownOpenPrice;
|
||||
|
||||
double gLotSize=InpDefaultLotSize, point;
|
||||
|
||||
int gTickValue=0;
|
||||
int gTickValue=0, lastTicketId;
|
||||
long Spread;// = SymbolInfoInteger(gSymbol,SYMBOL_SPREAD) / 100; //Check the impact. It's originally a double
|
||||
|
||||
int gOrderOpRetry = 1;
|
||||
|
||||
@@ -28,18 +28,22 @@ void ScanPositions()
|
||||
Print("ERROR - Unable to select the order - ",Error," - ",Error);
|
||||
return;
|
||||
}
|
||||
//If the order is not for the instrument on chart we can ignore it
|
||||
if(PositionGetSymbol(i)!=gSymbol)
|
||||
continue;
|
||||
//If the order has Magic Number different from the Magic Number of the EA then we can ignore it
|
||||
if(PositionGetInteger(POSITION_MAGIC)!=InpMagicNumber)
|
||||
continue;
|
||||
//If it is a buy order then increment the total count of buy orders
|
||||
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
|
||||
gTotalBuyPositions++;
|
||||
Print(gSymbol, " Position ticket ",PositionGetTicket(i), " POSITION_PRICE_OPEN ", PositionGetDouble(POSITION_PRICE_OPEN));
|
||||
if(PositionSelect(gSymbol))
|
||||
{
|
||||
//If the order is not for the instrument on chart we can ignore it
|
||||
if(PositionGetSymbol(i)!=gSymbol)
|
||||
continue;
|
||||
//If the order has Magic Number different from the Magic Number of the EA then we can ignore it
|
||||
if(PositionGetInteger(POSITION_MAGIC)!=InpMagicNumber)
|
||||
continue;
|
||||
//If it is a buy order then increment the total count of buy orders
|
||||
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
|
||||
gTotalBuyPositions++;
|
||||
|
||||
lastTicketId = i;
|
||||
}
|
||||
Print("Total ", gSymbol, " buy position ", gTotalBuyPositions, " lastTicketId ", PositionGetTicket(i), " lastTicket price ", PositionGetDouble(POSITION_PRICE_OPEN));
|
||||
}
|
||||
Print("Total ", gSymbol, " buy position ", gTotalBuyPositions);
|
||||
|
||||
|
||||
for(int i=0; i<gTotalOrders; i++)
|
||||
|
||||
Reference in New Issue
Block a user