Help with seeminlgy simple code

To add comments, please log in or register
hughbriss
131
hughbriss  

Hi, I coded this quick dashboard to tell me which pairs are above or below the 3 sma on 3 different timeframes, daily, weekly and monthly. After a few problems I managed to get the pairs displaying as I want them but they are all colored lime which can't be right. I wonder if anyone can see what I am doing wrong? I think it may be something to do with the values being returned by either the iMA function, the iClose function or both?

 

#property indicator_chart_window

extern int fontsize = 20;

string pairss[34];
color pcol[34];
double ppma[34][3];
string tf;
int x,y;

int start() 
  {
  // Pause and refresh
  Sleep(2000); x = 40; y = 0;
  
  // Define strings
  
  pairss[0] = "EURUSD"; pairss[1] = "EURGBP"; pairss[2] = "EURJPY"; pairss[3] = "EURCAD"; pairss[4] = "EURAUD"; pairss[5] = "EURNZD"; pairss[6] = "USDCHF"; 
  pairss[7] = "USDJPY"; pairss[8] = "USDCAD";
  pairss[9] = "USDSGD"; pairss[10] = "GBPUSD"; pairss[11] = "GBPCHF"; pairss[12] = "GBPJPY"; pairss[13] = "GBPCAD"; pairss[14] = "GBPAUD"; 
  pairss[15] = "GBPNZD"; pairss[16] = "AUDUSD"; pairss[17] = "AUDCHF";
  pairss[18] = "AUDJPY"; pairss[19] = "AUDCAD"; pairss[20] = "AUDNZD"; pairss[21] = "NZDUSD"; pairss[22] = "NZDJPY"; pairss[23] = "NZDCAD"; 
  pairss[24] = "CADCHF"; pairss[25] = "CADJPY"; pairss[26] = "CHFJPY";
  pairss[27] = "XAUUSD"; pairss[28] = "XAGUSD"; pairss[29] = "DAX30"; pairss[30] = "FTSE100"; pairss[31] = "SPI200"; pairss[32] = "SP500"; pairss[33] = "CL-OIL";
  
  // Calculate ma
  
  int counter3 = 0, counter4 = 0;
  while(counter3 < 34)
  {
  while(counter4 < 3)
  {
  if(counter4 == 0) tf = "PERIOD_D1";
  if(counter4 == 1) tf = "PERIOD_W1";
  if(counter4 == 2) tf = "PERIOD_MN1";
  
  ppma[counter3,counter4] = iMA(pairss[counter3],tf,3,0,0,PRICE_TYPICAL,0);
  counter4++;
  }
  counter3++;
  }
  
  // Compare ma and assign colours
  
  int counter5 = 0;
  while(counter5 < 34)
  {
  pcol[counter5] = White;
  if(iClose(pairss[counter5],PERIOD_D1,0) > ppma[counter5,0] && iClose(pairss[counter5],PERIOD_W1,0) > ppma[counter5,1] && 
     iClose(pairss[counter5],PERIOD_MN1,0) > ppma[counter5,2]) pcol[counter5] = Lime;
  
  if(iClose(pairss[counter5],PERIOD_D1,0) < ppma[counter5,0] && iClose(pairss[counter5],PERIOD_W1,0) < ppma[counter5,1] && 
     iClose(pairss[counter5],PERIOD_MN1,0) < ppma[counter5,2]) pcol[counter5] = Red;  
  
  counter5++;
  }
  
  // Display pairs
  
  int counter6 = 0;
  while(counter6 < 34)
  {
  
  if(counter6 == 9) {x = 200; y = 0;}
  if(counter6 == 18){x = 400; y = 0;}
  if(counter6 == 27){x = 600; y = 0;}
  y = y + 40;
  
  Display(pairss[counter6],x,y); ObjectSetText(pairss[counter6],pairss[counter6],fontsize,"Verdana", pcol[counter6]);
  counter6++;
  }
    
return(0);
  }

void Display(string name, int x, int y)
   {
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
   ObjectSet(name, OBJPROP_CORNER, 0);
   ObjectSet(name, OBJPROP_XDISTANCE, x);
   ObjectSet(name, OBJPROP_YDISTANCE, y);
   ObjectSet(name, OBJPROP_BACK, FALSE);
   } 
AGNERAY
207
AGNERAY  

you can do more simple for all of your paires:


extern string SymbolslList = "EURUSD;GBPUSD;EURJPY;USDJPY;EURGBP;";

string Sbl[];

//-------------------------

init()

UpdateSymbolsList();

return(0)

//-------------------------

void UpdateSymbolsList()
   {
      string sbl = "";
      int count = 0;
      for(int i=0; i<StringLen(SymbolslList); i++)
        {
           if(StringSubstr(SymbolslList, i, 1) != ";")
              {
                 sbl= sbl+ StringSubstr(SymbolslList, i, 1);
              }
              else
              {                     {
                    ArrayResize(Sbl, ArraySize(Sbl)+1);
                    Sbl[count] = sbl;
                    sbl ="";
                    count +=1;
             }
        }
   }
use Sbl[i] to replace "counter5"
hughbriss
131
hughbriss  

I've cracked it, after the first loop of counter3 counter4 is not being reset and so the ma is not being calculated properly. Thanks for looking anyway.

hughbriss
131
hughbriss  
Kane59:

you can do more simple for all of your paires:

use Sbl[i] to replace "counter5"

OK, thanks, I will try that next time. 

Simon Gniadkowski
17271
Simon Gniadkowski  
hughbriss:

Hi, I coded this quick dashboard to tell me which pairs are above or below the 3 sma on 3 different timeframes, daily, weekly and monthly. After a few problems I managed to get the pairs displaying as I want them but they are all colored lime which can't be right. I wonder if anyone can see what I am doing wrong? I think it may be something to do with the values being returned by either the iMA function, the iClose function or both?

This is wrong . . . 

if(counter4 == 0) tf = "PERIOD_D1";
  if(counter4 == 1) tf = "PERIOD_W1";
  if(counter4 == 2) tf = "PERIOD_MN1";    //  tf is a string . . . 
    
  ppma[counter3,counter4] = iMA(pairss[counter3],tf,3,0,0,PRICE_TYPICAL,0);    // you are using a string for the timeframe,  it should be an int

 do this instead . . .

int tf;                                 //  tf is an int . . . 

  if(counter4 == 0) tf = PERIOD_D1;
  if(counter4 == 1) tf = PERIOD_W1;
  if(counter4 == 2) tf = PERIOD_MN1;    //  tf is an int . . . 
    
  ppma[counter3,counter4] = iMA(pairss[counter3],  tf,  3,0,0,PRICE_TYPICAL,0);    
hughbriss
131
hughbriss  
RaptorUK:

This is wrong . . . 

 do this instead . . .

 

 

Ok, thank you, I didn't think it would make a difference? 

William Roeder
22318
William Roeder  
Didn't think it would make a difference? 2+3 == 5 but "2"+"3" == "23"
To add comments, please log in or register