help with EA running with iCustom

 

Hello, I'm trying to get an EA to run off of buffers from a custom indicator.

The indicator creates arrows indicating long and short, and closes the trades with arrows indicating close. The SL is drawn on the chart with a buffer as well.

I want the EA to open trades when a buy/sell arrow is printed and close them when a close arrow is printed. The SL is whatever value the line is when it is drawn with the indicator 

Here is the code I have currently, however it doesn't work. I was wondering if anyone could help point out where the kinks are in the system as it compiles fine.


//+------------------------------------------------------------------+
//|                                                       ABC EA.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

double lots = 0.10;
double order;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick()
  {
      for(int i=0;i<OrdersTotal();i++)
         {
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            
         }
      double longenter = iCustom(Symbol(),0,"PLANextEdit",3,0);
      double shortenter = iCustom(Symbol(),0,"PLANextEdit",5,0);
      double longexit = iCustom(Symbol(),0,"PLANextEdit",2,0);
      double shortexit = iCustom(Symbol(),0,"PLANextEdit",4,0);
      double longstop = iCustom(Symbol(),0,"PLANextEdit",0,0);
      double shortstop = iCustom(Symbol(),0,"PLANextEdit",1,0);
      
      if(longenter !=EMPTY_VALUE && i==0)
         {
            int Order=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,longstop,NULL,NULL,0,0,Green);
            if(Order==0)
               {
                  Print("Error Opening Buy Order: ",GetLastError());
                  
               }
            else if(Order>0)
               {
                  Print("Buy Order Opened");
                  
               }
         }
      if(shortenter !=EMPTY_VALUE && i==0)
         {
            Order=OrderSend(_Symbol,OP_BUY,0.10,Bid,3,shortstop,NULL,NULL,0,0,Red);
            if(Order==0)
               {
                  Print("Error Opening Sell Order: ",GetLastError());
                  
               }
            else if(Order>0)
               {
                  Print("Sell Order Opened");
                  
               }
         }
      
      if(shortexit !=EMPTY_VALUE && i==0)
      {
         OrderClose(Order,0.10,Ask,3,Black);
      }
      
      if(longexit !=EMPTY_VALUE && i==0)
      {
         OrderClose(Order,0.10,Bid,3,Black);
      }
      

  }
//+

thanks in advance for any help

 
  1. As previously told: Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor
  2. Don't double post! You already had another thread open.

              General rules and best pratices of the Forum. - General - MQL5 programming forum

  3.       for(int i=0;i<OrdersTotal();i++)
             {
                OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
                
             }
    
    What do you think that is doing? Using OrdersTotal (or OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  4.             int Order=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,longstop,NULL,NULL,0,0,Green);
                if(Order==0)
    
    Perhaps you should read the manual. Your if will never be true.

  5. NULL is not a valid price. Don't use NULL.
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
 

This here seems to be ok:

      double longenter = iCustom(Symbol(),0,"PLANextEdit",3,0);
      double shortenter = iCustom(Symbol(),0,"PLANextEdit",5,0);
      double longexit = iCustom(Symbol(),0,"PLANextEdit",2,0);
      double shortexit = iCustom(Symbol(),0,"PLANextEdit",4,0);
      double longstop = iCustom(Symbol(),0,"PLANextEdit",0,0);
      double shortstop = iCustom(Symbol(),0,"PLANextEdit",1,0);

Provided the indicator buffers match.

But you need to align this block with the rest of your code. That is first, you get the indicator values to set up the flags. And second, you walk through the open orders to find out which one to close. And finally, you open new orders.

So this all makes up 3 blocks:

// Block 1: get the indicator buffer values and set the flags

string indicator="PLANextEdit";
int shift=0;
int mode;
double longenter  = iCustom(_Symbol,_Period,indicator,mode=3,shift);
double shortenter = iCustom(_Symbol,_Period,indicator,mode=5,shift);
double longexit   = iCustom(_Symbol,_Period,indicator,mode=2,shift);
double shortexit  = iCustom(_Symbol,_Period,indicator,mode=4,shift);
double longstop   = iCustom(_Symbol,_Period,indicator,mode=0,shift);
double shortstop  = iCustom(_Symbol,_Period,indicator,mode=1,shift);

// Block 2: go through all open orders and close where required

for(int i=OrdersTotal()-1;i>=0;i--)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()==MagicID)
     {
      if(longexit!=EMPTY_VALUE && OrderType()==OP_BUY)
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Black);
        }
      if(shortexit!=EMPTY_VALUE && OrderType()==OP_SELL)
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,Black);
        }
     }
  }

// Block 3: open orders where indicated

int ticket;
if(longenter!=EMPTY_VALUE)
  {
    ticket=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,longstop,0,"Buy",0,0,Green);
    if(ticket<0)
      {
       Print("Error Opening Buy Order: ",GetLastError());
      }
    else
      {
       Print("Buy Order Opened");
      }
  }
if(shortenter!=EMPTY_VALUE)
  {
   ticket=OrderSend(_Symbol,OP_SELL,0.10,Bid,3,shortstop,0,"Sell",0,0,Red);
   if(ticket<0)
     {
      Print("Error Opening Sell Order: ",GetLastError());
     }
   else
     {
      Print("Sell Order Opened");
     }
  }
Reason: