My ATR variable loses its value when passing to function

 

I am trying to set stop loss according to an iATR value. Here is my commented code where I am having problems.

double fourWeekHigh = iHigh(NULL, PERIOD_W1, iHighest(NULL, PERIOD_W1, MODE_HIGH, 4,1));
double fourWeekLow = iLow(NULL, PERIOD_W1, iLowest(NULL, PERIOD_W1, MODE_LOW, 4, 1));
 
double lastWeekHigh = iHigh(NULL, PERIOD_W1, 1);
double lastWeekLow = iLow(NULL, PERIOD_W1, 1);

//Alert(fourWeekHigh);
//SL's
   double ATRStop = (iATR(Symbol(), PERIOD_D1, 2, 2));  //2 day ATR for last 2 days
   double buyStopLoss = fourWeekHigh - ATRStop;
   double sellStopLoss = fourWeekLow + ATRStop;

Alert(ATRStop);// this works, gives proper value
Alert(buyStopLoss); //works

//Position size

if (timeCheck() == 2)
{

   Alert (ATRStop); // returns "0", screws up all other calculations. 

   double RiskAmount = 10000 * (EquityPercent / 100);

   double TickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   if(Point == 0.001 || Point == 0.00001) TickValue *=10;

   double BuyStopPip = buyStopLoss / UsePoint;
   double SellStopPip = sellStopLoss / UsePoint;

   double calcBuyLot = (RiskAmount / BuyStopPip) / TickValue;
   double calcSellLot = (RiskAmount / SellStopPip) / TickValue;


   if(calcBuyLot < MarketInfo(Symbol(), MODE_MINLOT))
      {
         calcBuyLot = MarketInfo(Symbol(), MODE_MINLOT);
      }
   
   if(calcSellLot < MarketInfo(Symbol(), MODE_MINLOT))
      {
         calcSellLot = MarketInfo(Symbol(), MODE_MINLOT);
      }
   
   if(calcBuyLot > MarketInfo(Symbol(), MODE_MAXLOT))
      {
         calcBuyLot = MarketInfo(Symbol(), MODE_MAXLOT);
      }
   
   if(calcSellLot > MarketInfo(Symbol(), MODE_MAXLOT))
      {
         calcSellLot = MarketInfo(Symbol(), MODE_MAXLOT);
      }
   
   if(MarketInfo(Symbol(), MODE_LOTSTEP) == 0.1)
      {
         calcBuyLot = NormalizeDouble(calcBuyLot,1);
         calcSellLot = NormalizeDouble(calcSellLot, 1);
      }
   else 
      {
         calcBuyLot = NormalizeDouble(calcBuyLot, 2);
         calcSellLot = NormalizeDouble(calcSellLot, 2);
      }
}


///Set orders

if (timeCheck() == 3)
   {   
      if (activeOrderCheck() == 0)
      {
         placeOrders(fourWeekHigh, fourWeekLow, buyStopLoss, sellStopLoss, calcBuyLot, calcSellLot, ATRStop);
      } 
   }

clearExtraPending();
  
   
//----
   return(0);
  }
  






//--------------------------------------------------------------------------------
//Place orders
//-------------------------------------------------------------------------------- 
int placeOrders(double fourWeekHigh, double fourWeekLow, double buyStopLoss, double sellStopLoss, double calcBuyLot, double calcSellLot, double ATRStop) {

//Alert(fourWeekHigh);
//Alert(buyStopLoss);  this returns same as fourWeekHigh...ATR not working
Alert(ATRStop); //returns value of 0

OrderSend(Symbol(),OP_BUYSTOP, calcBuyLot, fourWeekHigh,3, buyStopLoss,0,NULL,0,0,CLR_NONE);


OrderSend(Symbol(),OP_SELLSTOP, calcSellLot, fourWeekLow,3, sellStopLoss,0,NULL,0,0,CLR_NONE);

Again, I am adding subtracting ATR value from my trade openings for SL placement. My ATR variable loses it value at some point, making it return "0", and I can't figure out why. Help would be greatly appreciated.

 

You declare the ATRStop variable at the time you call the function . . . hence it is 0.0

placeOrders(fourWeekHigh, fourWeekLow, buyStopLoss, sellStopLoss, calcBuyLot, calcSellLot,     double ATRStop    );

. . . does it actually compile ?

 
RaptorUK:

You declare the ATRStop variable at the time you call the function . . . hence it is 0.0

. . . does it actually compile ?


Yes it does. Sorry, I modified code to shorten it for this post, the double is not supposed to be there.
 
nubhubbins:

Yes it does. Sorry, I modified code to shorten it for this post, the double is not supposed to be there.
And now you have removed it the value is no longer 0.0 ?
 
RaptorUK:
And now you have removed it the value is no longer 0.0 ?

Negatory. Value still "0". The only place I get a good value from the ATR is my first Alert right after I define it. It then becomes 0 inside each of the functions.
 
nubhubbins:

Negatory. Value still "0". The only place I get a good value from the ATR is my first Alert right after I define it. It then becomes 0 inside each of the functions.

What you are doing is bad practice . . . but I assumed it should work.

Your local variables within the placeOrders function have the same names as your variables outside of the function . . . . for clarity and maybe even functionality you should distinguish between the two sets of variables . . . try adding an l for local to the start of the local variable names, e.g. lATRStop

//--------------------------------------------------------------------------------
//Place orders
//-------------------------------------------------------------------------------- 
int placeOrders(double lfourWeekHigh, double lfourWeekLow, double lbuyStopLoss, double lsellStopLoss, double lcalcBuyLot, double lcalcSellLot, double lATRStop) {

//Alert(lfourWeekHigh);
//Alert(lbuyStopLoss);  this returns same as fourWeekHigh...ATR not working
Alert(lATRStop); //returns value of 0

OrderSend(Symbol(),OP_BUYSTOP, lcalcBuyLot, lfourWeekHigh,3, lbuyStopLoss,0,NULL,0,0,CLR_NONE);


OrderSend(Symbol(),OP_SELLSTOP, lcalcSellLot, lfourWeekLow,3, lsellStopLoss,0,NULL,0,0,CLR_NONE);
 
RaptorUK:

What you are doing is bad practice . . . but I assumed it should work.

Your local variables within the placeOrders function have the same names as your variables outside of the function . . . . for clarity and maybe even functionality you should distinguish between the two sets of variables . . . try adding an l for local to the start of the local variable names, e.g. lATRStop


Thanks for the tip, I am not familiar with good practice, but trying. Still just trying to figure out why everything in placeOrders() returns 0.

Upon further inspection, calcSellLot and calcBuyLot are returning "0" as well. Somethings not right.

 
nubhubbins:

Negatory. Value still "0". The only place I get a good value from the ATR is my first Alert right after I define it. It then becomes 0 inside each of the functions.


Do these comments represent a new functions ? i.e ///Set orders really means a new function like setOrders(parameters............) If so you need to post more of your code. Somewhere in your code you are getting the scope of ATRStop wrong. If you post the code that is really being executed then someone could probably help you figure this out pretty quick. You don't need to post your entire EA. Just post the functions and the order they are being called. Otherwise we are all just guessing.

 
Alert(ATRStop);// this works, gives proper value

//Position size

if (timeCheck() == 2){
   Alert (ATRStop); // returns "0", screws up all other calculations. 
What you posted here is impossible. Your problem is in between - post the code.
 
WHRoeder:
What you posted here is impossible. Your problem is in between - post the code.


Alright guys I think I figured it out, there were 2 things happening.

1) My position calculation happens if timecheck == 2, but the place order happens if timecheck == 3. I thought this wouldnt be a problem because position calculation would happen 1minute before the ordersend. But apparently because the code runs new with each tick, then at timecheck ==3, the position calculation is not run and therefor all those values (calcLots, etc) become "0". I am not sure if this makes sense, but to fix it I just put the placeorder call inside the same if statement as the position calculation, so that they both run at the same time.

2) I was also getting ATR value of "0" because I did not have data for the "last 2 days" on my broker for the first run of the code. Ie my ea starts at the beginning of my available data, but requires values for days prior to those dates. Since that data is not available to me, I was getting an initial "0", but after that the values start working. Hope this makes sense.

if (timeCheck() == 3)
{

   double RiskAmount = 10000 * (EquityPercent / 100);

   double TickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   if(Point == 0.001 || Point == 0.00001) TickValue *=10;

   double BuyStopPip = (fourWeekHigh - buyStopLoss) / UsePoint;
   double SellStopPip = (sellStopLoss - fourWeekLow) / UsePoint;

   double calcBuyLot = (RiskAmount / BuyStopPip) / TickValue;
   double calcSellLot = (RiskAmount / SellStopPip) / TickValue;

   if(calcBuyLot < MarketInfo(Symbol(), MODE_MINLOT))
      {
         calcBuyLot = MarketInfo(Symbol(), MODE_MINLOT);
      }
   
   if(calcSellLot < MarketInfo(Symbol(), MODE_MINLOT))
      {
         calcSellLot = MarketInfo(Symbol(), MODE_MINLOT);
      }
   
   if(calcBuyLot > MarketInfo(Symbol(), MODE_MAXLOT))
      {
         calcBuyLot = MarketInfo(Symbol(), MODE_MAXLOT);
      }
   
   if(calcSellLot > MarketInfo(Symbol(), MODE_MAXLOT))
      {
         calcSellLot = MarketInfo(Symbol(), MODE_MAXLOT);
      }
   
   if(MarketInfo(Symbol(), MODE_LOTSTEP) == 0.1)
      {
         calcBuyLot = NormalizeDouble(calcBuyLot,1);
         calcSellLot = NormalizeDouble(calcSellLot, 1);
      }
   else 
      {
         calcBuyLot = NormalizeDouble(calcBuyLot, 2);
         calcSellLot = NormalizeDouble(calcSellLot, 2);
      }
      
      if (activeOrderCheck() == 0)
      {
         placeOrders(fourWeekHigh, fourWeekLow, buyStopLoss, sellStopLoss, calcBuyLot, calcSellLot, ATRStop);
      }
}

Thank you for taking the time to help me guys, I am new to this and really appreciate the help.

Reason: