
Kod: Zaznacz cały
//+------------------------------------------------------------------+
//| NOWA PIOTREK.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
extern double TakeProfit = 80;
extern double Lots = 0.01;
extern double TrailingStop = 1;
extern double MACDOpenLevel =10;
extern double MACDCloseLevel =10;
extern double MA1TrendPeriod = 24;
extern double MA2TrendPeriod = 9;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, Ma1Current, Ma1Previous;
double Ma2Current, Ma2Previous;
int cnt, ticket, total;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0);
}
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
Ma1Current=iMA(NULL,0,MA1TrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
Ma1Previous=iMA(NULL,0,MA1TrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
Ma2Current=iMA(NULL,0,MA2TrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
Ma2Previous=iMA(NULL,0,MA2TrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
total=OrdersTotal();
if(total<10)
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//---
if(MacdCurrent<0 &&
MacdCurrent>SignalCurrent &&
MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) &&
Ma1Current>Ma2Current)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd NOWA PIOTREK",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//---
if(MacdCurrent>0 &&
MacdCurrent<SignalCurrent &&
MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) &&
Ma1Current<Ma2Previous)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd NOWA PIOTREK",16384,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);
}
//---
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
if(OrderStopLoss()<Ask-Point*TrailingStop)
{
if(OrderType()==OP_BUY)
{
//---
if(MacdCurrent>0 &&
MacdCurrent<SignalCurrent &&
MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDCloseLevel*Point)&&
Ma2Current>Ma1Current)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
//---
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
//---
{
if(MacdCurrent<0 &&
MacdCurrent>SignalCurrent &&
MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDCloseLevel*Point) &&
Ma1Current<Ma2Current)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
//---
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);
}