• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 9:59pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 9:59pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Simple Reversal Spotting Trades (MA/Elliott Wave Osc) 1 reply

Trend-spotting 151 replies

Spotting the best entry point on M1 6 replies

Spotting patterns with camera 9 replies

Trend spotting 42 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 2
Attachments: Need Help Spotting Coding Error
Exit Attachments
Tags: Need Help Spotting Coding Error
Cancel

Need Help Spotting Coding Error

  • Post #1
  • Quote
  • First Post: Sep 19, 2009 10:36pm Sep 19, 2009 10:36pm
  •  simran99
  • | Joined Sep 2009 | Status: Member | 66 Posts
I programmed this EA to (close short trade and) buy when price closes above 5 ema and (close long trade and) sell when price closes below 5 ema.

The logic: (From MetaStock)
Close short: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Enter long: CLOSE > Ref(Mov(CLOSE,5,E),-1)

Close long: CLOSE < Ref(Mov(CLOSE,5,E),-1)
Enter short: CLOSE < Ref(Mov(CLOSE,5,E),-1)

And If trend remains the same, do nothing.

There must be some error in the code (Mql 4 for metatrader) because this is how the backtesting went (See Attached Picture).

Anyone familiar with this error ? Can anyone correct the code ? Thanks.

Inserted Code
//+------------------------------------------------------------------+
//|                                                        MA-EA.mq4 |
//|                                         Copyright © 2009, Simran |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Simran"
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int       MA_Method=2;
extern int       MA_Period=5;
extern int       Lots=1;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int BarsCount = 0;

//----
   
int start()
{
  if (Bars > BarsCount)
  {
    double MA1 = iMA(NULL,0,MA_Period,0,MA_Method,0,1);
    double MA2 = iMA(NULL,0,MA_Period,0,MA_Method,0,2);
    
    double C1 = iClose(NULL,0,1);
    double C2 = iClose(NULL,0,2);
    
  
  if ((C2<MA2 && C1<MA1)||(C2>MA2 && C1>MA1)) //No trend change
   {
   return; //Do nothing. Just wait for opposing signal.
   }
//---------------------------------------------------------------------  
 
  if (C2<MA2 && C1>MA1) //Trend change from down to up.
   {
      int total = OrdersTotal(); //Script to close all open orders.
      for(int i=total-1;i>=0;i--)
      {
        OrderSelect(i, SELECT_BY_POS);
        int type   = OrderType();
        bool result = false;
    
        switch(type)
        {
          //Close opened long positions
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                              break;
          //Close opened short positions
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        }
    
        if(result == false)
        {
          Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          Sleep(3000);
        }  
     }      //End of Script to close all orders.
     
      int ticket; //Place Buy Order
         RefreshRates();
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"Buy Order",9999,0,Green);
         if(ticket<0)
           {
            Print("Buy Order failed with error #",GetLastError());
           }
    return;
   }
 
 //---------------------------------------------------------
 
   if(C2>MA2 && C1<MA1) //Trend change from up to down.
    {
      total = OrdersTotal(); //Script to close all open orders.
      for(i=total-1;i>=0;i--)
      {
        OrderSelect(i, SELECT_BY_POS);
        type   = OrderType();
        result = false;
    
        switch(type)
        {
          //Close opened long positions
          case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                              break;
          //Close opened short positions
          case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        }
    
        if(result == false)
        {
          Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          Sleep(3000);
        }  
     }      //End of Script to close all orders.
     
      //Place Sell Order
         RefreshRates();
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+1000*Point,Bid-1000*Point,"Sell Order",9999,0,Green);
         if(ticket<0)
           {
            Print("Sell Order failed with error #",GetLastError());
           }
    return;
    }
       
    BarsCount = Bars;
  }
   return(0);
}
   
  
//+------------------------------------------------------------------+[ATTACH]318015[/ATTACH]
Attached Image (click to enlarge)
Click to Enlarge

Name: Capture.PNG
Size: 34 KB
  • Post #2
  • Quote
  • Sep 20, 2009 2:51am Sep 20, 2009 2:51am
  •  simran99
  • | Joined Sep 2009 | Status: Member | 66 Posts
29 views and no replies ? Someone pls. help...

Thanks.
 
 
  • Post #3
  • Quote
  • Sep 20, 2009 10:55am Sep 20, 2009 10:55am
  •  wskanaan
  • | Joined Sep 2008 | Status: Member | 35 Posts
Hi...

I think one problem you have is limiting the number of orders opened. You need to use a magic number or a specific comment. Once you have a change check if there is an order with this magic number and comment is already opened. If there are no open orders from your EA then open a new one. If you check sample EA's you can find sample code for this.

Regards
 
 
  • Post #4
  • Quote
  • Sep 20, 2009 2:55pm Sep 20, 2009 2:55pm
  •  trendchaser
  • | Membership Revoked | Joined Oct 2007 | 2,124 Posts
you have the right idea, read through this tutorial
youl be able to fix the ea your self
http://trendchaser.org/forum/phpBB3/...php?f=64&t=350

Quoting simran99
Disliked
I programmed this EA to (close short trade and) buy when price closes above 5 ema and (close long trade and) sell when price closes below 5 ema.

The logic: (From MetaStock)
Close short: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Enter long:...
Ignored
 
 
  • Post #5
  • Quote
  • Sep 21, 2009 4:17am Sep 21, 2009 4:17am
  •  simran99
  • | Joined Sep 2009 | Status: Member | 66 Posts
Trendchaser,

thank you !!!
 
 
  • Post #6
  • Quote
  • Sep 21, 2009 12:53pm Sep 21, 2009 12:53pm
  •  sangmane
  • Joined Apr 2009 | Status: MT4 Programmer | 758 Posts
hi simran,

move "BarsCount = Bars;" line after
///////////
if (Bars > BarsCount)
{
//////////
This is the only cause why there are multiple open order on the same bar. The logic of " if (Bars > BarsCount)" is to detect the first tick on the new bar and open/close order only this event occurs. (sorry, my english is terrible)

Corrected code:
PHP Code
 //+------------------------------------------------------------------+
//|                                                        MA-EA.mq4 |
//|                                         Copyright © 2009, Simran |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Simran"
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int       MA_Method=2;
extern int       MA_Period=5;
extern int       Lots=1;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   
return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   
return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int BarsCount = 0;

//----
   
int start()
{
  if (
Bars > BarsCount)
  {
    
BarsCount = Bars; //moved to here
    
double MA1 = iMA(NULL,0,MA_Period,0,MA_Method,0,1);
    
double MA2 = iMA(NULL,0,MA_Period,0,MA_Method,0,2);
    
    
double C1 = iClose(NULL,0,1);
    
double C2 = iClose(NULL,0,2);
    
  
  if ((
C2<MA2 && C1<MA1)||(C2>MA2 && C1>MA1)) //No trend change
   
{
   return; 
//Do nothing. Just wait for opposing signal.
   
}
//---------------------------------------------------------------------  
 
  
if (C2<MA2 && C1>MA1) //Trend change from down to up.
   
{
      
int total = OrdersTotal(); //Script to close all open orders.
      
for(int i=total-1;i>=0;i--)
      {
        
OrderSelect(i, SELECT_BY_POS);
        
int type   = OrderType();
        
bool result = false;
    
        switch(
type)
        {
          
//Close opened long positions
          
case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                              break;
          
//Close opened short positions
          
case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        }
    
        if(
result == false)
        {
          
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          
Sleep(3000);
        }  
     }      
//End of Script to close all orders.
     
      
int ticket; //Place Buy Order
         
RefreshRates();
         
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"Buy Order",9999,0,Green);
         if(
ticket<0)
           {
            Print(
"Buy Order failed with error #",GetLastError());
           }
    return;
   }
 
 
//---------------------------------------------------------
 
   
if(C2>MA2 && C1<MA1) //Trend change from up to down.
    
{
      
total = OrdersTotal(); //Script to close all open orders.
      
for(i=total-1;i>=0;i--)
      {
        
OrderSelect(i, SELECT_BY_POS);
        
type   = OrderType();
        
result = false;
    
        switch(
type)
        {
          
//Close opened long positions
          
case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                              break;
          
//Close opened short positions
          
case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        }
    
        if(
result == false)
        {
          
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
          
Sleep(3000);
        }  
     }      
//End of Script to close all orders.
     
      //Place Sell Order
         
RefreshRates();
         
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+1000*Point,Bid-1000*Point,"Sell Order",9999,0,Green);
         if(
ticket<0)
           {
            Print(
"Sell Order failed with error #",GetLastError());
           }
    return;
    }
       
//    BarsCount = Bars; moved
  
}
   return(
0);
}

   
  
//+------------------------------------------------------------------+ 
Attached Image (click to enlarge)
Click to Enlarge

Name: screenshot.gif
Size: 9 KB
 
 
  • Post #7
  • Quote
  • Last Post: Sep 21, 2009 5:55pm Sep 21, 2009 5:55pm
  •  simran99
  • | Joined Sep 2009 | Status: Member | 66 Posts
Sangmane,

Thanks for the code !
 
 
  • Platform Tech
  • /
  • Need Help Spotting Coding Error
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023