EA meets order opening conditions, but doesn't trade - page 2

 

oops. i haven't noticed that u made a change and replacement in the code above i remembered SwingValue[0]

try

if (LastBarHigh < SwingValue[2] && Ask > SwingValue[2]) {SIGNAL_BUY = true;}
else if (LastBarLow > SwingValue[2] && Bid < SwingValue[2]) {SIGNAL_SELL = true;}
i'm not sure & didn't tested
 

zzuegg don't bother to backtest it because you don't have the file "ATR MA"

 

If anyone wants it, here's the ATR MA indicator. It's a very simple custom I made, of the ATR plus two of its moving averages. The histogram shows the difference between the first and second MAs. I find it very useful for day trading, provides an easy way of detecting when the market has begun to accelerate...

qjol, thanks for the suggestion, but that didn't work either =\

If you wanted to code the condition "buy when price crosses X line and sell when it crosses Y", how would you do it? I mean... this seems like an extremely simple thing to put into MQL4, it's strange that I'm having such a hard time with it.

Files:
atrtma.mq4  3 kb
 

i think

if (SwingValue[0] > SwingValue[1] && SwingValue[1] < SwingValue[2])
   {
   if (Ask > SwingValue[2])
      {
      BuySignal = true;
      }
else if (SwingValue[0] < SwingValue[1] && SwingValue[1] > SwingValue[2])
   {
   if (Bid < SwingValue[2])
      {
      SellSignal = true;
      }

not tested

 

After staring at the screen for hours, I adopted a convoluted work-around, which seems to have put a band-aid on the problem.

Instead of using Bid and Ask, I use the two 1-period SMAs, applied to High/Low; when they cross the swing level, Buy/Sell signals are triggered:

double  CurrentLow = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
double  CurrentHigh = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);

bool BuySignal, SellSignal;

if (SwingValue[0] > SwingValue[1] && SwingValue[1] < SwingValue[2])
   {
   if (CurrentHigh > SwingValue[2])
      {
      BuySignal = true;
      }
else if (SwingValue[0] < SwingValue[1] && SwingValue[1] > SwingValue[2])
   {
   if (CurrentLow < SwingValue[2])
      {
      SellSignal = true;
      }
   }
}

But now the EA isn't modifying the orders with SL/TP correctly, so all it's doing is placing one order and and then closing it when the testing period is finished.

Can't things just work properly, the first time around?! =(

Files:
 
         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         if(Ticket > 0) {

            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
                Print("BUY order opened : ", OrderOpenPrice());
                
                        } else {
                                Print("Error opening BUY order : ", GetLastError());
                        }
        }

         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }
i don't c here OrderModify() for TP & SL
 

Scroll down to the next section. I have an ECN broker so I can't pass SL/TP with the order.

for(int n = OrdersTotal()-1; n >= 0; n--)   {
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)    {
  
//------- Exit Long
    if(OrderType()==OP_BUY)     {
      if(OrderProfit()>=TakeProfitLong*OrderLots()
      || OrderProfit()<=-StopLossLong*OrderLots()
      )     {
        OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
        }
    }
 }
 
//------- Exit Short
    if(OrderType()==OP_SELL)    {
      if(OrderProfit()>=TakeProfitShort*OrderLots()
      || OrderProfit()<=-StopLossShort*OrderLots()
      )     {
        OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
      }
    }
 }

^-- The order modifying section from one of ubzen's EAs =)... not technically OrderModify()