Results 1 to 2 of 2
 1 Attachment(s)    

Thread: Moving Average v2

  1. #1
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default Moving Average v2

    Expert advisor : Moving average v2
    Settings : Post#1

    EU H1. risk 6.

    Total Net Profit : -9.67%
    RDD : 14.73%
    Attached Images Attached Images Moving Average v2-08-10-15-moving-average-v2-eu-opt-risk-6-gif 

  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    1

    Default my first MOVING average EA

    this is my first ea ,some code borrowed from ea guru

    i add MA filter in it when it work over ma190 do buy, otherwise sell in 4h EU or GU . backtest is good but it didnot trigger in IBFX demo. I'm wondering some code is not correct. pls help me to find erro in it . MAY someone in this furum can help me

    many thx
    yours sincerily
    wem

    code here:

    //+------------------------------------------------------------------+
    //| EMA_CROSS.mq4 |

    //+------------------------------------------------------------------+
    //+------------------------------------------------------------------+
    //| TODO: Add Money Management routine |
    //+------------------------------------------------------------------+
    #property copyright "4H ema cross over"
    #property link " "
    //---- input parameters
    extern double TakeProfit=1000;
    extern double StopLoss = 800;
    extern double Lots=0.1;
    extern double TrailingStop=250;
    extern int ShortEma = 5;
    extern int LongEma = 8;
    extern double MATrendPeriod=190;
    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----

    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----

    //----
    return(0);
    }
    int Crossed (double line1 , double line2)
    {
    static int last_direction = 0;
    static int current_direction = 0;

    //Don't work in the first load, wait for the first cross!
    static bool first_time = true;
    if(first_time == true)
    {
    first_time = false;
    return (0);
    }

    if(line1>line2)current_direction = 1; //up
    if(line1<line2)current_direction = 2; //down
    if(current_direction != last_direction) //changed
    {
    last_direction = current_direction;
    return (last_direction);
    }
    else
    {
    return (0); //not changed
    }
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----
    int cnt, ticket, total;
    double SEma, LEma, MaCurrent,MaPrevious;


    if(Bars<100)
    {
    Print("bars less than 100");
    return(0);
    }
    if(TakeProfit<10)
    {
    Print("TakeProfit less than 10");
    return(0); // check TakeProfit
    }

    SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
    LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,0);
    MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRIC E_CLOSE,0);
    MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRI CE_CLOSE,1);

    static int isCrossed = 0;
    isCrossed = Crossed (LEma,SEma);

    total = OrdersTotal();
    if(total < 1)
    {
    if(isCrossed == 1 && MaCurrent>MaPrevious)
    {
    //ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+ TakeProfit*Point,"EMA_CROSS",12345,0,Green);
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"EMA_CROSS",12 345,0,Green);
    if(ticket>0)
    {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
    }
    else Print("Error opening BUY order : ",GetLastError());
    return(0);
    }
    if(isCrossed == 2 && MaCurrent<MaPrevious)
    {
    //ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EMA_CROSS",12345,0,Red);
    ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+S topLoss*Point,Bid-TakeProfit*Point,"EMA_CROSS",12345,0,Red);
    if(ticket>0)
    {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());
    }
    else Print("Error opening SELL order : ",GetLastError());
    return(0);
    }
    return(0);
    }

    for(cnt=0;cnt<total;cnt++)
    {
    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    //OrderPrint();
    if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
    {
    if(OrderType()==OP_BUY) // long position is opened
    {
    // should it be closed?

    /* REMOVED - Trailling stop only close
    if(isCrossed == 2)
    {
    OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ; // close position
    return(0); // exit
    }
    */

    // check for trailing stop
    if(TrailingStop>0)
    {
    if(Bid-OrderOpenPrice()>Point*TrailingStop)
    {
    if(OrderStopLoss()<Bid-Point*TrailingStop)
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
    return(0);
    }
    }
    }
    }
    else // go to short position
    {
    // should it be closed?

    /* REMOVED - Trailling stop only close
    if(isCrossed == 1)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet) ; // close position
    return(0); // exit
    }
    */

    // check for trailing stop
    if(TrailingStop>0)
    {
    if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
    {
    if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poi nt*TrailingStop,OrderTakeProfit(),0,Red);
    return(0);
    }
    }
    }
    }
    }
    }
    return(0);
    }
    Last edited by wem33; 01-10-2013 at 14:13. Reason: add some words

Similar Threads

  1. Replies: 0
    Last Post: 07-21-2014, 11:18
  2. MTF Moving Average EA
    By danwork in forum Ideas for expert advisors
    Replies: 1
    Last Post: 03-19-2010, 05:06
  3. Moving average EA
    By Rascacio in forum Expert advisors backtesting
    Replies: 3
    Last Post: 02-16-2010, 18:46
  4. Moving Average EA
    By best2004 in forum Ideas for expert advisors
    Replies: 11
    Last Post: 12-10-2009, 20:15
  5. Moving average v2
    By funyoo in forum Expert advisors backtesting
    Replies: 5
    Last Post: 08-12-2009, 20:39

Tags for this Thread

100, advisor, average, backtest, backtesting, demo, expert, expert advisor, moving average, mq4, profit, stoploss, time, trailingstop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •