EA Based on Custom Indicator

Post Reply
StarShines
Posts: 1
Joined: Tue Jan 08, 2013 9:28 am

EA Based on Custom Indicator

Post by StarShines »

Hi All !

I am new to this forum. I would like to thank Steve from the bottom of my heart for providing such a nice personalised dedicated forum for traders. Many many thanks to all contributors as well.

Well, recently I came across a custom indicator, for which I coded an EA (from forexeadvisor.com ) as I am not a MQL4 guy. Initially I coded a MACD from the website and then changed the EA for custom indicator. When I back test it, there is an error OrderModify Error 1, which I don't understand. Even when I put SL, TP and TS, some errors do come but if no SL, TP and TS, then it only opens one trade in back tests, which it closes at the end of the test.

The EA has been coded in such a way that if the custom indi is above 0, it will BUY and it will only close the BUY when the custom indi is below 0 (by the time it is above 0 and the BUY trade opens, and it comes below 0, we would have gained pips/made profits. Vice versa for SELL.

Since the custom indi is accurate, personally I think we don't need SL, TP and TS, nevertheless your attention is required in this regard also.

The custom indi is already in my experts/indicators directory, compiled. So no problems from the indi but the problem is coming from the EA. I am attaching the code here for experts' kind consideration:

Code: Select all

//+------------------------------------------------------------------+
// SUPER SONIC EA BASED ON CUSTOM INDICATOR
//+------------------------------------------------------------------+


//-----------------------------------------------------------------|-----+
// INPUTS                                                          |  1  |
//-----------------------------------------------------------------|-----+

extern int        MagicNumber    =  778   ;
extern double     Lots           =  0.1   ;
extern double     StopLoss       =  0   ;
extern double     TakeProfit     =  0   ;
extern int        TrailingStop   =  0    ;
extern int        Slippage       =  3     ;

//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;

      double SS1 = iCustom(Symbol(),0,"SS",0,1);
      double SS2 = iCustom(Symbol(),0,"SS",1,1);


     if(SS1>0) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,AdvancedMM(),Ask,Slippage,0,0,"SS BUY",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if(SS2<0) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,AdvancedMM(),Bid,Slippage,0,0,"SS SELL",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if(SS1<0) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if(SS2>0) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
double AdvancedMM()
{
 int i;
 double AdvancedMMLots = 0;
 bool profit1=false;
 int SystemHistoryOrders=0;
  for( i=0;i<OrdersHistoryTotal();i++)
  {  OrderSelect(i,SELECT_BY_POS ,MODE_HISTORY);
     if (OrderMagicNumber()==MagicNumber) SystemHistoryOrders++;
  }
 bool profit2=false;
 int LO=0;
 if(SystemHistoryOrders<2) return(Lots);
 for( i=OrdersHistoryTotal()-1;i>=0;i--)
  {
     if(OrderSelect(i,SELECT_BY_POS ,MODE_HISTORY))
     if (OrderMagicNumber()==MagicNumber) 
     {
        if(OrderProfit()>=0 && profit1) return(Lots);
        if( LO==0)
        {  if(OrderProfit()>=0) profit1=true;
           if(OrderProfit()<0)  return(OrderLots());
           LO=1;
        }
        if(OrderProfit()>=0 && profit2) return(AdvancedMMLots);
        if(OrderProfit()>=0) profit2=true;
        if(OrderProfit()<0 ) 
        {   profit1=false;
            profit2=false;
            AdvancedMMLots+=OrderLots();
        }
     }
  }
 return(AdvancedMMLots);
}
Can an expert kindly help me removing the errors from the code above?

Your help is highly appreciated.

Thanks all

Regards,


StarShines
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: EA Based on Custom Indicator

Post by SteveHopwood »

Instead of placing code that coders have to copy/paste into a new shell, upload your source code as an attachment. That way, if a coder takes a passing interest, s/he is not offput by the faff of having to set up a new ea then copying your code.

You will find that your post one is fully editable and configurable, so return to it and repost. Then add a subsequent post describing what you have done - this is how I run my threads and it appears to work.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Post Reply

Return to “Coders Hangout”