Total newbie to Mql5, and need some help with this int OpenOrders() piece of code originally from Mql4 EA's, that finds open positions - (if any), and selects originally by SYMBOL, and MAGIC Number, and the expert doesn't open any other positions for as long as a trade it has opened on that symbol has not yet been closed.
I wish the EA to identify a TICKET it has opened, related SYMBOL, and POSITION_TYPE to be used in the ONTICK, and not open any other trades on the same chart until that one position is closed, but can continue trading on any other charts.
input double LotSize =0.3;
input double Incriment =0.01;
input int StopLoss =50;
input int TakeProfit =100;
input int Trend =21;
input int Momentum =21;
input int Strength =13;
int adxlevel =34;
int buylevel =62;
int selllevel =36;
//---------------------
double pips;
#include <TradeTrade.mqh>
CTrade Execute;
ulong passport, StopLevel;
double ask, bid;
double takeout=0,stopout=0;
int moving,rsi,adx,Spread;
//+------------------------------------------------------------------+
int OnInit()
{
//---
double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
if(_Digits==3||_Digits==4)
pips=ticksize*1000;
else
pips =ticksize;
//---
moving=iMA(_Symbol,PERIOD_CURRENT,Trend,0,MODE_SMA,PRICE_CLOSE);
rsi=iRSI(_Symbol,PERIOD_CURRENT,Momentum,PRICE_MEDIAN);
adx=iADX(_Symbol,PERIOD_CURRENT,Strength);
//---
return(INIT_SUCCEEDED);
}
***int OpenOrders()
{
int BUYS=0,SELLS=0;
for(int i=0; i<PositionsTotal(); i++)
{
if(PositionSelectByTicket(passport)==false) break;
int dealtype=(int)PositionGetInteger(POSITION_TYPE); // buy or sell
// string position_symbol=PositionGetString(POSITION_SYMBOL); // chart symbol
if(Symbol()==PositionGetSymbol(i) && passport==PositionGetTicket(POSITION_TICKET))
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) BUYS++;
if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) SELLS++;
}
}
//---
if(BUYS>0) return(BUYS);
else return(SELLS);
}***
//+------------------------------------------------------------------+
void OnTick()
{
//---
MqlRates rates[3];
double movingarray[],rsiarray[],adxarray[];
CopyRates(_Symbol,PERIOD_CURRENT,1,3,rates);
CopyBuffer(rsi,0,1,3,rsiarray);
CopyBuffer(adx,0,1,3,adxarray);
ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//---
if(OpenOrders()==0)
{
if(rates[0].open > moving )// CONDITION
if(rsiarray[0] > buylevel && rsiarray[1] < buylevel )//SIGNAL
{
if(TakeProfit>0) takeout=ask+TakeProfit*pips;
if(StopLoss>0) stopout=ask-StopLoss*pips;
Execute.Buy(LotSize,NULL,ask,stopout,takeout,NULL);
passport=Execute.ResultOrder();
Print("BUY Opened");
}
if(rates[0].open < moving )//CONTITION
if(rsiarray[0] < selllevel &&rsiarray[1] > selllevel )//SIGNAL
{
if(TakeProfit>0) takeout=bid+TakeProfit*pips;
if(StopLoss>0) stopout=bid-StopLoss*pips;
Execute.Sell(LotSize,NULL,bid,stopout,takeout,NULL);
passport=Execute.ResultOrder();
Print("SELL Opened");
}
}
//---
if(OpenOrders()>0)
{
int dealtype=(int)PositionGetInteger(POSITION_TYPE);
if(dealtype==POSITION_TYPE_BUY)
if(rsiarray[0] < buylevel )
{
Execute.PositionClose(passport);
passport=0;
}
else if(dealtype==POSITION_TYPE_SELL)
if(rsiarray[0] > selllevel )
{
Execute.PositionClose(passport);
passport=0;
}
}
}
question from:
https://stackoverflow.com/questions/65949362/mql5-function-for-selecting-open-orders-by-ticket-then-symbol-and-trade-direct