Ask! - page 174

 

question about indicator code-----(3 lines)

1.why the 2 marked functions are in the deinit ?

2.why 720 value on the marked line?

the code:

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

//| DailyBreakout.mq4 |

//| Copyright © 2008, Robert Hill. |

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

#property copyright "Copyright © 2008, Robert Hill"

#property link "NONE"

#property indicator_chart_window

//---- input parameters

extern bool Alerts = false;

extern int GMTshift = 0;

extern int LabelShift = 20;

extern int LineShift = 40;

extern string pd = "PipsAboveBelowSR for Alert";

extern int PipDistance = 1;

extern color StandardFontColor = White;

extern int StandardFontSize = 8;

extern color SupportColor = Red;

extern color ResistanceColor = Lime;

datetime LabelShiftTime, LineShiftTime;

double yesterday_high=0;

double yesterday_low=0;

double LastHigh,LastLow,x;

double R1=0;

double S1=0;

bool firstS1=true;

bool firstR1=true;

double myPoint;

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

myPoint = SetPoint(Symbol());

//----

return(0);

}

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

//| Custor indicator deinitialization function |

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

int deinit()

{

//---- TODO: add your code here

//----

ObjectDelete("R1 Label");

ObjectDelete("R1 Line");

ObjectDelete("S1 Label");

ObjectDelete("S1 Line");

return(0);

}

double SetPoint(string mySymbol)// <<<<<<<-----why here on the deinit?????----------------

{

double mPoint, myDigits;

myDigits = MarketInfo (mySymbol, MODE_DIGITS);

if (myDigits < 4)

mPoint = 0.01;

else

mPoint = 0.0001;

return(mPoint);

}

int DoAlerts()//<<<<<<<<<-------why here on the deint??????-----------------

{

double DifAboveR1,PipsLimit;

double DifBelowS1;

DifBelowS1 = S1 - Close[0];

DifAboveR1 = Close[0] - R1;

PipsLimit = PipDistance * myPoint;

if (DifBelowS1 > PipsLimit) firstS1 = true;

if (DifBelowS1 0)

{

if (firstS1)

{

Alert("Below S1 Line by ",DifBelowS1, " for ", Symbol(),"-",Period());

PlaySound("alert.wav");

firstS1=false;

}

}

if (DifAboveR1 > PipsLimit) firstR1 = true;

if (DifAboveR1 0)

{

if (firstR1)

{

Alert("Above R1 Line by ",DifAboveR1," for ", Symbol(),"-",Period());

Sleep(2000);

PlaySound("timeout.wav");

firstR1=false;

}

}

}

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

//| Custom indicator iteration function |

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

int start()

{

int counted_bars=IndicatorCounted();

//---- TODO: add your code here

double day_high=0;

double day_low=0;

double yesterday_open=0;

double today_open=0;

double cur_day=0;

double prev_day=0;

int cnt=720;//<<<<<----why 720 ????????--------------------------------------------------

//---- exit if period is greater than 4 hr charts

if(Period() > 240)

{

Print("Error - Chart period is greater than 4 hr.");

return(-1); // then exit

}

//---- Get new daily prices & calculate pivots

cur_day=0;

prev_day=0;

//---- Get new daily prices & calculate pivots

while (cnt!= 0)

{

cur_day = TimeDay(Time[cnt]- (GMTshift*3600));

if (prev_day != cur_day)

{

yesterday_high = day_high;

yesterday_low = day_low;

day_high = High[cnt];

day_low = Low[cnt];

prev_day = cur_day;

}

if (High[cnt]>day_high)

{

day_high = High[cnt];

}

if (Low[cnt]<day_low)

{

day_low = Low[cnt];

}

cnt--;

}

S1 = yesterday_low;

R1 = yesterday_high;

LabelShiftTime = Time[LabelShift];

LineShiftTime = Time[LineShift];

//---- Set line labels on chart window

DisplayLabel("R1 label", "R1", R1, StandardFontSize, StandardFontColor);

DisplayLabel("S1 label", "S1", S1, StandardFontSize, StandardFontColor);

//--- Draw Pivot lines on chart

DisplayLine("S1 line", S1, 0, STYLE_DASHDOTDOT, SupportColor);

DisplayLine("R1 line", R1, 0, STYLE_DASHDOTDOT, ResistanceColor);

//---- done

// Now check for Alert

if (Alerts) DoAlerts();

//----

return(0);

}

//---- Set line labels on chart window

void DisplayLabel(string LabelName, string LabelText, double LabelPos, int LabelFontSize, color LabelColor)

{

if(ObjectFind(LabelName) != 0)

{

ObjectCreate(LabelName, OBJ_TEXT, 0, LabelShiftTime, LabelPos);

ObjectSetText(LabelName, LabelText, LabelFontSize, "Arial", LabelColor);

}

else

{

ObjectMove(LabelName, 0, LabelShiftTime, LabelPos);

}

}

//--- Draw Pivot lines on chart

void DisplayLine(string LineName, double LinePos, int LineWidth, int LineStyle, color LineColor)

{

if(ObjectFind(LineName) != 0)

{

ObjectCreate(LineName, OBJ_HLINE, 0, LineShiftTime, LinePos);

ObjectSet(LineName, OBJPROP_STYLE, LineStyle);

ObjectSet(LineName, OBJPROP_COLOR, LineColor);

if (LineWidth > 0) ObjectSet(LineName, OBJPROP_WIDTH, LineWidth);

}

else

{

ObjectMove(LineName, 0, LineShiftTime, LinePos);

}

}

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

thanks a loot.

 

ERAN123

1. They are not in the deinit() but just behind deinit() (between deinit() and start()). In mql you do not need to follow any order how procedures and functions are written. You can put the init() at the end of the code and it will still work OK )a small digression " mql does not allow nested functions or procedures, so they never can be within the body of another function or procedure in mql)

2. It fixes the calculation to 720 bars at each tick. Why? I think you should ask the author about that.

ERAN123:
1.why the 2 marked functions are in the deinit ?

2.why 720 value on the marked line?

the code:

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

//| DailyBreakout.mq4 |

//| Copyright © 2008, Robert Hill. |

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

#property copyright "Copyright © 2008, Robert Hill"

#property link "NONE"

#property indicator_chart_window

//---- input parameters

extern bool Alerts = false;

extern int GMTshift = 0;

extern int LabelShift = 20;

extern int LineShift = 40;

extern string pd = "PipsAboveBelowSR for Alert";

extern int PipDistance = 1;

extern color StandardFontColor = White;

extern int StandardFontSize = 8;

extern color SupportColor = Red;

extern color ResistanceColor = Lime;

datetime LabelShiftTime, LineShiftTime;

double yesterday_high=0;

double yesterday_low=0;

double LastHigh,LastLow,x;

double R1=0;

double S1=0;

bool firstS1=true;

bool firstR1=true;

double myPoint;

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

myPoint = SetPoint(Symbol());

//----

return(0);

}

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

//| Custor indicator deinitialization function |

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

int deinit()

{

//---- TODO: add your code here

//----

ObjectDelete("R1 Label");

ObjectDelete("R1 Line");

ObjectDelete("S1 Label");

ObjectDelete("S1 Line");

return(0);

}

double SetPoint(string mySymbol)// <<<<<<<-----why here on the deinit?????----------------

{

double mPoint, myDigits;

myDigits = MarketInfo (mySymbol, MODE_DIGITS);

if (myDigits < 4)

mPoint = 0.01;

else

mPoint = 0.0001;

return(mPoint);

}

int DoAlerts()//<<<<<<<<<-------why here on the deint??????-----------------

{

double DifAboveR1,PipsLimit;

double DifBelowS1;

DifBelowS1 = S1 - Close[0];

DifAboveR1 = Close[0] - R1;

PipsLimit = PipDistance * myPoint;

if (DifBelowS1 > PipsLimit) firstS1 = true;

if (DifBelowS1 0)

{

if (firstS1)

{

Alert("Below S1 Line by ",DifBelowS1, " for ", Symbol(),"-",Period());

PlaySound("alert.wav");

firstS1=false;

}

}

if (DifAboveR1 > PipsLimit) firstR1 = true;

if (DifAboveR1 0)

{

if (firstR1)

{

Alert("Above R1 Line by ",DifAboveR1," for ", Symbol(),"-",Period());

Sleep(2000);

PlaySound("timeout.wav");

firstR1=false;

}

}

}

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

//| Custom indicator iteration function |

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

int start()

{

int counted_bars=IndicatorCounted();

//---- TODO: add your code here

double day_high=0;

double day_low=0;

double yesterday_open=0;

double today_open=0;

double cur_day=0;

double prev_day=0;

int cnt=720;//<<<<<----why 720 ????????--------------------------------------------------

//---- exit if period is greater than 4 hr charts

if(Period() > 240)

{

Print("Error - Chart period is greater than 4 hr.");

return(-1); // then exit

}

//---- Get new daily prices & calculate pivots

cur_day=0;

prev_day=0;

//---- Get new daily prices & calculate pivots

while (cnt!= 0)

{

cur_day = TimeDay(Time[cnt]- (GMTshift*3600));

if (prev_day != cur_day)

{

yesterday_high = day_high;

yesterday_low = day_low;

day_high = High[cnt];

day_low = Low[cnt];

prev_day = cur_day;

}

if (High[cnt]>day_high)

{

day_high = High[cnt];

}

if (Low[cnt]<day_low)

{

day_low = Low[cnt];

}

cnt--;

}

S1 = yesterday_low;

R1 = yesterday_high;

LabelShiftTime = Time[LabelShift];

LineShiftTime = Time[LineShift];

//---- Set line labels on chart window

DisplayLabel("R1 label", "R1", R1, StandardFontSize, StandardFontColor);

DisplayLabel("S1 label", "S1", S1, StandardFontSize, StandardFontColor);

//--- Draw Pivot lines on chart

DisplayLine("S1 line", S1, 0, STYLE_DASHDOTDOT, SupportColor);

DisplayLine("R1 line", R1, 0, STYLE_DASHDOTDOT, ResistanceColor);

//---- done

// Now check for Alert

if (Alerts) DoAlerts();

//----

return(0);

}

//---- Set line labels on chart window

void DisplayLabel(string LabelName, string LabelText, double LabelPos, int LabelFontSize, color LabelColor)

{

if(ObjectFind(LabelName) != 0)

{

ObjectCreate(LabelName, OBJ_TEXT, 0, LabelShiftTime, LabelPos);

ObjectSetText(LabelName, LabelText, LabelFontSize, "Arial", LabelColor);

}

else

{

ObjectMove(LabelName, 0, LabelShiftTime, LabelPos);

}

}

//--- Draw Pivot lines on chart

void DisplayLine(string LineName, double LinePos, int LineWidth, int LineStyle, color LineColor)

{

if(ObjectFind(LineName) != 0)

{

ObjectCreate(LineName, OBJ_HLINE, 0, LineShiftTime, LinePos);

ObjectSet(LineName, OBJPROP_STYLE, LineStyle);

ObjectSet(LineName, OBJPROP_COLOR, LineColor);

if (LineWidth > 0) ObjectSet(LineName, OBJPROP_WIDTH, LineWidth);

}

else

{

ObjectMove(LineName, 0, LineShiftTime, LinePos);

}

}

//+------------------------------------------------------------------+
thanks a loot.
 

hi mladen

thanks for your replay.

1. just noticed thise one (i misted thise one didnt notice)

2. its real mystery

 

pending orders need lite help !!!!!!!!

hello friends

i have a question about pending order:

i have 2 pending orders buy and sell once one of them hit i what to close the other .

i am just a newbie mql programer and its beyond my ability right now.

any direction friends????

many thanks.

 

You can use something like this :

void CleanPendingOrders()

{

bool trade.BuyEntered = false;

bool trade.SellEntered = false;

for (int i=OrdersTotal()-1; i>=0; i--)

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

//

//

//

//

//

if ( OrderSymbol()==trade.symbol && OrderMagicNumber()==MagicNumber )

{

int type = OrderType();

if (type==OP_BUY && !trade.ByEntered) trade.BuyEntered = true;

if (type==OP_SELL && !trade.SellEntered) trade.SellEntered = true;

if (type >OP_SELL && (trade.ByEntered || trade.BuyEntered))

OrderDelete(OrderTicket());

}

}

}

It will delete any pending order if a regular order with same magic number and symbols is found

ERAN123:
hello friends

i have a question about pending order:

i have 2 pending orders buy and sell once one of them hit i what to close the other .

i am just a newbie mql programer and its beyond my ability right now.

any direction friends????

many thanks.
 

mladen

thanks a lot , i will check it.

thanks again

 

It should work OK

Have a nice weekend

ERAN123:
mladen

thanks a lot , i will check it.

thanks again
 

Hello fellow traders!

I've been trading for a few months now with a strategy that i am trying to program in mql4.

I execute an order with a take profit like so

"OrderSend(Symbol(), OP_BUY, lots, Ask, 10, 30, Ask+takeprofit, "test", 12345, 0, Green);"

Now when the order closes (t/p or s/l), i want to make another identical order:

"OrderSend(Symbol(), OP_BUY, lots, Ask, 10, 30, Ask+takeprofit, "test", 12345, 0, Green);"

so on and so forth so that it introduces a buy position whenever the previous buy has closed.

I've started learning mql4 a couple of days ago and i'm stuck on this. Please help!

 

Why don't you simply count the number of currently opened orders (either buy orders or sell orders) and when it is 0, open a new position?

qwertet:
Hello fellow traders!

I've been trading for a few months now with a strategy that i am trying to program in mql4.

I execute an order with a take profit like so

"OrderSend(Symbol(), OP_BUY, lots, Ask, 10, 30, Ask+takeprofit, "test", 12345, 0, Green);"

Now when the order closes (t/p or s/l), i want to make another identical order:

"OrderSend(Symbol(), OP_BUY, lots, Ask, 10, 30, Ask+takeprofit, "test", 12345, 0, Green);"

so on and so forth so that it introduces a buy position whenever the previous buy has closed.

I've started learning mql4 a couple of days ago and i'm stuck on this. Please help!
 
qwertet:

so on and so forth so that it introduces a buy position whenever the previous buy has closed.

I've started learning mql4 a couple of days ago and i'm stuck on this. Please help!

Hey, MLaden is right - you need to count orders to check if you are ready for new one.

Here is the function that you may want to use.

It will count how many orders you have already opened from selected with specified magic number and order type.

If you will put -1 as your order type it will count all orders by selected magic number.

Enjoy.

int orderCount(int type,int magic)

{

int oc = 0;

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderMagicNumber() == magic && (OrderType() == type || type == -1))

oc+=1;

}

return(oc);

}

Reason: