mql - How to export to a CSV file from an Existent MT4 Indicator code -
i export results csv file. iexposure indicator (iexposure.mq4) mt4. there way know how export values csv
this code tried use export csv.
int h1; h1 = fileopen("data3.csv", file_csv | file_write | file_read, ','); fileseek( extsymbolssummaries[i][deals]=0, extsymbolssummaries[i], [buy_price]=0,extsymbolssummaries[i][sell_lots]=0 ,extsymbolssummaries[i] [sell_price]=0,extsymbolssummaries[i][net_lots]=0, extsymbolssummaries[i][profit]=0, seek_end); filewrite(h1, symbols[i]=symbolname, extsymbolssummaries[i][deals]=0, extsymbolssummaries[i] [buy_price]=0,extsymbolssummaries[i][sell_lots]=0 ,extsymbolssummaries[i] [sell_price]=0,extsymbolssummaries[i][net_lots]=0, extsymbolssummaries[i][profit]=0 ); fileclose(h1)
this original code :
//+------------------------------------------------------------------+ //| iexposure.mq4 | //| copyright © 2007, metaquotes software corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "copyright © 2007, metaquotes software corp." #property link "http://www.metaquotes.net" #property indicator_separate_window #property indicator_buffers 1 #property indicator_minimum 0.0 #property indicator_maximum 0.1 #define symbols_max 1024 #define deals 0 #define buy_lots 1 #define buy_price 2 #define sell_lots 3 #define sell_price 4 #define net_lots 5 #define profit 6 extern color extcolor=lightseagreen; string extname="exposure"; string extsymbols[symbols_max]; int extsymbolstotal=0; double extsymbolssummaries[symbols_max][7]; int extlines=-1; string extcols[8]={"symbol", "deals", "buy lots", "buy price", "sell lots", "sell price", "net lots", "profit"}; int extshifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 }; int extvertshift=14; double extmapbuffer[]; //+------------------------------------------------------------------+ //| custom indicator initialization function | //+------------------------------------------------------------------+ void init() { indicatorshortname(extname); setindexbuffer(0,extmapbuffer); setindexstyle(0,draw_none); indicatordigits(0); setindexemptyvalue(0,0.0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void deinit() { int windex=windowfind(extname); if(windex>0) objectsdeleteall(windex); } //+------------------------------------------------------------------+ //| custom indicator iteration function | //+------------------------------------------------------------------+ void start() { string name; int i,col,line,windex=windowfind(extname); //---- if(windex<0) return; //---- header line if(extlines<0) { for(col=0; col<8; col++) { name="head_"+col; if(objectcreate(name,obj_label,windex,0,0)) { objectset(name,objprop_xdistance,extshifts[col]); objectset(name,objprop_ydistance,extvertshift); objectsettext(name,extcols[col],9,"arial",extcolor); } } extlines=0; } //---- arrayinitialize(extsymbolssummaries,0.0); int total=analyze(); if(total>0) { line=0; for(i=0; i<extsymbolstotal; i++) { if(extsymbolssummaries[i][deals]<=0) continue; line++; //---- add line if(line>extlines) { int y_dist=extvertshift*(line+1)+1; for(col=0; col<8; col++) { name="line_"+line+"_"+col; if(objectcreate(name,obj_label,windex,0,0)) { objectset(name,objprop_xdistance,extshifts[col]); objectset(name,objprop_ydistance,y_dist); } } extlines++; } //---- set line int digits=marketinfo(extsymbols[i],mode_digits); double buy_lots=extsymbolssummaries[i][buy_lots]; double sell_lots=extsymbolssummaries[i][sell_lots]; double buy_price=0.0; double sell_price=0.0; if(buy_lots!=0) buy_price=extsymbolssummaries[i][buy_price]/buy_lots; if(sell_lots!=0) sell_price=extsymbolssummaries[i][sell_price]/sell_lots; name="line_"+line+"_0"; objectsettext(name,extsymbols[i],9,"arial",extcolor); name="line_"+line+"_1"; objectsettext(name,doubletostr(extsymbolssummaries[i][deals],0),9,"arial",extcolor); name="line_"+line+"_2"; objectsettext(name,doubletostr(buy_lots,2),9,"arial",extcolor); name="line_"+line+"_3"; objectsettext(name,doubletostr(buy_price,digits),9,"arial",extcolor); name="line_"+line+"_4"; objectsettext(name,doubletostr(sell_lots,2),9,"arial",extcolor); name="line_"+line+"_5"; objectsettext(name,doubletostr(sell_price,digits),9,"arial",extcolor); name="line_"+line+"_6"; objectsettext(name,doubletostr(buy_lots-sell_lots,2),9,"arial",extcolor); name="line_"+line+"_7"; objectsettext(name,doubletostr(extsymbolssummaries[i][profit],2),9,"arial",extcolor); } } //---- remove lines if(total<extlines) { for(line=extlines; line>total; line--) { name="line_"+line+"_0 "; objectsettext(name,""); name="line_"+line+"_1"; objectsettext(name,""); name="line_"+line+"_2"; objectsettext(name,""); name="line_"+line+"_3"; objectsettext(name,""); name="line_"+line+"_4"; objectsettext(name,""); name="line_"+line+"_5"; objectsettext(name,""); name="line_"+line+"_6"; objectsettext(name,""," "); name="line_"+line+"_7"; objectsettext(name,""," "); } } //---- avoid minimum==maximum extmapbuffer[bars-1]=-1; //---- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int analyze() { double profit; int i,index,type,total=orderstotal(); //---- for(i=0; i<total; i++) { if(!orderselect(i,select_by_pos)) continue; type=ordertype(); if(type!=op_buy && type!=op_sell) continue; index=symbolsindex(ordersymbol()); if(index<0 || index>=symbols_max) continue; //---- extsymbolssummaries[index][deals]++; profit=orderprofit()+ordercommission()+orderswap(); extsymbolssummaries[index][profit]+=profit; if(type==op_buy) { extsymbolssummaries[index][buy_lots]+=orderlots(); extsymbolssummaries[index][buy_price]+=orderopenprice()*orderlots(); } else { extsymbolssummaries[index][sell_lots]+=orderlots(); extsymbolssummaries[index][sell_price]+=orderopenprice()*orderlots(); } } //---- total=0; for(i=0; i<extsymbolstotal; i++) { if(extsymbolssummaries[i][deals]>0) total++; } //---- return(total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int symbolsindex(string symbolname) { bool found=false; //---- for(int i=0; i<extsymbolstotal; i++) { if(symbolname==extsymbols[i]) { found=true; break; } } //---- if(found) return(i); if(extsymbolstotal>=symbols_max) return(-1); //---- i=extsymbolstotal; extsymbolstotal++; extsymbols[i]=symbolname; extsymbolssummaries[i][deals]=0; extsymbolssummaries[i][buy_lots]=0; extsymbolssummaries[i][buy_price]=0; extsymbolssummaries[i][sell_lots]=0; extsymbolssummaries[i][sell_price]=0; extsymbolssummaries[i][net_lots]=0; extsymbolssummaries[i][profit]=0; //---- return(i); } //+------------------------------------------------------------------+
end of code
csv output example:
for example, describing way achieve following:
- add input strings folder , csv file names.
- create button generating csv.
- listen events can detect when button has been clicked.
- parse objects list , write name , description objects csv file.
note: if unable complete step 2, skip step 3, , @ code inside onchartevent()
, customize when want write csv file. however, aware lot of file writing if writing file every time oncalculate()
event occurs.
since iexposure.mq4
comes available default (at least version: 4.0 build 765), let's start creating duplicate of original. named example iexposuretest.
step 1: add input strings our folder , csv file.
this 1 of our easiest steps, add input strings our folder , csv file. added these next other input variable.
//step 1 input string inputfilename="iexposure.csv"; // file name input string inputdirectoryname="data"; // folder name
step 2: create csv button.
include header chartobjectpanel.mqh
. chose place include line directly above input variables.
//part of step 2 #include <chartobjects\chartobjectpanel.mqh>
this gives access creating cchartobjectbutton
type, , need access button globally
example, add next line of code before oninit()
.
//part of step 2 cchartobjectbutton csvbutton;
we have button type of cchartobjectbutton
, it's not instantiated yet. let's function call oninit()
.
inside oninit()
, add below:
//part of step 2 createcsvbutton(0);
now need actual function, @ end of indicator code let's add function next.
//part of step 2 bool createcsvbutton(const long chart_id=0) { //--- reset error value resetlasterror(); //--- set property values if(!csvbutton.create(0,"csvbutton",0,10,25,75,15)) { //--- display error message in experts journal print(__function__+", error code = ",getlasterror()); return(false); } csvbutton.description("csv"); csvbutton.fontsize(10); csvbutton.corner(corner_left_lower); csvbutton.anchor(anchor_center); csvbutton.state(false); return true; }
at point, when compile there should csv button available on window. created can generate csv click. should similar image below.
if have csv button, finished step 2.
step 3: listen events , capture csv button clicked.
we able capture event need using built in onchartevent()
.
in our onchartevent()
, checking if event chartevent_object_click
event, if sparam
equals our csvbutton
, button state true, our file work.
also, notice call function writecsvobjects
doesn't exist yet pass file_handle
to. create in step 4.
//step 3 void onchartevent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id==chartevent_object_click) { if (sparam=="csvbutton") { if(csvbutton.state() == true) { resetlasterror(); int file_handle=fileopen(inputdirectoryname+"//"+inputfilename,file_read|file_write|file_csv); if(file_handle!=invalid_handle) { printformat("%s file available writing",inputfilename); printformat("file path: %s\\files\\",terminalinfostring(terminal_data_path)); //call step 4 writecsvobjects(file_handle); //--- close file fileclose(file_handle); printformat("data written, %s file closed",inputfilename); } else { printformat("failed open %s file, error code = %d",inputfilename,getlasterror()); } } chartredraw(); } } }
step 4: parse objects list , write name , description csv.
almost there! final part solve question. add function end of our code example.
//step 4 void writecsvobjects(int file_hand) { int obj_total=objectstotal(); string name; string desc; for(int i=obj_total;i>=0;i--) { name = objectname(i); desc = objectdescription(name); if(name == "") { name = "empty name"; } if(desc == "") { desc = "empty desc"; } filewrite(file_hand, name + "," + desc); } }
in above function, notice looping objects , writing out name , description each object. order of output , objects may not goal is, modify accordingly.
at point, when compile , click csv button, should generate values csv file stored @ files//data//iexposure.csv
.
Comments
Post a Comment