Please help me with my ADX EA, and the role of shift in curve functions.

 
Hello, I am a total newbie, and Im just creating my first EA, using ADX crossovers as signals for buy. My problem is, that even though I wrote a code, which should trigger a buy signal when the Di+ line crosses the Di- line from the bottom, and should close the order either if SL/TP functions are hit, or Di- crosses Di+ from above, but seems to be not working (on the strategy tester). Signals seem to be tirggered by any crossovers, and the orders are not closed in any way the Di- crossing Di+ from above. What is more, I have another question, concerning the shift in these functions
b4plusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
b4minusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MINUSDI, 1);
nowplusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_PLUSDI, 0);
nowminusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MINUSDI, 0);

Does the digit in the last place (in this case 1) mean, that the point that will be used comes from the one bar before the last one? Do I interpret that correctly?

Here is the code to the whole EA, and some screenshots showing the problem.

//+------------------------------------------------------------------+
//|                                                         tom2.mq4 |
//|                                                  Tomasz Bulinski |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Tomasz Bulinski"


// external variables


extern double LotSize = 0.1;
extern double StopLoss = 10;
extern double TakeProfit = 20;


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  { 
  
  
double nowmain;      // new main line
double b4main;       // past main line
double b4plusdi;     // past line +DI
double b4minusdi;    // past line -DI
double nowplusdi;    // present line +DI
double nowminusdi;   // present line -DI
int ticket;          // ticket number
double price;        // price
int stoploss;        // stoploss
double OpenPrice; 
double BuyStopLoss;
double BuyTakeProfit; 
int total = OrdersTotal(); 

b4main = iADX(0,0,14,PRICE_CLOSE,MODE_MAIN,1);    // past main line
nowmain = iADX(0,0,14,PRICE_CLOSE,MODE_MAIN,0); // formula for the new main line
b4plusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_PLUSDI, 0);
b4minusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MINUSDI, 0);
nowplusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_PLUSDI, 1);
nowminusdi = iADX(NULL, 0, 14, PRICE_CLOSE, MODE_MINUSDI, 1); 



TakeProfit = 20;
StopLoss = 10; 
OpenPrice = Ask;
BuyStopLoss = OpenPrice - (StopLoss * Point);
BuyTakeProfit = OpenPrice + (TakeProfit * Point);



    if(total < 1)
      {
        

if  ((nowmain>20) && (b4plusdi<b4minusdi) && (nowplusdi>nowminusdi)) //condition
{
ticket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,2, BuyStopLoss, BuyTakeProfit,0,Green);
            if(ticket > 0)
              {
                if((b4plusdi>b4minusdi) && (nowplusdi<nowminusdi))
                OrderClose(ticket,LotSize,OpenPrice,2)
                ;}
            else 
                Print("Error opening BUY order : ",GetLastError()); 
            return(0); 
          }}





return(0);
}


                      
        

 

And here are the screenshots concerning the problems, as you can see, the buy order is triggered by the Di- line crossing Di+ line from below, which should not happen! I hope I was clear with my problem, Im really counting on some tips, as I spent the whole day trying to figure out what is the problem with that!

 
 
  1. tomekbuka:
    Does the digit in the last place (in this case 1) mean, that the point that will be used comes from the one bar before the last one? Do I interpret that correctly?

    Yes, however on the first tick the b4+ and now+ will be very close. As the new bar forms you may get many small crosses.

    instead look at the last two complete bars b4p... = IADX(.... 2); nowp... = IADX(... 1);


  2.             if(ticket > 0)
                  {
                    if((b4plusdi>b4minusdi) && (nowplusdi<nowminusdi))
                    OrderClose(ticket,LotSize,OpenPrice,2)
                    ;}
    This wont work. The first comparison will always be false or you wouldn't have opened. The next tick comes in and you will open another order. Instead wait for a new bar, then check for close.
    int start(){
        static datetime Time0;
        if (Time0 == Time[0]) return(0); Time0 = Time[0];
        
        ...
        
        // Check for close
        if((b4plusdi>b4minusdi) && (nowplusdi<nowminusdi)){
            for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
                OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
            &&  OrderMagicNumber()  == magic.number             // my magic number
            &&  OrderSymbol()       == Symbol() ){              // and my pair.
                if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),2))
                Alert("OrderClose failed: ", GetLastError());
        }
    
        // Check for open.
    Also always check return codes (orderSend)
 
Thanks a lot!