Select "Ticket" Help required...

 

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?

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

double DetermineRiskClose()
{
            
            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
            
            
            for(trade=OrdersTotal()-1;trade>=0;trade--)
              {
               OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
               if((OrderSymbol()=="GBPUSD"))
                  if(OrderType()==OP_SELL || OrderType()==OP_BUY)
                     count++;  
              }
              {
                  if(count>0 && count!=0)
                     CurrentTrades=count;}
               
               if((OrderSymbol()=="GBPUSD"))
               FindFirstticketnumber=OrderSelect(count-count,SELECT_BY_POS,MODE_TRADES);
               TicketNumber=OrderTicket();
               RiskAversionBuy=OrderProfit();
               RiskAversionSell=OrderProfit();
               {
               if((OrderSelect(count-count,SELECT_BY_POS)==true) /*&& (count==6)*/ && (OrderSymbol()=="GBPUSD")&& (OrderType()==OP_SELL))
               RiskAversionSell=OrderProfit();
               if( (OrderSymbol()=="GBPUSD") && (OrderType()==OP_SELL) && (RiskAversionSell == (-50.0))/*&& (count==6)*/)
                     OrderClose(TicketNumber,OrderLots(),Ask,3,Red);
                     //ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Bid-Stoploss*Point,Bid+55*Point,EAName+"-"+" Skyfloat Risk Aversion! "+NumOfTrades,MagicNumber,0,Green);
               Print("Risk Aversion is Sell ",RiskAversionSell," for Ticket ",TicketNumber);
               
               
               if(OrderSelect(count-count,SELECT_BY_POS)==true /*&& (count==6) */&& (OrderSymbol()=="GBPUSD")&& (OrderType()==OP_BUY))
               RiskAversionSell=OrderProfit();
                
               if((OrderSymbol()=="GBPUSD") && (OrderType()==OP_BUY) && (RiskAversionBuy == (-50.0)) /*&& (count==6)*/)
                     OrderClose(TicketNumber,OrderLots(),Bid,3,Blue);
                     //ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+Stoploss*Point,Ask-55*Point,EAName+"-"+" Skyfloat Risk Aversion! "+NumOfTrades,MagicNumber,0,Gold);
               Print("Risk Aversion is Buy ",RiskAversionSell," for Ticket ",TicketNumber);
               }
            return(TicketNumber);

           }

            
 

Hi there,

There are a couple of glaring errors, so I will point out the easy ones and leave it to the others to find the harder ones.

FindFirstticketnumber=OrderSelect(count-count,SELECT_BY_POS,MODE_TRADES);

count-count is always going to give you zero, which is why it always returns the first order.

I should also point out that OrderSelect is a boolean function, which you have defined as an int. Given that true or false are integers anyway(0 & 1) I don't think this matters, but it is worth being aware of.

Why count the tickets.....why not just OrderSelect in the first loop when you are counting?

Also, you don't need to separate the closing of the buy orders and the sell orders. You can use OrderClosePrice() to close determine the Bid or Ask price for closing and save yourself a bunch of code.(Thanks Gordon:-)

 
kennyhubbard:

Hi there,

There are a couple of glaring errors, so I will point out the easy ones and leave it to the others to find the harder ones.

count-count is always going to give you zero, which is why it always returns the first order.

I should also point out that OrderSelect is a boolean function, which you have defined as an int. Given that true or false are integers anyway(0 & 1) I don't think this matters, but it is worth being aware of.

Why count the tickets.....why not just OrderSelect in the first loop when you are counting?

Also, you don't need to separate the closing of the buy orders and the sell orders. You can use OrderClosePrice() to close determine the Bid or Ask price for closing and save yourself a bunch of code.(Thanks Gordon:-)


Thanks Gordon,

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()), however used Orderprofit() for testing purposes in the mean time. The Tickets to be selected thus is to ensure that if a trade which has a negative (Orderprofit()-Openprice() of $50 and more, gets selecte and thus will be closed. Having said that, a new buy order should be opened simultaneously.

While I appreciate the Errors you have noted, I still require an answer to the selection of the symbol().

Thank you anyway, I'll give attention to the errors you noted in the mean time. ;)

Reason: