我如何声明变量,比较它们然后在函数内使用它们

时间:2015-04-05 07:43:28

标签: mt4 mql5

我正在开发一个ea,要求我比较之前2个柱的高点和哪个高点,将其用作止损值。

对于相反的交易,我需要比较之前的2个低点并使用较低的一个作为止损值。

我正在做的是: -

void onTick()
{
  static int ticket=0;

  double ab=(//calculation for ab);
  double de=(//calculation for de); 
   if(Low[1]<Low[2])
      double sll=Low[1];
   if(Low[1]>Low[2])
      double sll=Low[2];
  if(buy logic comes here) 
  {
   double entryPrice=////////;
   double stoploss=sll-xyz;
   double takeprofit=entryPrice+((entryPrice-stoploss)*3);
   ticket = OrderSend(Symbol(),...entryPrice,stoploss,takeprofit,.....);
  }
    if(ticket == false)
         {
           Alert("Order Sending Failed");
         }
}

问题是我无法引用sll的值并收到错误消息“sll unclared identifier”

我对编程很新,如果有人可以帮我解决这个问题,我将不胜感激。 我已经添加了大部分代码供您理解逻辑。

1 个答案:

答案 0 :(得分:0)

如果你想在其他任何地方使用变量,你必须在if语句的范围之外声明它们,所以不要这样做,看看这个

double sll; // declare sll outside the if statements
if(Low[1]<Low[2])
   sll=Low[1];
if(Low[1]>Low[2])
   sll=Low[2];
if(buy logic comes here) 
{
 bool res = OrderSend(..........);
} 

根据你写的内容判断,你可能在其他地方使用res,然后你需要在if语句之外定义,因为作用域。

相关问题