simple question about previous orders, but i do need some help

 
I really appreciate any help, my head is doin circles right now after a week of tryin different things, I think this will work out for me once i get a few details straight.

All my system needs to know is what the previous trade was(BUY/SELL BUYSTOP/SELLSTOP), before it places it's new STOP order or skips it and continues on.

I've tried about 15 different things, that I won't bother telling you where I'm at right now with it, I think a fresh perspective would help me out more than fixing anything I was trying previously.

Thanks again for any help.
 
check the manual and earlier threads on using OrderSelect() with MODE_HISTORY

to see some examples how OrderSelect is being used, check the example on the MACD in your expert samples or explained at:

"Expert Sample"

but make sure you build the loop using HistoryTotal and not OrdersTotal, as explained in:

"How to get historical trades"

good luck - this has worked fine for me!
 
Thanks for the response, I figured I would show what I have written so far, as those suggestions are good but I am using part the MACD sample code already...There is clearly something fundamental I am not understanding on how values are assigned and passed through these programs, maybe i can't shake some of the MQL3 i was learning prior to this.

extern int T_P = 44;    //take profit
extern int S_L = 5;     //stop loss
extern int E_M = 10;    //Entry margin
extern int Lot_Size = 1;//Lot size

//Create Internal Variables


void start()
{
//--------------------------------------------------------------------------------------------------------------
//              CHECK FOR OPEN TRADES
//--------------------------------------------------------------------------------------------------------------  
int P_Order;               //*****Previous_Order value to trigger the proper trades below
int total=OrdersTotal();   
int cnt;

if (total >= 2)     //*****if there are 2 trades open then it doesn't matter what they are no more are opening
 {P_Order = 3;}     //*****Bogus value, no trades will call for that, therefore no trades can be placed.
 
if (total == 0)    //*****if there are no open orders, it doesn't matter what previous orders were either.
 {P_Order = 0;}    //*****All trades below will call for 0 and execute if it is there. 
 
if (OrdersTotal() ==1)    //*****if one trade is open have to find it's type, can't open another of the same value.
 {
  for(cnt=0;cnt<OrdersTotal();cnt++)  //***Start loop to get last trades type****
  {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()==OP_BUY || OrderType()== OP_BUYSTOP &&   // check for opened position 
      OrderSymbol()==Symbol())  // check for symbol
     { P_Order = 1;}
    else
     { 
      if(OrderType() == OP_SELL || OrderType() == OP_SELLSTOP &&
         OrderSymbol() == Symbol())
       {P_Order = 2;}  
     }
   }   
//----------------------------------------------------------------------------------------------
//              INITIALIZE EMA'S USED IN ORDER PLACEMENT 
//---------------------------------------------------------------------------------------------
double EMA169, EMA144, EMA12;
EMA169=iMA(NULL,0,169,0,MODE_EMA,PRICE_CLOSE,0);
EMA144=iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,0);
EMA12=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);    

//-----------------------------------------------------------------------------------------------
//                 OPEN AVAILABLE ORDERS

//----------------------------------------------------------------------------------------------

   if(EMA144 > EMA169) 
    {
     if (P_Order == 0 || P_Order == 2)
      {
       if(Ask >= EMA144-1*Point && Ask <= EMA144+1*Point)
        {
        OrderSend(Symbol(),OP_BUYSTOP,Lot_Size,EMA144+E_M*Point,1,EMA169-S_L*Point,EMA144+T_P*Point,"VEGAS TRADE",0,CurTime()+3*3600, Yellow);
        return;
        }
       }
     if (P_Order == 0 || P_Order == 1)
      {
       if(Bid <= EMA169+1*Point && Bid >= EMA169-1*Point)
        {
         OrderSend(Symbol(),OP_SELLSTOP,Lot_Size,EMA169-E_M*Point,1,EMA144+S_L*Point,EMA169-T_P*Point,"My order #2",,CurTime()+3*3600,Purple);
         return;
        }    
      } 
     }  
   if(EMA169 > EMA144)
    {
     if(P_Order == 0 || P_Order == 2)
      {
       if(Ask >= EMA169-1*Point && Ask <= EMA169+1*Point)
        {
         OrderSend(Symbol(),OP_BUYSTOP,Lot_Size,EMA169+E_M*Point,1,EMA144- S_L*Point,EMA169+T_P*Point,"VEGAS TRADE",0,CurTime()+3*3600, Aqua);
         return;
        }
       } 
     if(P_Order == 0 || P_Order == 1)
       {
        if(Ask <= EMA144+1*Point && Ask >= EMA144-1*Point)
         {
          OrderSend(Symbol(),OP_SELLSTOP,Lot_Size,EMA144-E_M*Point,1,EMA169+S_L*Point,EMA144-T_P*Point,"My order #2",0,CurTime()+3*3600,Blue);
          return;
         } 
        } 
       }
      }
      
   return(0);
  }
//+------------------------------------------------------------------+



My Orders are being placed and executed well without all the P_Order business, it's missing some entry points, not to sure if that's just from backtesting, or a problem, i really need this part working before i continue on though.

Any ideas or pushes in the right direction would be great...is there something wrong with how the code is layed out?

 
Also about the HistoryTotal(), it really doesn't matter to the EA what has closed, just what is open/pending. From what I understand I should still be using OrdersTotal if that is the case.

And if there are 2 trades open/pending, nothing will be done
And if there are 0 trades open/pending, somethign will be placed if the price touches an EMA either way
And if there is 1 trade open/pending, it has to know which tradetype it is, so it doesn't open another STOP trade, but can open the opposite if the proper EMA is touched.


Hope that makes sense, thanks again to any who can help me out.
 
Also about the HistoryTotal(), it really doesn't matter to the EA what has closed, just what is open/pending. From what I understand I should still be using OrdersTotal if that is the case.

And if there are 2 trades open/pending, nothing will be done
And if there are 0 trades open/pending, somethign will be placed if the price touches an EMA either way
And if there is 1 trade open/pending, it has to know which tradetype it is, so it doesn't open another STOP trade, but can open the opposite if the proper EMA is touched.


Hope that makes sense, thanks again to any who can help me out.

   SellCount = 0;
   BuyCount = 0;
   SellLimitCount = 0; 
   BuyLimitCount = 0;
   for(i = 0; i < OrdersTotal(); i ++)   
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderSymbol()!= Symbol() || OrderMagicNumber() != Magic) continue;     
      if (OrderType() == OP_BUY) BuyCount ++;
      if (OrderType() == OP_SELL) SellCount ++;
      if (OrderType() == OP_BUYLIMIT) BuyLimitCount ++;
      if (OrderType() == OP_SELLLIMIT) SellLimitCount ++;
   }
 
Thanks alot MichelB, your way is much clearer than mine, also when I start to write the code on how my trades will be exited if not by the set Take Profit level, I will need to know what trade is what. I'll admit though I'm not positive why there are the ! marks in there, but I should be able to learn why they are needed on my own, thanks again!
 
Thanks alot MichelB, your way is much clearer than mine, also when I start to write the code on how my trades will be exited if not by the set Take Profit level, I will need to know what trade is what. I'll admit though I'm not positive why there are the ! marks in there, but I should be able to learn why they are needed on my own, thanks again!

"!=" means "Not Equal"
Reason: