selecting last open position

 

Dear All,

The system I am coding open only one position at a time. This condition is closed later on following a few rules.

In order to select this unique open position and apply the closing rules I use the following code:

{ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"Hearbeat 15 min",12345,0,Red);

or

{ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Hearbeat 15 min",12345,0,Green);


Then

          if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)
          
            {if(order_type==OP_BUY)
               if(RSIcurrent>=90 || STOCHcurrent>=90)
               {OrderClose(ticket,1,Bid,3,Red);
               Print("OP_BUY");}
               
             {if(order_type==OP_SELL)
               if(RSIcurrent<=10 || STOCHcurrent<=10)    
               {OrderClose(ticket,1,Ask,3,Blue);
               Print("OP_SELL");}}


Sometimes the open position is short, but the EA prints "OP_BUY" and vice versa, so in fact using this code the behavior is totally random.

I guess I am missing something, I am trying to find a simple way to do that, anyone can point me to the right direction?

Maybe I did not write the following line well ?

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)

there is only 1 position open at a time.

May thx and regards,

Movingaverage

 

MA

From the code we see, this is wrong...

if(order_type==OP_BUY)

should be...

if(OrderType()==OP_BUY)

FWIW

-BB-

 
BarrowBoy wrote >>

MA

From the code we see, this is wrong...

should be...

FWIW

-BB-

I forgot to mention that I had defined at the beginning the following:

int order_type

order_type=OrderType()

Nevertheless i deledted the above lines from my EA and replaced order_type by OrderType() and it worked ! (dunno with though)

So thx for your help it works now.

MovingAverage

 

MA

> I had defined at the beginning...

order_type=OrderType()

This would only work if done within the {} after the OrderSelect()...

-BB-

 

ok thx for the tip BarrowBoy.

Reason: