moving average expressions not working correctly

 

The ea is like this:

If two bars form higher highs and the third bar closes 50% of second bar AND is below the moving average, then place order to sell. Placing an order to buy would be just the opposite. The buys happen when the statement is not even true, and the sells do not happen at all. The code I wrote does every fine until:

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,0,PRICE_CLOSE,0);
CurrentBar = CurrentBar + 1;

if (total < OrdersAllowed)
//---- Buy Signal
{ CurrentBar = 0;
OrderModByHighLow = 0;
if (Low[0]< Low[1])
if (Close[0]/(High[0]-Low[0]) > 0.5)
if ((Close[0] > ma) && ( Close[0] != ma))
{ Print("Current Bar Number buy = ", CurrentBar, " moving average = ", ma," ", Close[0]);
ticket = OrderSend(Symbol(),OP_BUY, Lots, Ask, Slip, Low[0]- BuyLoss, Ask+TP,"TwoUP Low", 4061, 0);
Alert(GetLastError());
}
//----- Sell Signal
if (High[0] > High[1])
if (Close[0]/(High[0]-Low[0]) < 0.5)
if (Close[0] < ma)
{ Print("Current Bar Number sell = ", CurrentBar, " moving average = ", ma," ", Close[0]);
ticket = OrderSend(Symbol(),OP_SELL, Lots, Ask, Slip, High[0] + SellLoss, Ask+TP,"TwoUP High", 4061, 0);
Alert(GetLastError());
}
}

//----
return(0);

}

Here is the whole code:

extern double MaximumRisk = 0.02;
extern double MovingPeriod = 24;
extern double MovingShift = 6;
extern double Lots=0.3;
extern double Slip=3;
extern int TakeProfit = 300; // 0: disable; >0: enable TakeProfit of 200 > 20 pips
extern int StopLoss = 170; //0: disable; >0: enable StopLoss of 200 > 20 pips
extern int TrailStop = 170; //0: disable; >0: enable (StopLoss must be enabled too)
extern double PercentTop = 0.85; // percentage of the distance the close must be from end of move
extern double DistanceFirstMove = 90; //pips to go on first fast move
extern double DistanceSecondMove = 60; // pips to go on the second fast move
extern double BuyLoss = 70; // pips to have a stop loss for a buy order when we want it Close
extern double SellLoss = 70; // pips to have a stop loss for a sell orde whe we want is close
extern int InToMoveLoss = 2; // bars to move stop loss to break even
extern int CountToProfit = 16; // bars to take profit
extern double Fudge = 10 ;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{ double ma;
int res;
int ticket;
int total = 0 ;
int CurrentBar = 0;
int OrdersAllowed = 1;
int OrderModByHighLow = 0;
double TP=TakeProfit*Point;
double SL=StopLoss*Point;
double TS=TrailStop*Point;
double RangeCurrent = 0;
double RangeOne = 0.0;
double RangeTwo = 0.0;
double RangeThree = 0.0;


DistanceFirstMove = DistanceFirstMove*Point;
DistanceSecondMove = DistanceSecondMove*Point;
BuyLoss = BuyLoss*Point;
SellLoss = SellLoss*Point;
RangeCurrent = High[0] - Low[0];
RangeOne = High[1] - Low[1];
RangeTwo = High[2] - Low[2];
RangeThree = High[3] - Low[3];
total = OrdersTotal();


//Modifying stop-loss of open orders------------------------------------------------------+
if(StopLoss>0 && TrailStop>0)
for(int i=0;i<total;i++)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY)
{ if ( (
( RangeCurrent > RangeOne) ||
( RangeCurrent > RangeTwo ) ||
( RangeCurrent > RangeThree )
)
&&
( Close[0] < Close[1] )
)
{ OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
}
if ( (CurrentBar > InToMoveLoss) && ( OrderModByHighLow < 1) )
{ OrderModByHighLow = 1 ;
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
}
if ( CurrentBar > CountToProfit )
{ OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
}
if ( OrderModByHighLow < 1 )
if(Bid-OrderOpenPrice()>TS)
if(OrderStopLoss()<Bid-TS)
{ OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS,OrderTakeProfit(),0,Blue);
}
}

if(OrderType()==OP_SELL)
{
if ( (
( RangeCurrent > RangeOne) ||
( RangeCurrent > RangeTwo ) ||
( RangeCurrent > RangeThree )
)
&&
( Close[0] > Close[1] )
)
{ OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
if ( (CurrentBar > InToMoveLoss) && ( OrderModByHighLow < 1) )
{ OrderModByHighLow = 1 ;
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
}
if(CurrentBar > CountToProfit)
{ OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
if ( OrderModByHighLow < 1 )
if(OrderOpenPrice()-Ask>TS)
if(OrderStopLoss()>Ask+TS)
{ OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS,OrderTakeProfit(),0,Red);
}
}

}



//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,0,PRICE_CLOSE,0);
CurrentBar = CurrentBar + 1;

if (total < OrdersAllowed)
//---- Buy Signal
{ CurrentBar = 0;
OrderModByHighLow = 0;
if (Low[0]< Low[1])
if (Close[0]/(High[0]-Low[0]) > 0.5)
if ((Close[0] > ma) && ( Close[0] != ma))
{ Print("Current Bar Number buy = ", CurrentBar, " moving average = ", ma," ", Close[0]);
ticket = OrderSend(Symbol(),OP_BUY, Lots, Ask, Slip, Low[0]- BuyLoss, Ask+TP,"TwoUP Low", 4061, 0);
Alert(GetLastError());
}
//----- Sell Signal
if (High[0] > High[1])
if (Close[0]/(High[0]-Low[0]) < 0.5)
if (Close[0] < ma)
{ Print("Current Bar Number sell = ", CurrentBar, " moving average = ", ma," ", Close[0]);
ticket = OrderSend(Symbol(),OP_SELL, Lots, Ask, Slip, High[0] + SellLoss, Ask+TP,"TwoUP High", 4061, 0);
Alert(GetLastError());
}
}

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

//+------------------------------------------------------------------+

Thanks a million in advance!

 

What are you doing with current bar? You do current bar+1 and then when youtake a trade set current bar =0 to print the value 0... current bar is declared in start and will therefore be reset to zero every tick .If you want variables to hold there value declare them as static of on the global scope.

Where you are using volume for new bar there are no brackets so the only line this statement affects is the MA. current bar+1 and everything below will execute every tick. Don't use volume to determin new bar. it is unreliable. Use Time

// Identify new bars
   bool Fun_New_Bar()
      {
      static datetime New_Time = 0;
      bool New_Bar = false;
      if (New_Time!= Time[0])
         {
         New_Time = Time[0];
         New_Bar = true;
         }
      return(New_Bar);
      }

Use print to establish that the values being calculated are as expected.

There may be more problems but it's difficult to read unformated code. So....

Make these changes and see how it goes

hth V

 
  1. Use the SRC button
  2. //---- go trading only for first tiks of new bar
    if(Volume[0]>1) return;
    This is unreliable, volume can skip values.
    static datetime Time0; if (Time[0]==Time0)return; Time0=Time[0];

  3. if (Low[0]< Low[1])
    if (Close[0]/(High[0]-Low[0]) > 0.5)
    if ((Close[0] > ma) && ( Close[0] != ma))
    if close>ma then close != ma. The second condition is irrelevant.
    Since you are only executing on the first tick Close[0]==High[0]==Low[0] Your code doesn't make sense. Do you mean
    if( Low[1]< Low[2]
    &&  Close[1]/(High[1]-Low[1]) > 0.5
    &&  Close[1] > ma) ...
    Also you'd better check for High[1]==Low[1] or the EA dies with divide by zero.