Only open one order at a time
Forum on trading, automated trading systems and testing trading strategies
When you post code please use the CODE button (Alt-S)!

int OrdersTotalT(int _type) { int _total=0; for(int cnt=OrdersTotal()-1;cnt>=0;cnt--) { bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type) { _total++; } } return(_total); }
if(OrdersTotalT(OP_BUY)==0 && // your buy signal )
For some reason my code is only recognizing when a sell position is open and not a buy position.
//+------------------------------------------------------------------+
//| My_First_EA.mq5 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
input int StopLoss=11;
input int TakeProfit=0;
input int ADX_Period=8;
input int Fast_MA_Period=5;
input int Slow_MA_Period=12;
input int EA_Magic=12345;
input double ADX_Min=22.0;
input double Lot=0.1;
int adxHandle;
int FastMAHandle;
int SlowMAHandle;
int pSarHandle;
double plsDI[],minDI[],adxVal[];
double FastMAVal[];
double SlowMAVal[];
double pSarVal[];
double p_close;
int STP,TKP;
int OnInit()
{
//---
//adxHandle=iADX(NULL,0,ADX_Period);
FastMAHandle=iMA(_Symbol,_Period,Fast_MA_Period,0,MODE_EMA,PRICE_CLOSE);
SlowMAHandle=iMA(_Symbol,_Period,Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE);
pSarHandle=iSAR(_Symbol,_Period,0.02,0.2);
if( FastMAHandle<0 || pSarHandle<0 || SlowMAHandle<0)
{
Alert("Error creating handles for PSar and MA's - error: ",GetLastError(),"!!");
}
STP=StopLoss;
TKP=TakeProfit;
if(_Digits==5 || _Digits==3)
{
STP=STP*10;
TKP=TKP*10;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
//IndicatorRelease(adxHandle);
IndicatorRelease(FastMAHandle);
IndicatorRelease(SlowMAHandle);
IndicatorRelease(pSarHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(Bars(_Symbol,_Period)<60)
{
Alert("We have less than 60 bars, EA will now exit!");
return;
}
//+------------------------------------------------------------------+
static datetime Old_Time;
datetime New_Time[1];
bool IsNewBar=false;
int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
if(copied>0)
{
if(Old_Time!=New_Time[0])
{
IsNewBar=true;
if(MQL5InfoInteger(MQL5_DEBUGGING))
{
Print("We have new bar here ",New_Time[0],"old time was",Old_Time);
Old_Time=New_Time[0];
}
}
}
else
{
Alert("Error in copying historical times data, error=",GetLastError());
ResetLastError();
return;
}
if(IsNewBar==false)
{
return;
}
int MyBars=Bars(_Symbol,_Period);
if(MyBars<60)
{
Alert("We have fewer than 60 bars, EA will now exit!!");
return;
}
MqlTick latest_price;
MqlTradeRequest mrequest;
MqlTradeResult mresult;
MqlRates mrate[];
ZeroMemory(mrequest);
//ZeroMemory(mresult);
ArraySetAsSeries(mrate,true);
//ArraySetAsSeries(plsDI,true);
//ArraySetAsSeries(minDI,true);
//ArraySetAsSeries(adxVal,true);
ArraySetAsSeries(FastMAVal,true);
ArraySetAsSeries(SlowMAVal,true);
ArraySetAsSeries(pSarVal,true);
if(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
{
Alert("Error copying rates/history data - error:",GetLastError(),"!!");
return;
}
/*if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0 || CopyBuffer(adxHandle,2,0,3,minDI)<0)
{
Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
return;
}*/
if(CopyBuffer(FastMAHandle,0,0,3,FastMAVal)<0)
{
Alert("Error copying MA indicator buffer - error:",GetLastError(),"!!");
return;
}
if(CopyBuffer(SlowMAHandle,0,0,3,SlowMAVal)<0)
{
return;
}
if(CopyBuffer(pSarHandle,0,0,3,pSarVal)<0)
{
return;
}
bool Buy_opened=false; // variable to hold the result of Buy opened position
bool Sell_opened=false; // variable to hold the result of Sell opened position
if (PositionSelect(_Symbol) ==true) // we have an opened position
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
Buy_opened = true;
}
else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
Sell_opened = true;
}
}
p_close=mrate[1].close;
bool Buy_Condition_1_Entry = (FastMAVal[0]>SlowMAVal[0] && FastMAVal[1]<SlowMAVal[1]);
bool Buy_Condition_2_Entry = p_close>pSarVal[0];
if(Buy_Condition_1_Entry && Buy_Condition_2_Entry)
{
if(Buy_opened)
{
Alert("We already have a buy position!");
return;
}
Print("Buy conditions met at",TimeCurrent());
mrequest.action=TRADE_ACTION_DEAL;
mrequest.price=NormalizeDouble(latest_price.ask,_Digits);
mrequest.sl=NormalizeDouble(latest_price.ask-STP*_Point,_Digits);
// mrequest.tp=NormalizeDouble(latest_price.ask+TKP*_Point,_Digits);
mrequest.symbol=_Symbol;
mrequest.volume=Lot;
mrequest.magic=EA_Magic;
mrequest.type=ORDER_TYPE_BUY;
mrequest.type_filling=ORDER_FILLING_FOK;
mrequest.deviation=100;
OrderSend(mrequest,mresult);
}
bool Sell_Condition_1_Entry = (FastMAVal[0]<SlowMAVal[0] && FastMAVal[1]>SlowMAVal[1]);
bool Sell_Condition_2_Entry = p_close<pSarVal[0];
bool Long_Position_Exit_1 = p_close<pSarVal[0];
bool Short_Position_Exit_1 = p_close>pSarVal[0];
if(Sell_Condition_1_Entry && Sell_Condition_2_Entry)
{
if(Sell_opened)
{
Alert("We already have a sell position!");
return;
}
Print("Sell conditions met at a bid of: ",NormalizeDouble(latest_price.bid,_Digits),"other bid method: ",NormalizeDouble(SYMBOL_BID,_Digits));
mrequest.action=TRADE_ACTION_DEAL;
mrequest.price=NormalizeDouble(latest_price.bid,_Digits);
mrequest.sl=NormalizeDouble(latest_price.bid+STP*_Point,_Digits);
// mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits);
Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits));
mrequest.symbol=_Symbol;
mrequest.volume=Lot;
mrequest.magic=EA_Magic;
mrequest.type=ORDER_TYPE_SELL;
mrequest.type_filling=ORDER_FILLING_FOK;
mrequest.deviation=100;
OrderSend(mrequest,mresult);
}
if(Long_Position_Exit_1 && Buy_opened)
{
mrequest.action=TRADE_ACTION_DEAL;
mrequest.price=NormalizeDouble(latest_price.bid,_Digits);
//mrequest.sl=NormalizeDouble(latest_price.bid+STP*_Point,_Digits);
// mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits);
//Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits));
mrequest.symbol=_Symbol;
mrequest.volume=Lot;
mrequest.magic=EA_Magic;
mrequest.type=ORDER_TYPE_SELL;
mrequest.type_filling=ORDER_FILLING_FOK;
mrequest.deviation=100;
OrderSend(mrequest,mresult);
Print("Long position closed");
}
if(Short_Position_Exit_1 && Sell_opened)
{
mrequest.action=TRADE_ACTION_DEAL;
mrequest.price=NormalizeDouble(latest_price.ask,_Digits);
//mrequest.sl=NormalizeDouble(latest_price.ask+STP*_Point,_Digits);
// mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits);
//Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits));
mrequest.symbol=_Symbol;
mrequest.volume=Lot;
mrequest.magic=EA_Magic;
mrequest.type=ORDER_TYPE_BUY;
mrequest.type_filling=ORDER_FILLING_FOK;
mrequest.deviation=100;
OrderSend(mrequest,mresult);
Print("Short position closed");
}
}