Help with this code please - page 2

 
peteshr2st:

Here is my problem. This now shows me all active trades I have on each chart as I go through them but when I set the boolean to show all trades it displays all of them but the symbols are off. Again it will only display a max of 2 currency symbols. As you said this is down to the variables buysymbol and sellsymbol only containing one at any one time but I don't know how to get around this. I tried using arrays but since the symbols are letters not numbers I am running into some problems it seems.

You can have arrays of strings as well as other types.

Code fragment ...

  double buys_list[50][3];
  double sells_list[50][3];
  string buysymbol[50];  // new array
  string sellsymbol[50]; // new array
  string res;
  

  // Count of all open positions at this time of the current
  for(i = 0; i < OrdersTotal(); i++)
  {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) continue;
   
   if (show_all_open_orders != false)
      { 
      }
      else
      {
      if(OrderSymbol() != Symbol()) continue;
      }
    
    if(OrderType() == OP_BUY)
    {
      buysymbol[nb] = OrderSymbol();  // buysymbol is now indexed like the other arrays. Obviously you need to do this throughout the program
      buys_list[nb][1] = OrderProfit(); 

Also get rid of the global strings buysymbol and sellsymbol now you are using arrays.

 
peteshr2st:

One other small thing I can't figure out yet is creating some space between the trades on screen. There strings that are responsible for displaying the trades. Here is an example.

I thought I could add a new line between trades by adding the + "\n" as shown below but it didn't have any effect at all.

extern int    labs_space  =  10;           // Y spacing between labels 
Apparently new lines don't work in labels but you could always adjust the above variable. I don't know but I might "guess" that it adjusts the Y spacing between the labels :-)
 

Awesome Leslie I have it working pretty much perfectly now. I have added more input changes and Boolean statements to make it a bit more customizable too.

There are 2 final small changes I would like to make. These are not a major changes at all just things I would like to get done before I can say it is completed.

First one is to do with the line break. When there are 1 or more open buy and sell orders on screen it looks great but when there is just 1 buy order on its own it is still visible and not needed. This is because the line break code tells it to appear above all buy orders.

The logic I need is to tell the indicator to display the line break only when there are 1 or more open buy and sell orders. When there is just 1 sell order open it doesn't appear and looks great because the line break code was only added to the code applying to buy orders. Any ideas on this?

Here is the how it looks with just one buy order active. Notice the line break above the order. Of course if I have more than one buy order open they stack up under that line break as well.

Ideally I would like it to appear in the same way sell orders appear i.e no line break

The next small edit I am hoping to figure out is the order display. As you can see below open orders are displayed starting with sell orders then a dashed line break and the buy orders. Is it possible to rearrange these so buys appear on top of the sells? This is not a big deal just something I would like to hear your opinion on because for all I know it might just be a straightforward code edit to do it.

As always I am really grateful for any suggestions you can give me dabbler :)

Seriously, thanks a lot for your kind effort to help me out here!

* Latest indicator attached below

Files:
 

I will have a look a bit later, for now I would make the general observation that the code is more difficult to read than it should be because it is not indented correctly. The indentation has absolutely no effect on the functionality but it is key to debugging code because the indenting helps you to see how the code functions. Let's take the init function. When everything lines up you can see it better. It may seem insignificant to you but it drives me crazy and I would need to fix it before reading on. Maybe its OCD, or maybe it's just coding discipline from years of experience.

void init()
{
        // create 15 text boxes to display
        for(int i = 1; i <= labs_max; i++)
        {
          ObjectCreate("lab"+i, OBJ_LABEL, 0, 0, 0);
        ObjectSet("lab"+i, OBJPROP_CORNER, position_corner);
          ObjectSet("lab"+i, OBJPROP_XDISTANCE, labs_xdist);
        ObjectSet("lab"+i, OBJPROP_YDISTANCE, i * labs_space + labs_ydist);
    ObjectSetText("lab"+i, "",0,"",0);
  }
}

should be

void init()
{
        // create 15 text boxes to display
        for(int i = 1; i <= labs_max; i++)
        {
          ObjectCreate("lab"+i, OBJ_LABEL, 0, 0, 0);
          ObjectSet("lab"+i, OBJPROP_CORNER, position_corner);
          ObjectSet("lab"+i, OBJPROP_XDISTANCE, labs_xdist);
          ObjectSet("lab"+i, OBJPROP_YDISTANCE, i * labs_space + labs_ydist);
          ObjectSetText("lab"+i, "",0,"",0);
        }
}

or I prefer it as

void init(){

   for(int i = 1; i <= labs_max; i++){                  // create 15 text boxes to display
      ObjectCreate("lab"+i, OBJ_LABEL, 0, 0, 0);
      ObjectSet("lab"+i, OBJPROP_CORNER, position_corner);
      ObjectSet("lab"+i, OBJPROP_XDISTANCE, labs_xdist);
      ObjectSet("lab"+i, OBJPROP_YDISTANCE, i * labs_space + labs_ydist);
      ObjectSetText("lab"+i, "",0,"",0);
   }
}
 

Look at this part ...

        if( !show_profit_values )
        {
           
        res = "Buy " +buysymbol[i]+  "  Pips  +" + DoubleToStr(buys_list[i][2]/10, 1);
        ObjectSetText("lab"+lab, res, font_size, font_name, profit_colour);   
        
        }
        else
        res = "Buy " +buysymbol[i]+  "  Pips  +" + DoubleToStr(buys_list[i][2]/10, 1) + "  Profit +" + DoubleToStr(buys_list[i][1], 2);
        ObjectSetText("lab"+lab, res, font_size, font_name, profit_colour);

The final ObjectSetText is not associated with the final else. It would need braces to be associated with it.

 

So we have this error often

        if( !show_profit_values )
        {
        res = "Sell " +sellsymbol[i]+  "  Pips  " + DoubleToStr(sells_list[i][2]/10, 1);
        ObjectSetText("lab"+lab, res, font_size, font_name, loss_colour);
        }
        else
        res = "Sell " +sellsymbol[i]+  "  Pips  " + DoubleToStr(sells_list[i][2]/10, 1) + "  Profit  " + DoubleToStr(sells_list[i][1], 2);
        ObjectSetText("lab"+lab, res, font_size, font_name, loss_colour);

But it works because you are just duplicating the same instruction twice if the first test makes the first braces executed. In other words you execute both ObjectSetText commands!

        if( !show_profit_values )
            res = "Sell " +sellsymbol[i]+  "  Pips  " + DoubleToStr(sells_list[i][2]/10, 1);
        else
            res = "Sell " +sellsymbol[i]+  "  Pips  " + DoubleToStr(sells_list[i][2]/10, 1) + "  Profit  " + DoubleToStr(sells_list[i][1], 2);
        
        ObjectSetText("lab"+lab, res, font_size, font_name, loss_colour);
 
even more
res = "Sell " +sellsymbol[i]+  "  Pips  " + DoubleToStr(sells_list[i][2]/10, 1);
if( show_profit_values ) res = res +"  Profit  "+ DoubleToStr(sells_list[i][1], 2) );
ObjectSetText("lab"+lab, res, font_size, font_name, loss_colour);
 

Now we have ...

            if( !show_profit_values )
              res = "Buy " +buysymbol[i]+  "  Pips  " + DoubleToStr(buys_list[i][2]/10, 1);             
            else
              res = "Buy " +buysymbol[i]+  "  Pips  " + DoubleToStr(buys_list[i][2]/10, 1) + "  Profit  " + DoubleToStr(buys_list[i][1], 2);

Which is mostly the same thing twice, and is easier to write and to maintain when written like this

            res = "Buy " +buysymbol[i]+  "  Pips  " + DoubleToStr(buys_list[i][2]/10, 1);
            if( show_profit_values )
               res = res + "  Profit  " + DoubleToStr(buys_list[i][1], 2);    
 

This line (and another like it) is actually a bug.

      ArrayResize(buys_list, nb);

The array size started off as 50. We are now shrinking the size to exactly fit the number of buy orders. But if the number of buy orders subsequently increases the program will crash by over-running the end of the array. All you need to do is delete these well intentioned but actually badly flawed lines.

 

Here's where we are to date, with the same functionality but cleaned up a little.

Files:
Reason: