Bonjour à tous, je voudrais que mon EA (stratégie simple) ferme les ordres au CROISEMENT des moyennes mobiles, tout simplement.
Mais je n'arrive pas à trouver "la formule" qui peut exécuter cette fonction.
J'ai pourtant essayé des tas de solutions, comme par ex: if(Ask<=(ma5(1)<ma8(1)&&ma5(0)>ma8(0))).....CloseAll(); etc...et bien d'autres,
mais ça ne donne rien. Pire, à chaque modif, le compilateur m'indique: 0 errors/ 0 warning ! Mais sur le test, il ne se passe rien.
Le résultat est tjs le même, comme si je n'avais rien ajouté ou écrisdans le code.
Autrement, L'EA fonctionne à merveille, il exécute les ordres Buy/Sell , ferme au TP et SL, sans problème.
Pouvez-vous m'aider pour trouver ce bug svp?
Je vous remercie d'avance.
Stéphane.
Mais je n'arrive pas à trouver "la formule" qui peut exécuter cette fonction.
J'ai pourtant essayé des tas de solutions, comme par ex: if(Ask<=(ma5(1)<ma8(1)&&ma5(0)>ma8(0))).....CloseAll(); etc...et bien d'autres,
mais ça ne donne rien. Pire, à chaque modif, le compilateur m'indique: 0 errors/ 0 warning ! Mais sur le test, il ne se passe rien.
Le résultat est tjs le même, comme si je n'avais rien ajouté ou écrisdans le code.
Autrement, L'EA fonctionne à merveille, il exécute les ordres Buy/Sell , ferme au TP et SL, sans problème.
Pouvez-vous m'aider pour trouver ce bug svp?
Je vous remercie d'avance.
Stéphane.
Code
[ input string EAsettings = "[EA settings]"; //Paramètres
input double lots = 0.1; //Lot size
input double TakeProfit = 20; //Take profit
input double StopLoss = 0; //SoptLoss
input double distance= 0; //X Pips (Distance)
input int MagicNumber = 55545; //Magic Nb
input string EA_Comment = ""; // ~~~~
//-------------
input string Rsi = "RSI Settings"; //RSI
input int rsi_period = 14; //Rsi period
input ENUM_APPLIED_PRICE rsi_applied_price=PRICE_CLOSE;//~~
//-------------
input string Ma1 = "[===MA1===]"; //Fast MA
input int ma_period1 = 5; //MA 1 period
input ENUM_MA_METHOD ma_type1 = MODE_EMA; //MA 1 type
//-------------
input string Ma2 = "[===MA2===]"; //Low MA
input int ma_period2 = 8; //MA 2 period
input ENUM_MA_METHOD ma_type2 = MODE_EMA; //MA 2 type
datetime time;
double _point=1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
string _sym=Symbol();
double _digits=MarketInfo(_sym,MODE_DIGITS);
if(_digits==5||_digits==3) _point=1/MathPow(10,(_digits-1));
if(_digits==4||_digits==2) _point=1/MathPow(10,(_digits));
if(_digits==1) _point=0.1;
EventSetTimer(60);
time=Time[0];
{ return (0); }
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if( time==Time[0])return;
time=Time[0];
//-----Fonctions Achat et Vente ----------------
if(OrdersTotalT(OP_BUY)==0 && ma1(1)>ma2(1) && Ask>ma1(0)+distance*_point && rsi(1)>=50) SendOrder(OP_BUY);
if(OrdersTotalT(OP_SELL)==0 && ma1(1)<ma2(1) && Bid<ma1(0)-distance*_point && rsi(1)<50) SendOrder(OP_SELL);
//-------- Fonction de fermeture d'ordres au croisement des moyennes mobiles --------------
double Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0)); //(buy)
double Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0)); //(sell)
if (Ask == Cr1) CloseAll();
if (Bid == Cr2) CloseAll();
//-----------------------------------------------------------------------------------------
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool SendOrder(int type,double _price=0)
{
while(IsTradeContextBusy());
int ticket=-1;
double SL,TP;
if(type==OP_BUY)
{
if(StopLoss==0) {SL=0;}else{SL=Ask-StopLoss*_point;}
if(TakeProfit==0) {TP=0;} else {TP=Ask+TakeProfit*_point;}
ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue);
}
if(type==OP_SELL)
{
if(StopLoss==0){SL=0;}else{SL=Bid+StopLoss*_point;}
if(TakeProfit==0) {TP=0;} else {TP=Bid-TakeProfit*_point;}
ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotManage(),Symbol()),Bid,3,SL,TP,EA_Comment,MagicNumber,0,clrRed);
}
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(false);
}
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LotManage()
{
return (NormalizeDouble(lots,2));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double NormalizeLots(double _lots,string pair="")
{
if(pair=="") pair=Symbol();
double lotStep=MarketInfo(pair,MODE_LOTSTEP),
minLot=MarketInfo(pair,MODE_MINLOT);
_lots=MathRound(_lots/lotStep)*lotStep;
if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT);
if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT);
return(_lots);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double rsi(int _shift)
{ return(iRSI(NULL,0,rsi_period,rsi_applied_price,_shift)); }
double ma1(int _shift=0)
{ return(iMA(NULL,0,ma_period1,0,ma_type1,PRICE_CLOSE,_shift));}
double ma2(int _shift=0)
{ return(iMA(NULL,0,ma_period2,0,ma_type2,PRICE_CLOSE,_shift));}
//+------------------------------------------------------------------+
// |
//-------------------------------------------------------------------+
int OrdersTotalT(int _type)
{
int _total=0;
for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type)
{
_total++;
}
}
return(_total);
}
//........................................................................................................
void CloseOrders(int type)
{
bool result=false;
int count=0,total=OrdersTotal();
for(count=total-1; count>=0; count--)
{
if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type)
{
if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);
else result=OrderDelete(OrderTicket());
}
}
}
//......................................................................................................
void CloseAll()
{
bool result=false;
for(int _count=OrdersTotal()-1; _count>=0; _count--)
{
RefreshRates();
if(OrderSelect(_count,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow);
}
}
}
}
//........................................................................................................]
Modifié le 2017-12-18 11:52:20 par
AliX
: [code] [/code]