Grid Problem

 

Hi all

I want to open the 3rd trade at 100 pips. Below the code I've shown it will not open the 3rd trade at 100 pips, instead it will open the 2nd trade at 50 pips which is correct. I would like to know what I'm I doing wrong. If you know the problem  please help!

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 EngulfingBuy=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar>OpenBarNext&&OpenBar<CloseBar&&OpenBarNext>CloseBarNext;
  double HaramiBuy=OpenBarNext>CloseBarNext&&CloseBar>OpenBar&&HighBarNext>HighBar&&OpenBarNext>CloseBar&&LowBarNext<LowBar;
  
  static double BuyPrice=0,BuyPrice2=0;
  int LowestCandle=iLowest(Symbol(),0,MODE_LOW,50,0);
  
  if(Ask>=BuyPrice&&BuyPrice2)
  if(Ask>LowestCandle)
  if(HaramiBuy)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   BuyPrice=Ask+50*_Point;
   BuyPrice2=Ask+100*_Point;
  }
  
  if(Ask>=BuyPrice&&BuyPrice2)
  if(Ask>LowestCandle)
  if(EngulfingBuy)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   BuyPrice=Ask+50*_Point;
   BuyPrice2=Ask+100*_Point;
  }
  
 }
 
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 EngulfingSell=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar<OpenBarNext&&OpenBar>CloseBar&&OpenBarNext<CloseBarNext;
  double HaramiSell=OpenBarNext<CloseBarNext&&CloseBar<OpenBar&&HighBarNext<HighBar&&OpenBarNext<CloseBar&&LowBarNext>LowBar;
  
  static double SellPrice=0,SellPrice2=0;
  int HighestCandle=iHighest(Symbol(),0,MODE_HIGH,50,0);
  
  if((Bid<=SellPrice&&SellPrice2)||(SellPrice==0))
  if(Bid<HighestCandle)
  if(HaramiSell)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
   SellPrice=Bid-50*_Point;
   SellPrice2=Bid-100*_Point;
  }
  
  if((Bid<=SellPrice&&SellPrice2)||(SellPrice==0))
  if(Bid<HighestCandle)
  if(EngulfingSell)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
   SellPrice=Bid-50*_Point;
   SellPrice2=Bid-100*_Point;
  } 
  
 }
 
void OnTick()
 {
  if(AccountBalance()>10&&CountTrades()<1){Buy();}
  else if(CountTrades()<2){Buy();}
  else if(CountTrades()<3){Buy();}

  if(AccountBalance()>10&&CountTrades()<1){Sell();}
  else if(CountTrades()<2){Sell();}
  else if(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;
                             
    }
   }
  }
   
 }
 

Hi Jack,

i will try to help you - there are some logical problems with your Buy() and Sell() functions:

int LowestCandle=iLowest(Symbol(),0,MODE_LOW,50,0);

gives you the offset of the lowest candle, but it makes no sense to compare it with ask:

if(Ask>LowestCandle)

so you have to get the value of the lowest candle first and compare it then:

int LowestCandle=iLowest(Symbol(),0,MODE_LOW,50,0);
double LowesValue=Low[LowestCandle];
if(Ask>LowesValue)

If you want to check if Ask is greater then BuyPrice and BuyPrice2 you should use:

if(Ask>=BuyPrice && Ask>=BuyPrice2)

Your code will be cleaner if you declare EngulfingBuy and HaramiBuy as bool.
I hope, this will help you.

Best regards

 

Hi Werner

I changed as you have explained but opens 2 trades both at 100 pips instead of 2nd trade 50 pips & then 3rd trade at 100 pips.

static double BuyPrice=0,BuyPrice2=0;
int LowestCandle=iLowest(Symbol(),0,MODE_LOW,50,0);
double LowValue=Low[LowestCandle];

 HaramiBuy=true;
 if(Ask>=BuyPrice&&Ask>=BuyPrice2)
 if(Ask>=LowValue)
 if(HaramiBuy)
 {
  int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
  BuyPrice=Ask+50*_Point;
  BuyPrice2=Ask+100*_Point;
 }

EngulfingBuy=true;
if(Ask>=BuyPrice&&Ask>=BuyPrice2)
if(Ask>=LowValue)
if(EngulfingBuy)
{
 int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
 BuyPrice=Ask+50*_Point;
 BuyPrice2=Ask+100*_Point;
}

 //Sell
 static double SellPrice=0,SellPrice2=0;
 int HighestCandle=iHighest(Symbol(),0,MODE_HIGH,50,0);
 double HighValue=High[HighestCandle];
  
 HaramiSell=true;
 if((Bid<=SellPrice&&Bid<=SellPrice2)||(SellPrice==0&&SellPrice2))
 if(Bid<HighValue)

 if(HaramiSell)
 {
  int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
  SellPrice=Bid-50*_Point;
  SellPrice2=Bid-100*_Point;
 }

 EngulfingSell=true;
 if((Bid<=SellPrice&&Bid<=SellPrice2)||(SellPrice==0&&SellPrice2))
 if(Bid<HighValue)
 if(EngulfingSell)
 {
  int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
  SellPrice=Bid-50*_Point;
  SellPrice2=Bid-100*_Point;
 } 

  If there's a problem please help.

 

Your logic seems wrong.

Let's check the buy section for example.

First run of the function , BuyPrice & BuyPrice2 = 0 , so

if(Ask>=BuyPrice&&Ask>=BuyPrice2)

this is true , and you have your first buy. then, both of them get values :

 BuyPrice=Ask+50*_Point;
 BuyPrice2=Ask+100*_Point;

the second time (when Ask price reaches 50 point higher)  this is not true obviously :

if(Ask>=BuyPrice&&Ask>=BuyPrice2)

because ask is greager or equal to buyprice , not buyprice2! so it waill wait until it reaches to 100 Point , then this condition will be true and then you'll have your 2nd and 3rd buy with same price.

 

So my question is how do i change it to 2nd trade 50 pips & then 3rd trade 100 pips. I tried to do as follows:

HaramiBuy=true;
if(Ask>=BuyPrice)//&&Ask>=BuyPrice2)
if(Ask>=LowValue)
if(HaramiBuy)
{
 int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
 BuyPrice=Ask+50*_Point;
 BuyPrice2=Ask+100*_Point;
}
This opens the 2nd trade at 50 pips(Which is correct) & 3rd trade after 50 pips instead of 100 pips. I want the 3rd trade to open after 100 pips not 50.
 

you could use OrderOpenPrice() from your last position and then add  X points to it for new position price (instead of using ask price )

or simply delete the BuyPrice2 variable and just use BuyPrice. then your first position open by ask price , after that BuyPrice is ask + 50 , in second run , your position will open at 50 and then agian , buy price is ask + 50 (which will be your old ask price + 100 ) and then you'll have 3rd position with 100 pips.

 
Jack Buda: This opens the 2nd trade at 50 pips(Which is correct) & 3rd trade after 50 pips instead of 100 pips. I want the 3rd trade to open after 100 pips not 50.
BuyPrice=Ask+50*_Point;
 BuyPrice2=Ask+100*_Point;

A PIP is not a point.

PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum 2014.08.03

 
William Roeder:

A PIP is not a point.

PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum 2014.08.03

  I see, correct me if I'm wrong. Is this function a PIP?

double Pips=0;
double TickSize=MarketInfo(Symbol(),MODE_TICKSIZE);
if(TickSize==0.00001||0.001)
Pips=TickSize*10;
else Pips=TickSize;
 
Jack Buda:  I see, correct me if I'm wrong. Is this function a PIP?
  1. It tries to be, but is broken.
    Zero is false, non-zero is true
    if(TickSize==0.00001||0.001)
    Equivalent.
    if(TickSize==0.00001||true)
    anything or true equals true.
    if(true)

  2. Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017.02.09)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018.01.15)

 
William Roeder:
  1. It tries to be, but is broken.
    Zero is false, non-zero is true
    Equivalent.
    and anything or true equals true.

  2. Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017.02.09)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018.01.15)

Thank you William, won't be using Point anymore.
Reason: