Download MetaTrader 5
To add comments, please log in or register
You can test an application before buying it. Find out how to do that!
Martin
6
Martin 2014.06.05 08:28 
Hello lads, could somebody help me? I am totally novice with this.
I am just trying with tester this code and it is not working 100%, not every possible trade is executed. (I hope u can see, that I want to buy/sell when current price reaches min/max). Should I write some other conditions or something, or why it is not doing every trade?

void OnTick()
  {

   openOrder = false;
   
   //monitor open trades & place trailing stops
   for(counter=0;counter<OrdersTotal();counter++)   
   {
      OrderSelect(counter, SELECT_BY_POS, MODE_TRADES);     
      if (OrderMagicNumber() == magicNumber)
      {
         openOrder = true;
      }
   }


   if (!openOrder && Time[0] != previousSignal)
   {
      generateSignals();
      
      if (buySignal)
         placeBuy();
      else if (sellSignal)
         placeSell();
         
      previousSignal = Time[0];
   }

   
  }
//+------------------------------------------------------------------+


void generateSignals()
{
   buySignal = false;
   sellSignal = false;   
   
   min = NormalizeDouble(iLow(NULL,0,1),Digits);
   max = NormalizeDouble(iHigh(NULL,0,1),Digits);
   current = NormalizeDouble(MarketInfo(NULL,MODE_BID),Digits);
   
   if (min == current)
      buySignal = true;
   else if (max == current)
      sellSignal = true;
}

void placeBuy()
{
   RefreshRates();            
   ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,NormalizeDouble(Bid - SL,Digits),NormalizeDouble(Ask + TP,Digits),"buy",magicNumber,0,Green);  
   if(ticket<0)
   {
      Print("BUY failed with error #",GetLastError()," at ",Ask);
      return;
   }
}

void placeSell()
{
   RefreshRates();            
   ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,slippage,NormalizeDouble(Ask + SL,Digits),NormalizeDouble(Bid - TP,Digits),"sell",magicNumber,0,Red);  
   if(ticket<0)
   {
      Print("SELL failed with error #",GetLastError()," at ",Bid);
      return;
   }
Keith Watford
Moderator
7899
Keith Watford 2014.06.05 12:50  
matolfc:
Hello lads, could somebody help me? I am totally novice with this.
I am just trying with tester this code and it is not working 100%, not every possible trade is executed. (I hope u can see, that I want to buy/sell when current price reaches min/max). Should I write some other conditions or something, or why it is not doing every trade?


Bid or Ask may not be exactly equal to the trigger price and may just jump over it.

So don't use

if (min == current)

use

if (min <= current)

or

if (min - current >=0 && min - current < x)
whroeder1
13602
whroeder1 2014.06.06 18:03  
GumRai: Bid or Ask may not be exactly equal to the trigger price and may just jump over it.
Exactly. The == operand. - MQL4 forum
/
To add comments, please log in or register