hat mit deinem Code leider nicht geklappt, er hat jetzt garnicht geschlossen.
Ich habe hier einen Screenshot mit drangehängt, der rote MA ist der 21er.
Beim Pfeil hätte er nach dem Balkenende die Position schliessen sollen.
Hier mein Einbau-Code, hab ich es so eingebaut wie du gemeint hast?
Code:
extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=99999;
extern double TakeProfit=99999;
extern int TrailingStop=0;
extern int Slippage=3;
int timeframe;
static datetime update = 0;
int init()
{
timeframe = Period();
return(0);
}
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
if(update == 0 || TimeLocal() > update + Period() * 60)
{
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
double TheStopLoss=0;
double TheTakeProfit=0;
if( TotalOrdersCount()==0 )
{
int result=0;
}
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol()
//&&
//OrderMagicNumber()==MagicNumber
)
{
if(OrderType()==OP_BUY)
{
if((Close[0]<iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0))) //here is your close buy rule
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
}
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
{
if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
{
if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
update = TimeLocal()- TimeLocal()%timeframe;
}
return(0);
}
int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
if (OrderMagicNumber()==MagicNumber) result++;
}
return (result);
}