Réalisation avec mon ami JL d'un EA ,utilisant un indicateur existant,avec la fonction Icustom
Etant débutant ,merci d'être indulgent et de corriger mes erreurs si vous en trouvez.
J'ai récupéré l'indicateur sur un forum et je l'ai légèrement modifié.
https://www.mql5.com/en/code/10351
Le problème de l' Icustom est qu'il est gourmand en ressource et ralentit les backtests.
De ce fait mes backtests ont été fait sur un court laps de temps avec des valeurs arrondies.
Je vais poster dans un premier temps l'indicateur indispensable a installer dans les indicateurs sur la plateforme.
Voici maintenant le programme de l'expert advisor qu'il faudra retester !
Etant débutant ,merci d'être indulgent et de corriger mes erreurs si vous en trouvez.
J'ai récupéré l'indicateur sur un forum et je l'ai légèrement modifié.
https://www.mql5.com/en/code/10351
Le problème de l' Icustom est qu'il est gourmand en ressource et ralentit les backtests.
De ce fait mes backtests ont été fait sur un court laps de temps avec des valeurs arrondies.
Je vais poster dans un premier temps l'indicateur indispensable a installer dans les indicateurs sur la plateforme.
Code
//+------------------------------------------------------------------+
//| Smoothed RSI Inverse Fisher Transform.mq4 |
//| © 2011 MaryJane@ForexFactory |
//| Indicator formula © 2010 Sylvain Vervoort, http://stocata.org/ |
//| Alert routine code © 2011 hanover |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MaryJane"
#property link "http://www.forexfactory.com/MaryJane"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 Yellow
#property indicator_width1 1
#property indicator_level1 20
#property indicator_level2 80
#property indicator_levelcolor DimGray
//--- input parameters
extern int RsiPeriod=40;
extern int EmaPeriod=10;
extern int AlertCandle = 1;
extern int AlertLevelUp = 20;
extern int AlertLevelDown = 80;
extern bool ShowChartAlerts = false;
extern string AlertEmailSubject = "";
extern int BarCount = 500;
datetime OldTime;
datetime LastAlertTime = -999999;
string AlertTextCrossUp = "SVE RSI I-Fish UP";
string AlertTextCrossDown = "SVE RSI I-Fish DOWN";
//--- buffers
double wma0[],wma1[],wma2[],wma3[],wma4[],wma5[],wma6[],wma7[],wma8[],wma9[];
double ema0[],ema1[],rainbow[],rsi[],srsi[],fish[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName(StringConcatenate("SVE RSI I-Fish (",RsiPeriod,",",EmaPeriod,")"));
//---- indicators
IndicatorBuffers(6);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,fish);
SetIndexBuffer(1,rainbow);
SetIndexBuffer(2,rsi);
SetIndexBuffer(3,ema0);
SetIndexBuffer(4,ema1);
SetIndexBuffer(5,srsi);
ArrayResize(wma0,BarCount); ArraySetAsSeries(wma0,true);
ArrayResize(wma1,BarCount); ArraySetAsSeries(wma1,true);
ArrayResize(wma2,BarCount); ArraySetAsSeries(wma2,true);
ArrayResize(wma3,BarCount); ArraySetAsSeries(wma3,true);
ArrayResize(wma4,BarCount); ArraySetAsSeries(wma4,true);
ArrayResize(wma5,BarCount); ArraySetAsSeries(wma5,true);
ArrayResize(wma6,BarCount); ArraySetAsSeries(wma6,true);
ArrayResize(wma7,BarCount); ArraySetAsSeries(wma7,true);
ArrayResize(wma8,BarCount); ArraySetAsSeries(wma8,true);
ArrayResize(wma9,BarCount); ArraySetAsSeries(wma9,true);
//---- reset counter
OldTime=Time[0];
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
if(counted_bars==0) limit-=1;
//---- resize/shift extra buffers on first and every next bar
if(Time[0]!=OldTime) SyncExtraBuffers();
int size=Bars;
ArrayResize(wma0,size);
ArrayResize(wma1,size);
ArrayResize(wma2,size);
ArrayResize(wma3,size);
ArrayResize(wma4,size);
ArrayResize(wma5,size);
ArrayResize(wma6,size);
ArrayResize(wma7,size);
ArrayResize(wma8,size);
ArrayResize(wma9,size);
//---- prepare partial averages
for(int i=limit; i>=0; i--) wma0[i]=iMA(NULL,0,2,0,MODE_LWMA,PRICE_CLOSE,i);
for(i = limit; i >= 0; i--) wma1[i] = iMAOnArray(wma0, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma2[i] = iMAOnArray(wma1, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma3[i] = iMAOnArray(wma2, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma4[i] = iMAOnArray(wma3, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma5[i] = iMAOnArray(wma4, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma6[i] = iMAOnArray(wma5, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma7[i] = iMAOnArray(wma6, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma8[i] = iMAOnArray(wma7, 0, 2, 0, MODE_LWMA, i);
for(i = limit; i >= 0; i--) wma9[i] = iMAOnArray(wma8, 0, 2, 0, MODE_LWMA, i);
//---- weigh the averages
for(i=limit; i>=0; i--)
{
rainbow[i]=(5*wma0[i]+4*wma1[i]+3*wma2[i]+2*wma3[i]+wma4[i]+wma5[i]+wma6[i]+wma7[i]+wma8[i]+wma9[i])/20;
}
//---- calculate rsi from rainbow smoothed price curve
for(i=limit; i>=0; i--) rsi[i]=0.1 *(iRSIOnArray(rainbow,0,RsiPeriod,i)-50);
//---- smooth the rsi with Vervoort zero lag MA
for(i = limit; i >= 0; i--) ema0[i] = iMAOnArray(rsi, 0, EmaPeriod, 0, MODE_EMA, i);
for(i = limit; i >= 0; i--) ema1[i] = iMAOnArray(ema0, 0, EmaPeriod, 0, MODE_EMA, i);
for(i = limit; i >= 0; i--) srsi[i] = ema0[i] + (ema0[i] - ema1[i]);
//---- do the fish
for(i=limit; i>=0; i--) fish[i]=((MathExp(2*srsi[i])-1)/(MathExp(2*srsi[i])+1)+1)*50;
//----
ProcessAlerts();
//----
return(0);
}
//+------------------------------------------------------------------+
//| Shift array elements on new bar |
//+------------------------------------------------------------------+
void SyncExtraBuffers()
{
int size=ArraySize(wma1);
for(int i=size-1; i>=0; i--)
{
wma0[i + 1] = wma0[i];
wma1[i + 1] = wma1[i];
wma2[i + 1] = wma2[i];
wma3[i + 1] = wma3[i];
wma4[i + 1] = wma4[i];
wma5[i + 1] = wma5[i];
wma6[i + 1] = wma6[i];
wma7[i + 1] = wma7[i];
wma8[i + 1] = wma8[i];
wma9[i + 1] = wma9[i];
}
//---- reset counter
OldTime=Time[0];
}
//+------------------------------------------------------------------+
// Alert routine by hanover
// http://www.forexfactory.com/showthread.php?t=299520
//+------------------------------------------------------------------+ //
int ProcessAlerts()
{ //
//+------------------------------------------------------------------+ //
if(AlertCandle>=0 && Time[0]>LastAlertTime)
{ //
//
// === Alert processing for crossover UP (indicator line crosses ABOVE signal line) === //
if(fish[AlertCandle]>AlertLevelUp && fish[AlertCandle+1]<=AlertLevelUp)
{ //
string AlertText = Symbol()+ "," + TFToStr(Period()) + ": " + AlertTextCrossUp; //
if(ShowChartAlerts) Alert(AlertText); //
if(AlertEmailSubject > "") SendMail(AlertEmailSubject,AlertText); //
} //
//
// === Alert processing for crossover DOWN (indicator line crosses BELOW signal line) === //
if(fish[AlertCandle]<AlertLevelDown && fish[AlertCandle+1]>=AlertLevelDown)
{ //
AlertText = Symbol()+ "," + TFToStr(Period()) + ": " + AlertTextCrossDown; //
if(ShowChartAlerts) Alert(AlertText); //
if(AlertEmailSubject > "") SendMail(AlertEmailSubject,AlertText); //
} //
//
LastAlertTime = Time[0]; //
} //
return(0); //
} //
//
//+------------------------------------------------------------------+ //
string TFToStr(int tf)
{ //
//+------------------------------------------------------------------+ //
if(tf == 0) tf = Period(); //
if(tf >= 43200) return("MN"); //
if(tf >= 10080) return("W1"); //
if(tf >= 1440) return("D1"); //
if(tf >= 240) return("H4"); //
if(tf >= 60) return("H1"); //
if(tf >= 30) return("M30"); //
if(tf >= 15) return("M15"); //
if(tf >= 5) return("M5"); //
if(tf >= 1) return("M1"); //
return(""); //
} //
// ============================================================================================================================================ //
Voici maintenant le programme de l'expert advisor qu'il faudra retester !
Code
//+------------------------------------------------------------------+
//| SVE RSI I FISH SYMPHO_v2.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=0;
extern double TakeProfit=10;
extern int TrailingStop=10;
extern int Slippage=3;
extern int RsiPeriod=72;
extern int EmaPeriod=144;
extern int LevelUp=60;
extern int LevelDown=20;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
int MyPoint=1;
if(Digits==3 || Digits==5) MyPoint=10;
double SVE_T1 = iCustom(NULL,0,"SVE_RSI_I-Fish",RsiPeriod,EmaPeriod,0,1);
double SVE_T2 = iCustom(NULL,0,"SVE_RSI_I-Fish",RsiPeriod,EmaPeriod,0,2);
double TheStopLoss=0;
double TheTakeProfit=0;
if( TotalOrdersCount()==0 )
{
int result=0;
if((SVE_T2<LevelDown)&&(SVE_T1>LevelDown)&&(SVE_T1<LevelUp)) // Here is your open buy rule
{
if(CheckMoneyForTrade(NULL,Lots,OP_BUY))
result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint*_Point;
if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint*_Point;
if(OrderSelect(result, SELECT_BY_TICKET))
bool modif1= OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
if((SVE_T2>LevelUp)&&(SVE_T1<LevelUp)&&(SVE_T1>LevelDown)) // Here is your open Sell rule
{
if(CheckMoneyForTrade(NULL,Lots,OP_SELL))
result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint*_Point;
if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint*_Point;
if(OrderSelect(result, SELECT_BY_TICKET))
bool modif2=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
}
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)
{
if((SVE_T2<LevelUp)&&(SVE_T1>LevelUp)) //here is your close buy rule
{
bool modif3= OrderClose(OrderTicket(),OrderLots(),MarketInfo(_Symbol,MODE_BID),Slippage,Red);
}
if(TrailingStop>0)
{
TrailingAlls(TrailingStop, MyPoint);
}
}
else if(OrderType()==OP_SELL)
{
if((SVE_T2>LevelDown)&&(SVE_T1<LevelDown)) // here is your close sell rule
{
bool modif5= OrderClose(OrderTicket(),OrderLots(),MarketInfo(_Symbol,MODE_ASK),Slippage,Red);
}
if(TrailingStop>0)
{
TrailingAlls(TrailingStop, MyPoint);
}
}
}
}
return(0);
}
int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS ,MODE_TRADES) == True)
if(OrderMagicNumber()==MagicNumber)
result++;
}
return (result);
}
void TrailingAlls(double trail, int t)
{
if(trail==0)
return;
//----
double stopcal=0;
int trade=0;
int trades=OrdersTotal();
double profitcalc=0;
bool Order_Modif=False;
for(trade=0;trade<trades;trade++)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==False)
{
Print("Select Failed");
continue;
}
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==True)
{
if(OrderSymbol()==Symbol())
{
//continue;
//LONG
if(OrderType()==OP_BUY && (OrderMagicNumber()==MagicNumber))// && OrderStopLoss()>=OrderOpenPrice())
{
//Print("Number = " + trade);
if(OrderStopLoss()<OrderOpenPrice() && Bid>OrderOpenPrice()+(trail*t*_Point) && CheckStopLoss_Takeprofit(OP_BUY,OrderOpenPrice(),OrderTakeProfit()))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
if(Order_Modif==True)
Print("BE BUY");
continue;
}
stopcal=NormalizeDouble((OrderStopLoss()+(trail*t*_Point)), _Digits);
profitcalc=OrderTakeProfit();
if(OrderStopLoss()>=OrderOpenPrice() && stopcal<Bid && CheckStopLoss_Takeprofit(OP_BUY,NormalizeDouble(OrderStopLoss()+(trail/2*t*Point), _Digits),profitcalc))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss()+(trail/2*t*Point), _Digits),profitcalc,0,Blue);
if(Order_Modif==False)
Print("Modif failed");
//continue;
}
}//LONG
//Shrt
if(OrderType()==OP_SELL && (OrderMagicNumber()==MagicNumber))// && OrderStopLoss()<=OrderOpenPrice())
{
//Print("Number = " + trade);
if(OrderStopLoss()>OrderOpenPrice() && Ask<OrderOpenPrice()-(trail*t*_Point) && CheckStopLoss_Takeprofit(OP_SELL,OrderOpenPrice(),OrderTakeProfit()))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
if(Order_Modif==True)
Print("BE SELL");
continue;
}
stopcal=NormalizeDouble((OrderStopLoss()-(trail*t*_Point)), _Digits);
profitcalc=OrderTakeProfit();
if(OrderStopLoss()<=OrderOpenPrice() && stopcal>Ask && CheckStopLoss_Takeprofit(OP_SELL,NormalizeDouble(OrderStopLoss()-(trail/2*t*Point), _Digits),profitcalc))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss()-(trail/2*t*Point), _Digits),profitcalc,0,Red);
if(Order_Modif==False)
Print("Modif failed");
//continue;
}
}
}
}
}//Shrt
}
bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type, double SL, double TP)
{
//--- get the SYMBOL_TRADE_STOPS_LEVEL level
int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
//---
bool SL_check=false,TP_check=false;
//--- check only two order types
switch(type)
{
//--- Buy operation
case ORDER_TYPE_BUY:
{
//--- check the StopLoss
SL_check=(Bid-SL>stops_level*10*_Point);
if(SL==0)
SL_check=True;
//--- check the TakeProfit
TP_check=(TP-Bid>stops_level*10*_Point);
if(TP==0)
TP_check=True;
//--- return the result of checking
return(SL_check&&TP_check);
}
//--- Sell operation
case ORDER_TYPE_SELL:
{
//--- check the StopLoss
SL_check=(SL-Ask>stops_level*10*_Point);
if(SL==0)
SL_check=True;
//--- check the TakeProfit
TP_check=(Ask-TP>stops_level*10*_Point);
if(TP==0)
TP_check=True;
//--- return the result of checking
return(TP_check&&SL_check);
}
break;
}
//--- a slightly different function is required for pending orders
return false;
}
bool CheckMoneyForTrade(string symb,double lots,int type)
{
double free_margin=AccountFreeMarginCheck(symb,type, lots);
//-- if there is not enough money
if(free_margin<0)
{
string oper=(type==OP_BUY)? "Buy":"Sell";
//fPrint("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
return(false);
}
//--- checking successful
return(true);
}
Modifié le 2019-08-11 16:44:10 par
lefeuvr3
: Mauvaise couleur a modifier