Hi, im new learner, can someone please help/guide/correct me on the following ea code ? the whole idea is ea will enter position when the screen show/appear up or down arrow buy/sell accordingly, unlimit position open also multipe pair can be use at the same time as well as 1 pair with different timeframe, can someone guide me on that ? thanks
//---- input parameters
extern double TakeProfit=35.0;
extern double Lots=0.1;
extern double LockProfit=15.0;
extern double StopLoss=10.0;
extern double Slippage=0;
extern int Fast.MA.Period = 5;
extern int Slow.MA.Period = 34;
extern int Signal.period = 5;
//---- buffers
double Buffer1[],
Buffer2[],
b2[],
b3[],
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
indicator_buffers(4);
SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3);
SetIndexArrow(0,242); // down 226 234 242
SetIndexBuffer(0,b2);
SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3);
SetIndexArrow(1,241); //UP 225 233 241
SetIndexBuffer(1,b3);
// These buffers are not plotted, just used to determine arrows
SetIndexBuffer (2,Buffer1);
SetIndexBuffer (3,Buffer2);
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//To define the Direction
//+------------------------------------------------------------------+
int Crossed (double MA5 , double MA34)
{
static int last_direction = 0;
static int current_direction = 0;
if(MA5>MA34)current_direction = 1; //up
if(MA5<MA34)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
{
}
int cnt, ticket, total;
int i, counted_bars=IndicatorCounted();
double MA5,MA34,down,up;
int limit=Bars-counted_bars;
Print(" print limit = ", limit);
if(counted_bars>0) limit++;
for(i=0; i<limit; i++)
{
if(Buffer1[i] > Buffer2[i] && Buffer1[i-1] < Buffer2[i-1])=down;
//b2[i] = High[i]+10*Point;
if(Buffer1[i] < Buffer2[i] && Buffer1[i-1] > Buffer2[i-1])=up;
//b3[i] = Low[i]-10*Point;
}
for(i=0; i<limit; i++)
{
MA5=iMA(NULL,0,Fast.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);
MA34=iMA(NULL,0,Slow.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);
Buffer1[i]=MA5-MA34;
}
for(i=0; i<limit; i++)
{
Buffer2[i]=iMAOnArray(Buffer1,Bars,Signal.period,0,MODE_LWMA,i);
}
total = OrdersTotal();
if(total < 1)
{
if(up)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,StopLoss,Ask+TakeProfit*Point,"My EA",770823,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(down)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,StopLoss,Bid-TakeProfit*Point,"My EA",770823,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(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(down)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
//close position
return(0); // exit
}
//check for trailing stop
if(LockProfit>0)
{
if(Bid-OrderOpenPrice()>Point*LockProfit)
{
if(OrderStopLoss()<Bid-Point*LockProfit)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*LockProfit,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(up)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
//close position
return(0); // exit
}
// check for trailing stop
if(LockProfit>0)
{
if((OrderOpenPrice()-Ask)>(Point*LockProfit))
{
if((OrderStopLoss()>(Ask+Point*LockProfit)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*LockProfit,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//-------end of the coding-------//
//---- input parameters
extern double TakeProfit=35.0;
extern double Lots=0.1;
extern double LockProfit=15.0;
extern double StopLoss=10.0;
extern double Slippage=0;
extern int Fast.MA.Period = 5;
extern int Slow.MA.Period = 34;
extern int Signal.period = 5;
//---- buffers
double Buffer1[],
Buffer2[],
b2[],
b3[],
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
indicator_buffers(4);
SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3);
SetIndexArrow(0,242); // down 226 234 242
SetIndexBuffer(0,b2);
SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3);
SetIndexArrow(1,241); //UP 225 233 241
SetIndexBuffer(1,b3);
// These buffers are not plotted, just used to determine arrows
SetIndexBuffer (2,Buffer1);
SetIndexBuffer (3,Buffer2);
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//To define the Direction
//+------------------------------------------------------------------+
int Crossed (double MA5 , double MA34)
{
static int last_direction = 0;
static int current_direction = 0;
if(MA5>MA34)current_direction = 1; //up
if(MA5<MA34)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
{
}
int cnt, ticket, total;
int i, counted_bars=IndicatorCounted();
double MA5,MA34,down,up;
int limit=Bars-counted_bars;
Print(" print limit = ", limit);
if(counted_bars>0) limit++;
for(i=0; i<limit; i++)
{
if(Buffer1[i] > Buffer2[i] && Buffer1[i-1] < Buffer2[i-1])=down;
//b2[i] = High[i]+10*Point;
if(Buffer1[i] < Buffer2[i] && Buffer1[i-1] > Buffer2[i-1])=up;
//b3[i] = Low[i]-10*Point;
}
for(i=0; i<limit; i++)
{
MA5=iMA(NULL,0,Fast.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);
MA34=iMA(NULL,0,Slow.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);
Buffer1[i]=MA5-MA34;
}
for(i=0; i<limit; i++)
{
Buffer2[i]=iMAOnArray(Buffer1,Bars,Signal.period,0,MODE_LWMA,i);
}
total = OrdersTotal();
if(total < 1)
{
if(up)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,StopLoss,Ask+TakeProfit*Point,"My EA",770823,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(down)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,StopLoss,Bid-TakeProfit*Point,"My EA",770823,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(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(down)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
//close position
return(0); // exit
}
//check for trailing stop
if(LockProfit>0)
{
if(Bid-OrderOpenPrice()>Point*LockProfit)
{
if(OrderStopLoss()<Bid-Point*LockProfit)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*LockProfit,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(up)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
//close position
return(0); // exit
}
// check for trailing stop
if(LockProfit>0)
{
if((OrderOpenPrice()-Ask)>(Point*LockProfit))
{
if((OrderStopLoss()>(Ask+Point*LockProfit)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*LockProfit,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//-------end of the coding-------//