Reset the order values? what i am doing wrong?

 
I am writing code for loss recovery trade.

when order closed with loss the next coming orders do the multiple orders till it closes with profit(take profit).
after loss first trade open = (0.01)*2=0.02
if loss continues second trade open= (0.01+0.02)*2=0.06
if loss continues third trade open=(0.01+0.02+0.06)*2=0.18
if loss continues four trade open=(0.01+0.02+0.06+0.18)*2=0.54

i wrote the code to calculate the lot when previous order closed with loss.

the above calculation must be active when previous order closed with loss. my current code works fine @ first time, but when next time the order closed with loss, it adds previous all orders count instead of reset the order value?


double SL = 0, TP = 0;
int ticket = -1, ordertype = -1;
bool ret = false;
double lots = 0, oldlots = 0;

int orderid = -1;
datetime lastCloseTime = 0;
int cnt = OrdersHistoryTotal();

for (int i=0; i < cnt; i++)
{
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    if (OrderSymbol() == Symbol() && lastCloseTime < OrderCloseTime())
    {
        lastCloseTime = OrderCloseTime();
        orderid = OrderMagicNumber();
        oldlots = oldlots+OrderLots();
        ordertype = OrderType();
    }
}

if (orderid == ~OrderId~)
{
    lots = oldlots * 2;
    if (ordertype == OP_SELL)
    {
        SL = Ask - ~Stoploss~*PipValue*Point;
        if (~Stoploss~ == 0) SL = 0;
        TP = Ask + ~Takeprofit~*PipValue*Point;
        if (~Takeprofit~ == 0) TP = 0;
        ticket = -1;
        if (~ECNBroker~)
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 4, 0, 0, "test", 1, 0, blue);
        else
            ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 4, SL, TP, "test", 1, 0, blue);
        if (ticket > -1)
        {
            if (ECNBroker=true)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, red);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
    if (ordertype == OP_BUY)
    {
        SL = Bid + 40*PipValue*Point;
        if (40 == 0) SL = 0;
        TP = Bid - 50*PipValue*Point;
        if (50 == 0) TP = 0;
        ticket = -1;
        if (ECNBroker=true)
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 4, 0, 0, "test", 1, 0, red);
        else
            ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 4, SL, TP, "test", 1, 0, red);
        if (ticket > -1)
        {
            if (ECNBroker=true)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, blue);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
}
what could be the problom with my code?
 
   double order_array[][3];
   int as=0;
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_HISTORY)
         && OrderMagicNumber()==MagicNumber
         && OrderSymbol()==Symbol()
         && (OrderType()==OP_BUY || OrderType()==OP_SELL))
        {
         as++;
         ArrayResize(order_array,as);
         order_array[as-1][0]=(double)OrderCloseTime();
         order_array[as-1][1]=OrderProfit();
         order_array[as-1][2]=OrderLots();
        }
     }
   double lots=0;
   if(as>0)
     {
      ArraySort(order_array,WHOLE_ARRAY,0,MODE_ASCEND);
      for(int x=as-1;x>=0;x--)
        {
         if(order_array[x][1]>0)
            break;
         else
            lots+=order_array[x][2];
        }
     }
   double nextlots=0.01;
   if(lots>0)
      nextlots=lots*2;
Not tested
 
GumRai:
Not tested

Hi GumRai, i tried as our advice. no order is placing. and i dont get lot value while i check with print statement.

here is the code:

double SL = 0, TP = 0;
int ticket = -1, ordertype = -1;
bool ret = false;
double lots = 0, oldlots = 0;

int orderid = -1;
datetime lastCloseTime = 0;
int cnt = OrdersHistoryTotal();

   double order_array[][3];
   int MagicNumber=0;
   int as=0;
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_HISTORY)
         && OrderMagicNumber()==orderid
         && OrderSymbol()==Symbol()
         && (OrderType()==OP_BUY || OrderType()==OP_SELL))
        {
         as++;
         ArrayResize(order_array,as);
         order_array[as-1][0]=OrderCloseTime();
         order_array[as-1][1]=OrderProfit();
         order_array[as-1][2]=OrderLots();
        }
     }
   //double lots=0;
   if(as>0)
     {
      ArraySort(order_array,WHOLE_ARRAY,0,MODE_ASCEND);
      for(x=as-1;x>=0;x--)
        {
         if(order_array[x][1]>0)
            break;
         else
            lots+=order_array[x][2];
        }
     }
   double nextlots=0.01;
   if(lots>0)
      {
        nextlots=lots*2;
      }

if (orderid == ~OrderId~)
{
    lots = oldlots * 2;
    if (ordertype == OP_SELL)
    {
        SL = Ask - Stoploss*PipValue*Point;
        if (Stoploss == 0) SL = 0;
        TP = Ask + Takeprofit*PipValue*Point;
        if (Takeprofit == 0) TP = 0;
        ticket = -1;
        if (ECNBroker)
            ticket = OrderSend(Symbol(), OP_BUY, nextlots, Ask, 4, 0, 0, "test", MagicNumber, 0, red);
        else
            ticket = OrderSend(Symbol(), OP_BUY, nextlots, Ask, 4, SL, TP, "test", MagicNumber, 0, red);
        if (ticket > -1)
        {
            if (ECNBroker)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, red);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
    if (ordertype == OP_BUY)
    {
        SL = Bid + Stoploss*PipValue*Point;
        if (Stoploss == 0) SL = 0;
        TP = Bid - Takeprofit*PipValue*Point;
        if (Takeprofit == 0) TP = 0;
        ticket = -1;
        if (ECNBroker)
            ticket = OrderSend(Symbol(), OP_SELL, nextlots, Bid, 4, 0, 0, "test", MagicNumber, 0, blue);
        else
            ticket = OrderSend(Symbol(), OP_SELL, nextlots, Bid, 4, SL, TP, "test", MagicNumber, 0, blue);
        if (ticket > -1)
        {
            if (ECNBroker)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, blue);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
}
~next~






 

Well, it won't work if you just insert the code without adapting it.

I'm guessing that you want to open trades based on the last closed trade detail?

So add another dimension to the array

   double order_array[][4];

 so 4 instead of 3

Then in your loop add

         order_array[as-1][3]=OrderTicket();

 Then you can select the last closed order (after sorting the array)

   OrderSelect(order_array[as-1][3],SELECT_BY_TICKET);

 and retrieve the data that you need

 

 Incidentally, what's with all the squiggly lines?

~OrderId~   etc

I understand that ~ before the variable is a bitwise operation, but after as well? I don't know what that does. 

 
GumRai:

Well, it won't work if you just insert the code without adapting it.

I'm guessing that you want to open trades based on the last closed trade detail?


Yes GumRaj exactly which i need to do!, especially lot multiplication is as per above calculation. i am poor in using array.(can you explain with comments) so more confusion with this.

i can easily get your logic if i see the comment.

I am really kind of you.

Here i idid sofar. but couldn't get order lot!

double SL = 0, TP = 0;
int ticket = -1, ordertype = -1;
bool ret = false;
double lots = 0, oldlots = 0;

int orderid = -1;
datetime lastCloseTime = 0;
int cnt = OrdersHistoryTotal();

   //double order_array[][3];
   double order_array[][4];
   int MagicNumber=0;
   int as=0;
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_HISTORY)
         && OrderMagicNumber()==orderid
         && OrderSymbol()==Symbol()
         && (OrderType()==OP_BUY || OrderType()==OP_SELL))
        {
         as++;
         ArrayResize(order_array,as);
         order_array[as-1][0]=OrderCloseTime();
         order_array[as-1][1]=OrderProfit();
         order_array[as-1][2]=OrderLots();
         order_array[as-1][3]=OrderTicket();
        }
     }
   //double lots=0;
   if(as>0)
     {
      ArraySort(order_array,WHOLE_ARRAY,0,MODE_ASCEND);
      for(x=as-1;x>=0;x--)
        {
         if(order_array[x][1]>0)
            break;
         else
            lots+=order_array[x][2];
        }
     }
      OrderSelect(order_array[as-1][3],SELECT_BY_TICKET);

   double nextlots=0.01;
   if(lots>0)
      {
        nextlots=lots*2;
      }

