[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 461

 

Can you tell me why in my case OrderSended variable is not set to false when all conditions are met?

int start()
  {
//----
   int i;
   int i2;
   double MACD;
   double MACD2 = iCustom(Symbol(),0,"MACD",5,21,1,0,0);
   double MACD3 = iCustom(Symbol(),0,"MACD",5,21,1,0,1);
   double MyNull = 0.0;
   int bar1;
   int bar2;
   static bool OrderSended = false;
   
   if(MACD2 > 0 && MACD3 > 0 || MACD2 < 0 && MACD3 < 0) // Вот эти условия
     {
     OrderSended = false;
     }
   Print("MACD2 = ",MACD2,"MACD3 = ",MACD3,"OrderSended = ",OrderSended);
   
   if(MACD2 < 0 && MACD3 > 0 && OrderSended == false)
     {
     if(OrderSend(Symbol(),OP_SELL,Lot,Ask,Slippage,Ask + StopLoss * Point,0,OrdersComment,MagicNumber,0,Red) > 0)
       {
       OrderSended = true;
       }
     }
//----
   return(0);
  }

It says in the log MACD2 and MACD3 are greater than zero, but OrderSended is still true, although it should become false when both variables are greater than zero.

 
sss2019:

Can you tell me why in my case OrderSended variable is not set to false when all conditions are met?

It says in the log MACD2 and MACD3 are greater than zero, but OrderSended is still true, although it should become false when both variables are greater than zero.

if(MACD2 < 0 && MACD3 > 0

and in the code, one is greater than zero and the other is less than zero

 
ilunga:

and in the code, one is greater than zero and one is less than zero.


So it's just a signal to open a position, that's where the conditions are greater than zero

   if(MACD2 > 0 && MACD3 > 0 || MACD2 < 0 && MACD3 < 0) // Вот эти условия
     {
     OrderSended = false;
     }
 
sss2019:


So it's just a signal to open a position, that's where the conditions are greater than zero

But it's still a good idea to put parentheses here, because 99% of the conditions are not calculated in the order you would like them to be:

 if((MACD2 > 0 && MACD3 > 0) || (MACD2 < 0 && MACD3 < 0)) // Вот эти условия

 

Tell me if this design will work

if(OrdersTotal(MagicNumber))

That is, if there are warrants with a specific magic number then...

 
sss2019:

Tell me if this design will work

That is, if there are warrants with a specific magic number then...

only if you write the function yourself
int OrdersTotal(int magic)
 
Tell me, after executing a function, e.g. for, does this function return a single value or all of them? For example, in a loop a value has been assigned to two variables, will the loop return the value of both variables, or only the last one?
 

So, I did my own thing with fractals - I wrote my own indicator... Well, it's easier for me. Here's the question-- I got a problem.

how do I move the arrows back 3 bars? :(

A little piece of code...

SetIndexStyle(0, DRAW_ARROW,0,1);
SetIndexArrow(0,217);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexEmptyValue(0, 0.0);
SetIndexStyle(1, DRAW_ARROW,0,1);
SetIndexArrow(1, 218);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexEmptyValue(1, 0.0);

IndicatorShortName("My_iFractals");
SetIndexLabel(0, "iFractalsUp");
SetIndexLabel(1, "iFractalsDn");

UPD

Вопрос снят, сорри опять за кривость свою, но может кому будет интересно ответ- SetIndexShift(0,-3);

 
sss2019:
But tell me, after a function is executed, e.g. for, does this function return a single value or all of them? For example in a loop a value has been assigned to two variables, will the loop return the value of both variables, or only the last one?

What a mess...

1) for is not a function, it is a loop operator

2) the function only returns what it was told to return with return

3) Variables are loosely related to loops. You can change as many as 10 of them in a loop

 

Guys, I can't figure out what's wrong, it's not clear what's going on with orders closing:

when it's like this, it works fine:

double PriceBid = Bid;
        double PriceAsk = Ask;
        PrevPriceBid = PriceBid;
        PrevPriceAsk = PriceAsk;
        double be = GetWeightedBELevel();
        if(be != -1)
        {
                double BuyLots = GetBuyLotsSum();
                double SellLots = GetSellLotsSum();
                
                double Swap = GetSwap();
                //string str = be + " ";
                if(Swap < 0)
                {
                        double diff = BuyLots - SellLots;
                        //str = str + MarketInfo(Symbol(), MODE_TICKVALUE) * diff + " " + (-Swap / (MarketInfo(Symbol(), MODE_TICKVALUE) * diff) * Point) + " ";
                        be += ND(-Swap / (MarketInfo(Symbol(), MODE_TICKVALUE) * diff) * Point + diff / MathAbs(diff) * 1 * Point);
                }
                //str = str + Swap + " " + be;
                //Comment(str);
                
                
                if((BuyLots > SellLots && ND(PrevPriceBid) < ND(be + (TakeProfitBuy) * Point) && ND(PriceBid) >= ND(be + (TakeProfitBuy) * Point)) ||
                        (BuyLots < SellLots && ND(PrevPriceAsk) > ND(be - (TakeProfitSell) * Point) && ND(PriceAsk) <= ND(be - (TakeProfitSell) * Point)))
                {
                        CloseAllBuy();
                        CloseAllSell();
                }
                

        }
                        ObjectDelete("ZeroBuy");
                if(BuyLots > SellLots)
                {double ZeroBuy = ND(be + TakeProfitBuy * Point);
                ObjectCreate("ZeroBuy",OBJ_ARROW,0,Time[0],ZeroBuy,0,0,0,0);                     
      ObjectSet   ("ZeroBuy",OBJPROP_ARROWCODE,6);
      ObjectSet   ("ZeroBuy",OBJPROP_COLOR, Olive);}
                        ObjectDelete("ZeroSell");
                if(BuyLots < SellLots)
                {double ZeroSell = ND(be - TakeProfitSell * Point);
                ObjectCreate("ZeroSell",OBJ_ARROW,0,Time[0],ZeroSell,0,0,0,0);                     
      ObjectSet   ("ZeroSell",OBJPROP_ARROWCODE,6);
      ObjectSet   ("ZeroSell",OBJPROP_COLOR, Olive);}
        
        //---------------------

I add this code below the top one:

//+X================================================================X+X+

  int b,s;
   double price,price_b,price_s,lot,lot_s,lot_b,SLb,SLs,SLbTwo,SLsTwo; //
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {  if (OrderSymbol()==Symbol())
         {
            price = OrderOpenPrice();
            lot   = OrderLots();
            if (OrderType()==OP_BUY ) 
               {price_b = price_b+price*lot; b++;  lot_b=lot_b+lot;}
            if (OrderType()==OP_SELL) 
               {price_s = price_s+price*lot; s++;  lot_s=lot_s+lot;}
   }  }  }
   ObjectDelete("SLb");
   if (b!=0) 
   {  SLb = (price_b/lot_b)+TakeProfitBuy * Point;
      ObjectCreate("SLb",OBJ_ARROW,0,Time[0],SLb,0,0,0,0);                     
      ObjectSet   ("SLb",OBJPROP_ARROWCODE,6);
      ObjectSet   ("SLb",OBJPROP_COLOR, Navy);
   }
   ObjectDelete("SLbTwo");
   if (b!=0) 
   {  SLbTwo = (price_b/lot_b)+TakeProfitBuyTwo * Point;
      ObjectCreate("SLbTwo",OBJ_ARROW,0,Time[0],SLbTwo,0,0,0,0);                     
      ObjectSet   ("SLbTwo",OBJPROP_ARROWCODE,6);
      ObjectSet   ("SLbTwo",OBJPROP_COLOR, Navy);
   }
   
   ObjectDelete("SLs");
   if (s!=0) 
   {  SLs = (price_s/lot_s)-TakeProfitSell * Point;
      ObjectCreate("SLs",OBJ_ARROW,0,Time[0],SLs,0,0,0,0);                     
      ObjectSet   ("SLs",OBJPROP_ARROWCODE,6);
      ObjectSet   ("SLs",OBJPROP_COLOR, Maroon);
   }
   ObjectDelete("SLsTwo");
   if (s!=0) 
   {  SLsTwo = (price_s/lot_s)-TakeProfitSellTwo * Point;
      ObjectCreate("SLsTwo",OBJ_ARROW,0,Time[0],SLsTwo,0,0,0,0);                     
      ObjectSet   ("SLsTwo",OBJPROP_ARROWCODE,6);
      ObjectSet   ("SLsTwo",OBJPROP_COLOR, Maroon);
   }
   
   if ((totalBuy > 1) && (SLb > Ask) && (SLbTwo < Ask) && (DnM15 > Bid)) //
   {
    CloseAllBuy();
   }
   if ((totalSell > 1) && (SLs < Bid) && (SLsTwo > Bid) && (UpM15 < Ask)) //
   {
   CloseAllSell();
   }
   //--------------------------------- 

after the first function does not work and does not close orders. I don't see any errors in the log. I have made a mistake there, why?


I have moved all Object down both functions and it is working.

Reason: