Managing weird order placing

This commit is contained in:
Nkondog Anselme
2022-03-17 01:26:09 +01:00
parent 4683eb62fa
commit 46507a73bd
8 changed files with 151 additions and 41 deletions
Binary file not shown.
Binary file not shown.
+15 -7
View File
@@ -11,8 +11,9 @@
#include <Nkanven\GeminiHedge\TradingHour.mqh> //Trading hours checks
#include <Nkanven\GeminiHedge\Prechecks.mqh> //Trading conditions checks
#include <Nkanven\GeminiHedge\ScanPositions.mqh> //Trading conditions checks
#include <Nkanven\GeminiHedge\DCAManager.mqh> //DCA manager
#include <Nkanven\GeminiHedge\LotSizeCal.mqh> //Lot size calculator
#include <Nkanven\GeminiHedge\EntriesManager.mqh> //Lot size calculator
#include <Nkanven\GeminiHedge\EntriesManager.mqh> //Trade entries manager
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
@@ -45,18 +46,28 @@ void OnTick()
if(InpActivateDCAHedging)
{
string instruments[] = {InpInstrument1, InpInstrument2};
Print("DCA Hedging is activated");
ArrayResize(instruments,2);
instruments[0] = InpInstrument1;
instruments[1] = InpInstrument2;
}
else
{
string instruments[] = {InpInstrument1};
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();
@@ -66,10 +77,7 @@ void OnTick()
if(!gIsPreChecksOk)
return;
//Check if positions not exist
//Check pending orders
DcaManager();
Print("Good for trading...");
ExecuteEntry();
+105 -5
View File
@@ -12,6 +12,33 @@
void DcaManager()
{
//Compute pending orders levels
ENUM_DCA_STATUS dcaStatus = DcaWatcher();
switch(dcaStatus)
{
case NO_DEALS :
signal = NO_SIGNAL;
break;
case NO_BUY_POSITIONS :
signal = BUY_SIGNAL;
break;
case BUY_PENDING_ORDERS :
signal = PENDING_ORDERS;
break;
case NO_UPPER_BUY_ORDERS :
signal = BUY_STOP_SIGNAL;
break;
case NO_LOWER_BUY_ORDERS :
signal = BUY_LIMIT_SIGNAL;
break;
case BUY_POSITION_EXISTS :
signal = NO_SIGNAL;
break;
default:
signal = NO_SIGNAL;
break;
}
}
//+------------------------------------------------------------------+
@@ -19,11 +46,84 @@ void DcaManager()
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DcaWatcher()
ENUM_DCA_STATUS DcaWatcher()
{
if(gTotalBuyPositions > 0)
{
//return position exists
}
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);
return NO_DEALS;
}
if(PositionGetSymbol(lastTicketId)==gSymbol && PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
{
//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 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)
{
return NO_DEALS;
}
//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
{
return NO_BUY_POSITIONS;
}
return NO_DEALS;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
Binary file not shown.
@@ -42,6 +42,7 @@ void LotSizeCalculate(double SL=0)
}
}
//Normalize the Lot Size to satisfy the allowed lot increment and minimum and maximum position size
Print("gLotSize " , gLotSize, " SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP)) ", SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP), " gSymbol ", gSymbol, " SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP)", SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP));
gLotSize=MathFloor(gLotSize/SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP);
Print("LotSize ", gLotSize);
+24 -17
View File
@@ -55,19 +55,25 @@ enum ENUM_MODE_TRADING_TIME
enum ENUM_MODE_TRADE_SIGNAL
{
BUY_SIGNAL=0, //Buy trade
SELL_SIGNAL=1, //Sell trade
NO_SIGNAL=2, //No trade
BUY_STOP_SIGNAL=1, //Sell trade
BUY_LIMIT_SIGNAL=2,
NO_SIGNAL=3, //No trade
PENDING_ORDERS=4
};
//Enumerative for the Take Profit Mode
//Enumerative for the DCA mode
enum ENUM_DCA_STATUS
{
NO_DEALS=0,
NO_BUY_POSITIONS=1, //FIXED TAKE PROFIT
NO_UPPER_BUY_ORDERS=2, //AUTOMATIC TAKE PROFIT
NO_BUY_POSITIONS=1,
NO_UPPER_BUY_ORDERS=2,
NO_LOWER_BUY_ORDERS=3,
BUY_POSITION_EXISTS=4,
UPPER_BUY_ORDERS=5,
LOWER_BUY_ORDERS=6,
BUY_PENDING_ORDERS=7
};
v
//
// Input Section
//
@@ -87,7 +93,7 @@ input int InpDefaultStopLoss=200; //Defaul
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input string Comment_02="----------------------"; //Take profit settings
input int InpDefaultTakeProfit=60; //Default Take Profit In Points (0=No Take Profit)
input int InpDefaultTakeProfit=100; //Default Take Profit In Points (0=No Take Profit)
input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points
input int InpMaxTakeProfit=5000; //Maximum Allowed Take Profit In Points
input double InpTakeProfitPercent=1.0; //Take Profit percent on risk base
@@ -101,15 +107,14 @@ input int InpNightTradingHourStart=1; //Night
input int InpNightTradingHourEnd=5; //Night Trading End Hour (Broker Server Hour)
input string Comment_04="----------------------"; //DCA settings
input bool InpActivateDCAHedging=false; //Active DCA Hedging
input string InpInstrument1="EURUSD.i"; //Instrument 1
input string InpInstrument2="USDCHF.i"; //Instrument 2
input bool InpActivateDCAHedging=true; //Active DCA Hedging
input string InpInstrument1="EURUSD"; //Instrument 1
input string InpInstrument2="USDCHF"; //Instrument 2
input int InpBuyCallBack=100; //Buy call back pips
input int InpMaxCallBack=10; //Call back limit
input int InpWholePositionTP=0; //Whole position TP percent. 0 to disable
input string Comment_05="----------------------"; //Stop loss settings
// Fast moving average
input int InpPeriods = 21; // Fast periods
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Fast method
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Fast price
input string InpComment = __FILE__; //Default trade comment
input int InpMagicNumber = 198901; //Magic Number
input ENUM_TIMEFRAMES InpTimeFrame = PERIOD_CURRENT;
@@ -126,15 +131,17 @@ bool gIsPreChecksOk=false; //Indica
bool gIsSpreadOK=false; //Indicates if the spread is low enough to trade
bool IsSpreadOK=false;
bool gEmergencyClose=false; //Urgently close losing trade
double gUpOpenPrice, gDownOpenPrice;
double gLotSize=InpDefaultLotSize;
double gLotSize=InpDefaultLotSize, point;
int gTickValue=0;
long Spread;// = SymbolInfoInteger(gSymbol,SYMBOL_SPREAD) / 100; //Check the impact. It's originally a double
int gOrderOpRetry = 10;
int gOrderOpRetry = 1;
MqlTick last_tick, blast_tick;
MqlDateTime dt;
ENUM_MODE_TRADE_SIGNAL signal = NO_SIGNAL;
//+------------------------------------------------------------------+
+6 -12
View File
@@ -37,11 +37,9 @@ void ScanPositions()
//If it is a buy order then increment the total count of buy orders
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
gTotalBuyPositions++;
Print("POSITION_TYPE_BUY ", POSITION_TYPE_BUY, " PositionGetInteger(POSITION_TYPE) ", PositionGetInteger(POSITION_TYPE));
Print(gSymbol, " Position ticket ",PositionGetTicket(i), " POSITION_PRICE_OPEN ", PositionGetDouble(POSITION_PRICE_OPEN));
}
Print("Total positions ", gTotalPositions, " - Total buys ", gTotalBuyPositions);
Print("Total ", gSymbol, " buy position ", gTotalBuyPositions);
for(int i=0; i<gTotalOrders; i++)
@@ -62,15 +60,11 @@ void ScanPositions()
if(OrderGetInteger(ORDER_MAGIC)!=InpMagicNumber)
continue;
//If it is a buy order then increment the total count of buy orders
if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY)
gTotalBuyPositions++;
Print("ORDER_TYPE_BUY ", ORDER_TYPE_BUY, " PositionGetInteger(ORDER_TYPE) ", OrderGetInteger(ORDER_TYPE));
if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_LIMIT || OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_STOP)
gTotalBuyOrders++;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Print("Total Orders ", gTotalOrders, " - Total buys ", gTotalBuyOrders);
Print("Total ", gSymbol, " Orders ", gTotalOrders, " - Total ", gSymbol, " buy orders ", gTotalBuyOrders);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+