Getting an EA to take actions from the difference of two SMA's?

 

Hi!

I am a very new programmer and I am having trouble getting my EA to take any actions (Buy, Sell, or Close a trade). Can I get some advice here?


The code compiles, it just won't take action when it is supposed to. I want it to Buy when the requirements for VarianceBuy are met, sell when the requirements for VarianceSell are met, and close the position when ma2 changes direction. Any help would be greatly appreciated!


//+------------------------------------------------------------------+
//| PfX-mk3-mql4.mq4 |
//| PipfX, based off of MA from MQL Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA 20050611

extern double Lots = 0.1;
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3;
extern double VarianceBuy = 0.0030;
extern double VarianceSell = -0.0030;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma;
double ma2;
int res;

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
ma=iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0);
ma2=iMA(NULL,25,0,0,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
if(ma - ma2 <= VarianceSell)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(ma - ma2 >= VarianceBuy)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma;
double ma2;

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
ma=iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0);
ma2=iMA(NULL,25,0,0,MODE_SMA,PRICE_CLOSE,0);

//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(ma - ma2 >= VarianceBuy) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(ma - ma2 <= VarianceSell) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+
 

Hi,

ma=iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0);
ma2=iMA(NULL,25,0,0,MODE_SMA,PRICE_CLOSE,0);

is not correct. You try to Access MA from Timeframe 7 and Timeframe 25!

look iMA for correct parameters and try again :-)

 

u get any errors?

 

Hi!

EADeveloper - can you explain a little bit more? In the code I use ma - ma2 for my indicator. How would I get a moving average of weight x and a moving average of weight y, then do arithmetic operations against them?


qjol - I get no errors! :)

 
PipfX:

Hi!

EADeveloper - can you explain a little bit more? In the code I use ma - ma2 for my indicator. How would I get a moving average of weight x and a moving average of weight y, then do arithmetic operations against them?


qjol - I get no errors! :)


You was putting the wrong order of parameters..

iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0); = iMA(Symbol,Period,......

should be

iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)

 
EADeveloper:


You was putting the wrong order of parameters..

iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0); = iMA(Symbol,Period,......

should be

iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)


EADeveloper is right, i think u missing the TF parameter & supposed to be

ma=iMA(NULL,0,7,0,0,MODE_SMA,PRICE_CLOSE,0);
ma2=iMA(NULL,0,25,0,0,MODE_SMA,PRICE_CLOSE,0);
// or any TF u want
 

Thank you all for the help!


That fixed the program, and now I am able to spend time refining my strategy. ;D


Cheers!

 

PipfX:

The code compiles, it just won't take action when it is supposed to.


  1. lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
    ...
    if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
    ...
    if(lot<0.1) lot=0.1
    This assumes LotStep is 0.1 and MinLot is 0.1. Any other values breaks the code
        double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                LotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
            size = MathFloor(size/LotStep)*LotStep;
            if (size < minLot){     ...
    

    if(Volume[0]>1) return;
    
    Volume is unreliable, can skip ticks
    static datetime Time0;  bool    newBar  = Time0 < Time[0];
    if (!newBar) return(0);                   Time0 = Time[0];
    

  2. ma=iMA(NULL,7,0,0,MODE_SMA,PRICE_CLOSE,0);
    ma2=iMA(NULL,25,0,0,MODE_SMA,PRICE_CLOSE,0);
    
    Under the tester you can not access bar 0 for other timeframes/pairs thus 7,25 are wrong. If you meant length=7,25 then your missing TF=0. Since you're only looking at the start of a bar use ima(...,1) ima(...,0) may change/cross frequently.
  3. for(int i=0;i<OrdersTotal();i++)
    ...
    if(ma - ma2 >= VarianceBuy) OrderClose(...
    Always count down when closing multiple orders, or in the presense of EA's (on other charts) or manual orders.