Forex



Go Back   Forex Trading > Downloads > Expert Advisors - Metatrader 4






Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2008, 11:56 AM
Junior Member
 
Join Date: Nov 2008
Posts: 4
davdkh is on a distinguished road
Help needed to this powerful system.

Hi I have tried to make an EA for my system, let me try to explain:

I use the following indicators:

MACD(12,26,9)
Stoch(40,1,7)
EMA(60) applied to the Stoch-value

The buy signal is when:
The current MACD bar has a higher value than the previous bar and the EMA(60) is crossing the Stoch.

The sell signal is the opposite of the buy signal.

I have tried to code the EA so when the buy signal is showing it's supposed to close all sell-positions, but it's not working probably... The only way I could find the EMA(60) of the stoch-values was to make a indicator, which I called "Stoch_MA".

Can anyone please look at my code and see what's wrong? - and possibly improve it?

Thanks

The EA:
Code:
//+------------------------------------------------------------------+
//|                                                    davdkh v3.mq4 |
//|                                          davdkh Copyright © 2008 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link      ""

extern double    TakeProfit=10;
extern double    Lots=0.2;
extern double    StopLoss=30;
extern double    Risk=0.05;

string NameExpert="davdkh is thinking for you!";
int klartilstart;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Comment("davdkh is thinking for you!");
   klartilstart=0;
   
      //----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      
   string ResultSignal=VerifySignal();
   if(ResultSignal!="")
   {
      if(klartilstart==0)
      {
      Comment("davdkh has found a entry... is proceding");
         if(ResultSignal=="buy")
            {
            OpenBuy();
            ExitSell();
                        
            }
         else if (ResultSignal=="sell")
            {
            OpenSell();
            ExitBuy();
            }
                     
      }
      else Comment("davdkh is analyzing");
      
   }
   else
   {
      klartilstart=0;
      Comment("davdkh is analyzing");
      Modify();
   }

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
string VerifySignal()
{
   double rsi[];
  
   for(int i=0 ; i<=60 ; i++)
    {
      rsi[i]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,i);
    }
   
   double MA=iCustom(Symbol(),0,"Stoch_MA",1,0);
   double Stoch=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,0);
   double MACDc=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,0);
   double MACDp=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,2);
   
   if(MA==Stoch && MACDc>MACDp)
      return("buy");
   if(MA==Stoch && MACDc<MACDp)
      return("sell");
    
   return("");
}
//+------------------------------------------------------------------+
void OpenBuy()
{
   while(true)
   {
      RefreshRates();
      OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, NameExpert,0,0,Blue);
      
         break;
   }
   klartilstart++;
}
//+------------------------------------------------------------------+
void OpenSell()
{
   while(true)
   {
      RefreshRates();
      OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, NameExpert,0,0,Red);
         break;
   }
   klartilstart++;
}            
//+------------------------------------------------------------------+   
double   size()
{
double mr = (MarketInfo(Symbol(), MODE_MARGINREQUIRED)/10);
double mRisk = MathFloor((AccountBalance() / mr) * Risk);
double ls = mRisk * 0.1;
return(ls); 
}
//+------------------------------------------------------------------+
void Modify()
{
RefreshRates();
   bool   result;
   double stop_loss, point, TP;
   int    cmd,total,error;
//----
   total=OrdersTotal();
   point=MarketInfo(Symbol(),MODE_POINT);
//----
for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd=OrderType();
         TP=OrderTakeProfit();
         //---- buy or sell orders are considered
         if(cmd==OP_BUY || cmd==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd==OP_BUY) stop_loss=OrderOpenPrice();
               else            stop_loss=OrderOpenPrice();
               result=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss+5*Point,OrderOpenPrice()+TakeProfit*Point,0,CLR_NONE);
               if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
               else error=0;
               if(error==135) RefreshRates();
               else break;
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
//----
 return(0); 
//----
}
void ExitBuy()
{
   while(true)
   {
   RefreshRates();
   bool   result2;
   double point2;
   int    cmd2,total2,error2;
//----
   total2=OrdersTotal();
   point2=MarketInfo(Symbol(),MODE_POINT);
   
//----
   for(int e=0; e<total2; e++)
     {
      if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd2=OrderType();
         //---- buy or sell orders are considered
         if(cmd2==OP_BUY || cmd2==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd2==OP_BUY)
               {
               result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
               if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
               else error2=0;
               if(error2==135) RefreshRates();
               else break;
               }
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
     klartilstart--;
     }
}

void ExitSell()
{
while(true)
{
   RefreshRates();
   bool   result2;
   double point2;
   int    cmd2,total2,error2;
//----
   total2=OrdersTotal();
   point2=MarketInfo(Symbol(),MODE_POINT);
   
//----
for(int e=0; e<total2; e++)
     {
      if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd2=OrderType();
         //---- buy or sell orders are considered
         if(cmd2==OP_BUY || cmd2==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd2==OP_BUY)
               {
               result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
               if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
               else error2=0;
               if(error2==135) RefreshRates();
               else break;
               }
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
     }
     klartilstart--;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 12-01-2008, 11:57 AM
Junior Member
 
Join Date: Nov 2008
Posts: 4
davdkh is on a distinguished road
and the indicator:

And the indicator:

Code:
   //+------------------------------------------------------------------+
//|                                                     Stoch_MA.mq4 |
//|                                          davdkh Copyright © 2008 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double ExtMapBuffer1[1000];
double ExtMapBuffer2[1000];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorDigits(2);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   string short_name = "Stoch_MA is running";
   IndicatorShortName(short_name);
   
   
   
//---- 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
int bar, limit;
   
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-IndicatorCounted();
    
   
   for(bar=0; bar<limit; bar++)
   ExtMapBuffer1[bar]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,bar);
   
   for(bar=0; bar<limit; bar++)
   ExtMapBuffer2[bar]=iMAOnArray(ExtMapBuffer1,Bars,60,0,MODE_EMA,bar);
   
   
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 12-04-2008, 02:20 AM
increase's Avatar
Senior Member
 
Join Date: May 2006
Posts: 837
increase is on a distinguished road
Does anyone have an EA that uses a bar indicator, to buy and sell when the bar changes colour>
__________________
New to Forex? Get all you need for Free at my site Click Here
You can also get my Hot Forex System (scroll to the bottom of the page): Click Here
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 12-04-2008, 02:51 AM
Senior Member
 
Join Date: Aug 2006
Posts: 456
RickW00716 is on a distinguished road
Very interesting!

Quote:
Originally Posted by davdkh View Post
Hi I have tried to make an EA for my system, let me try to explain:

I use the following indicators:

MACD(12,26,9)
Stoch(40,1,7)
EMA(60) applied to the Stoch-value

The buy signal is when:
The current MACD bar has a higher value than the previous bar and the EMA(60) is crossing the Stoch.

The sell signal is the opposite of the buy signal.

I have tried to code the EA so when the buy signal is showing it's supposed to close all sell-positions, but it's not working probably... The only way I could find the EMA(60) of the stoch-values was to make a indicator, which I called "Stoch_MA".

Can anyone please look at my code and see what's wrong? - and possibly improve it?

Thanks

The EA:
Code:
//+------------------------------------------------------------------+
//|                                                    davdkh v3.mq4 |
//|                                          davdkh Copyright © 2008 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link      ""

extern double    TakeProfit=10;
extern double    Lots=0.2;
extern double    StopLoss=30;
extern double    Risk=0.05;

string NameExpert="davdkh is thinking for you!";
int klartilstart;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Comment("davdkh is thinking for you!");
   klartilstart=0;
   
      //----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      
   string ResultSignal=VerifySignal();
   if(ResultSignal!="")
   {
      if(klartilstart==0)
      {
      Comment("davdkh has found a entry... is proceding");
         if(ResultSignal=="buy")
            {
            OpenBuy();
            ExitSell();
                        
            }
         else if (ResultSignal=="sell")
            {
            OpenSell();
            ExitBuy();
            }
                     
      }
      else Comment("davdkh is analyzing");
      
   }
   else
   {
      klartilstart=0;
      Comment("davdkh is analyzing");
      Modify();
   }

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
string VerifySignal()
{
   double rsi[];
  
   for(int i=0 ; i<=60 ; i++)
    {
      rsi[i]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,i);
    }
   
   double MA=iCustom(Symbol(),0,"Stoch_MA",1,0);
   double Stoch=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,0);
   double MACDc=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,0);
   double MACDp=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,2);
   
   if(MA==Stoch && MACDc>MACDp)
      return("buy");
   if(MA==Stoch && MACDc<MACDp)
      return("sell");
    
   return("");
}
//+------------------------------------------------------------------+
void OpenBuy()
{
   while(true)
   {
      RefreshRates();
      OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, NameExpert,0,0,Blue);
      
         break;
   }
   klartilstart++;
}
//+------------------------------------------------------------------+
void OpenSell()
{
   while(true)
   {
      RefreshRates();
      OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, NameExpert,0,0,Red);
         break;
   }
   klartilstart++;
}            
//+------------------------------------------------------------------+   
double   size()
{
double mr = (MarketInfo(Symbol(), MODE_MARGINREQUIRED)/10);
double mRisk = MathFloor((AccountBalance() / mr) * Risk);
double ls = mRisk * 0.1;
return(ls); 
}
//+------------------------------------------------------------------+
void Modify()
{
RefreshRates();
   bool   result;
   double stop_loss, point, TP;
   int    cmd,total,error;
//----
   total=OrdersTotal();
   point=MarketInfo(Symbol(),MODE_POINT);
//----
for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd=OrderType();
         TP=OrderTakeProfit();
         //---- buy or sell orders are considered
         if(cmd==OP_BUY || cmd==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd==OP_BUY) stop_loss=OrderOpenPrice();
               else            stop_loss=OrderOpenPrice();
               result=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss+5*Point,OrderOpenPrice()+TakeProfit*Point,0,CLR_NONE);
               if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
               else error=0;
               if(error==135) RefreshRates();
               else break;
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
//----
 return(0); 
//----
}
void ExitBuy()
{
   while(true)
   {
   RefreshRates();
   bool   result2;
   double point2;
   int    cmd2,total2,error2;
//----
   total2=OrdersTotal();
   point2=MarketInfo(Symbol(),MODE_POINT);
   
//----
   for(int e=0; e<total2; e++)
     {
      if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd2=OrderType();
         //---- buy or sell orders are considered
         if(cmd2==OP_BUY || cmd2==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd2==OP_BUY)
               {
               result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
               if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
               else error2=0;
               if(error2==135) RefreshRates();
               else break;
               }
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
     klartilstart--;
     }
}

void ExitSell()
{
while(true)
{
   RefreshRates();
   bool   result2;
   double point2;
   int    cmd2,total2,error2;
//----
   total2=OrdersTotal();
   point2=MarketInfo(Symbol(),MODE_POINT);
   
//----
for(int e=0; e<total2; e++)
     {
      if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
        {
         //---- print selected order
         OrderPrint();
         cmd2=OrderType();
         //---- buy or sell orders are considered
         if(cmd2==OP_BUY || cmd2==OP_SELL)
           {
            //---- modify first market order
            while(true)
              {
               if(cmd2==OP_BUY)
               {
               result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
               if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
               else error2=0;
               if(error2==135) RefreshRates();
               else break;
               }
              }
             //---- print modified order (it still selected after modify)
             OrderPrint();
             break;
           }
        }
      else { Print( "Error when order select ", GetLastError()); break; }
     }
     }
     klartilstart--;
}
Are the stochastic settings %K..40 %D..1 slowing..7 ?

What timeframe do you use this on?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 12-08-2008, 01:34 PM
Junior Member
 
Join Date: Nov 2008
Posts: 4
davdkh is on a distinguished road
I use it on timeframe h1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CCI + MACD MQL help needed for trading system european Indicators - Metatrader 4 4 10-15-2009 10:22 PM
Powerful Fib Based System - by DAZfx DAZFX Suggestions for Trading Systems 21 01-21-2009 03:17 AM
Successful Manual System - EA Needed Please fxwealth Expert Advisors - Metatrader 4 5 09-29-2008 05:00 AM
A powerfull trading system(Programer needed) hany mohssen Suggestions for Trading Systems 1 08-23-2007 09:21 PM
metastock 9 trading system needed shalmahe Questions 2 01-21-2006 02:30 AM


All times are GMT. The time now is 09:00 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.