诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4 EA > 正文

根据ATR和SAR指标开发的EA Color Candle EA.mq4

Color Candle EA根据ATR和SAR指标信号在进行自动交易。

源码:

//+----------------------------------------------------------+
//|                                       Color Candle EA.mq4|
//|               Copyright @2005, MetaQuotes Software Corp. |
//+----------------------------------------------------------+
#property copyright "Copyright @2005, MetaQuotes Software Corp."
#property link      "http://www.125808047.com"

extern double Lots = 0.1;
extern double MaximumRisk = 0.03;
extern double stopFactor = 1;
extern double TakeProfit1 = 150;
extern double Stoploss1 = 140;
extern bool takeProfit = true;
extern bool sartrail = false;
extern bool MoneyManagement = false;

bool cg = false;
int MAGICMA=20070705; 
//+----------------------------------------------------------+
//| 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++;
}
}

if(buys>0) return(buys);
else return(-sells);
} 

double LotsOptimized()
{
double lot;
lot = NormalizeDouble( (AccountFreeMargin()*MaximumRisk)/( 
iATR(NULL,0,20,0)*(1/Point) )*0.1,1); 

if(lot<0.1) lot=0.1;
return(lot);
}
//+----------------------------------------------------------+
//| Check for open order conditions |
//+----------------------------------------------------------+
void CheckForOpen()
{
if(!NewBar())
return;
if(MoneyManagement)
Lots = LotsOptimized(); 

double close1 = Close[1];
double open1 = Open[1];

if ( close1< open1 )
{
openSell();
return;
}

if ( close1> open1 )
{
openBuy();
}
}

void openBuy(){
Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(), MODE_SPREAD) * Point;
int res;
if(takeProfit)
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,Ask+TakeProfit1*Point ,"preBar",MAGICMA,0,Blue);
else
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,0,"preBar",MAGICMA,0, Blue);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
Comment("preBar opened buy order " + Ask);
}
else Print("Error opening SELL order : ",GetLastError());
}

void openSell(){
Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(),MODE_SPREAD) * Point;
int res;
if(takeProfit)
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,Bid-TakeProfit1*Point,"preBar",MAGICMA,0,Red);
else
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,0,"preBar",MAGICMA,0 ,Red);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
Comment("preBar opened sell order " + Bid);
}
else Print("Error opening SELL order : ",GetLastError());
}

//+----------------------------------------------------------+
//| Check for close order conditions |
//+----------------------------------------------------------+
void CheckForClose()
{ 
for(int i=0;i<OrdersTotal();i++)
{
cg = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
if(!takeProfit && NewBar() && Open[1]> Close[1])
{ cg = OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
openSell();
}

if ( sartrail && (iSAR(NULL,0,0.02,0.2,1) > OrderStopLoss()) && 
(Bid > iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() < 
iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) > 
iSAR(NULL,0,0.02,0.2,2)))
{
cg = OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
}

}
if(OrderType()==OP_SELL)
{
if(!takeProfit && NewBar() && Open[1]< Close[1])
{ cg = OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
openBuy();
}

if (sartrail && (iSAR(NULL,0,0.02,0.2,1) < OrderStopLoss()) && (Ask < 
iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() > iSAR(NULL,0,0.02,0.2,1)) && 
(iSAR(NULL,0,0.02,0.2,1) < iSAR(NULL,0,0.02,0.2,2)))
{
cg = OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
}
}
}
}
}

//+----------------------------------------------------------+
//| Start function |
//+----------------------------------------------------------+
void OnTick()
{
if(Bars<100 || IsTradeAllowed()==false) return; 

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
//+----------------------------------------------------------
bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar){
lastbar=curbar;
return (true);
} else
{ return(false); }
} 
打赏
版权所有转载请注明标题及链接:峰汇在线 » 根据ATR和SAR指标开发的EA Color Candle EA.mq4
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