I created an EA that is supposed to do a calculation every day at "0:00"
It would measure the last 10 bars and calculate the highest and lowest close.
If the highest close comes before the lowest close, it would open a sell trade.
If the lowest close comes before the highest close, it would open a buy trade.
The EA is supposed to open a trade every day on the "0:00" bar, but it isn't doing so. Help me to find out what is wrong?
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
extern int MagicNumber = 3;
extern bool SignalMail = True;
extern bool EachTickMode = True;
extern int Slippage = 100;
extern bool UseStopLoss = False;
extern int StopLoss = 30;
extern bool UseTakeProfit = False;
extern int TakeProfit = 60;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;
int BarCount;
int Current;
bool TickCheck = False;
bool buyFlag = false, sellFlag = false;
datetime barStart = 0;
bool InTrade = false, nomoretrades = false, timeset = false;
bool fiboup = false, fibodown = false;
double highest, lowest;
double stoploss, takeprofit;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
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;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
bool FiboCalTime = false;
bool TradingTime = false;
double Duration = 48*30*60;
double resetperiod = 15*60;
double Lots = 6;
double lot2 = Lots/2, lot3 = Lots/4;
int cnt = 0;
int timecount = 0, timetotal = 4;
datetime StartTime;
StartTime = StrToTime("0:00");
double Close1 = iClose(NULL, 0, Current + 1);
double hightime, lowtime;
double FractalsUpper2 = iFractals(NULL, 0, MODE_UPPER, Current + 2);
double FractalsLower2 = iFractals(NULL, 0, MODE_LOWER, Current + 2);
double FractalsUpper3 = iFractals(NULL, 0, MODE_UPPER, Current + 3);
double FractalsLower3 = iFractals(NULL, 0, MODE_LOWER, Current + 3);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
if (barStart < Time[0]) //start of new bar
{
barStart = Time[0];
buyFlag = false;
sellFlag = false;
}
if(TimeCurrent() >= (StartTime + resetperiod) && TimeCurrent() < (StartTime + resetperiod*2))
{
nomoretrades = false;
timeset = false;
}
if(TimeCurrent() >= StartTime && TimeCurrent() < (StartTime + resetperiod))
{
FiboCalTime = true;
}
if(FiboCalTime == true)
{
highest = Close1;
lowest = Close1;
if(timeset == false)
{
hightime = 1;
lowtime = 1;
timeset = true;
}
for(timecount = 0; timecount < 10; timecount++)
{
if(Close[timecount] > highest)
{
highest = Close[timecount];
hightime = timecount;
}
if(Close[timecount] < lowest)
{
lowest = Close[timecount];
lowtime = timecount;
}
}
if(hightime < lowtime)
{
fiboup = false;
fibodown = true;
}
if(hightime > lowtime)
{
fiboup = true;
fibodown = false;
}
}
if(TimeCurrent() >= (StartTime + resetperiod) && TimeCurrent() < (StartTime+ Duration))
{
TradingTime = true;
}
//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()) {
IsTrade = false;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
if (Close[0] >= takeprofit)
{
Order = SIGNAL_CLOSEBUY;
}
if (Close[0] < stoploss)
{
Order = SIGNAL_CLOSEBUY;
}
}
}
}
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if(Ticket > 0) {
Print("BUY order closed : ", OrderMagicNumber());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
} else {
Print("Error closing BUY order : ", GetLastError());
InTrade = true;
}
}
}
}
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
if (Close[0] <= takeprofit)
{
Order = SIGNAL_CLOSESELL;
}
if (Close[0] > stoploss)
{
Order = SIGNAL_CLOSESELL;
}
}
}
}
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if(Ticket > 0) {
Print("SELL order closed : ", OrderMagicNumber());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
} else {
Print("Error closing SELL order : ", GetLastError());
InTrade = true;
}
}
}
}
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (TradingTime == true && fiboup == true && fibodown == false && nomoretrades == false && buyFlag == false && InTrade == false)
{
Order = SIGNAL_BUY;
buyFlag = true;
InTrade = true;
stoploss = Close1 - 0.00100;
takeprofit = Close1 + 0.00100;
nomoretrades = true;
}
if (TradingTime == true && fiboup == false && fibodown == true && nomoretrades == false && sellFlag == false && InTrade == false)
{
Order = SIGNAL_SELL;
sellFlag = true;
InTrade = true;
stoploss = Close1 + 0.00100;
takeprofit = Close1 - 0.00100;
nomoretrades = true;
}
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//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 + TakeProfit * Point; 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) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//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 - TakeProfit * Point; 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) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening SELL order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
if (!EachTickMode) BarCount = Bars;
return(0);
}
//+------------------------------------------------------------------+
It would measure the last 10 bars and calculate the highest and lowest close.
If the highest close comes before the lowest close, it would open a sell trade.
If the lowest close comes before the highest close, it would open a buy trade.
The EA is supposed to open a trade every day on the "0:00" bar, but it isn't doing so. Help me to find out what is wrong?
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
extern int MagicNumber = 3;
extern bool SignalMail = True;
extern bool EachTickMode = True;
extern int Slippage = 100;
extern bool UseStopLoss = False;
extern int StopLoss = 30;
extern bool UseTakeProfit = False;
extern int TakeProfit = 60;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;
int BarCount;
int Current;
bool TickCheck = False;
bool buyFlag = false, sellFlag = false;
datetime barStart = 0;
bool InTrade = false, nomoretrades = false, timeset = false;
bool fiboup = false, fibodown = false;
double highest, lowest;
double stoploss, takeprofit;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
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;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
bool FiboCalTime = false;
bool TradingTime = false;
double Duration = 48*30*60;
double resetperiod = 15*60;
double Lots = 6;
double lot2 = Lots/2, lot3 = Lots/4;
int cnt = 0;
int timecount = 0, timetotal = 4;
datetime StartTime;
StartTime = StrToTime("0:00");
double Close1 = iClose(NULL, 0, Current + 1);
double hightime, lowtime;
double FractalsUpper2 = iFractals(NULL, 0, MODE_UPPER, Current + 2);
double FractalsLower2 = iFractals(NULL, 0, MODE_LOWER, Current + 2);
double FractalsUpper3 = iFractals(NULL, 0, MODE_UPPER, Current + 3);
double FractalsLower3 = iFractals(NULL, 0, MODE_LOWER, Current + 3);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
if (barStart < Time[0]) //start of new bar
{
barStart = Time[0];
buyFlag = false;
sellFlag = false;
}
if(TimeCurrent() >= (StartTime + resetperiod) && TimeCurrent() < (StartTime + resetperiod*2))
{
nomoretrades = false;
timeset = false;
}
if(TimeCurrent() >= StartTime && TimeCurrent() < (StartTime + resetperiod))
{
FiboCalTime = true;
}
if(FiboCalTime == true)
{
highest = Close1;
lowest = Close1;
if(timeset == false)
{
hightime = 1;
lowtime = 1;
timeset = true;
}
for(timecount = 0; timecount < 10; timecount++)
{
if(Close[timecount] > highest)
{
highest = Close[timecount];
hightime = timecount;
}
if(Close[timecount] < lowest)
{
lowest = Close[timecount];
lowtime = timecount;
}
}
if(hightime < lowtime)
{
fiboup = false;
fibodown = true;
}
if(hightime > lowtime)
{
fiboup = true;
fibodown = false;
}
}
if(TimeCurrent() >= (StartTime + resetperiod) && TimeCurrent() < (StartTime+ Duration))
{
TradingTime = true;
}
//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()) {
IsTrade = false;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
if (Close[0] >= takeprofit)
{
Order = SIGNAL_CLOSEBUY;
}
if (Close[0] < stoploss)
{
Order = SIGNAL_CLOSEBUY;
}
}
}
}
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if(Ticket > 0) {
Print("BUY order closed : ", OrderMagicNumber());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
} else {
Print("Error closing BUY order : ", GetLastError());
InTrade = true;
}
}
}
}
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
if (Close[0] <= takeprofit)
{
Order = SIGNAL_CLOSESELL;
}
if (Close[0] > stoploss)
{
Order = SIGNAL_CLOSESELL;
}
}
}
}
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
for (cnt = Total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
{
if (OrderMagicNumber() == MagicNumber)
{
InTrade = false;
Ticket = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if(Ticket > 0) {
Print("SELL order closed : ", OrderMagicNumber());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
} else {
Print("Error closing SELL order : ", GetLastError());
InTrade = true;
}
}
}
}
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (TradingTime == true && fiboup == true && fibodown == false && nomoretrades == false && buyFlag == false && InTrade == false)
{
Order = SIGNAL_BUY;
buyFlag = true;
InTrade = true;
stoploss = Close1 - 0.00100;
takeprofit = Close1 + 0.00100;
nomoretrades = true;
}
if (TradingTime == true && fiboup == false && fibodown == true && nomoretrades == false && sellFlag == false && InTrade == false)
{
Order = SIGNAL_SELL;
sellFlag = true;
InTrade = true;
stoploss = Close1 + 0.00100;
takeprofit = Close1 - 0.00100;
nomoretrades = true;
}
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//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 + TakeProfit * Point; 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) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening BUY order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//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 - TakeProfit * Point; 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) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
}
else //*** ADDED ELSE to if (Ticket >0)
{
// We had an error opening the trade so deal with it
Print("Error opening SELL order : ", GetLastError());
InTrade = false;
buyFlag = false;
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
if (!EachTickMode) BarCount = Bars;
return(0);
}
//+------------------------------------------------------------------+