if (orderid == MagicNumber)
{
    if (ordertype == OP_SELL)
    {
        SL = Ask - Stoploss*PipValue*Point;
        if (Stoploss == 0) SL = 0;
        TP = Ask + Takeprofit*PipValue*Point;
        if (Takeprofit == 0) TP = 0;
        ticket = -1;
        if (~ECNBroker~)
            ticket = OrderSend(Symbol(), OP_BUY, nextlots, Ask, 4, 0, 0, "test", MagicNumber, 0, blue);
        else
            ticket = OrderSend(Symbol(), OP_BUY, nextlots, Ask, 4, SL, TP, "test", MagicNumber, 0, blue);
        if (ticket > -1)
        {
            if (~ECNBroker~)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, blue);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
    if (ordertype == OP_BUY)
    {
        SL = Bid + ~Stoploss~*PipValue*Point;
        if (Stoploss == 0) SL = 0;
        TP = Bid - Takeprofit*PipValue*Point;
        if (Takeprofit == 0) TP = 0;
        ticket = -1;
        if (ECNBroker)
            ticket = OrderSend(Symbol(), OP_SELL, nextlots, Bid, 4, 0, 0, "test", MagicNumber, 0, blue);
        else
            ticket = OrderSend(Symbol(), OP_SELL, nextlots, Bid, 4, SL, TP, "test", MagicNumber, 0, blue);
        if (ticket > -1)
        {
            if (ECNBroker)
            {
                if (!OrderSelect(ticket, SELECT_BY_TICKET)) return;
                ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, blue);
                if (ret == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            }
        }
        else
        {
            Print("OrderSend() error - ", ErrorDescription(GetLastError()));
        }
    }
}
return(0);

 

I don't know what you are trying to do with this

if (orderid == MagicNumber)

 you are checking history looking for trades with MagicNumber()==orderid

Then you are placing trades  with the variable MagicNumber

    if (ordertype == OP_SELL)

 Do you mean

    if (OrderType() == OP_SELL)

 ?

You have

ordertype = -1;

 earlier in the code, but don't give it a new value, so your order conditions will never be true.

 

I don't know how I could comment the code to make it easier to understand. The logic is simple. Details of each order are stored in the array. The order close time is placed in the first dimension of the array, then the array is sorted by time so the the most recent trade details are in the latest elements or cells, whatever you like to call them.

You probably need to study the documentation for arrays. They are so useful.

Personally, I hardly ever loop through open or closed orders as I store trade info in arrays. I generally only loop through open or closed orders  for recovery purposes in the event of a terminal shutdown.

 

I used following validation to find previous order type.

because i open opposite order for previous order it is closed with loss.

I think i am confused myself with array elements. so it makes me confusing my code.

int ticket = -1, ordertype = -1;  
bool ret = false;
double lots = 0, oldlots = 0;

int orderid = -1;

if (orderid == OrderId)
{
    if (ordertype == OP_SELL)   // finiding and confirming previous order type
    {
        SL = Ask - Stoploss*PipValue*Point;
        if (Stoploss == 0) SL = 0;
        TP = Ask + Takeprofit*PipValue*Point;
        if (Takeprofit == 0) TP = 0;
        ticket = -1;
        if (~ECNBroker~)
            ticket = OrderSend(Symbol(), OP_BUY, nextlots, Ask, Slippage, 0, 0, "testea", OrderId, 0, blue); //open opposite order
 

As I have already told you

if (ordertype == OP_SELL) 

 does nothing because you have set the variable to -1 earlier

ordertype = -1; 

 

What is this supposed to do?

int orderid = -1;

if (orderid == OrderId)

 The 2 lines are exactly the same as the single line


if (OrderId==-1)
 
GumRai:

As I have already told you

 does nothing because you have set the variable to -1 earlier

 

What is this supposed to do?

 The 2 lines are exactly the same as the single line

 

think i bit confused with coding logic. but feel GumRaj is experienced coder. i mingle with newbie and extreme coders. must recap my code from begining.

even i re arranged the code as GumRaj response no use for me. becasue i still didnt get exact logic for my needs i hope.

anyway thanks for the support all time GumRaj for doing great support.

onething GumRaj even i print the value of nextlots i dont et the value for nextlots?

 
sheriffonline:

onething GumRaj even i print the value of nextlots i dont et the value for nextlots?

What does it print?
 
GumRai:
What does it print?
it prints 2015.09.15 21:47:39.687    2015.09.15 19:17  temp EURUSD,M15: 0.01
 all time
Reason: