Forex mt4 mt5 trading free รับสอนเขียนเขียน EA จนกว่าจะเขียนเป็น สอนเขียนเป็นราย ชม. ทั้ง on line และ นอกสถานที่ ติดต่อได้ที่ www.appsgoods.com
Amps
Tuesday, February 23, 2016
Moving Average Activate Open and Close order Function MQL4
Moving Average Activate Open and Close order Function MQL4
Basic Function Moving Average Multi order EA Example MQL4
bool IsTradeAllowed | |
string symbol | |
datetime | Time to check status |
Full code on this part OnTick
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+
Finish Moving Average EA coding MQL4
Auto Close Order Moving Average EA MQL4
Auto Close Order Moving Average EA MQL4
This part very useful because this part we able to use protect the Take profit and Stop Loss, which would make us live in Forex Trading and got best profit and minimum loss.
MQL4 Basic function used Close Order Moving Average EA MQL4
Table Function OrderSelect MQL4
bool OrderSelect | It returns true if the function succeeds The order must be previously selected by the OrderSelect() function |
Int index | Order index or order ticket depending on the second parameter |
Int select | |
SELECT_BY_POS | index in the order pool |
SELECT_BY_TICKET | index is order ticket |
int pool=MODE_TRADES | |
MODE_TRADES (default) | order selected from trading pool(opened and pending orders) |
MODE_HISTORY | order selected from history pool (closed and canceled order) |
int OrderMagicNumber(); Returns an identifying (magic) number of the currently selected order
string OrderSymbol() Returns symbol name of the currently selected order.
Table Function Order Type MQL4
int OrderType(); | |
Returned value | Should specific when we using |
OP_BUY | |
OP_SELL | |
OP_BUYLIMIT | |
OP_BUYSTOP | |
OP_SELLLIMIT | |
OP_SELLSTOP |
double Open[] contains open prices of each bar of the current chart
double Close[] contains close prices for each bar of the current chart
Table Function Order Close MQL4
bool OrderClose | Closes opened order |
Int ticket | Unique number of the order ticket |
Double lots | Number of lots |
Double price | Closing price |
Int slippage | Value of the maximum price slippage in points |
color arrow_color | Color of the closing arrow |
long Volume[] contains tick volumes of each bar of the current chart
moving average EA close order conditions:
Close Buy when Candle open price over MA reference and close candle price under ma
Open[1]>ma && Close[1]<ma
Close Sell when Candle open price under MA reference and close candle price over ma
Open[1]<ma && Close[1]>ma
Full code moving average close order
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma;
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- get Moving Average
//original ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//new
ma=iMA(NULL,TimeFrameUsed,MovingPeriod,MovingShift,EMA_method1,Applied_Price1,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;
}
}
//---
}
This EA prepare example for activate open and close order, next chapster we would find out how to.
Moving Average EA Open Order with Modify Take profit and Stop Loss
Moving Average EA Open Order with Modify Take profit and Stop Loss
In previous chapter we guide moving average properties. In this chapster we would coding Moving Average EA MQL4 for modify take profit and Stop Loss.
Mention to Forex Broker have the exchange digits show 3,5 and 4 digits, which modify Take profit and Stop Loss that depend on broker we trading.
extern string TP_SL = " --- TP and Stop Loss ---";
extern bool Broke3_5Digits = True;
extern double My_TP = 10;
extern double My_SL = 10;
And Moving Average Properties part we would always change as below
//Add extern parameter as Label
extern string MovingAvr = " --- Moving Average parameter ---";
//Change from original
extern int MovingPeriod =12;
extern int MovingShift =6;
//Add properties
extern ENUM_TIMEFRAMES TimeFrameUsed = PERIOD_CURRENT;
extern ENUM_MA_METHOD EMA_method1 = MODE_SMA;
extern ENUM_APPLIED_PRICE Applied_Price1 = PRICE_CLOSE;
In checking the open order we would add the condition to design broker digits using as follow
void CheckForOpen()
{
double ma;
int res;
// calculate TP and Stop Loss
double BrokerTPSell,BrokerTPBuy;
double BrokerSLSell,BrokerSLBuy;
if(Broke3_5Digits=True)
{
if (My_TP!=0)
{
BrokerTPSell = Bid-((My_TP*Point)*10);
BrokerTPBuy = Ask+((My_TP*Point)*10);
}
if (My_TP==0)
{
BrokerTPSell = 0;
BrokerTPBuy = 0;
}
if (My_SL!=0)
{
BrokerSLSell = Bid+((My_SL*Point)*10);
BrokerSLBuy = Ask-((My_SL*Point)*10);
}
if (My_SL==0)
{
BrokerSLSell = 0;
BrokerSLBuy = 0;
}
}
if(Broke3_5Digits=False)
{
if (My_TP!=0)
{
BrokerTPSell = Bid-(My_TP*Point);
BrokerTPBuy = Ask+(My_TP*Point);
}
if (My_TP==0)
{
BrokerTPSell = 0;
BrokerTPBuy = 0;
}
if (My_SL!=0)
{
BrokerSLSell = Bid+(My_SL*Point);
BrokerSLBuy = Ask-(My_SL*Point);
}
if (My_SL==0)
{
BrokerSLSell = 0;
BrokerSLBuy = 0;
}
}
So the open order we would put the value from Moving Average EA as below
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- get Moving Average
// original ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//new
ma=iMA(NULL,TimeFrameUsed,MovingPeriod,MovingShift,EMA_method1,Applied_Price1,0);
//--- sell conditions
if(Open[1]>ma && Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,BrokerSLSell,BrokerTPSell,"",MAGICMA,0,Red);
return;
}
//--- buy conditions
if(Open[1]<ma && Close[1]>ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,BrokerSLBuy,BrokerTPBuy,"",MAGICMA,0,Blue);
return;
}
//---
}
In next chapster we would detail for auto close order
Subscribe to:
Posts (Atom)