Results 1 to 3 of 3
  1. #1
    tkuan77 is offline Newbie
    Join Date
    Jun 2012
    Posts
    10

    Default Please help with my TP SL Error

    Hi guys, I need some help on this. My EA seems to go a bit haywire. Occasionally it will auto SL or TP on its own even before the actual SL or TP is reached like the one shown in my screenshot. why is this so?

    Thanks and regards
    Terrance

    //--- input parameters
    extern double TakeProfit=1000.0;
    extern double Lots=0.1;
    extern double StopLoss=1500.0;
    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----
    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----
    //----
    return(0);
    }


    int mainCrossed (double mainline1 , double mainline2)
    {
    static int mainlast_direction = 0;
    static int maincurrent_dirction = 0;
    if(mainline1>mainline2)maincurrent_dirction = 1; //main up
    if(mainline1<mainline2)maincurrent_dirction = 2; //main down
    if(maincurrent_dirction != mainlast_direction) //main changed
    {
    mainlast_direction = maincurrent_dirction;
    return (mainlast_direction);
    }
    else
    {
    return (0);
    }
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----
    int cnt, ticket, total;
    double shortEma, longEma, mainshortEma, mainlongEma;
    if(Bars<100)
    {
    Print("bars less than 100");
    return(0);
    }
    if(TakeProfit<10)
    {
    Print("TakeProfit less than 10");
    return(0); // check TakeProfit
    }


    int isCrossed = 0;
    double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);
    double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);
    double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);
    double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);
    double diff1 = shortEma1-longEma1;
    double diff2 = shortEma2-longEma2;

    mainshortEma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,0);
    mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
    int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);

    if ((diff1*diff2)<0)
    {
    if (shortEma1>longEma1)
    isCrossed = 1;
    else isCrossed = 2;
    }


    total = OrdersTotal();
    if(total < 1)
    {
    if(isCrossed == 1 && mainshortEma > mainlongEma)
    {
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
    "My EA",12345,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 && mainshortEma < mainlongEma)
    {
    ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+S topLoss*Point,
    Bid-TakeProfit*Point,"My EA",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);
    }


    return(0);
    }
    //+------------------------------------------------------------------+


  2. #2
    JDeel's Avatar
    JDeel is offline Newbie
    Join Date
    Nov 2011
    Posts
    64
    I can't see screenshot here.
    This EA works normal. It's pretty simple to make such strange things. Check if other EA is messing with trades.

  3. #3
    tkuan77 is offline Newbie
    Join Date
    Jun 2012
    Posts
    10
    Hi, here is the screen shot. [IMG]http://www.forex-tsd.com/attachments/metatrader-programming/138859d1339816564-how-code-ps.jpg
    [/IMG]

Similar Threads

  1. Please help with (I'm sure) a very simple problem
    By cordite in forum Expert Advisors and Automated Trading
    Replies: 3
    Last Post: 06-05-2010, 02:25 PM
  2. Please help with leverage
    By Toka in forum Newbie Island
    Replies: 5
    Last Post: 11-18-2009, 08:34 AM
  3. Please help with...
    By learningfx in forum Newbie Island
    Replies: 13
    Last Post: 07-04-2008, 01:53 PM
  4. Please Help with adding a command to EA
    By mtandk0614 in forum Newbie Island
    Replies: 2
    Last Post: 03-05-2008, 02:33 PM
  5. Please, help with Charts
    By Hypechic in forum Newbie Island
    Replies: 4
    Last Post: 09-07-2007, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
"There is nothing so useless as doing efficiently that which should not be done at all."
Peter Drucker