I wrote an EA for opening a trade when Stochastic of D1 and H4 and H1 are in agreement, however as soon as I drop the EA on the chart it's open a trade without respecting the strategy,
here are the code's line for the strategy,
double StoH4ValueS0, StoH4SignalS0, StoH4ValueS1, StoH4SignalS1;
double StoH1Value, StoH1Signal, StoD1Value, StoD1Signal ;
StoH4ValueS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH4SignalS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoH4ValueS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,Shiftt);
StoH4SignalS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,Shiftt);
StoH1Value = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH1Signal = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoD1Value = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_MAIN,0);
StoD1Signal = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_SIGNAL,0);
if(StoH4ValueS0>StoH4SignalS0 && StoH4ValueS1<StoH4SignalS1 && StoH1Value>StoH1Signal && StoD1Value>StoD1Signal && StoD1Value>LowLevel)
{ EnterLong (Sym, Lots, ""); }
else
if(StoH4ValueS0<StoH4SignalS0 && StoH4ValueS1>StoH4SignalS1 && StoH1Value<StoH1Signal && StoD1Value<StoD1Signal && StoD1Value<HighLevel)
{ EnterShrt (Sym, Lots, ""); }
}
return (0);
}
For those who want to see the entire code, here it is
//Ben Stochastic.mq4
#include <stderror.mqh>
//-----------------------------------------------Block 01----------------------------------------------------
datetime CurrTime = 0;
datetime PrevTime = 0;
string Sym = "";
int TimeFrame = 0;
int Shift = 1;
int SymDigits = 5;
double SymPoints = 0.0001;
//-----------------------------------------------Block 02----------------------------------------------------
extern bool UseCompletedBars = true;
extern int Shiftt=3;
extern int sto1=240;
extern int sto2=60;
extern int sto3=1440;
extern int K1=5;
extern int K2=5;
extern int K3=5;
extern int KE=5;
extern int LowLevel = 20;
extern int HighLevel = 80;
extern double Lots = 1.0;
extern int MagicNumber = 1235;
extern int ProfitTarget = 100;
extern int StopLoss = 0;
extern int Slippage = 3;
//-----------------------------------------------Block 03----------------------------------------------------
int init ()
{
Sym = Symbol();
TimeFrame = Period();
SymPoints = MarketInfo( Sym, MODE_POINT );
SymDigits = MarketInfo( Sym, MODE_DIGITS );
if ( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; }
else
if ( SymPoints == 0.00001) { SymPoints = 0.0001; SymDigits = 5; }
return (0);
}
//-----------------------------------------------Block 04----------------------------------------------------
int deinit () { return(0); }
//-----------------------------------------------Block 05----------------------------------------------------
int start ()
{
int RealTime = 0;
if (UseCompletedBars)
{
CurrTime = iTime ( Sym, TimeFrame, 1);
if ( CurrTime == PrevTime )
{
return(0);
}
PrevTime = CurrTime;
RealTime = 1;
}
if ( CountAll ( Sym, MagicNumber) == 0 )
{
double StoH4ValueS0, StoH4SignalS0, StoH4ValueS1, StoH4SignalS1;
double StoH1Value, StoH1Signal, StoD1Value, StoD1Signal ;
StoH4ValueS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH4SignalS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoH4ValueS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,Shiftt);
StoH4SignalS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,Shiftt);
StoH1Value = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH1Signal = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoD1Value = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_MAIN,0);
StoD1Signal = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_SIGNAL,0);
if(StoH4ValueS0>StoH4SignalS0 && StoH4ValueS1<StoH4SignalS1 && StoH1Value>StoH1Signal && StoD1Value>StoD1Signal && StoD1Value>LowLevel)
{ EnterLong (Sym, Lots, ""); }
else
if(StoH4ValueS0<StoH4SignalS0 && StoH4ValueS1>StoH4SignalS1 && StoH1Value<StoH1Signal && StoD1Value<StoD1Signal && StoD1Value<HighLevel)
{ EnterShrt (Sym, Lots, ""); }
}
return (0);
}
//-----------------------------------------------Block 06----------------------------------------------------
int CountAll (string Symbole, int Magic)
{
int count = 0;
for (int i = OrdersTotal() - 1; i >=0; i --)
{
OrderSelect (i, SELECT_BY_POS,MODE_TRADES);
if ( OrderMagicNumber () != Magic) continue;
if ( OrderSymbol() != Symbole ) continue;
if ( OrderType () == OP_BUY) { count ++; }
else
if ( OrderType () == OP_SELL) { count ++; }
}
return (count);
}
//-----------------------------------------------Block 07----------------------------------------------------
double StopLong(double price,double stop,double point,double SymDgts)
{
if(stop==0) {return(0);}
else
{return(NormalizeDouble(price-(stop*point),SymDgts));}
}
double StopShrt(double price,double stop,double point,double SymDgts)
{
if(stop==0) {return(0);}
else
{return(NormalizeDouble(price+(stop*point),SymDgts));}
}
double TakeLong(double price, double take,double point, double SymDgts)
{
if(take==0) { return(0); }
else
{return(NormalizeDouble(price +(take*point),SymDgts));}
}
double TakeShrt(double price,double take,double point, double SymDgts)
{
if(take==0){return(0);}
else
{return(NormalizeDouble(price-(take*point),SymDgts));}
}
//-----------------------------------------------Block 08----------------------------------------------------
int EnterLong(string FinalSymbol,double FinalLots,string EA_Comment)
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;
while( !OrderLoop)
{
while (IsTradeContextBusy()) { Sleep (10); }
RefreshRates();
double SymAsk = NormalizeDouble(MarketInfo(FinalSymbol,MODE_ASK), SymDigits);
double SymBid = NormalizeDouble(MarketInfo(FinalSymbol,MODE_BID), SymDigits);
Ticket = OrderSend(FinalSymbol,OP_BUY,FinalLots,SymAsk,0,0.0,0.0,EA_Comment,MagicNumber,0,CLR_NONE);
int Err = GetLastError();
switch(Err)
{
case ERR_NO_ERROR: OrderLoop = true;
if(OrderSelect(Ticket,SELECT_BY_TICKET))
{OrderModify(Ticket,OrderOpenPrice(),StopLong(SymBid, StopLoss, SymPoints, SymDigits), TakeLong(SymAsk, ProfitTarget, SymPoints, SymDigits), 0, CLR_NONE);}
break;
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;
case ERR_INVALID_STOPS: OrderLoop = true; Print("Invalid Stops" );break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print("Invalid Lots" );break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print("Market Close" );break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print("Trades Disabled" );break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print("Not Enough Money");break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print("Too Many Orders" );break;
case ERR_NO_RESULT:
default: OrderLoop = true; Print("Unknown Error -"+Err);break;
}
if(TryCount>10) {OrderLoop=true;}
}
return(Ticket);
}
//-----------------------------------------------Block 09----------------------------------------------------
int EnterShrt(string FinalSymbol, double FinalLots, string EA_Comment)
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;
while (!OrderLoop)
{
while(IsTradeContextBusy()) { Sleep(10); }
RefreshRates();
double SymAsk = NormalizeDouble(MarketInfo(FinalSymbol, MODE_ASK ), SymDigits);
double SymBid = NormalizeDouble(MarketInfo(FinalSymbol, MODE_BID ), SymDigits);
Ticket = OrderSend(FinalSymbol,OP_SELL,FinalLots,SymBid,0,0.0,0.0,EA_Comment,MagicNumber,0,CLR_NONE);
int Err=GetLastError();
switch(Err)
{
case ERR_NO_ERROR: OrderLoop = true;
if(OrderSelect(Ticket, SELECT_BY_TICKET))
{OrderModify(Ticket,OrderOpenPrice(),StopShrt(SymAsk, StopLoss, SymPoints, SymDigits), TakeShrt(SymBid, ProfitTarget, SymPoints, SymDigits),0,CLR_NONE);}
break;
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;
case ERR_INVALID_STOPS: OrderLoop = true; Print("Invalid Stops" );break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print("Invalid Lots" );break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print("Market Close" );break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print("Trades Disabled" );break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print("Not Enough Money");break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print("Too Many Orders" );break;
case ERR_NO_RESULT:
default: OrderLoop = true; Print("Unknown Error -"+Err);break;
}
if(TryCount>10) {OrderLoop=true;}
}
return(Ticket);
}
I would much appreciate any inside on this matter.
thank you
here are the code's line for the strategy,
double StoH4ValueS0, StoH4SignalS0, StoH4ValueS1, StoH4SignalS1;
double StoH1Value, StoH1Signal, StoD1Value, StoD1Signal ;
StoH4ValueS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH4SignalS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoH4ValueS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,Shiftt);
StoH4SignalS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,Shiftt);
StoH1Value = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH1Signal = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoD1Value = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_MAIN,0);
StoD1Signal = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_SIGNAL,0);
if(StoH4ValueS0>StoH4SignalS0 && StoH4ValueS1<StoH4SignalS1 && StoH1Value>StoH1Signal && StoD1Value>StoD1Signal && StoD1Value>LowLevel)
{ EnterLong (Sym, Lots, ""); }
else
if(StoH4ValueS0<StoH4SignalS0 && StoH4ValueS1>StoH4SignalS1 && StoH1Value<StoH1Signal && StoD1Value<StoD1Signal && StoD1Value<HighLevel)
{ EnterShrt (Sym, Lots, ""); }
}
return (0);
}
For those who want to see the entire code, here it is
//Ben Stochastic.mq4
#include <stderror.mqh>
//-----------------------------------------------Block 01----------------------------------------------------
datetime CurrTime = 0;
datetime PrevTime = 0;
string Sym = "";
int TimeFrame = 0;
int Shift = 1;
int SymDigits = 5;
double SymPoints = 0.0001;
//-----------------------------------------------Block 02----------------------------------------------------
extern bool UseCompletedBars = true;
extern int Shiftt=3;
extern int sto1=240;
extern int sto2=60;
extern int sto3=1440;
extern int K1=5;
extern int K2=5;
extern int K3=5;
extern int KE=5;
extern int LowLevel = 20;
extern int HighLevel = 80;
extern double Lots = 1.0;
extern int MagicNumber = 1235;
extern int ProfitTarget = 100;
extern int StopLoss = 0;
extern int Slippage = 3;
//-----------------------------------------------Block 03----------------------------------------------------
int init ()
{
Sym = Symbol();
TimeFrame = Period();
SymPoints = MarketInfo( Sym, MODE_POINT );
SymDigits = MarketInfo( Sym, MODE_DIGITS );
if ( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; }
else
if ( SymPoints == 0.00001) { SymPoints = 0.0001; SymDigits = 5; }
return (0);
}
//-----------------------------------------------Block 04----------------------------------------------------
int deinit () { return(0); }
//-----------------------------------------------Block 05----------------------------------------------------
int start ()
{
int RealTime = 0;
if (UseCompletedBars)
{
CurrTime = iTime ( Sym, TimeFrame, 1);
if ( CurrTime == PrevTime )
{
return(0);
}
PrevTime = CurrTime;
RealTime = 1;
}
if ( CountAll ( Sym, MagicNumber) == 0 )
{
double StoH4ValueS0, StoH4SignalS0, StoH4ValueS1, StoH4SignalS1;
double StoH1Value, StoH1Signal, StoD1Value, StoD1Signal ;
StoH4ValueS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH4SignalS0 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoH4ValueS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_MAIN,Shiftt);
StoH4SignalS1 = iStochastic(Symbol(),sto1,K1,3,3,MODE_SMA,1,MODE_SIGNAL,Shiftt);
StoH1Value = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_MAIN,0);
StoH1Signal = iStochastic(Symbol(),sto2,K2,3,3,MODE_SMA,1,MODE_SIGNAL,0);
StoD1Value = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_MAIN,0);
StoD1Signal = iStochastic(Symbol(),sto3,K3,3,3,MODE_SMA,1,MODE_SIGNAL,0);
if(StoH4ValueS0>StoH4SignalS0 && StoH4ValueS1<StoH4SignalS1 && StoH1Value>StoH1Signal && StoD1Value>StoD1Signal && StoD1Value>LowLevel)
{ EnterLong (Sym, Lots, ""); }
else
if(StoH4ValueS0<StoH4SignalS0 && StoH4ValueS1>StoH4SignalS1 && StoH1Value<StoH1Signal && StoD1Value<StoD1Signal && StoD1Value<HighLevel)
{ EnterShrt (Sym, Lots, ""); }
}
return (0);
}
//-----------------------------------------------Block 06----------------------------------------------------
int CountAll (string Symbole, int Magic)
{
int count = 0;
for (int i = OrdersTotal() - 1; i >=0; i --)
{
OrderSelect (i, SELECT_BY_POS,MODE_TRADES);
if ( OrderMagicNumber () != Magic) continue;
if ( OrderSymbol() != Symbole ) continue;
if ( OrderType () == OP_BUY) { count ++; }
else
if ( OrderType () == OP_SELL) { count ++; }
}
return (count);
}
//-----------------------------------------------Block 07----------------------------------------------------
double StopLong(double price,double stop,double point,double SymDgts)
{
if(stop==0) {return(0);}
else
{return(NormalizeDouble(price-(stop*point),SymDgts));}
}
double StopShrt(double price,double stop,double point,double SymDgts)
{
if(stop==0) {return(0);}
else
{return(NormalizeDouble(price+(stop*point),SymDgts));}
}
double TakeLong(double price, double take,double point, double SymDgts)
{
if(take==0) { return(0); }
else
{return(NormalizeDouble(price +(take*point),SymDgts));}
}
double TakeShrt(double price,double take,double point, double SymDgts)
{
if(take==0){return(0);}
else
{return(NormalizeDouble(price-(take*point),SymDgts));}
}
//-----------------------------------------------Block 08----------------------------------------------------
int EnterLong(string FinalSymbol,double FinalLots,string EA_Comment)
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;
while( !OrderLoop)
{
while (IsTradeContextBusy()) { Sleep (10); }
RefreshRates();
double SymAsk = NormalizeDouble(MarketInfo(FinalSymbol,MODE_ASK), SymDigits);
double SymBid = NormalizeDouble(MarketInfo(FinalSymbol,MODE_BID), SymDigits);
Ticket = OrderSend(FinalSymbol,OP_BUY,FinalLots,SymAsk,0,0.0,0.0,EA_Comment,MagicNumber,0,CLR_NONE);
int Err = GetLastError();
switch(Err)
{
case ERR_NO_ERROR: OrderLoop = true;
if(OrderSelect(Ticket,SELECT_BY_TICKET))
{OrderModify(Ticket,OrderOpenPrice(),StopLong(SymBid, StopLoss, SymPoints, SymDigits), TakeLong(SymAsk, ProfitTarget, SymPoints, SymDigits), 0, CLR_NONE);}
break;
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;
case ERR_INVALID_STOPS: OrderLoop = true; Print("Invalid Stops" );break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print("Invalid Lots" );break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print("Market Close" );break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print("Trades Disabled" );break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print("Not Enough Money");break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print("Too Many Orders" );break;
case ERR_NO_RESULT:
default: OrderLoop = true; Print("Unknown Error -"+Err);break;
}
if(TryCount>10) {OrderLoop=true;}
}
return(Ticket);
}
//-----------------------------------------------Block 09----------------------------------------------------
int EnterShrt(string FinalSymbol, double FinalLots, string EA_Comment)
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;
while (!OrderLoop)
{
while(IsTradeContextBusy()) { Sleep(10); }
RefreshRates();
double SymAsk = NormalizeDouble(MarketInfo(FinalSymbol, MODE_ASK ), SymDigits);
double SymBid = NormalizeDouble(MarketInfo(FinalSymbol, MODE_BID ), SymDigits);
Ticket = OrderSend(FinalSymbol,OP_SELL,FinalLots,SymBid,0,0.0,0.0,EA_Comment,MagicNumber,0,CLR_NONE);
int Err=GetLastError();
switch(Err)
{
case ERR_NO_ERROR: OrderLoop = true;
if(OrderSelect(Ticket, SELECT_BY_TICKET))
{OrderModify(Ticket,OrderOpenPrice(),StopShrt(SymAsk, StopLoss, SymPoints, SymDigits), TakeShrt(SymBid, ProfitTarget, SymPoints, SymDigits),0,CLR_NONE);}
break;
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;
case ERR_INVALID_STOPS: OrderLoop = true; Print("Invalid Stops" );break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print("Invalid Lots" );break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print("Market Close" );break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print("Trades Disabled" );break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print("Not Enough Money");break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print("Too Many Orders" );break;
case ERR_NO_RESULT:
default: OrderLoop = true; Print("Unknown Error -"+Err);break;
}
if(TryCount>10) {OrderLoop=true;}
}
return(Ticket);
}
I would much appreciate any inside on this matter.
thank you