Questions from Beginners MQL4 MT4 MetaTrader 4 - page 100

 

Hi everyone, can you please help me find an error in the code of the EA, I think I have looked through it all, it seems to be all written correctly in the code, but the program does not trade correctly for some reason! The idea is this: The advisor has to look for two long candles of the same direction (the length between the candles is adjustable in the advisor, ie between the two minimum or maximum candles, depending on the direction), if the price in the opposite direction breaks the minimum or maximum of the last candle, a deal should open (Example picture situations on the chart attached to the file). The adviser should open deals at every such suitable situation, but for some reason it opens deals only on the trading windows between days. Here is the situation, who is not difficult from programmers, please help, fix the error. EA code see below, as well as in the attached file.


//+-----------------------------------------------------------------------------------------------+

//| Spacing_Candles.mq4 |

//| Copyright 2017, Vladim |

//| vk.com/id229534564 |

//| Mail: Vladim120385@yandex.ru |

//+-----------------------------------------------------------------------------------------------+

#property copyright "Copyright 2017, Vladim"

#property link "vk.com/id229534564"

#property version "1.00"

#property strict


//--- EA parameters

extern string paramEA = ""; // Parameters EA

extern double volume = 0.01; // Volume

extern double stopLoss = 5; // StopLoss.

extern double takeProfit = 1.5; // TakeProfit

extern double maxSpacing = 150; // MaxSpacing

extern double minSpacing = 30; // MinSpacing

extern double TrailingStop = 0; // TrailingStop

extern int magic = 127; // Magic


//--- global variables

datetime newCandle;

int tip;


//+-----------------------------------------------------------------------------------------------+

int OnInit()

{

return(INIT_SUCCEEDED);

}

//+-----------------------------------------------------------------------------------------------+

void OnDeinit(const int reason)

{

}

//+-----------------------------------------------------------------------------------------------+

void OnTick()

{

if(newCandle != Time[0]) FindPattern();

newCandle = Time[0];

}

//+-----------------------------------------------------------------------------------------------+

void OpenOrder(int type) // Open a market order

{

if(type == OP_BUY) if(OrderSend(_Symbol, OP_BUY, volume, Ask, 0, 0, 0, "", magic, 0)) SetSLTP(OP_BUY);

if(type == OP_SELL) if(OrderSend(_Symbol, OP_SELL, volume, Bid, 0, 0, 0, 0, "", magic, 0)) SetSLTP(OP_SELL);

}

//+-----------------------------------------------------------------------------------------------+

void SetSLTP(int type) //set stop orders

{

double sl = 0;

double tp = 0;

if(type == OP_BUY)

for(int i = 0; i < OrdersTotal(); i++)

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic && OrderType() == OP_BUY && OrderStopLoss() == 0)

{

sl = NormalizeDouble(Low[1] - stopLoss * _Point, _Digits);

tp = NormalizeDouble(OrderOpenPrice() + (OrderOpenPrice() - Low[1]) * takeProfit, Digits);

if(OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0)) return;

}

if(type == OP_SELL)

for(int i = 0; i < OrdersTotal(); i++)

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

if(OrderSymbol() == _Symbol && OrderMagicNumber() == magic && OrderType() == OP_SELL && OrderStopLoss() == 0)

{

sl = NormalizeDouble(High[1] + stopLoss * _Point, _Digits);

tp = NormalizeDouble(OrderOpenPrice() - (High[1] - OrderOpenPrice()) * takeProfit, Digits);

if(OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0)) return;

}

}

//+-----------------------------------------------------------------------------------------------+

void FindPattern() // Looking for long distance between candlesticks

{

if(High[1] < High[2] && Bid > High[1] && Low[1] < Low[2])

{

double spacing = NormalizeDouble((High[2] - High[1]) / _Point, 0);

if(maxSpacing >= spacing && minSpacing <= spacing)

OpenOrder(OP_BUY);

}

if(Low[1] > Low[2] && Bid < Low[1] && High[1] > High[2])

{

double spacing = NormalizeDouble((Low[1] - Low[2]) / _Point, 0);

if(maxSpacing >= spacing && minSpacing <= spacing)

OpenOrder(OP_SELL);

}

{

if(TrailingStop!=0) TrailingStop();

}

}

//+-----------------------------------------------------------------------------------------------+

void TrailingStop()

{

double StLo,OSL,OOP;

bool error=true;

for (int i=0; i<OrdersTotal(); i++)

{

if (OrderSelect(i, SELECT_BY_POS))

{

tip = OrderType();

if (tip<2 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)

{

OSL = NormalizeDouble(OrderStopLoss(),Digits);

OOP = NormalizeDouble(OrderOpenPrice(),Digits);

if (tip==0)

{

StLo = NormalizeDouble(Bid - TrailingStop*Point,Digits);

if (StLo < OOP) continue;

if (StLo > OSL)

error=OrderModify(OrderTicket(),OrderOpenPrice(),StLo,OrderTakeProfit(),0,White);


}

if (tip==1)

{

StLo = NormalizeDouble(Ask + TrailingStop*Point,Digits);

if (StLo > OOP) continue;

if (StLo < OSL || OSL==0 )

error=OrderModify(OrderTicket(),OrderOpenPrice(),StLo,OrderTakeProfit(),0,White);

}

if (!error) Alert("Error TrailingStop ",GetLastError()," ",Symbol()," SL ",StLo);

}

}

}

}

//+-----------------------------------------------------------------------------------------------+

Files:
 
Vladim1203:

Hi everyone, can you please help me find an error in the code of the EA, I think I have looked through it all, it seems to be all written correctly in the code, but the program does not trade correctly for some reason! The idea is this: The advisor has to look for two long candles of the same direction (the length between the candles is adjustable in the advisor, ie between the two minimum or maximum candles, depending on the direction), if the price in the opposite direction breaks the minimum or maximum of the last candle, a deal should open (Example picture situations on the chart attached to the file). The adviser should open deals at every such suitable situation, but for some reason it opens deals only on the trading windows between days. Here is the situation, who is not difficult from programmers, please help, fix the error. EA code look below and also in attached file.

Maybe enough already, you've piled up posts in every thread, and at the same time created your own.

 
kuzhakov:

Hi.
Can you tell me how to limit EA trading by time.
I tried a lot of methods from the forum, but nothing worked, I don't understand anything about writing.
It would be nice if you could just point the finger at what to put in.


Insert it after start. and the eXpert will trade every day at a given time interval on the terminal


int val=Hour();
 double min=Minute();
 double Dl = val + min/100;  
 double L = xxxx+xxxx/100; //час + минуты вместо хххх - это начало времени
 double P = xxxx+xxxx/100; //час + минуты вместо хххх - это конец времени
 if(Dl<L)return;
 if(Dl>P)return;
 
Vladim1203:

Hi everyone, can you please help me find an error in the code of the EA, I think I have looked through it all, it seems to be all written correctly in the code, but the program does not trade correctly for some reason! The idea is this: The advisor has to look for two long candles of the same direction (the length between the candles is adjustable in the advisor, ie between the two minimum or maximum candles, depending on the direction), if the price in the opposite direction breaks the minimum or maximum of the last candle, a deal should open (Example picture situations on the chart attached to the file). The adviser should open deals at every such suitable situation, but for some reason it opens deals only on the trading windows between days. Here is the situation, who is not difficult from programmers, please help, fix the error. See the EA code below and also in the attached file.


check condition for buy and sell entry

 
Vladim1203:

Hi everyone, can you please help me find an error in the code of the EA, I think I have looked through it all, it seems to be all written correctly in the code, but the program does not trade correctly for some reason! The idea is this: The advisor has to look for two long candles of the same direction (the length between the candles is adjustable in the advisor, ie between the two minimum or maximum candles, depending on the direction), if the price in the opposite direction breaks the minimum or maximum of the last candle, a deal should open (Example picture situations on the chart attached to the file). The adviser should open deals at every such suitable situation, but for some reason it opens deals only on the trading windows between days. Here is the situation, who is not difficult from programmers, please help, fix the error. See the EA code below as well as in the attached file.

It is better to first write the part of the EA that would mark on the chart the candles found, so that everything becomes clear. And the following lines are unnecessary in your case:

extern string paramEA    = "";     // Parameters EA

и

//+-----------------------------------------------------------------------------------------------+
int OnInit()
{
   
   return(INIT_SUCCEEDED);
}
//+-----------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   
}
//+-----------------------------------------------------------------------------------------------+
 

Please help the community ...
*
Downloaded the Kalman Filter indicator from the forum (source code attached).
Everything is fine on the chart.


When I try to read it in Expert Advisor the following line



iValue=iCustom(NULL,timeframe,"Kalmanfilter",  4,1,1  ,0,1);

Outputs the same big number, obviously not related to the indicator:

What can it be ?

Files:
 
MikeZv:

Help from the community ...
*
Downloaded the Kalman Filter indicator from the forum (source code attached).
Everything is fine on the chart.


When I try to read it in Expert Advisor the following line



iValue=iCustom(NULL,timeframe,"Kalmanfilter",  4,1,1  ,0,1);

Outputs the same big number, obviously not related to the indicator:

What can it be ?

EMPTY_VALUE

Empty value in indicator buffer

2147483647 (0x7FFFFFFFF)

 
Alekseu Fedotov:

EMPTY_VALUE

Empty value in indicator buffer

2147483647 (0x7FFFFFFFF)


:) I already found it ...
Why does it come out ?

 
MikeZv:

:) I already found it ...
But why does it come out ?


The indicator has 2 arrays, because 2 colours. While there is a line of the first colour on the bar, the array with the second colour is equal to EMPTY_VALUE,

iValue=iCustom(NULL,timeframe, "Kalmanfilter", 4,1,1,0,1);

iValue = EMPTY_VALUE, then the line on 1 bar is orange. (because array blue = EMPTY_VALUE)

 
Nikolay Ivanov:

The indicator has 2 arrays because there are 2 colours. While there is a line of the first colour on the bar, the array with the second colour is equal to EMPTY_VALUE,

iValue=iCustom(NULL,timeframe, "Kalmanfilter", 4,1,1,0,1);

iValue = EMPTY_VALUE, then the line on 1 bar is orange. (Because blue array = EMPTY_VALUE)


Thanks, Nikolai...
I took from both lines.
Then I merged them into one (left one buffer). Same result.
*
I have such a question:
The indicator has draw_begin=500 variable, i.e. the number of candlesticks from the end (from the freshest candlestick) where the chart is drawn.
And when we extract the indicator value using the iCustom() method, these 500 points are counted from which candlestick ?
Why it's important - draw_begin is used not only for drawing, but also participates in calculation of indicator ...

Reason: