Coding Problem: Open[1] gt EMA12

thread: Coding Problem: Open[1] gt EMA12

  1. #1

    Coding Problem: Open[1] gt EMA12

    I'm having some trouble in programing a line. I'm looking to have among the opening requirements be the 12 EMA be not greater than the open of the bar. I'm doing transactions to try and not open at a sideways market.

    This is the code line:

    if((Close[2] gt;= Close[1]) (Open[1] gt;= Open[0]) (Open[1] gt; ema12) (ema12 gt; ema24))upcandle = true;

    The ema12gt;ema24 code works just fine, but the Open[1] gt; ema12 does not do the job. In back testing there are occasions where there is a trade opened below the ema12.

    Suggestions???

    Thanks!

  2. #2
    Member
    70
    Open[1] is the open price of the prior bar...

    It's feasible for the current bar to have gone below the 12ema

    Additionally, what's the shift for your EMA12 variable? Is it 1 or 0?

  3. #3
    Show us the code that is

  4. #4
    1 Attachment(s) Ronald - I tried adding a shift to the EMA, but only ended up with an error response from it unexpected [ .

    I have attached a picture showing how the machine is supposed to work in the first commerce exhibited. The second trade should not have occurred because the [1] is under the 12 ema line.

    And Oksana, as per your request:

    // ------------------------------------------------------------------
    //| Safe Pip EA.mq4 |
    //| |
    //| |
    // ------------------------------------------------------------------
    #property copyright
    #property Hyperlink

    extern double Lots = 0.1;
    extern double StopLoss = 15;

    extern double TakeProfit = 15;
    extern double MaxTotalOrders = 1;
    extern double MaxOmegaIBOrders= 1;
    extern bool MakeTrades = True;
    extern bool NoisyAlerts = True;
    extern int TrailingStop = 10;
    extern int BreakEven = 6;
    extern int maxLots=1;

    // ------------------------------------------------------------------
    //| expert initialization function |
    // ------------------------------------------------------------------
    static int order_num = -1;
    int init()

    //--

    order_num = -1;
    //--
    return(0);

    // ------------------------------------------------------------------
    //| expert deinitialization function |
    // ------------------------------------------------------------------
    int deinit()

    //--

    //--

    return(0);




    // ------------------------------------------------------------------
    //| expert start function |
    // ------------------------------------------------------------------
    int start()

    //--
    int cnt, ticket, total;

    double ema12, ema24;

    //-- Move to break even Set



    //-- Ema lines defined

    ema12=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);
    ema24=iMA(NULL,0,24,0,MODE_EMA,PRICE_CLOSE,0);




    //-- Check to Find out if there is a signal candle

    bool upcandle = false;
    bool downcandle = false;

    if((Close[2] gt;= Close[1]) (Open[1] gt;= Open[0]) (Open[1] gt; ema12) (ema12 gt; ema24))upcandle = true;
    if((Close[1] lt; Open[0]) (High[1] lt; ema12)) downcandle = true;




    // check for buy or sell signal
    bool buy = false;
    bool sell = false;

    // If the asking price in the current bar is greater than 2 pips than the second previous candle and the 12ema
    // is over the 24 ema then it is a buy signal
    if((Ask == High[2] 3*Point) (ema12 gt; ema24)) buy = true;
    // If the bidding price in the current bar is less than 2 pips than the second previous candle and the 12ema
    // is below the 24 ema then it is a buy signal
    if(((Low[2] == Bid) gt; 2*Point) (ema24 gt; ema12)) sell = true;


    if(upcandle)

    ObjectCreate(upcandle Time[1],OBJ_ARROW,0,Time[1],Low[1]-2*Point);

    total=OrdersTotal();

    if(totallt;1)

    if(buy)



    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask TakeProfit*Point, SafePip, 12345,0, Green);

    ObjectCreate(buyl Time[1],OBJ_RECTANGLE,0,Time[0] - (Time[0]-Time[1])*0.5,Low[0]-2*Point, Time[0] (Time[0]-Time[1])*0.5, Low[0]-2.5*Point);
    if(NoisyAlerts) PlaySound(alert.wav);

    if(ticketgt;0)



    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))

    Print(BUY order opened:,OrderOpenPrice());



    else Print(Error opening BUY order:,GetLastError());

    return(0);

    if (BreakEvengt;0)

    if(Bid-OrderOpenPrice()gt;Point*BreakEven)

    if(OrderStopLoss()lt;OrderOpenPrice())

    OrderModify(OrderTicket(),OrderOpenPrice(),OrderOp enPrice() Point*1,OrderTakeProfit(),0,Gray);



    /* update trailing stop */
    if(TrailingStopgt;0)

    if(Bid-OrderOpenPrice()gt;Point*(TrailingStop))

    if(OrderStopLoss()lt;Bid-Point*TrailingStop)

    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Gray);
    return(0);











    return(0);




    // ------------------------------------------------------------------

    It's a work in progress, and there are things that I'm already slating to change. I am aware there is no coding for a conditionI'm just using the SL or TP levels.

  5. #5
    The problem is probably in the fact that you're calculating the EMA on the current candle and using the start of the previous candle to determine entrance point. Change your ema settings to Examine the previous candle as well:

    ema12=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,1);
    ema24=iMA(NULL,0,24,0,MODE_EMA,PRICE_CLOSE,1);

  •