iHigh and iLow function

 

Hello there

I'm trying to work around iHigh and iLow functions but when i set the shift argument on " 1 " it doesn't execute the program but it sounds fine when the shift is set on " 2 " . Here is my following code....

it's just a simple code whenever the price passes the High of the candle it opens Buy and vice versa, one more thing when i remove that iTime function it works fine but the problem is it opens many positions by every Tick even though i put that expression in if statements ( breakCandle )...

I will appreciate if anyone gives me any solutions....


#include <Trade\Trade.mqh>

CTrade   *Trade;



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

//| Expert initialization function                                   |

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

input double vLot = 0.1;

input ENUM_TIMEFRAMES timeFrame = PERIOD_M15;

input int     MagicNumber =30003;

input string  tradeComment = "AutoTrading";



static datetime candleTime = 0;

double High = 0;

double Low = 0;

double bidPrice = 0;

double takeProfit = 0;

double stopLoss = 0;

bool newCandle = false;

int breakCandle = 0;







int OnInit()

  {

//---

Trade = new CTrade;

Trade.SetExpertMagicNumber(MagicNumber);





//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---



   string GetConditionPrice = ConditionPrice();

   

   if (GetConditionPrice == "Long")

  {

  TradeOrder(ORDER_TYPE_BUY);

  }

  else if (GetConditionPrice == "Short")

  {

  TradeOrder(ORDER_TYPE_SELL);

  }

  }

  

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





string ConditionPrice() {



newCandle = false;

if (candleTime != iTime(_Symbol, timeFrame, 0)) 

{

newCandle = true;

candleTime = iTime(_Symbol, timeFrame, 0);

}



if (newCandle == true) {



High = NormalizeDouble (iHigh(_Symbol, timeFrame, 1), _Digits);

Low = NormalizeDouble (iLow(_Symbol, timeFrame, 1), _Digits);

bidPrice = NormalizeDouble (SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);



if ( bidPrice > High && breakCandle <= 0 )

{

return "Long";

breakCandle = 1;

}



else if (bidPrice < Low && breakCandle >= 0 ) 

{

return "Short";

breakCandle = -1;

}

}

return "not";

}









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



bool TradeOrder(ENUM_ORDER_TYPE OrderType){



   if (OrderType == ORDER_TYPE_BUY){



    takeProfit = bidPrice + 90*_Point;

    stopLoss = Low;

    

}

    else if (OrderType == ORDER_TYPE_SELL){



     takeProfit = bidPrice - 90*_Point;

     stopLoss = High;

     

}

    

Trade.PositionOpen(_Symbol, OrderType, vLot, NULL, stopLoss, takeProfit, tradeComment);



return (true);



}
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Sergey Golubev #:

When you post code please use the CODE button (Alt-S)!

yep it looks much better
 
int breakCandle = 0;
⋮
if ( bidPrice > High && breakCandle <= 0 ){
return "Long";
breakCandle = 1;
}
else if (bidPrice < Low && breakCandle >= 0 ){
return "Short";
breakCandle = -1;
Your variable, breakCandle, is never set, always zero..
 
William Roeder #:
Your variable, breakCandle, is never set, always zero..

I'm not pretty sure what you mean, but i initialized it first as zero at the beginning then lets say an upward candle is happening so the price will be more than High and because that " breakCandle " is zero then a position will be opened so if it goes the same direction because breakCandle is now " 1 " no extra position should open till it goes down....

anyway that iTime function will prevent it to open .... any idea?

 
Kourosh Emami #: but i initialized it first as zero at the beginning .
That is the only time. Your posted code never changes it.
Kourosh Emami #: position will be opened so if it goes the same direction because breakCandle is now " 1 "

Your posted code never changes it. It is still zero. Your posted code doesn't open positions.

 
William Roeder #:
That is the only time. Your posted code never changes it.

Your posted code never changes it. It is still zero. Your posted code doesn't open positions.

So is there any way to get around it?
I'm honestly out of ideas. I tried " Bars() " or even " PositionsTotal() " non of them worked for me.
You know " PositionsTotal " is keeping one position opened but as soon as the price hit Take Profit it instantly open another position in the same candle which i don't want it.....
May you please paste the code in your Meta Trader and check it how it goes ? I will appreciate it
 
Kourosh Emami #: So is there any way to get around it?

When in doubt, think!

Your posted code never changes the variable.
if ( bidPrice > High && breakCandle <= 0 ){
return "Long";
breakCandle = 1;
}
else if (bidPrice < Low && breakCandle >= 0 ){
return "Short";
breakCandle = -1;
This will.
if ( bidPrice > High && breakCandle <= 0 ){
breakCandle = 1;
return "Long";
}
else if (bidPrice < Low && breakCandle >= 0 ){
breakCandle = -1;
return "Short";
 
William Roeder #:

When in doubt, think!

Your posted code never changes the variable.
This will.

Damn man it actually worked ! omg now i see what you mean saying " this part will always be zero " you'd been trying to make me realized my mistake by addressing it  ... you are genius.... thanks man.

Reason: