Coding help - page 242

 
Mastercash:
Can somebody tell me what did I do wrong, the below code is 2 EMA cross over with alerts.It refuse to work.Pls can some one help out to compile and work on mt4 build 600 +:

#property copyright "wnk"

#property link "www.wnk.com"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Lime

#property indicator_color2 Red

//--- buffers

double ExtMapBuffer1[];

double ExtMapBuffer2[];

//external variable......

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0,217);

SetIndexBuffer(0,ExtMapBuffer1);

SetIndexEmptyValue(0,0.0);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1,217);

SetIndexBuffer(1,ExtMapBuffer2);

SetIndexEmptyValue(1,0.0);

//----

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

static datetime LastAlertTime = TimeCurrent();

int counted_bars=IndicatorCounted(),

limit;

if(counted_bars<0)

return(-1);

if(counted_bars>0)

counted_bars--;

limit=Bars-counted_bars;

while(limit)

{

double ema13=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);

double ema5=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);

double b4ema13=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,1);

double b4ema5=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);

double mom=iMomentum(NULL,0,14,PRICE_CLOSE,0);

double b4mom=iMomentum(NULL,0,14,PRICE_CLOSE,1);

//up alerts

if((LastAlertTime!=Time[0])&&(ema5>ema13)&&(ema5>b4ema5)&&(ema13>b4ema13)&&(mom>b4mom)&&(mom>98.6591))

ExtMapBuffer1[limit]=High[limit]+5*Point;

LastAlertTime = Time[0];

Alert(Symbol()," ",Period(),"M Price UP");

//sell alerts

if((LastAlertTime!=Time[0])&&(ema5<ema13)&&(ema5<b4ema5)&&(ema13<b4ema13)&&(mom<b4mom)&&(mom<100.6872))

ExtMapBuffer2[limit]=Low[limit]-5*Point;

LastAlertTime = Time[0];

Alert(Symbol()," ",Period(),"M Price Down");

}

return(0);

}

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

Mastercash

Try it out now

Files:
_test_mc.mq4  3 kb
 
mladen:
Axel

It can be done.

Simply the open orders should be scanned for desired type and their open prices should be compared to the desired new open price or the bars at which they were opened can be compared to the bar that serves as a criteria for opening

EDIT: Seems like I forgot to add the function.

Would it work to write a function like the one below 20 times or so for different "i=OrdersTotal()-1" and then call in all the function and match them against new opening price?

double OpenOrderPrice()

{

double TempOrderPrice = 0;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

TempOrderPrice = OrderOpenPrice();

}

}

return(TempOrderPrice);

}

 
BlackCoq:
EDIT: Seems like I forgot to add the function.

Would it work to write a function like the one below 20 times or so for different "i=OrdersTotal()-1" and then call in all the function and match them against new opening price?

double OpenOrderPrice()

{

double TempOrderPrice = 0;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

TempOrderPrice = OrderOpenPrice();

}

}

return(TempOrderPrice);

}

Try something like this :

double OpenOrderPrice(double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

break;

}

}

return(TempOrderPrice);

}

BlackCoq

You have to passe the price it should be opened at and the maximal deviation (an absolute value : for example not 5 for five points, but 5*_Point) from the price, and if the difference is smaller than or equal to that price deviation it will return the price of the order opened at that approximate price. Otherwise it will return -1 as a result (which mean in that case that there are no orders with similar prices)

 
mladen:
Try something like this :
double OpenOrderPrice(double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

break;

}

}

return(TempOrderPrice);

}

BlackCoq

You have to passe the price it should be opened at and the maximal deviation (an absolute value : for example not 5 for five points, but 5*_Point) from the price, and if the difference is smaller than or equal to that price deviation it will return the price of the order opened at that approximate price. Otherwise it will return -1 as a result (which mean in that case that there are no orders with similar prices)

So if I want to check if there are any open orders deviating 2 pips from the price I want to buy at, I call for this function:

double BuyOpenOrderPrice(double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

priceDeviation = 2*Point;

priceToCompareTo = Ask;

break;

}

}

return(TempOrderPrice);

}

And then make another one for shorts?

 
BlackCoq:
So if I want to check if there are any open orders deviating 2 pips from the price I want to buy at, I call for this function:

double BuyOpenOrderPrice(double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

priceDeviation = 2*Point;

priceToCompareTo = Ask;

break;

}

}

return(TempOrderPrice);

}

And then make another one for shorts?

No

You call the function like this :

if (OpenOrderPrice(OP_BUY,Ask ,2.0*_Point) == -1) allows longs and

if (OpenOrderPrice(OP_SELL,Ask,2.0*_Point) == -1) allows shorts

The call should be from your code part where you have a logic to open an order.

But then the function has to be different (like this) :

double OpenOrderPrice(int orderType, double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

if (OrderType()==orderType && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

break;

}

}

return(TempOrderPrice);

}

The function covers both cases now and you should not change the code withing the function

___________________

PS: _Point variable does not exist in older builds of metatrader 4. The upper example is written for the new metatrader4. If you use older build (509 or earlier) then "_Point" should be "Point"

 
mladen:
No

You call the function like this :

The call should be from your code part where you have a logic to open an order.

But then the function has to be different (like this) :

The function covers both cases now and you should not change the code withing the function

___________________

PS: _Point variable does not exist in older builds of metatrader 4. The upper example is written for the new metatrader4. If you use older build (509 or earlier) then "_Point" should be "Point"

Thank you. I copied the function to the EA and called for it as you wrote, but for some reason it opens positions even though there are others within the deviation of new price. Does that function check all open prices, or just the latest one?

Including the EA below.

//+------------------------------------------------------------------+//| expert start function |

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

int start()

{

//----

bool result;

PipsUntilNextCandle--;

DisplayUserFeedback();

if (OldBars != Bars)

{

PipsUntilNextCandle = RenkoBoxSize;

OldBars = Bars;

DoesTradeExist();

double take;

double stop;

RefreshRates();

//Have the last candles risen , 1=last candle , 2 = last two candles

if (Open[0] > Open[CheckCandle] && (OpenOrderPrice(OP_BUY,Ask ,20.0*Point) == -1))

{

if (TakeProfit > 0) take = NormalizeDouble(Ask + (TakeProfit * Point), Digits);

if (StopLoss > 0) stop = NormalizeDouble(Ask - (StopLoss * Point), Digits);

result = SendSingleTrade(OP_BUY, TradeComment, Lot, Ask, stop, take, MagicNumber);

if (!result) OldBars = 0;

}

//if (Open[0] > Open[2])

//Have the last candles fallen , , 1=last candle , 2 = last two candles

if (Open[0] < Open[CheckCandle] && (OpenOrderPrice(OP_SELL,Ask,20.0*Point) == -1))

{

if (TakeProfit > 0) take = NormalizeDouble(Bid - (TakeProfit * Point), Digits);

if (StopLoss > 0) stop = NormalizeDouble(Bid + (StopLoss * Point), Digits);

result = SendSingleTrade(OP_SELL, TradeComment, Lot, Bid, stop, take, MagicNumber);

if (!result) OldBars = 0;

}//if (Open[0] > Open[2])

}//if (OldBars != Bars)

//----
 
BlackCoq:
Thank you. I copied the function to the EA and called for it as you wrote, but for some reason it opens positions even though there are others within the deviation of new price. Does that function check all open prices, or just the latest one?

Including the EA below.

//+------------------------------------------------------------------+//| expert start function |

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

int start()

{

//----

bool result;

PipsUntilNextCandle--;

DisplayUserFeedback();

if (OldBars != Bars)

{

PipsUntilNextCandle = RenkoBoxSize;

OldBars = Bars;

DoesTradeExist();

double take;

double stop;

RefreshRates();

//Have the last candles risen , 1=last candle , 2 = last two candles

if (Open[0] > Open[CheckCandle] && (OpenOrderPrice(OP_BUY,Ask ,20.0*Point) == -1))

{

if (TakeProfit > 0) take = NormalizeDouble(Ask + (TakeProfit * Point), Digits);

if (StopLoss > 0) stop = NormalizeDouble(Ask - (StopLoss * Point), Digits);

result = SendSingleTrade(OP_BUY, TradeComment, Lot, Ask, stop, take, MagicNumber);

if (!result) OldBars = 0;

}

//if (Open[0] > Open[2])

//Have the last candles fallen , , 1=last candle , 2 = last two candles

if (Open[0] < Open[CheckCandle] && (OpenOrderPrice(OP_SELL,Ask,20.0*Point) == -1))

{

if (TakeProfit > 0) take = NormalizeDouble(Bid - (TakeProfit * Point), Digits);

if (StopLoss > 0) stop = NormalizeDouble(Bid + (StopLoss * Point), Digits);

result = SendSingleTrade(OP_SELL, TradeComment, Lot, Bid, stop, take, MagicNumber);

if (!result) OldBars = 0;

}//if (Open[0] > Open[2])

}//if (OldBars != Bars)

//----

There is an error in that function code. I assumed that the OrderSelect() worked on position and did not check it. Use this one :

double OpenOrderPrice(int orderType, double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderType()==orderType && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

break;

}

}

return(TempOrderPrice);

}

It should work properly now

 
mladen:
There is an error in that function code. I assumed that the OrderSelect() worked on position and did not check it. Use this one :
double OpenOrderPrice(int orderType, double priceToCompareTo, double priceDeviation)

{

double TempOrderPrice = -1;

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

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderType()==orderType && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{

if (MathAbs(OrderOpenPrice()-priceToCompareTo)<=priceDeviation)

TempOrderPrice = OrderOpenPrice();

break;

}

}

return(TempOrderPrice);

}

It should work properly now

And so it does. Thank you again for your great work, Mladen.

 

Hello Mladen ,

could you please take a look at this code am trying to select the last order and open additional orders based on last open price. Everything seems to be working except that

IfOrderDoesNotExist7(); IfOrderDoesNotExist5();

seems to be interferring with each other if i should comment one of the two like bellow the order will work fine.

// IfOrderDoesNotExist7();

IfOrderDoesNotExist5();

can you tell me what im doing wrong.

Files:
564.mq4  10 kb
 
sulaimoney:
Hello Mladen ,

could you please take a look at this code am trying to select the last order and open additional orders based on last open price. Everything seems to be working except that

IfOrderDoesNotExist7(); IfOrderDoesNotExist5();

seems to be interferring with each other if i should comment one of the two like bellow the order will work fine.

// IfOrderDoesNotExist7();

IfOrderDoesNotExist5();

can you tell me what im doing wrong.

sulaimoney

I think that the problem was not in those two functions but in the way how new metatrader 4 checks for boolean conditions (if you were using some of the new builds of metatrader 4). Simplified the code a bit and resolved the one place where boolean conditions needed to be strictly defined. Try it out

Files:
564_1.mq4  10 kb
Reason: