Questions and Answers to Programmers on MQL 4 - page 4

 

So far, just like this:

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

//| Fibo trading.mq4 |

//| Minodi |

//| minodi@yandex.ru |

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

#property copyright "Minodi"

#property link "minodi@yandex.ru"


extern int period_EMA = 233,

period_SMA = 233,

stoploss = 30,

extern double Lot = 0.01;

extern string SesEnd ="22:00"; //boundary time for Hai or Low

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

//| script program start function |

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

int start()

{

//------- Daily Prices, Fibs and Hi-Low rendering time--------------+

int shift = iBarShift(NULL,1440,Time[0]) + 1;

double HiPrice = iHigh(NULL,1440,shift); //High price

double LoPrice = iLow(NULL,1440,shift); // low price

double OpPrice = iOpen (NULL,1440,shift); //open price

double ClPrice = iClose (NULL,1440,shift); // Closing price

datetime StTime = iTime(NULL,1440,shift); // ?

if(TimeDayOfWeek(StTime)==0/*Sunday*/)

{//Add fridays high and low

HiPrice = MathMax(HiPrice,iHigh(NULL,1440,shift+1));

LoPrice = MathMin(LoPrice,iLow(NULL,1440,shift+1));

OpPrice = iOpen(NULL,1440,shift+1);

ClPrice = iClose (NULL,1440,shift+1);

}

double Pivot =(HiPrice+LoPrice+ClPrice)/3;

double Range =HiPrice-LoPrice;

double Fibo138 =1.382*Range+LoPrice;

double Fibo162 =1.618*Range+LoPrice;

double Fibo_138 =LoPrice-(1.382*Range-Range);

double Fibo_162 =LoPrice-(1.618*Range-Range);

//---------------- HiTime and Low-----------------------------------------+

int HiTime,LoTime;

int time; // on the next bar roll the values


if (time!=Time[0]){HiTime=0;LoTime=0;time=Time[0];}

if (High[0]>HiPrice)HiTime=TimeCurrent();

if (Low[0]<LoPrice)LoTime=TimeCurrent();


Comment("On this bar: "+"\n Time High Point="+HiTime+"\n Time Low Point="+LoTime);

//-----------------Ценовой диапазон Демарка-------------------------------+

int currDay, i=0; //

if (currDay!=TimeDay(Time[i]))

{

if (ClPrice<OpPrice) double x=(HiPrice + LoPrice + LoPrice)/2;

if (ClPrice>OpPrice) x=(HiPrice + LoPrice + ClPrice + HiPrice)/2;

if (ClPrice==OpPrice) x=(HiPrice + LoPrice + ClPrice + ClPrice)/2;

i++;

}

double MaxDay = x - LoPrice; // Demark price range

double MinDay = x - HiPrice; // Demark price range

// -----------Скользящие средние---------------------------------------------+

double EMA = iMA(15,0,period_EMA,0,MODE_EMA, PRICE_CLOSE,0);

double SMA = iMA(15,0,period_SMA,0,MODE_SMA,PRICE_CLOSE,0);

double EMA1 = iMA(60,0,period_EMA,0,MODE_EMA,PRICE_CLOSE,0);

double SMA1 = iMA(60,0,period_SMA,0,MODE_SMA,PRICE_CLOSE,0);

//----------------Удаление вчерашних ордеров---------------------------------+

//------------------Ордера----------------------------------------------------+

 

Dear forum members, check the code, I plan to gradually add

 

These are the parameters I gave you, place them before you start


int HiTime,LoTime;

int time; // на следующем баре обнкляем значения

 
Hello, dear programmers! Can you advise a newbie how to write, if possible, a script (not an Expert Advisor) that would place a trade order and at the same time enable the Trailing Stop on a minimum level. Maybe there is a ready-made one, then if you can give me a link.
 
Vladon >>:

Вот эти параметры ято я давал, поместите перд стартом


int HiTime,LoTime;

int time; // на следующем баре обнкляем значения


Moved. Is everything else OK?
 

Dear forum members, please pay attention and tell us what mistakes have been made:

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

//| Fibo trading.mq4 |

//| Minodi |

//| minodi@yandex.ru |

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

#property copyright "Minodi"

#property link "minodi@yandex.ru"


extern int period_EMA = 233,

period_SMA = 233,

stoploss = 30,

Magic = 55;

extern double Lot = 0.01;

extern string SesEnd ="22:00"; //boundary time for Hai or Low


int HiTime,LoTime;

int time; // on the next bar roll up values


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

//| script program start function |

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

int start()

{

//------- Daily Prices, Fibs and Hi-Low rendering time--------------+

int shift = iBarShift(NULL,1440,Time[0]) + 1;

double HiPrice = iHigh(NULL,1440,shift); //High price

double LoPrice = iLow(NULL,1440,shift); // low price

double OpPrice = iOpen (NULL,1440,shift); //open price

double ClPrice = iClose (NULL,1440,shift); // Closing price

datetime StTime = iTime(NULL,1440,shift); // ?

if(TimeDayOfWeek(StTime)==0/*Sunday*/)

{//Add fridays high and low

HiPrice = MathMax(HiPrice,iHigh(NULL,1440,shift+1));

LoPrice = MathMin(LoPrice,iLow(NULL,1440,shift+1));

OpPrice = iOpen(NULL,1440,shift+1);

ClPrice = iClose (NULL,1440,shift+1);

}

double Pivot =(HiPrice+LoPrice+ClPrice)/3;

double Range =HiPrice-LoPrice;

double Fibo138 =1.382*Range;

double Fibo162 =1.618*Range;

double Fibo_138 =(LoPrice-1.382*Range)-Range;

double Fibo_162 =(LoPrice-1.618*Range)-Range;

//---------------- time hai and low-----------------------------------------+


if (time!=Time[0]){HiTime=0;LoTime=0;time=Time[0];}

if (High[0]>HiPrice)HiTime=TimeCurrent();

if (Low[0]<LoPrice)LoTime=TimeCurrent();


Comment("On this bar: "+"\n Time High Point="+HiTime+"\n Time Low Point="+LoTime);

//-----------------Ценовой диапазон Демарка-------------------------------+

int currDay, i=0; //

if (currDay!=TimeDay(Time[i]))

{

if (ClPrice<OpPrice) double x=(HiPrice + LoPrice + LoPrice)/2;

if (ClPrice>OpPrice) x=(HiPrice + LoPrice + ClPrice + HiPrice)/2;

if (ClPrice==OpPrice) x=(HiPrice + LoPrice + ClPrice + ClPrice)/2;

i++;

}

double MaxDay = x - LoPrice; // Demark price range

double MinDay = x - HiPrice; // Demark price range

// -----------Скользящие средние---------------------------------------------+

double EMA = iMA(15,0,period_EMA,0,MODE_EMA, PRICE_CLOSE,0);

double SMA = iMA(15,0,period_SMA,0,MODE_SMA,PRICE_CLOSE,0);

double EMA1 = iMA(60,0,period_EMA,0,MODE_EMA,PRICE_CLOSE,0);

double SMA1 = iMA(60,0,period_SMA,0,MODE_SMA,PRICE_CLOSE,0);

//------------------Ордера----------------------------------------------------+

if (HiTime>LoTime){

int ticket1 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,HiPrice,0,0,Pivot,NULL,Magic,58000,Red);

int ticket2 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,HiPrice,0,0,LoPrice,NULL,Magic,58000,Red);

int ticket3 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,Fibo138,0,0,HiPrice,NULL,Magic,58000,Red);

int ticket4 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,Fibo138,0,0,LoPrice,NULL,Magic,58000,Red);

int ticket5 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,Fibo162,0,0,HiPrice,NULL,Magic,58000,Red);

int ticket6 =OrderSend(Symbol(),OP_SELLLIMIT,Lot,Fibo162,0,0,Pivot,NULL,Magic,58000,Red);

}

if (HiTime<LoTime){

int ticket7=OrderSend(Symbol(),OP_BUYLIMIT,Lot,LoPrice,0,0,Pivot,NULL,Magic,58000,Red);

int ticket8=OrderSend(Symbol(),OP_BUYLIMIT,Lot,LoPrice,0,0,HiPrice,NULL,Magic,58000,Red);

int ticket9=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Fibo_138,0,0,LoPrice,NULL,Magic,58000,Red);

int ticket10=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Fibo_138,0,0,Pivot,NULL,Magic,58000,Red);

int ticket11=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Fibo_162,0,0,LoPrice,NULL,Magic,58000,Red);

int ticket12=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Fibo_162,0,0,Pivot,NULL,Magic,58000,Red);

}

if (HiTime>LoTime && ClPrice>Pivot) {

int ticket13=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Pivot,0,0,HiPrice,NULL,Magic,58000,Red);

int ticket14=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Pivot,0,0,Fibo138,NULL,Magic,58000,Red);

}

