Hallo in die Runde Seit kurzem versuche ich mich in der MQL-Programmierung. Habe auch entsprechende Foren durchforstet und mitgelesen. Nun habe ich aber ein Problem und ehe ich verzweifle stelle ich das mal in die Runde. Der EA, den ich mir gebastelt habe, habe ich als Demo laufen lassen. Im Journal erkenne ich, daß er entsprechende Signale liefert. Bei einem Bachtest scheitere ich aber immer wieder wieder kläglich. Was ist hier grundsätzlich falsch?
#define MagicNumber 1001
double Profit = 50;
double Stopp = 30;
int TimeClose = 0;
double lot = 0.1;
double Slippage = 2;
datetime prevtime = 0;
int Fall;
int Fall1_vHP, Fall3_vHP;
int i_CCI_long, i_CCI_short, CCI_long, CCI_short;
double CCI50_0, Diff_CCI5, Osma1_Diff0, Osma5_Diff0, Osma5_Diff1, MACD5_Diff0, MACD5_Diff1, Osma5;
double MACD1_Diff0;
int i_entry_long, i_entry_short, entry_long, entry_short;
double Kauf;
int Pip;
int Stop0; int Stop = -10;
int Stop_1; int Stop_exit;
int i_Stop_long, i_Stop_short;
extern int Start_Trailing_Stop = -15;
int order_long = 0; int order_short = 0;
int exit = 0; int exit_long = 0; int exit_short = 0;
int buys, sells;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//Ermittlung der Ausgangssituation (Fall)
/
CCI50_0 = iCCI(NULL,5,14,PRICE_TYPICAL,0);
Diff_CCI5 = iCCI(NULL,5,14,PRICE_TYPICAL,0) - iCCI(NULL,5,14,PRICE_TYPICAL,1);
Osma5_Diff0 = 10000*(iOsMA(NULL,5,12,26,9,PRICE_TYPICAL,0)-iOsMA(NULL,5,12,26,9,PRICE_TYPICAL,1));
if(CCI50_0 < -130) Fall = 1;
if(CCI50_0 > -130 && CCI50_0 < 130) Fall = 2;
if(CCI50_0 > 130) Fall = 3;
//if (Fall == 1 && (Diff_CCI5 > 0 || Osma5_Diff0 > 0)) Fall1_vHP = 1;
//if (Fall == 3 && (Diff_CCI5 < 0 || Osma5_Diff0 < 0)) Fall3_vHP = 1;
// Alert("Fall: ", Fall, ", CCI: ", CCI50_0, ", ", Fall1_vHP, ", ", Fall3_vHP);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int CalcCurOrd()
{
buys=0;sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//----
if(buys>0) return(buys);
else return(-sells);
}
double PointX()
{
if (Digits==5 || Digits ==3)
return(Point*10); else return(Point);
}
void Count_for_CCI5()
{
CCI50_0 = iCCI(NULL,5,14,PRICE_TYPICAL,0);
Diff_CCI5 = iCCI(NULL,5,14,PRICE_TYPICAL,0) - iCCI(NULL,5,14,PRICE_TYPICAL,1);
if(CCI50_0 > -130 && CCI50_0 < 130) Fall = 2;
if(CCI50_0 > 130 && Fall == 2)
{
Fall = 3;
Alert("CCI5-aktuell, Fall: ", Fall, ", CCI5: ", CCI50_0);
}
if(CCI50_0 < -130 && Fall == 2)
{
Fall = 1;
Alert("CCI5-aktuell, Fall: ", Fall, ", CCI5: ", CCI50_0);
}
if (Fall == 1 && Fall1_vHP == 0)
{
if (Diff_CCI5 > 0 && i_CCI_long == 0) { CCI_long = 1;
{ if (i_CCI_long == 0) Alert("CCI_long: ", CCI_long);
// Alert(OrdersTotal(), " ", buys, " ", sells, " ", OrderMagicNumber());
i_CCI_long = 1;
}
}
}
if (Fall == 3 && Fall3_vHP == 0)
{
if (Diff_CCI5 < 0 && i_CCI_short == 0) { CCI_short = 1;
{ if (i_CCI_short == 0) Alert("CCI_short: ", CCI_short);
// Alert(OrdersTotal(), " ", buys, " ", sells, " ", OrderMagicNumber());
i_CCI_short = 1;
}
}
}
return;
}
void CheckForEntry()
{
Count_for_CCI5();
Osma1_Diff0 = 10000*(iOsMA(NULL,1,12,26,9,PRICE_TYPICAL,0)-iOsMA(NULL,1,12,26,9,PRICE_TYPICAL,1));
Osma5_Diff0 = 10000*(iOsMA(NULL,5,12,26,9,PRICE_TYPICAL,0)-iOsMA(NULL,5,12,26,9,PRICE_TYPICAL,1));
MACD1_Diff0 = 1000*(iMACD(NULL,1,12,26,9,PRICE_TYPICAL,MODE_MAIN,0) - iMACD(NULL,1,12,26,9,PRICE_TYPICAL,MODE_MAIN,1));
Osma5 = iOsMA(NULL,1,12,26,9,PRICE_TYPICAL,0);
if (CCI_long == 1 && Osma1_Diff0 > 0 && MACD1_Diff0 > 0 && Osma5_Diff0 > 0 && i_entry_long == 0)
{ entry_long = 1;
// Alert("CCI_long: ", CCI_long, entry_long, " Ask: ", Ask);
i_CCI_long = 0;
CCI_long = 0;
}
if (CCI_short == 1 && Osma1_Diff0 < 0 && MACD1_Diff0 < 0 && Osma5_Diff0 < 0 && i_entry_short == 0)
{ entry_short = -1;
// Alert("CCI_short: ", CCI_short, entry_short, " Bid: ", Bid);
i_CCI_short = 0;
CCI_short = 0;
}
return;
}
void CheckForOpen()
{
int res;
if(prevtime == Time[0]) return;
CheckForEntry();
if (order_long == 0 && order_short == 0 && entry_long == 1)
{
res=OrderSend(Symbol(),OP_BUY,lot,Ask,Slippage,
Bid-PointX()*Stopp,Ask+PointX()*Profit,"long",MagicNumber,0,Green);
Kauf = Ask;
order_long = 1; Stop_1 = 0; i_Stop_long = 0; i_entry_long = 1;
// Alert ("res-long: ", res, " Ask: ", Ask, " Kauf: ", Kauf);
prevtime = Time[0]; return;
}
if (order_short == 0 && order_long == 0 && entry_short == -1)
{
res=OrderSend(Symbol(),OP_SELL,lot,Bid,Slippage,
Ask+PointX()*Stopp,Bid-PointX()*Profit,"short",MagicNumber,0,Red);
Kauf = Bid;
order_short = 1; Stop_1 = 0; i_Stop_short = 0; i_entry_short = 1;
// Alert ("res-short: ", res, " Bid: ", Bid, " Kauf: ", Kauf);
prevtime = Time[0]; return;
}
}
void CheckForExit()
{
Osma5_Diff0 = 10000*(iOsMA(NULL,5,12,26,9,PRICE_CLOSE,0)-iOsMA(NULL,5,12,26,9,PRICE_CLOSE,1));
Osma1_Diff0 = 10000*(iOsMA(NULL,1,12,26,9,PRICE_CLOSE,0)-iOsMA(NULL,1,12,26,9,PRICE_CLOSE,1));
MACD5_Diff0 = 10000*(iMACD(NULL,0,12,26,9,PRICE_TYPICAL,MODE_MAIN,0) - iMACD(NULL,0,12,26,9,PRICE_TYPICAL,MODE_MAIN,1));
MACD5_Diff1 = 10000*(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1) - iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2));
if (order_long == 1)
{
Profit = Bid - Kauf;
Pip = Profit / PointX();
if(Osma1_Diff0 > 0 || Osma5_Diff0 > 0 || MACD5_Diff0 > 0)
{ Stop0 = Pip;
Alert("Stop0: ", Stop0, " Stop_1: ", Stop_1);
if(Stop0 > Stop_1) {Stop_1 = Stop0;
Alert ("Stopp_long: ", Stop_1);
if(Stop_1 > 3) Stop = 3;
}
}
Stop_exit = Stop_1 - 6;
if(entry_short == -1 || Pip < Stop_exit || Pip <= Stop) {exit_long = 1;
Alert ("entry_short: ", entry_short, " Pip: ", Pip, " Stop_exit: ", Stop_exit, " Stop: ", Stop);
}
}
if (order_short == 1)
{
Profit = Kauf - Ask;
Pip = Profit / PointX();
if(Osma1_Diff0 < 0 || Osma5_Diff0 < 0 || MACD5_Diff0 < 0)
{ Stop0 = Pip;
Alert("Stop0: ", Stop0, " Stop_1: ", Stop_1);
if(Stop0 > Stop_1) {Stop_1 = Stop0;
Alert ("Stopp_short: ", Stop_1);
if(Stop_1 > 3) Stop = 3;
}
}
Stop_exit = Stop_1 - 6;
if(entry_long == 1 || Pip < Stop_exit || Pip <= Stop) {exit_short = 1;
Alert ("entry_long: ", entry_long, " Pip: ", Pip, " Stop_exit: ", Stop_exit, " Stop: ", Stop);
}
}
}
void CheckForClose()
{
if (order_long == 1 || order_short == 1) CheckForExit();
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY && order_long == 1 && exit_long == 1) //&& OrderType()==OP_BUY
{ Alert("Verkauf long: ",exit_long, ", Bid: ", Bid);
order_long = 0; exit_long = 0; i_Stop_long = 0; entry_long = 0; i_entry_long = 0;
OrderClose(OrderTicket(),lot,Bid,Slippage,Red);
return(0);
}
if (OrderType()==OP_SELL && order_short == 1 && exit_short == 1) //&& OrderType()==OP_SELL
{ Alert("Verkauf short: ",exit_short, ", Ask: ", Ask);
order_short = 0; exit_short = 0; i_Stop_short = 0; entry_short = 0; i_entry_short = 0;
OrderClose(OrderTicket(),lot,Bid,Slippage,Green);
return(0);
}
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(CalcCurOrd()==0) CheckForOpen();
if(CalcCurOrd()!=0) CheckForClose();
//----
return(0);
}
//+------------------------------------------------------------------+
Habe den Quelltext auch als Datei angehängt. Wäre sehr schön, wenn da ein "Versierter" mich aufklären könnte.Test26.mq4