EA work fine with demo account with Alpari - But not Live with Oanda

 

Hello,

I have a simple TimeBased EA,
It does work fine with the demo account with Alpari
but not in live accound with Oanda

Without any errors in the Experts section
Noticethat the:

- Enable expert Advisors(except for Custom Indicators and Scripts)
-Allow live trading

-Allow external experts imports

are checked in the Options in MT4 program with the Start button in the menu

Here is the EA

//+------------------------------------------------------------------+
//| Oanda.mq4 |
//| Leopardos |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Leopardos"
#property link "https://www.metaquotes.net//"

extern double Lots = 0.01;
extern int StopLoss = 100;
extern int TakeProfit = 200;

extern bool Long_Entry = false; // Set to "true" or "false".
extern bool Short_Entry = true; // You can have both Long_Entry & Short Entry set to "true".
extern int Day_One = 10;
extern int Month_One = 06;
extern int Year_One = 2011;
extern int Hour_For_Long_Entry = 09; //this is your broker's time as displayed in the MArket Watch window.
extern int Minute_For_Long_Entry = 05;
extern int Hour_For_Short_Entry = 09; //this is your broker's time as displayed in the Market Watch window.
extern int Minute_For_Shor_Entry = 05;
extern double BuyTrailingStop = 320;
extern double SellTrailingStop = 380;
extern double StepTrailingStop = 40;
extern int Slippage =5;


int MagicNumber=0546708440, total, i;
bool Place_Sell=true;
bool Place_Buy=true;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

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

//----
if(Long_Entry==true && Day()== Day_One && Month()==Month_One && Year()==Year_One && Hour()==Hour_For_Long_Entry && Minute()>=Minute_For_Long_Entry && IsTradeAllowed()==true)
{
if(Place_Buy == true)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"WNV Buy",MagicNumber,0,Blue);
Place_Buy=false;
{
if(IsTradeAllowed()==false) Print("Trade not allowed");
}
return(0);
}
}

if(Short_Entry==true && Day()== Day_One && Month()==Month_One && Year()==Year_One && Hour()==Hour_For_Short_Entry && Minute()>=Minute_For_Long_Entry && IsTradeAllowed()==true)
{
if(Place_Sell == true)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"WNV Sell",MagicNumber,0,Red);
Place_Sell=false;
{
if(IsTradeAllowed()==false) Print("Trade not allowed");
}
return(0);
}
}

//+------------------------------------------------------------------+
//----
if(BuyTrailingStop > 0) total = OrdersTotal();
{
for(i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol() != Symbol()) continue; //

if(OrderType() == OP_BUY)
{
if(Bid > (OrderOpenPrice() + Point * BuyTrailingStop))
{
if((OrderStopLoss() == 0) || (OrderStopLoss() < (Bid - Point * (BuyTrailingStop + StepTrailingStop))))
{
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * BuyTrailingStop, OrderTakeProfit(), 0);
}
}
}
}
}

if(SellTrailingStop > 0) total = OrdersTotal();
{
for(i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol() != Symbol()) continue; //

if(OrderType() == OP_SELL)
{
if(Ask < (OrderOpenPrice() - Point * SellTrailingStop))
{
if((OrderStopLoss() == 0) || (OrderStopLoss() > (Ask + Point * (SellTrailingStop + StepTrailingStop))))
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * SellTrailingStop, OrderTakeProfit(), 0);
}
}
}
}
}

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

What could be the problem ?!

I have to admit, i have lost alot of money yesterday because the EA didnt work, mmm

Thanks in advance

 

  1. On 5 digit brokers you must adjust TP, SL, AND slippage. On ECN brokers you must open the order 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
    

  2. if(Place_Buy == true) 
    if (true == true) is redundant. if(Place_Buy)... if( !IsTradeAllowed() ) Print("Trade not allowed");
  3. Always test return codes. On a SELL, the stops are relative to the ASK
    //OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Bid+StopLoss*Point, Bid-TakeProfit*Point,
                "WNV Sell", MagicNumber, 0, Red);
    int ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Bid+StopLoss*Point, Bid-TakeProfit*Point,
                           "WNV Sell", MagicNumber, 0, Red);
    if (ticket < 0) Alert("OrderSend(SELL) failed: ", GetLastError());

  4. if(SellTrailingStop > 0) total = OrdersTotal();
    {
    for(i = 0; i < total; i++)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    
    if(OrderSymbol() != Symbol()) continue; //
    
    if(OrderType() == OP_SELL) 
    The total = OrdersTotal() breaks your code structure, the rest is NOT inside the IF. Since you're not filtering by magic number, this makes the EA incompatable with any other EA or manual trading. Get in the habit of counting down, You MUST in the presence of multiple order (multiple charts) or when closing/deleteing. Always test return codes.
    if(SellTrailingStop > 0){
        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(OrderType() == OP_SELL) ...

 

Well,
First of all sorry about the Code and thanks for you help,

But sadly, im trying to reconfigure it from the start with your fixes, i cant get it work ...
im still new to this langauge :<
Is there any possible way to post the Full ea with the fix ?
i just get errors on every section i add or edit ..

Thanks again, i would really appreciate your help

 

OANDA has MT4?

I need to keep up with the news a bit more.

 

Yes they have the MT4 ...
and yet again, i just keep errors trying to fix it to work with oanda,
is there any one could help me with this ea ? its just simple as it is, time based EA,
i just cant make it work with oanda live ..

Thanks again ....

 

Your problem is with OrderSend statement:

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"WNV Buy",MagicNumber,0,Blue);

First, remove Ask-StopLoss*Point from above and set it to 0(zero).

When OrderSend is successful, the statement returns an Order Number.

Then, with that Order Number, do OrderSelect in order to modify the stoploss to Ask-StopLoss*Point.

Note that Some brokerages do not allow you to embed the stoploss in the OrderSend statement for a market order.

 

Thanks very much, now i get the point :O

so, Is this right ? im sorry but im realy not good with this scripting

//----
 if(Long_Entry==true && Day()== Day_One && Month()==Month_One && Year()==Year_One && Hour()==Hour_For_Long_Entry && Minute()>=Minute_For_Long_Entry && IsTradeAllowed()==true)
 {
 if(Place_Buy == true) 
{
 OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"WNV Buy",MagicNumber,0,Blue); 
Place_Buy=false;
 {
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 OrderModify(OrderTicket(), OrderOpenPrice(), Bid-StopLoss*Point, Bid+TakeProfit*Point ,OrderTakeProfit(), 0);
 }
 {
 if(IsTradeAllowed()==false) Print("Trade not allowed");
 }
 return(0);
 }
 } 

if(Short_Entry==true && Day()== Day_One && Month()==Month_One && Year()==Year_One && Hour()==Hour_For_Short_Entry && Minute()>=Minute_For_Long_Entry && IsTradeAllowed()==true)
 {
 if(Place_Sell == true)
 {
 OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"WNV Sell",MagicNumber,0,Red);
 Place_Sell=false; 
 {
 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 OrderModify(OrderTicket(), OrderOpenPrice(), Bid+StopLoss*Point, Bid-TakeProfit*Point ,OrderTakeProfit(), 0);
 }
{
 if(IsTradeAllowed()==false) Print("Trade not allowed");
 }
 return(0);
 }
 }
 

Additionally, you need to multiply StopLoss by 10 for Oanda because its default digits are 1 more than your pip count for this broker. If you do not do this, your intended stop pips of 100 will turn into 10 pips.

To give you examples for the eur/usd pair:

100 pips*Point(.00001) =.00100 (for 5 digits, this is equivalent to 10 pips)

100 pips*Point(.00001)*10 =.01000 (for 5 digits, this is your intended 100 pips)

So, your statement must be changed as such.

Bid+StopLoss*10*Point

In fact, this was already noted by WHRoeder in his note #2.

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; }

Good luck.

 

I noticed new errors in your codes

Change your codes as below:

int i=OrderSend(...);

if(i>0)

{

OrderSelect(i, SELECT_BY_TICKET);
double Stop = NormalizeDouble(Ask-stop*Point,Digits);
OrderModify(OrderTicket(),OrderOpenPrice(),Stop,OrderTakeProfit(),0,0);
}

else

{

<error routine>

}

Reason: