Creat a Buy every 5 pips

 
Hi,
How can I create a buy order every 5 pips?
Thanks
Brave0Heart
 
Something like the following perhaps, which is an EA that would make an order whenever it discovers a price rise of 5 pips or more.
double price = 0;
start() {
    if ( price == 0 || Bid < price ) {
        price = Bid;
    } else if ( ( Bid - price ) / Point ) >= 5 ) {
        OrderSend( ... );
        price = Bid;
    }
}
 
richplank wrote:
Something like the following perhaps, which is an EA that would make an order whenever it discovers a price rise of 5 pips or more.
double price = 0;
start() {
    if ( price == 0 || Bid < price ) {
        price = Bid;
    } else if ( ( Bid - price ) / Point ) >= 5 ) {
        OrderSend( ... );
        price = Bid;
    }
}


 
brave0heart wrote:
richplank wrote:
Something like the following perhaps, which is an EA that would make an order whenever it discovers a price rise of 5 pips or more.
double price = 0;
start() {
    if ( price == 0 || Bid < price ) {
        price = Bid;
    } else if ( ( Bid - price ) / Point ) >= 5 ) {
        OrderSend( ... );
        price = Bid;
    }
}
Great. Is there a way to send a message every time the price rises by 5 pips as well? Thanks so much.
Brave0Heart.


 

might need to put that previous price in a global var, and read it next time, as price would have no value each run.
double price = 0;
start() {
price=GlobalVariableGet("price");// get what you put in last trade
if ( price == 0 || Bid < price ) {
price = Bid;
} else if ( ( Bid - price ) / Point ) >= 5 ) {
OrderSend( ... );
price = Bid;
GlobalVariableSet("price",price); // set new price of last trade

}
}

 
oldman wrote:

might need to put that previous price in a global var, and read it next time, as price would have no value each run.
double price = 0;
start() {
price=GlobalVariableGet("price");// get what you put in last trade
if ( price == 0 || Bid < price ) {
price = Bid;
} else if ( ( Bid - price ) / Point ) >= 5 ) {
OrderSend( ... );
price = Bid;
GlobalVariableSet("price",price); // set new price of last trade

}
}

I'm not sure I understand. what is the difference between the above and something like this?
double price = 0;
double OffSet = 0;
price = Bid; // make price = bid
OffSet = MathAbs(Price - Bid); // to start with, the offset between the price and the bid will be <5
While (OffSet < 5) // Keep going as long as the offset < 5
{
Print("The Offset is ",OffSet); // just to show off,keep printing this
OffSet = MathAbs(Price - Bid); // every cycle refresh the offset
}
// now make an order because Offset must be >= 5

But my problem with the above is this sort of thing is CPU intensive, isn't it? How do indicators get around this problem. Is there some sort of function that tells us when the next tick has happened? If so, then we would simply check what it was.
Thanks so much, hoping you can help me clarify this soon.
Steve
 
brave0heart wrote:
oldman wrote:

might need to put that previous price in a global var, and read it next time, as price would have no value each run.
double price = 0;
start() {
price=GlobalVariableGet("price");// get what you put in last trade
if ( price == 0 || Bid < price ) {
price = Bid;
} else if ( ( Bid - price ) / Point ) >= 5 ) {
OrderSend( ... );
price = Bid;
GlobalVariableSet("price",price); // set new price of last trade

}
}

I'm not sure I understand. what is the difference between the above and something like this?
double price = 0;
double OffSet = 0;
price = Bid; // make price = bid
OffSet = MathAbs(Price - Bid); // to start with, the offset between the price and the bid will be <5
While (OffSet < 5) // Keep going as long as the offset < 5
{
Print("The Offset is ",OffSet); // just to show off,keep printing this
OffSet = MathAbs(Price - Bid); // every cycle refresh the offset
}
// now make an order because Offset must be >= 5

But my problem with the above is this sort of thing is CPU intensive, isn't it? How do indicators get around this problem. Is there some sort of function that tells us when the next tick has happened? If so, then we would simply check what it was.
Thanks so much, hoping you can help me clarify this soon.
Steve




Files:
crawler.mq4  2 kb
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   double Price = 0;
   double OffSet = 0;
   Price = Bid; // make price = bid
   OffSet = MathAbs(Price - Bid); // to start with, the offset between the price and the bid will be <5
   while(OffSet < 5) // Keep going as long as the offset < 5
   {
   Print("The Offset is ",OffSet); // just to show off,keep printing this
   OffSet = MathAbs(Price - Bid); // every cycle refresh the offset
   }
   // now make an order because Offset must be >= 5
   return(0);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
// the end.
 
This is 101 stuff for a computer nerd like myself :-) but the main difference between the first snippet that I dropped, and the one above is whether or not the start() method returns. As I understand it, normally the Bid and Ask remain constant throughout a start() method call, and are changed as caused by new ticks in between start() method calls. Therefore, the snippet above simply enters an "infinite loop"; the Bid variable is not changed, beacuse the start() function does not return.

In my first snippet, the Bid price of the start() invocation is stored away in a variable that is declared outside of functions. Such a declaration makes the variable "live" throughout the EA instance life-time, and in particular it is preserved between successive start() function invocations. More precisely, the last assignment made to it in one start() function call can be retrieved in the subsequent start() method call.

A global variable, as oldman suggest, could be useful, to make the value live and be accessible also between EA instances, but it is not needed for use within the same EA instance, and should even be avoided unless you do want interactions between EA instances.

Thus, (https://docs.mql4.com/runtime/start) when a "tick" arrives, the start() method is invoked, and it is expected to return so that when the next "tick" arrives, the start() method is invoked again. If the start() method has not returned when the next "tick" arrives, then your EA will fail to observe that "tick".
Reason: