How to select a ticket for a specific symbol irrespective whether there are other symbols opened?

 

Question

How can you select a ticket for a specific symbol irrespective whether there are other symbols opened ie. both GBP and EUR in the terminal window? What I'm finding is that irrespective of the code below, it selects the first trade opened ie. Eur and reflects the EUR Ticket?

I use the counter to ensure for each pair EUR and GBP 6 trades are opened. If I use (count-count), I ensure that the first trade's ticket number will be selected. What I'm trying to do with the seperation of code (Buy and Sell) is that I want to check the (ordertakeprofit()-Orderopenprice()). The Tickets to be selected thus is to ensure that if a trade which has a negative (Ordertakeprofit()-Openprice() of $ 0 and more, gets selected and thus will be closed. Having said that, a new buy order should be opened simultaneously.

Suffice to say, the code appears to close all the trades irrespective of the condition to only close if the OrderTakeProfit ()-Orderopenprice() is negative, that is $ 0 or more.

//+------------------------------------------------------------------+
//| Set Risk Level Parameters to close GBP |
//+------------------------------------------------------------------+

double DetermineRiskClose()
{//(a)

int FindFirstticketnumber;// If trades are opened check the first trade that was opened
int CurrentTrades=0; // Has a Branch been opened
int TicketNumber=0; // Get the Trade Ticket Number
double RiskAversionBuy=0;// The Number of Pips calculated as Acceptable Risk within Branch
double RiskAversionSell=0;
int count=0; // Count the trades
int trade; // Used Counter Variable
static int Triggered = 0;// Check if Close has been triggered

for(trade=OrdersTotal()-1;trade>=0;trade--)
{//(b)
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()=="GBPUSD"))
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
count++;
}//(b)
{//(c)
if(count>0 && count!=0)
CurrentTrades=count;}//(c)
{//(d)
if((OrderSymbol()=="GBPUSD"))
FindFirstticketnumber=OrderSelect(count-count,SELECT_BY_POS,MODE_TRADES);
TicketNumber=OrderTicket();
RiskAversionBuy= (OrderTakeProfit()-OrderOpenPrice())*100000;
RiskAversionSell= (OrderOpenPrice()-OrderTakeProfit())*100000;}//(d)


//---------------- Check If Short Positions are open ---------

{//(e)
if((OrderType()==OP_SELL)&&(CurrentTrades > 0)&& (RiskAversionSell < 0))
Print("Risk Aversion is Sell ",RiskAversionSell," for Ticket ",TicketNumber);
Triggered=3;}//(e) This means that the above statement holds true
Print("Trigger value is: ",Triggered);
{//(f)
if(Triggered==3)
OrderClose(TicketNumber,OrderLots(),Ask,3,Red);
Sleep(10000);//Give server time to process the above transaction first
Triggered=4;}// (f)

{//(g)
if(Triggered==4)
ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Bid-Stoploss*Point,Bid-DetermineTakeProfitSpread()*Point/*Bid+55*Point*/,EAName+"-"+" Skyfloat Risk Aversion! "+NumOfTrades,MagicNumber,0,Green);
Triggered=0;//This means that the Order send has been activated and we reset the variable back to static position
Sleep(10000);//Give server time to process the above transaction first
}//(g)

//---------------- Check If Long Positions are open ---------

{//(h)
if(((OrderType()==OP_BUY)&& (CurrentTrades > 0)&& (RiskAversionBuy < 0)))
Print("Risk Aversion is Buy ",RiskAversionBuy," for Ticket ",TicketNumber);
Triggered=1;}//(h) This means that the above statement holds true

{//(i)
if(Triggered==1)
OrderClose(TicketNumber,OrderLots(),Bid,3,Blue);
Sleep(10000);//Give server time to process the above transaction first
Triggered=2;}//(i)

{//(j)
if(Triggered==2)
ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+Stoploss*Point,Bid-DetermineTakeProfitSpread()*Point/*Ask-55*Point*/,EAName+"-"+" Skyfloat Risk Aversion! "+NumOfTrades,MagicNumber,0,Gold);
Triggered=0;//This means that the Order send has been activated and we reset the variable back to static position
}//(j)


return(TicketNumber);

Reason: