Problems checking for open trade - page 5

 
BrotherPyrus:
Hey Daz & Co,

hothand works on five digit MT4, but not on a four digit platform.
Can you be more specific about how it doesn't work ?
 
BrotherPyrus:
hothand works on five digit MT4, but not on a four digit platform. Any idea what causes this, and any clues on how to fix it?
EA's must adjust for 4/5 digits, TP, SL, AND slippage. On ECN brokers you must open first and THEN set stops.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
 
   if(iBarHour >= iOpenHour && iBarHour <= iOpenHour + iBarsToTrade && Tradeopen()==false) // Check if time is ok to trade & no trade is open
                  {

                          
                              //  if(High[2]>High[1] && Low[2]<Low[1]) // see if we have an inside bar
                                if(iHigh(hothand(), 60, 2)>iHigh(hothand(), 60, 1) && iLow(hothand(), 60, 2) < iLow(hothand(), 60, 1)) // see if we have an inside bar
                                 {                                                                                                     // on the hothand pair
                                           
                                        // IB found 
                                        if(iHigh(hothand(), 60, 1) > dDayOpenPrice) // IB high on hothand pair is greater than day open price bullish signal
                                          {
                                             int    buyspread=MarketInfo(hothand(),MODE_SPREAD);
                              double buyentry =  iHigh(hothand(), 60, 1) + 10 + buyspread;  //Low of prev bar + 1 pip + spread
                              double buystop = iLow(hothand(), 60, 1) - 10;    //Low of prev bar - 1 pip
                              double buytpx1 = iHigh(hothand(), 60, 1)+(iHigh(hothand(), 60, 1)-iLow(hothand(), 60, 1)); //1:1 r:r
                              double buytpx3 = iHigh(hothand(), 60, 1)+(iHigh(hothand(), 60, 1)-iLow(hothand(), 60, 1))*3; // 1:3 r:r
                              
                              
                           OrderSend(Symbol(),OP_BUY,1 , Ask, 3, buystop, buytpx1, "DIBSBUY", Mnumber1);  //Open buy order
                           OrderSend(Symbol(),OP_BUY,1 , Ask, 3, buystop, buytpx3, "DIBSBUY", Mnumber3);
                            }
                            
                         if(iLow(hothand(), 60, 1) < dDayOpenPrice) // IB low on hothand pair is lower than day open price bearish signal
                            {
                              int    sellspread=MarketInfo(hothand(),MODE_SPREAD);
                              double sellentry = iLow(hothand(), 60, 1)-10; //Low of prev bar - 1 pip 
                              double sellstop = iHigh(hothand(), 60, 1)+ 10 + sellspread; // High of prev bar + 1 pip + Spread
                              double selltpx1 = iLow(hothand(), 60, 1)-(iHigh(hothand(), 60, 1)- iLow(hothand(), 60, 1)); //1:1 r:r
                              double selltpx3 = iLow(hothand(), 60, 1)-(iHigh(hothand(), 60, 1)- iLow(hothand(), 60, 1))*3; // 1:3 r:r
                              
                              
                           OrderSend(Symbol(),OP_SELL,1 , Ask, 3, sellstop, selltpx1,"DIBSSELL",Mnumber1);  //Open Sell Order
                           OrderSend(Symbol(),OP_SELL,1 , Ask, 3, sellstop, selltpx3,"DIBSSELL",Mnumber3);  //Open Sell Order
               
                                      } 
                        }
                               
                   }
     //  
  
    
      }

Alright I am back,


This is the part of my code where trades get executed, I am just going to work with 5 digits for the moment. The idea here is I used my hothand function to pick which pair is best to trade for the day. This code should scan for an inside bar on the 1 hour chart. ( The hothand function returns a string of the pair btw) When it finds an inside bar I have just got it to open a flat out buy or sell trade for testing purposes. I am not sure if the backtester can handle trading multiple pairs from 1 chart? I downloaded the 1min history for all the pairs it uses. But it only seems to open trades on the eurusd which is the chart I have attached it to,

WHRoeder thanks for dropping by and helping again also. So is the correct way to open a trade with an ea to open it then go back and fill in the sl and tp after it has opened? I am sure you can spot a million things wrong with that code also.

 
  1. OrderSend(Symbol(),OP_BUY,1 , Ask, 3, buystop, buytpx1, "DIBSBUY", Mnumber1);  //Open buy order
    

    Not adjusting slippage for 4/5 digits. Not ECN compatable

    Always test return codes so you find out WHY it doesn't work

    int ticket = OrderSend(Symbol(),OP_BUY,1 , Ask, 3, buystop, buytpx1, "DIBSBUY", Mnumber1);  //Open buy order
    if (ticket < 0) Alert("OrderSend [1] failed: ",GetLastError());

  2. buystop = iLow(hothand()...
    Your stops are based on the pair hothand(), but you're opening an order with Symbol(). If they're not the same the stops are completely bogus
 
dazamate:

Alright I am back,


This is the part of my code where trades get executed, I am just going to work with 5 digits for the moment. The idea here is I used my hothand function to pick which pair is best to trade for the day. This code should scan for an inside bar on the 1 hour chart. ( The hothand function returns a string of the pair btw) When it finds an inside bar I have just got it to open a flat out buy or sell trade for testing purposes. I am not sure if the backtester can handle trading multiple pairs from 1 chart? I downloaded the 1min history for all the pairs it uses. But it only seems to open trades on the eurusd which is the chart I have attached it to,


Some general comments:

you buy at Ask and sell at Bid

if your broker is an ECN type broker you cannot set SL and TP when you place the order (in this case set them to 0), you have to place the order, then modify the order to add the SL & TP

always check the result of placing an order and check for returned errors, you will need to handle these by acting accordingly

if you plan on having this EA on multiple charts, i.e. placing Orders from multiple charts, you will need some kind of Mutex to avoid Order Context Busy (check WHRoeder's posts, he has an excellent Mutex)

 
Damm WHRoeder, your right "our stops are based on the pair hothand(), but you're opening an order with Symbol()" I did not notice that. How stupid. This stuff just gets so mentally fatiguing whilst your trying to learn it . But yes stupid. Thanks for going over that mess. Ill probably have a break and come back tomorrow with a freash mind. Ill fix them things up you pointed out.
 
dazamate:

But it only seems to open trades on the eurusd which is the chart I have attached it to,

Yep, . . .

OrderSend(Symbol(),OP_SELL,1 , Ask, 3, sellstop, selltpx1,"DIBSSELL",Mnumber1);  //Open Sell Order

Symbol() is the pair that the EA/Indicator is attached to, did you mean to do this instead ?

OrderSend(hothand(),OP_SELL,1 , Ask, 3, sellstop, selltpx1,"DIBSSELL",Mnumber1);  //Open Sell Order
 

Yes I did mean to do that, but now I get this error


http://clip2net.com/s/144hq


Like it doesn't recognize the pair output of the hothand function. The hothand function's output is a string so I dont see what it wont work

 
dazamate:
Like it doesn't recognize the pair output of the hothand function. The hothand function's output is a string so I dont see what it wont work
OrderSend(hothand(),OP_SELL,1 , Ask, 3, sellstop, selltpx1,"DIBSSELL",Mnumber1);  //Open Sell Order
  1. Ask/Bid is the current Symbol() price but you're trying to open another pair. Bogus price.
  2. You can not get bar zero values from the tester for other TF/pairs
  3. You can not open trades of other pairs in the tester, IIRC
  4. unknown symbol name USDCHF for OrderSend function
    Where do you get that string? Is the current chart the same pattern, or USD,EUR, or USD/EUR, or USDEURm, or USDEURfxf etc.
  5. I suggest you ONLY trade the current pair. If hothand() isn't the current, no trades. That way it works in the tester, you can always put the EA on other pairs thus controlling which pairs it trades, And you avoid all the pitfalls.
 

Hey there guys, I am trying to make some code that does the following


extern string  sComment4                = "Max Hours allowed before pending orders are deleted";
extern int     pendinglimit        = 4;

// * EVERYTIME A TRADE GETS TRIGGERED 

bartraded = TimeHour(TimeCurrent());


// DELETE PENDING ORDERS THAT HAVE NOT BEEN TRIGGERED WITHIN 

   if(TimeHour(TimeCurrent()) > bartraded + pendinglimit && Tradeopen()==true) // Check to see if pending orders have expired
     {
        for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
           OrderType()==(OP_BUYSTOP||OP_SELLSTOP) &&     // The order selected is either a pending buy on stop order or a buy on sell order
            OrderMagicNumber()==(Mnumber1||Mnumber3))    // The orders magic number is the same as the magic number used in this ea
          
               {
                OrderSelect(tnumber, SELECT_BY_POS);     // Select order
                OrderDelete(tnumber);                    // Delete it
               }
        }
     }

If a pending order has been open for x amount of bars delete it if bar number have exceeded x


Something might be wrong with my logic maybe?