//+------------------------------------------------------------------+//| Moving Average.mq4 |//| Copyright ?2005, MetaQuotes Software Corp. |//| https://www.metaquotes.net// |//+------------------------------------------------------------------+#define MAGICMA 20050610externdoubleLots = 0.1;
externdoubleMaximumRisk = 0.02;
externdoubleDecreaseFactor = 3;
externdoubleMovingPeriod = 12;
externdoubleMovingShift = 6;
//+------------------------------------------------------------------+//| Calculate open positions |//+------------------------------------------------------------------+intCalculateCurrentOrders(stringsymbol){intbuys=0,sells=0;
//----for(inti=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 |//+------------------------------------------------------------------+doubleLotsOptimized(){doublelot=Lots;
intorders=HistoryTotal(); // history orders totalintlosses=0; // number of losses orders without a break//---- select lot sizelot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a breakif(DecreaseFactor>0){for(inti=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 |//+------------------------------------------------------------------+voidCheckForOpen(){doublema;
intres;
//---- 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 |//+------------------------------------------------------------------+voidCheckForClose(){doublema;
//---- 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(inti=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)OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}if(OrderType()==OP_SELL){if(Open[1]<ma && Close[1]>ma)OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}}//----}//+------------------------------------------------------------------+//| Start function |//+------------------------------------------------------------------+voidstart(){//---- check for history and tradingif(Bars<100 || IsTradeAllowed()==false)return;
//---- calculate open orders by current symbolif(CalculateCurrentOrders(Symbol())==0)CheckForOpen();
elseCheckForClose();
//----}//+------------------------------------------------------------------+
this is the script i downloaded from metatrader as a learning tool. i a am writing
a sample one with 3 moving averages, wandering about this line :
this line is called twice in this script, if i use 3 moving averages, they will
each be called twice, taking up a lot more time to calculate and allow for slippage,
could anyone give me some pointers to how to declare it once? i tried
int init() and declaring the moving average under it, but it doesnt work.
please!
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
this line is called twice in this script, if i use 3 moving averages, they will each be called twice, taking up a lot more time to calculate and allow for slippage, could anyone give me some pointers to how to declare it once? i tried
int init() and declaring the moving average under it, but it doesnt work.
please!