下面是我编写的一个EA程序,订单开单没问题,平单时候和预期的不一样,希望多单在当前周期开盘价格如果低于前五个周期的最低价则平仓单,空单反之,求高手指点。
//+------------------------------------------------------------------+
//| PBX Sample.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
extern double TakeProfit = 500;
extern double Lots = 1;
extern double TrailingStop = 500;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double PBX1Current;
double PBX6Current,PBX1Previous;
double PBX6Previous;
int cnt, ticket, total;
datetime Closetime,Opentime;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
PBX1Current = (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 0) + iMA(NULL, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 0) + iMA(NULL, 0, 16, 0, MODE_SMA, PRICE_CLOSE, 0)) / 3.0;
PBX6Current = (iMA(NULL, 0, 24, 0, MODE_EMA, PRICE_CLOSE, 0) + iMA(NULL, 0, 48, 0, MODE_SMA, PRICE_CLOSE, 0) + iMA(NULL, 0, 96, 0, MODE_SMA, PRICE_CLOSE, 0)) / 3.0;
PBX1Previous = (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) + iMA(NULL, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 1) + iMA(NULL, 0, 16, 0, MODE_SMA, PRICE_CLOSE, 1)) / 3.0;
PBX6Previous = (iMA(NULL, 0, 24, 0, MODE_EMA, PRICE_CLOSE, 1) + iMA(NULL, 0, 48, 0, MODE_SMA, PRICE_CLOSE, 1) + iMA(NULL, 0, 96, 0, MODE_SMA, PRICE_CLOSE, 1)) / 3.0;
total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(PBX1Current>PBX6Current&&PBX1Previous<PBX6Previous)// 金叉
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,10,Ask-TrailingStop*Point,0,"PBX sample",787912,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(),OrderOpenTime());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(PBX1Current<PBX6Current && PBX1Previous>PBX6Previous)//死叉
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+TrailingStop*Point,0,"PBX sample",787912,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, 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(iOpen(Symbol(),0,0)<Low[iLowest(Symbol(),0,MODE_LOW,5,1)])
{
OrderClose(OrderTicket(),OrderLots(),Bid,10,Violet); // close position
if(OrderSelect(16384,SELECT_BY_TICKET,MODE_HISTORY)==true)
Closetime=OrderCloseTime();
return(0); // exit
}
else{
Print(iOpen(Symbol(),0,0),"Low",Low[iLowest(Symbol(),0,MODE_LOW,5,1)]);
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
// if(Bullmediancurrent>Bullmedianprevious )
//if(SigmaCurrent>SigmaPrevious)
if(iOpen(Symbol(),0,0)>High[iHighest(Symbol(),0,MODE_HIGH,5,1)])
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
else
{Print(iOpen(Symbol(),0,0),"High",High[iHighest(Symbol(),0,MODE_HIGH,5,1)]);
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
// the end. |