store symbol currency to a array variable

 

Hi,

I want to store symbol name and symbol volume into an array. I found that array function ArrayFill can't be used on string data type, the only option that I found is using CArrayString. This is the first time that I used this library and this is the code that I tried to used to store 1 symbol currency.

void OnStart() {
   CArrayString *MultiCurrencySymbol = new CArrayString;
   
   if(!MultiCurrencySymbol.Add((Symbol()))) {
      printf("Element addition error");
      delete MultiCurrencySymbol;
      return;
   }
//}
   Print("symbol: ", MultiCurrencySymbol);
   
   delete MultiCurrencySymbol;
}

using this code will return this print statement "symbol: 1048576" rather than EURUSD, and I tried to other currency it resulted in the same print statement. Can anyone tell me how to store the symbol using CArrayString function?


 
Luandre Ezra:

Hi,

I want to store symbol name and symbol volume into an array. I found that array function ArrayFill can't be used on string data type, the only option that I found is using CArrayString. This is the first time that I used this library and this is the code that I tried to used to store 1 symbol currency.

using this code will return this print statement "symbol: 1048576" rather than EURUSD, and I tried to other currency it resulted in the same print statement. Can anyone tell me how to store the symbol using CArrayString function?


You need to call the index like so : 

Print("symbol: ", MultiCurrencySymbol[0]);

You can also deploy your own custom structure and pack all you need in one place . Its more fun and a nice exercise . Heres an example : 

#property version   "1.00"
//first you can create a string handler
class myText{
  char array[];
       public:
string text;
       myText(void){reset();}
       myText(string new_text){reset();setup(new_text);}
      ~myText(void){reset();}
  void reset(){
       text=NULL;
       ArrayFree(array);
       }
  void save(int file_handle){
       //length
         FileWriteInteger(file_handle,ArraySize(array),SHORT_VALUE);
       //characters
         if(ArraySize(array)>0){
         FileWriteArray(file_handle,array,0,ArraySize(array));
         }
       }
  void load(int file_handle){
       reset();
       //length
         int total=(int)FileReadInteger(file_handle,SHORT_VALUE);
         if(total>0){
         FileReadArray(file_handle,array,0,total);
         }
       calc();
       }
       private:
  void setup(string new_text){
       StringToCharArray(new_text,array,0,StringLen(new_text),CP_UTF8);
       calc();
       }
  void calc(){text=CharArrayToString(array,0,ArraySize(array),CP_UTF8);}
      public:
 void operator=(string new_text){setup(new_text);}
 bool operator==(string other_text){if(text==other_text){return(true);}return(false);}
 bool operator!=(string other_text){if(text!=other_text){return(false);}return(true);}
 bool operator==(myText &other_text){if(text==other_text.text){return(true);}return(false);}
 bool operator!=(myText &other_text){if(text!=other_text.text){return(false);}return(true);}
 
};
class mySymbolInfo{
       public:
myText symbol;
double volume;
       mySymbolInfo(void){reset();}
       mySymbolInfo(string _symbol,double initial_volume=0.0){reset();setup(_symbol,initial_volume);}
      ~mySymbolInfo(void){reset();}
  void reset(){
       symbol.reset();
       volume=0.0;
       }
  void setup(string _symbol,double initial_volume=0.0){
       symbol=_symbol;
       volume=initial_volume;
       }
  void save(int file_handle){
       symbol.save(file_handle);
       FileWriteDouble(file_handle,volume);
       }
  void load(int file_handle){
       reset();
       symbol.load(file_handle);
       volume=(double)FileReadDouble(file_handle);
       }
};

