and the for loop with all the array like the on in SMA calculation
void CalculateSimpleMA(int rates_total,int prev_calculated,constdouble &price[])
{
int i,limit;
//--- first calculation or number of bars was changedif(prev_calculated==0)//prima iterazione, cioè quando prev_calculated==0
{
limit=InpMAPeriod;
//--- calculate first visible valuedouble firstValue=0;
for(i=0; i<limit; i++)
firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else
limit=prev_calculated-1;////--- main loopfor(i=limit; i<rates_total && !IsStopped(); i++)
ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
//---
}
The EA code is this one
//+------------------------------------------------------------------+//| Moving Average.mq4 |//| Copyright 2005-2014, MetaQuotes Software Corp. |//| https://www.mql4.com |//+------------------------------------------------------------------+#property copyright"2005-2014, MetaQuotes Software Corp."#property link"https://www.mql4.com"#property description"Moving Average sample expert advisor"#define MAGICMA 20131111//--- Inputsinputdouble Lots =0.1;
inputdouble MaximumRisk =0.02;
inputdouble DecreaseFactor=3;
inputint MovingPeriod =12;
inputint MovingShift =6;
//+------------------------------------------------------------------+//| 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 volumeif(buys>0) return(buys);
elsereturn(-sells);
}
//+------------------------------------------------------------------+//| Calculate optimal lot size |//+------------------------------------------------------------------+double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders totalint losses=0; // number of losses orders without a break//--- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//--- calculate number of losses orders without a breakif(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 sizeif(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+//| Check for open order conditions |//+------------------------------------------------------------------+void CheckForOpen()
{
double ma;
int res;
//--- go trading only for first tiks of new barif(Volume[0]>1) return;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditionsif(Open[1]>ma && Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
//--- buy conditionsif(Open[1]<ma && Close[1]>ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}
//---
}
//+------------------------------------------------------------------+//| Check for close order conditions |//+------------------------------------------------------------------+void CheckForClose()
{
double ma;
//--- go trading only for first tiks of new barif(Volume[0]>1) return;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,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(Open[1]>ma && Close[1]<ma)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<ma && Close[1]>ma)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+//| OnTick function |//+------------------------------------------------------------------+voidOnTick()
{
//--- check for history and tradingif(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbolif(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+
and i find diffucult as before to understand loops.
Hi all,
i'm studying MQL4 through the book here and documentation, i'm also watching some youtube video, but everything is before build 600+.
So i need someone who can kindly help me adding some comment on Custom Moving Avarage Indicator, the one as example in MT4 and Moving Avarage EA.
Here i find difficul to understand
and the for loop with all the array like the on in SMA calculation
The EA code is this one
and i find diffucult as before to understand loops.
Thank You