extern int TP=300;
extern int SL=300;
int MagicNumber=12345;
int start()
{
OpenOrder();
if(Ask-avg()>=100*Point)
{
if(OrderType()==OP_BUY)
{
CloseAllOrder();
}
}
if(avg()-Bid>=100*Point)
{
if(OrderType()==OP_SELL)
{
CloseAllOrder();
}
}
return(0);
}
double sum;
int count;
double average;
int avg()
{
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol())
{
sum=sum+OrderOpenPrice();
count++;
average=sum/count;
}
}
Comment("average=",average);
return(average);
}
void OpenOrder()
{
double tpb=Ask+TP*Point;
double slb=Ask-SL*Point;
double tps=Bid-TP*Point;
double sls=Bid+SL*Point;
if(OrdersTotal()==0)
{
if(iOpen(Symbol(),PERIOD_D1,0)-Ask >=500*Point)
{
OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,tpb,"My order",MagicNumber,0,Green);
}
if(Bid-iOpen(Symbol(),PERIOD_D1,0)>=500*Point)
{
OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,tps,"My order",MagicNumber,0,Red);
}
}
if(OrdersTotal()>0)
{
double LastOpenPrice;
OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
LastOpenPrice=OrderOpenPrice();
if(OrderType()==OP_BUY)
{
if(LastOpenPrice-Ask>=200*Point)
{
OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,"OOO",MagicNumber,0,Green);
}
}
if(OrderType()==OP_SELL)
{
if(Bid-LastOpenPrice>=200*Point)
{
OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0,"OOO",MagicNumber,0,Red);
}
}
}
}
int CloseAllOrder()
{
int total=OrdersTotal();
int x=0;
while(x<1)
{
for(int i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
int type=OrderType();
bool result=false;
switch(type)
{
case OP_BUY:result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Pink);
break;
case OP_SELL:result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Pink);
break;
}
}
if(OrdersTotal()==0)
x=1;
}
return(0);
}