Need help by programing

 

Hi,


i need a small routine will count my gain and according to part of the gain close open position or part of it,  reset the gain counter and from the point of closed position count again gain till again will rich opportunity to close open position.

I tried different opportunity don't work at all what i have as base is:

//+-----------------------------------------------------------------+
//| Localise lowest position                                   |
//+-----------------------------------------------------------------+

 {for (y = 0; y < OrdersTotal(); y++)
    {if(!OrderSelect (0, SELECT_BY_POS, MODE_TRADES)) continue;
       LowPos=ND((OrderProfit()*(-1)),1);
       LowPosTicket=(OrderTicket());
    }
 }

 

---- ok to make it easier temporary i choose the oldest position

 

//+-----------------------------------------------------------------+
//| History of Trades                                             |
//+-----------------------------------------------------------------+

 {for(y=0;y<OrdersHistoryTotal();y++)
    {if(!OrderSelect(y,SELECT_BY_POS,MODE_HISTORY)) continue;
       {HProfit+=OrderProfit()+OrderCommission()+OrderSwap();
       }
    }
 }

 

--- here is counting the gain from closed positions no mater gain or lost to build a budget for closing position

 

//+-----------------------------------------------------------------+
//| Close latest/lowest position according to gain                |
//+-----------------------------------------------------------------+

 {if(LowPos<=(HProfit/3))
    {OrderClose(LowPosTicket,OrderLots(),Bid,0,0);
       HProfit=0;
    }

 }

--- here after closing position that is equal the budget i build. Actually logically its cant work cause is going back to history and counting again from 0 till end of history.

I tried this in history so my counting of history will be every time from the last position and history i will reset by closing position... but its dont work.

int Count;

 {for(y=Count;y<OrdersHistoryTotal();y++)
    {if(!OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY)) continue;

       {HProfit+=OrderProfit()+OrderCommission()+OrderSwap();

        Count++;

       }
    }
 }

I treed even that

    //+-----------------------------------------------------------------+
    //| History of Trades                                               |
    //+-----------------------------------------------------------------+

   {  for(y=0;y<OrdersHistoryTotal();y++)
      {  if(!OrderSelect(y,SELECT_BY_POS,MODE_HISTORY)) continue;
      {  if(TimeCurrent()>closedate)
         {  HProfit+=OrderProfit()+OrderCommission()+OrderSwap();           
         }
         }
      }
   }

    //+-----------------------------------------------------------------+
    //| Close latest/lowest position according to gain                  |
    //+-----------------------------------------------------------------+
   if(CloseLowestByGain==1)
   {  if(CbB>1&&LowPos<=(HProfit/3))
      {  OrderClose(LowPosTicket,OrderLots(),Bid,0,0);
           HProfit=0;
           closedate=OrderCloseTime();
        }

   }

i thought it will work with time. A kind of gate  starting to count profit from a moment of closing the position.... but again dont work.


So have somebody an idea how can i solve the problem ?


Cu Rosco

Reason: