MQL4 - automated forex trading   /  

Forum

Creat a Buy every 5 pips

Back to topics list To post a new topic, please log in or register

avatar
28
brave0heart 2006.10.07 21:39 
Hi,
How can I create a buy order every 5 pips?
Thanks
Brave0Heart
Idleness is the Stimulus to Progress. Semiautomatic Marking a Template

Idleness is the Stimulus to Progress. Semiautomatic Marking a Template

Among the dozens of examples of how to work with charts, there is a method of manual marking a template. Trend lines, channels, support/resistance levels, etc. are imposed in a chart. Surely, there are some special programs for this kind of work. Everyone decides on his/her own which method to use. In this article, I offer you for your consideration the methods of manual marking with subsequent automating some elements of the repeated routine actions.


avatar
155
richplank 2006.10.08 04:33 
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;
    }
}



avatar
28
brave0heart 2006.10.08 07:58 
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;
    }
}




avatar
28
brave0heart 2006.10.08 08:01 
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.




avatar
155
richplank 2006.10.08 08:10 

avatar
120
oldman 2006.10.10 02:13 

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

}
}


avatar
28
brave0heart 2006.10.10 06:17 
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





avatar
28
brave0heart 2006.10.10 06:31 
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





Attached files:
  crawler.mq4 (1.50 KB)

avatar
28
brave0heart 2006.10.10 06:31 
//+------------------------------------------------------------------+
//| 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.


avatar
155
richplank 2006.10.10 11:31 
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, (http://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".

Back to topics list  

To add comments, please log in or register