How to check direction of graph using an EA?

 

How can change a trade that is gong on with the opposite one e.g. BUY with SELL(if BUY is in loss and vice-versa...).

How can we make EA detect the direction of the graph?

 
  1. With OrderCloseBy command
  2. By checking the previous Highs and Lows..

    Use High[1] or Low[1] or iHigh[..] and iLow[..]
James
 
JJF:


  1. With OrderCloseBy command
  2. By checking the previous Highs and Lows..

    Use High[1] or Low[1] or iHigh[..] and iLow[..]
James


Thank you James

But isn't OrderCloseBy command used to close two trades (BUY and SELL)

OrderCloseBy(order_id,opposite_id);

Because it requires id's of both the trades!

I tried to use the following logic but it didn't help. It should ideally reverse the trade, but it doesn't....

//for buy

if(Ask<OrderOpenPrice())
{
close BUY & open SELL
}


//for SELL
if(Bid>OrderOpenPrice)
{
close SELL & Open BUY
}


OR

//BUY && SELL
if(orderprofit()<0)
{
close and open opposite
}

I even tried using Close[] and Open[].

 
ksrohit2712:


Thank you James

But isn't OrderCloseBy command used to close two trades (BUY and SELL)

OrderCloseBy(order_id,opposite_id);

Because it requires id's of both the trades!

I tried to use the following logic but it didn't help. It should ideally reverse the trade, but it doesn't....

I even tried using Close[] and Open[].

//for buy

if(Ask<OrderOpenPrice())
{
close BUY & open SELL
}


//for SELL
if(Bid>OrderOpenPrice)
{
close SELL & Open BUY
}


OR

//BUY && SELL
if(orderprofit()<0)
{
close and open opposite
}

i hope this is not actually the code u used

 
qjol:

i hope this is not actually the code u used


Why do you say so Sir?

Actually I'm learning to code and I'm also new to forex! If anything is wrong or missing guidance would be much appriciated.

Thank you

 

if(Bid>OrderOpenPrice)
{
close SELL & Open BUY

1) OrderOpenPrice comes with () (OrderOpenPrice())

& Order must be first selected by the OrderSelect() function.

2) no such thing close SELL & Open BUY

3) i would highly recommend you read it first

 
qjol:

if(Bid>OrderOpenPrice)
{
close SELL & Open BUY

1) OrderOpenPrice comes with () (OrderOpenPrice())

& Order must be first selected by the OrderSelect() function.

2) no such thing close SELL & Open BUY

3) i would highly recommend you read it first


Sir, I've used code above just to explain the logic!

The EA compiles without errors.

I wanted to know if there is any other method that can determine the profit or loss.

 
for (.....)
   {
   OrderSelect(....);
   if (OrderProfit() > X)
      {
      //do X;
      }
   if (OrderProfit() < X)
      {
      //do Y;
      }
   }
this is the idea (I tired to write the whole code)
 
ksrohit2712:


Sir, I've used code above just to explain the logic!

The EA compiles without errors.

I wanted to know if there is any other method that can determine the profit or loss.

Hi, Ksrohit

A few suggestions: 

- set a static datetime variable, e.g LastStoredTime, to memorize the last opened trade time, each time you generate a trade. [EDITED]

- grab each order with OrderTotals() as the maximum counter loop (use SELECT_BY_POS) & compare it with the last stored LastStoredTime, if it is our last trade... check its profit state & type. Use the OrderProfit() & OrderType(). If it is negative or met any other criteria of your choice, then generate a trade with opposite type. Hope this helps. 

 
cameofx:

Hi, Ksrohit

A few suggestions:

- set a static datetime variable, e.g LastStoredTime, to memorize the last opened trade time, accessible with OrderOpenTime() (after grabbing with OrderSelect() of course).

- grab all orders with OrderTotals() as the maximum counter loop (use SELECT_BY_POS) & compare it with the last stored LastStoredTime, if it is our last trade... check its profit state & type. Use the OrderProfit() & OrderType(). If it is negative or met any other criteria of your choice, then generate a trade flag and opposite type. Hope that helps.


Thank you sir.

That definitely helps.

@gjol: Sir, I missed the part you have in the code. That would refine the code more.

 
Why not simply use the OrderSend() function as the second argument of the OrderCloseBy() function?
Reason: