How To use OrderLots() & OrderProfit() properly?

 

Hi all

I'm in need of help with the code below. I would like to open 2 more trades if/once they go in 5 pips profit.

#property strict

extern int MagicNumber=111;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
   {
    if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    Count++;
   }
  return(Count);
 } 
 
void Buy()
 {
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBarNext=iOpen(NULL,0,2);
  double LowBarNext=iLow(NULL,0,2);
  double HighBarNext=iHigh(NULL,0,2);
  
  double HaramiBuy=OpenBarNext>CloseBarNext&&CloseBar>OpenBar&&HighBarNext>HighBar&&OpenBarNext>CloseBar&&LowBarNext<LowBar;
  
  if(HaramiBuy)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
  } 
  
 } 
 
void Sell()
 {
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBarNext=iOpen(NULL,0,2);
  double LowBarNext=iLow(NULL,0,2);
  double HighBarNext=iHigh(NULL,0,2);
  
  double HaramiSell=OpenBarNext<CloseBarNext&&CloseBar<OpenBar&&HighBarNext<HighBar&&OpenBarNext<CloseBar&&LowBarNext>LowBar;
  
  if(HaramiSell)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
  }
  
 } 

void OnTick()
 {
   for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
   {
    bool OSProfit=OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES);
   }
   
   double InProfit=OrderLots()*50;
   
   if(AccountBalance()>10&&CountTrades()<1){Buy();}
   else if(OrderProfit()>InProfit&&CountTrades()<3){Buy();}
   
   if(AccountBalance()>10&&CountTrades()<1){Sell();}
   else if(OrderProfit()>InProfit&&CountTrades()<3){Sell();}
   
   for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   { 
    bool CloseTrade=false;
    int Type=OrderType();
    double Profit=OrderLots()*200;
    switch(Type)
    {
     case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     //Print("ErrorCode:",GetLastError());                         
    }
   }
  }
 }

I know noticed by using OrderLots()*50 is completely wrong. At times they would not open at all or open at 8 or 14 pips. I would like to know what functions are the correct one or other ways to do this the right way.

double InProfit=OrderLots()*50;
   
   if(AccountBalance()>10&&CountTrades()<1){Buy();}
   else if(OrderProfit()>InProfit&&CountTrades()<3){Buy();}
   
   if(AccountBalance()>10&&CountTrades()<1){Sell();}
   else if(OrderProfit()>InProfit&&CountTrades()<3){Sell();}
 

Hi Jack,

you should try something link this:

void OnTick()
{
  int CountProfit = 0;
  double InProfit=OrderLots()*50;

  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
   {
     if (OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES) == true)
     {
       if (OrderProfit() > InProfit) CountProfit++;
     }
   }

I think, this is something the way you want to do.

Best regards,

Werner

 
Werner Klehr:

Hi Jack,

you should try something link this:

I think, this is something the way you want to do.

Best regards,

Werner

Hi Werner, tried the code you posted, it still does the same thing. It will not open trades at 5 pips in profit but will open at 14 pips or not at all.
 

Hi Jack,

my code was just a suggestion how you can solve your problem. You have to
check the CountProfit variable and you should think about the way you calculate
the InProfit variable.
If you want to leave it that way, you should put if in the
for-loop before like this:

  int CountProfit = 0;

  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
   {
     if (OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES) == true)
     {
       double InProfit=OrderLots()*50;
       if (OrderProfit() > InProfit) CountProfit++;
     }
   }

Best regards,


 
Werner Klehr:

Hi Jack,

my code was just a suggestion how you can solve your problem. You have to
check the CountProfit variable and you should think about the way you calculate
the InProfit variable.
If you want to leave it that way, you should put if in the
for-loop before like this:

Best regards,


Will give it a try, thanks once again
Reason: