Senin, 17 Desember 2012

ContohEA3.mq4


//+------------------------------------------------------------------+
//| ContohEA3.mq4 |
//| Copyright © 2008, Forexindo |
//| http://www.forexindo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Forexindo"
#property link "http://www.forexindo.com"

extern double RiskPercent=5;
extern int StopLoss=3000;
extern int TakeProfit=3000;
extern string txComment="Order EA1";
extern int MagicNumber=12345;
extern double Lots=0.1;
extern int Slippage=5;


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

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if (BuySignal() ) //kalau ada signal buy
{
Print("Switch to buy");

//1=sell
closePos(1);
if (bolehTrade())
{
OrderSend(Symbol(),OP_BUY,itungLot(),Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,txComment,MagicNumber,Blue);
}
}
else if (SellSignal() ) //kalau ada signal sell
{
Print("Switch to sell");

//0=buy

closePos(0);
if (bolehTrade())
{
OrderSend(Symbol(),OP_SELL,itungLot(),Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,txComment,MagicNumber,Red);
}
}


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

bool BuySignal()
{
//kalau harga close candle sebelumnya lebih besar/diatas EMA 10 pada candle sebelumnya
if (iClose(Symbol(),0,1) > iMA(Symbol(),0,10,0,MODE_EMA,PRICE_CLOSE,1) )
{
return(true);
}
else
{
return(false);
}
}

bool SellSignal()
{
//kalau harga close candle sebelumnya lebih kecil/dibawah EMA 10 pada candle sebelumnya
if (iClose(Symbol(),0,1) < iMA(Symbol(),0,10,0,MODE_EMA,PRICE_CLOSE,1) )
{
return(true);
}
else
{
return(false);
}
}

bool bolehTrade()
{
//kalau jumat, EA ga trade ; atau kalau ada order terbuka, EA juga ga trade
if (DayOfWeek()==5 || OrdersTotal()>0) { return (false); } else { return(true); }
}

double itungLot()
{
//ini untuk itung lot secara otomatis berdasarkan SL dan persen risk
double xLots=Lots;
//xLots=NormalizeDouble(AccountBalance()*RiskPercent/100 /StopLoss / 10,1);
return (xLots);
}

int closePos(int otype)
{

for (int a=0; a<OrdersTotal(); a++)
{
Print("Order found");
OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber()==MagicNumber)
{
if (otype==0)
{
if (OrderType()==OP_BUY)
{
Print("Closing buy");
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
}
}
else if (otype==1)
{
if (OrderType()==OP_SELL)
{
Print("Closing sell");
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
}
}
}
}
}

Tidak ada komentar:

Posting Komentar