if (HiTime<LoTime && ClPrice<Pivot)

int ticket15=OrderSend(Symbol(),OP_SELLLIMIT,Lot,Pivot,0,0,LoPrice,NULL,Magic,58000,Red);

int ticket16=OrderSend(Symbol(),OP_SELLLIMIT,Lot,Pivot,0,0,Fibo_138,NULL,Magic,58000,Red);

}

return(0);


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


 

at first glance the parameters:

int currDay;


for start

 
Forgot about the stops, put them at 30p
 
Point out the errors, I want the code to work
 

Gentlemen of the forum, help to understand....

Here is a part of the code (the main code is taken from the Expert Advisor construction manual)

// Order function
// Global variables:
// Mas_Ord_New[31][9] // Array of orders last known
// Mas_Ord_Old[31][9] // Array of orders previous (old)
// 1st index = order number
// [][0] is not defined
// [][1] open order price (abs. of an order (abc rate)
// [][2] StopLoss of an order (abc rate)
// [][3] TakeProfit of an order (abc rate)
// [][4] Order number
// [][5] number of lots of an order. (Abc Rate)
// [][6] Order type 0=B,1=S,2=BL,3=SL,4=BS,5=SS
// [][7] magic number of the order
// [][8] 0/1

if (Mas_Ord_Old[old][6]==0 && Bid>Mas_Ord_Old[old][2] && Bid<Mas_Ord_Old[old][3])
Inform(1, Mas_Ord_Old[old][4]); // So the Buy order was closed by the counter signal
else if (Mas_Ord_Old[old][6]==0 && Bid<=Mas_Ord_Old[old][2])
Inform(19,Mas_Ord_Old[old][4],Mas_Ord_Old[old][2]) // it means SL has triggered
else if (Mas_Ord_Old[old][6]==0 && Bid>=Mas_Ord_Old[old][3])
Inform(20,Mas_Ord_Old[old][4],Mas_Ord_Old[old][3]) // TA triggered

else if (Mas_Ord_Old[old][6]==1 && Ask<Mas_Ord_Old[old][2] && Ask>Mas_Ord_Old[old][3])
Inform(2, Mas_Ord_Old[old][4]); // So Sell order is closed by counter signal
else if (Mas_Ord_Old[old][6]==1 && Ask>=Mas_Ord_Old[old][2])
Inform(19,Mas_Ord_Old[old][4],Mas_Ord_Old[old][2]) // it means that SL has triggered
else if (Mas_Ord_Old[old][6]==1 && Ask<=Mas_Ord_Old[old][3])
Inform(20,Mas_Ord_Old[old][4],Mas_Ord_Old[old][3]); // TA triggered

The rest is of no interest...

The problem: In 90% of cases the correct parameter is shown, i.e. if SL is triggered, the parameter is "19", if TP is "20", if order is closed manually, or by a counter signal, the parameter is "1" or "2". It was designed that way.

But sometimes (quite often) instead of "20" it returns "19" (or vice versa), or even "1"("2"). Please, help me correctly describe the "if" function: as conditions of order closing reasons.

I think the reason may be in rounding of prices?

Reason: