hi, newbie here. i am trying to get this EA to not trade on the next bar or number of bars after a trade is closed but there is a valid entry signal? This could probably do with a bit of a clean up as well as i have modified this from somewhere else.
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (MAc > MAo && Psar < Close[0] && StochMain > StochSignal && StochMain < 75 && BarsCount != Bars ) Order = SIGNAL_BUY;
if (MAc > MAo && Psar > Close[0] && StochMain < StochSignal && StochMain > 25 && BarsCount != Bars ) Order = SIGNAL_SELL;
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
hi, newbie here. i am trying to get this EA to not trade on the next bar or number of bars after a trade is closed but there is a valid entry signal? This could probably do with a bit of a clean up as well as i have modified this from somewhere else.
cheers in advance
wayne
//+------------------------------------------------------------------+
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
#property copyright "wbh"
extern string Remark1 = "== Main Settings ==";
extern int MagicNumber = 0;
extern bool AlertOnly = False;
extern bool Alerts = False;
extern bool SignalMail = False;
extern bool PlaySounds = False;
extern bool EachTickMode = True;
extern double Lots = 0.1;
extern int Slippage = 5;
extern bool UseStopLoss = True;
extern int StopLoss = 100;
extern bool UseTakeProfit = True;
extern bool UsePsarStop = True;
int BarCount;
int BarsCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
BarsCount = Bars - 1;
if (EachTickMode) Current = 0; else Current = 1;
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel, PsarStopLevel;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
double StochMain = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 0);
double StochMain1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 1);
double StochMain2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 2);
double StochMain3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 3);
double StochSignal = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);
double StochSignal1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 1);
double AC = iAC(NULL, 0, Current + 0);
double AC1 = iAC(NULL, 0, Current + 1);
double AO = iAO(NULL, 0, Current + 0);
double AO1 = iAO(NULL, 0, Current + 1);
double Psar = iSAR(NULL, 0, 0.015, 0.2, Current + 0);
double MAc = iMA(NULL, 0, 11, 0, MODE_EMA, 0, Current + 0);
double MAo = iMA(NULL, 0, 11, 0, MODE_EMA, 1, Current + 0);
double MAh = iMA(NULL, 0, 11, 0, MODE_EMA, 2, Current + 0);
double MAl = iMA(NULL, 0, 11, 0, MODE_EMA, 3, Current + 0);
double ATR = iATR(NULL, 0, 7, Current + 0);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
//Check position
bool IsTrade = False;
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
if (StochMain < StochSignal && StochMain1 > StochSignal1) Order = SIGNAL_CLOSEBUY;
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
IsTrade = False;
continue;
}
//PsarStop
if (UsePsarStop && Psar > 0){
if (Bid - OrderOpenPrice() > Point * Psar){
if (OrderStopLoss() < Bid - Point * Psar){
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * Psar, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
continue;
}
}
}
if((UseStopLoss && StopLoss > 0) || (UseTakeProfit && ATR > 0))
BarsCount = Bars;
continue;
} else {
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
if (StochMain > StochSignal && StochMain1 < StochSignal1) Order = SIGNAL_CLOSESELL;
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
IsTrade = False;
continue;
}
//PsarStop
if (UsePsarStop && Psar > 0){
if ((OrderOpenPrice() - Ask) > (Point * Psar)){
if((OrderStopLoss() > (Ask + Point * Psar)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * Psar, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
continue;
}
}
}
if((UseStopLoss && StopLoss > 0) || (UseTakeProfit && ATR > 0))
BarsCount = Bars;
continue;
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (MAc > MAo && Psar < Close[0] && StochMain > StochSignal && StochMain < 75 && BarsCount != Bars ) Order = SIGNAL_BUY;
if (MAc > MAo && Psar > Close[0] && StochMain < StochSignal && StochMain > 25 && BarsCount != Bars ) Order = SIGNAL_SELL;
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(AlertOnly) {
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
if (PlaySounds) PlaySound("alert.wav");
}
if(!IsTrade && !AlertOnly ) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + ATR ; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
if (PlaySounds) PlaySound("alert.wav");
} else {
Print("Error opening BUY order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(AlertOnly) {
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
if (PlaySounds) PlaySound("alert.wav");
}
if(!IsTrade && !AlertOnly ) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Bid - ATR ; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
if (PlaySounds) PlaySound("alert.wav");
} else {
Print("Error opening SELL order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
BarsCount = Bars;
return(0);
}
}
if (!EachTickMode) BarCount = Bars;
return(0);
}