struct mySymbols{
 mySymbolInfo symbols[];
              mySymbols(void){reset();}
             ~mySymbols(void){reset();}
         void reset(){
              ArrayFree(symbols);
              }
mySymbolInfo* new_symbol(string symbol,double initial_volume=0.0){
              int ns=ArraySize(symbols)+1;
              ArrayResize(symbols,ns,0);
              symbols[ns-1].setup(symbol,initial_volume);
              return(GetPointer(symbols[ns-1]));
              }         
          int find_symbol(string symbol){
              for(int i=0;i<ArraySize(symbols);i++){
                 if(symbols[i].symbol==symbol){
                   return(i);
                   }
                 }
              return(-1);
              }
         void sort_by_volume(){
              mySymbolInfo draft[];
              ArrayResize(draft,ArraySize(symbols),0);
              int sorter[][2];
              ArrayResize(sorter,ArraySize(symbols),0);
              for(int i=0;i<ArraySize(symbols);i++){
                 draft[i]=symbols[i];
                 sorter[i][0]=(int)(symbols[i].volume*1000.0);
                 sorter[i][1]=i;
                 }
              ArraySort(sorter);
              for(int i=ArraySize(symbols)-1;i>=0;i--){
                 int ix=sorter[i][1];
                 symbols[ArraySize(symbols)-1-i]=draft[ix];
                 }
              } 
         void save(string folder,string filename){
              string location=folder+"\\"+filename;
              if(StringLen(folder)<1){location=filename;}
              if(FileIsExist(location)){FileDelete(location);}
              int f=FileOpen(location,FILE_WRITE|FILE_BIN);
              if(f!=INVALID_HANDLE){
              //amount of symbols 
                FileWriteInteger(f,ArraySize(symbols),0);
                for(int i=0;i<ArraySize(symbols);i++){
                symbols[i].save(f);
                }
              FileClose(f);
              }
              }
         void save_csv(string folder,string filename){
              string location=folder+"\\"+filename;
              if(StringLen(folder)<1){location=filename;}
              if(FileIsExist(location)){FileDelete(location);}
              string tab="\t";
              string newline="\n";
              int f=FileOpen(location,FILE_WRITE|FILE_TXT);
              if(f!=INVALID_HANDLE){
                FileWriteString(f,"Symbols"+tab+"Volume"+tab+newline);
                for(int i=0;i<ArraySize(symbols);i++){
                string line=symbols[i].symbol.text+tab+DoubleToString(symbols[i].volume,2)+tab+newline;
                StringReplace(line,".",",");
                FileWriteString(f,line);
                }
                FileClose(f);
                }
              }
         bool load(string folder,string filename){
              reset();
              string location=folder+"\\"+filename;
              if(StringLen(folder)<1){location=filename;}
              if(FileIsExist(location)){
              int f=FileOpen(location,FILE_READ|FILE_BIN);
              if(f!=INVALID_HANDLE){
              int total=(int)FileReadInteger(f,INT_VALUE);
              if(total>0){
                ArrayResize(symbols,total,0);
                for(int i=0;i<total;i++){
                   symbols[i].load(f);
                   }
                }
              FileClose(f);
              return(true);
              }
              }
              return(false);
              }
mySymbolInfo* operator[](int ix){return(GetPointer(symbols[ix]));}
};

mySymbols Collection;

int OnInit()
  {
//---
  Collection.reset();
  //add to collection
    Collection.new_symbol("Potatoes",3.0);
    Collection.new_symbol("Onions",5.0);
    Collection.new_symbol("Carrots",3.5); 
    Collection.sort_by_volume();
    Collection.save_csv("","list.csv");
//---
   return(INIT_SUCCEEDED);
  }

 

Hi @Lorentzos Roussos,

Thanks for the your answer. Apparently I forgot to specify the index of the array it works now.

Thanks for your code it really helps a lot with what I working now. A quick question though since I'm new to structure, if I want to select one of the array (let's say onion from your example) and want to print it, how can I do that?

Print("collection: ", Collection[1][1]);

I tried to use code above but it returns error message "array required", is it not a two dimension array?

and If I used this code it returns number instead of "Potatoes" or "Onions"

Print("collection: ", Collection[1]);
 
Luandre Ezra #:

Hi @Lorentzos Roussos,

Thanks for the your answer. Apparently I forgot to specify the index of the array it works now.

Thanks for your code it really helps a lot with what I working now. A quick question though since I'm new to structure, if I want to select one of the array (let's say onion from your example) and want to print it, how can I do that?

I tried to use code above but it returns error message "array required", is it not a two dimension array?

and If I used this code it returns number instead of "Potatoes" or "Onions"

you use .onion and .potato 

You can have an array of things though , this is the most likely case . 

For instance collection[] has an operator that returns a pointer , well if the returned object has an operator too then you can [][] i think .

something like this

class item{
    public:
int type;
};
class list_of_items{
     public:
item items[];
item* add_item(){
      ArrayResize(items,ArraySize(items)+1,0);
      return(GetPointer(items[ArraySize(items)-1]));
      }
item* operator[](int i){return(GetPointer(items[i]));}
};
class list_of_lists{
              public:
list_of_items lists[];
list_of_items* add_list(){
               ArrayResize(lists,ArraySize(lists)+1,0);
               return(GetPointer(lists[ArraySize(lists)-1]));
               }
list_of_items* operator[](int i){return(GetPointer(lists[i]));}
};
int OnInit()
  {
//---
  list_of_lists COLLECTION;
  //create list [0] and item[0]
  COLLECTION.add_list().add_item().type=7;
  //create list [1] and item[0]
  COLLECTION.add_list().add_item().type=6; 
  // on list[0] add item[1]
    COLLECTION[0].add_item().type=8;
  // on list[1] add item[1]
    COLLECTION[1].add_item().type=9;
  //get type of [0,0] should be 7
    Print("Type(0,0) ="+IntegerToString(COLLECTION[0][0].type));
//---
   return(INIT_SUCCEEDED);
  }
 
Lorentzos Roussos #:

you use .onion and .potato 

You can have an array of things though , this is the most likely case . 

For instance collection[] has an operator that returns a pointer , well if the returned object has an operator too then you can [][] i think .

something like this

Thanks for your reply, will try your suggestion

Reason: