当指标缓冲区不再提供值时,如何使字符串值保持不变?

How do I keep the string value the same when the indicator buffer no longer gives a value?

我编写了一个基本的 Expert Advisor,当参数为真时触发交易。但是,在这种情况下,代码永远不会满足所有要求,因为当指标缓冲区未提供值时,字符串值也没有提供任何值。

我需要帮助的是让 saisignal 的字符串值在指标缓冲区触发时保持不变,在柱形图通过箭头指示器之后,以便当其他信号最终指示时一笔交易,它可以触发一笔交易。

double closeAllTradesThisPair()
{
  for (int i=OrdersTotal();i>=0;i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol())
    {
     OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrNONE);
    }
   }
 }

void OnTick()
{
 double saiup = iCustom( Symbol(), PERIOD_H1, "super-arrow-indicator", 0, 0 );
 double saidn = iCustom( Symbol(), PERIOD_H1, "super-arrow-indicator", 1, 0 );
 double osma1 =   iOsMA( Symbol(), PERIOD_H1, 12, 26, 9, PRICE_OPEN, 1 );
 double osma0 =   iOsMA( Symbol(), PERIOD_H1, 12, 26, 9, PRICE_OPEN, 0 );
 double stup  = iCustom( Symbol(), PERIOD_H1, "super-trend", 0, 0 );
 double stdn  = iCustom( Symbol(), PERIOD_H1, "super-trend", 1, 0 );
 double sar   =    iSAR( Symbol(), PERIOD_H1, 0.02, 0.2, 0 );
 double ma    =     iMA( Symbol(), PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE, 0 );

 string saisignal  = "";
 string osmasignal = "";
 string stsignal   = "";
 string sarsignal  = "";
 string masignal   = "";

 if(saiup < 1000)
  {
    saisignal = "123";
  }
 if(saidn < 1000)
  {
    saisignal = "321";
  }
  
 if(osma1 < osma0)
   {
    osmasignal = "123";
  }
 if(osma1 > osma0)
  {
    osmasignal = "321";
  }

if(stup < 1000)
  {
    stsignal = "123";
  }
if(stdn < 1000)
  {
    stsignal = "321";
  }
 
if(sar < Bid)
  {
    sarsignal = "123";
  }
if(sar > Bid)
  {
    sarsignal = "321";
  }

if(ma < Bid)
  {
    masignal = "123";
  }
if(ma > Bid)
  {
    masignal = "321";
  } 

for(int b=OrdersTotal()-1;b>=0;b--)
  {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      {
        if(OrderSymbol()==Symbol())
          {
            if(OrderType()==OP_BUY)
              {
                if(OrderStopLoss() < Ask - (150*_Point))
                  {
                    OrderModify(OrderTicket(),OrderOpenPrice(),Ask-(150*_Point),OrderTakeProfit(),0,CLR_NONE);
                  }
              }
            if(OrderType()==OP_SELL)
              {
                if(OrderStopLoss() > Bid + (150*_Point))
                  {
                    OrderModify(OrderTicket(),OrderOpenPrice(),Bid+(150*_Point),OrderTakeProfit(),0,CLR_NONE);
                  }
              }
          }
      }
  }
                   
if(saisignal == "123")
  {
    if(osmasignal == "123")
      {
        if(stsignal == "123")
          {
            if(sarsignal == "123")
              {
                if(masignal == "123")
                  {
                    double buyticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,Ask-150*_Point,0,NULL,0,0,CLR_NONE);
                  }
              }
          }
      }
  }

if(saisignal == "321")
  {
    if(osmasignal == "321")
      {
        if(stsignal == "321")
          {
            if(sarsignal == "321")
              {
                if(masignal == "321")
                  {
                    double sellticket = OrderSend(Symbol(),OP_SELL,0.01,Ask,3,Bid+150*_Point,0,NULL,0,0,CLR_NONE);
                  }
              }
          }
      }
  }
Comment("   sai: ",saisignal,"   osma: ",osmasignal,"   st: ",stsignal,"   sar: ",sarsignal,"   ma: ",masignal);
}  

Q : "How do I keep the string value the same...?"

此问题只有一个原因和一对可用的解决步骤:

OnTick(){...} 代码 as-is 为每个条目创建空字符串。无论如何都必须更换它。

但是我们必须在某处以某种方式声明这些字符串。

第一个选项

仍然让它们在 OnTick(){...}-code-block 的“范围”({...})内声明,但将它们声明为 static string saisignal = ""; // STATIC modifier is key here,因为在这种情况下,static 使编译器保持此类声明变量的实际值从一个 OnTick(){...} 调用到另一个(所以永远不会 re-declaring 和 re-storing 初始化值(这里是一个空的 (string)"")。

    #property strict
    ...
    void OnTick(){
         ...
         static string saisignal  = "";
         static string osmasignal = "";
         static string stsignal   = "";
         static string sarsignal  = "";
         static string masignal   = "";
         ...
    }

第二个选项

将此类变量声明移至“Top-Level”作用域(对于全局可见变量),这使得编译器保留一个永久内存位置来存储和访问任何此类“全局 declared/visible”变量( s).

    #property strict

    string saisignal  = "";
    string osmasignal = "";
    string stsignal   = "";
    string sarsignal  = "";
    string masignal   = "";
    ...
    void OnTick(){
         ...
    }