"Virtual" balance indicator?

 

Hi,

Is it possible to make an indicator which would calculate profit/loss of possible trades done by EA? I don't want it to show the real balance but something like the balance graph in strategy tester. Basically EA would buy and sell without actually placing any orders, calculate the profit/loss of the "virtual" trades and then plot them.

 
yes it's possible.
 
WHRoeder:
yes it's possible.
lol
 
WHRoeder:
yes it's possible.

Great! So how would you do it? Can you give me some code examples? Have you ever seen such indicator, maybe I can just simply download it from somewhere?

Cheers!

 
Synthacon:

Great! So how would you do it? Can you give me some code examples? Have you ever seen such indicator, maybe I can just simply download it from somewhere?

Cheers!

It could be possible by writing code to place results of virtual trades in an excel file and then drawing a graph in excel

or by using a buffer in a separate window indicator, but the buffer would have to be re-populated every time a trade closes.

I don't know of any examples. It would not be a quick job to write such code so I doubt that anyone can give you an example unless the idea really interests them.

 
GumRai:

It could be possible by writing code to place results of virtual trades in an excel file and then drawing a graph in excel

or by using a buffer in a separate window indicator, but the buffer would have to be re-populated every time a trade closes.

I don't know of any examples. It would not be a quick job to write such code so I doubt that anyone can give you an example unless the idea really interests them.

Here is what I have managed to glue together. I'm not really good programmer... The indicator saves the value(profit loss) into the CSV file once per new bar (so use M1 for testing). It then reads the data from the CSV file and plots it into separate window. For some reason line is plotted from right to left?! I have no clue on how to fix this. Can you help?

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property strict
#property indicator_separate_window

#property indicator_buffers 1
#property indicator_color1 Red

extern string file_name = "Indi2csv.csv";
extern string       font = "Arial";
extern int   fontSize = 10; // 10
extern color     clrText = clrGray; // default only, user color choice
extern int    draw_begin= 10;
int fileh =0;
int lasterror;

double old_p=1.10945;
double new_p;
double v_balance=0;

int save_counter;
string pref = "VB_";
int winID; // subwindow
int ThisBarTrade = 0;
string data[10][2];
double ExtMap[]; 
//+------------------------------------------------------------------+

int init()
  {
 
   IndicatorShortName("VirtualBalance");
   ObjectsDeleteAll(); 
  
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
   
  }

//+------------------------------------------------------------------+

int deinit()
  {
      if(fileh>0) 
      {
         FileClose(fileh);
      }
   
   return(0);
   
  }
  
//+------------------------------------------------------------------+
  
int start()
  {
  

if (Bars != ThisBarTrade ) {
   ThisBarTrade = Bars;  // ensure only one trade opportunity per bar
  
   fileh = FileOpen(file_name,FILE_CSV|FILE_READ|FILE_WRITE,',');
   if(fileh<1)
   {
      lasterror = GetLastError();
      Print("Error updating file: ",lasterror);
      return(false);
   }
   
   

   if(fileh>0)
   {
   
   FileSeek(fileh, 0, SEEK_END);
      Print("appending to file");
         ExportIndiData(); 
         FileWrite(fileh,v_balance);
         Print("Value:",v_balance);
   FileClose(fileh);
   Print("File closed!");
   }

   
   read_f();
   
   }
   return(0);
   
  }
//+------------------------------------------------------------------+

void ExportIndiData() 
{
     v_balance=0;

     new_p = Ask;    
     double old_b = (old_p * 10000);       
     double new_b = (new_p * 10000);
     v_balance =(new_b-old_b); //The balance
             
   
   if(winID < 1) winID = WindowFind("VirtualBalance");
        Labels(DoubleToStr(v_balance,2),22,22,1);          //Display current profit/loss
        Labels(DoubleToStr(save_counter,0),22,10,2);    //How many values have been saved (this session)
        save_counter ++;
                
}


void Labels(string datax, int y, int x,int d) //Create labels
{


   string obj = StringConcatenate(pref, d);
   if(!ObjectCreate(obj, OBJ_LABEL, winID ,0,0)) ObjectCreate(obj, OBJ_LABEL, winID ,0,0);
      if(!ObjectSet(obj, OBJPROP_XDISTANCE, x))      ObjectSet(obj, OBJPROP_XDISTANCE, x);
      if(!ObjectSet(obj, OBJPROP_YDISTANCE, y))      ObjectSet(obj, OBJPROP_YDISTANCE, y);
      if(!ObjectSet(obj, OBJPROP_CORNER, 0))         ObjectSet(obj, OBJPROP_CORNER, 0);
   if(!ObjectSetText(obj, datax, fontSize, font, clrText))  ObjectSetText(obj, datax, fontSize, font, clrText);



   return;
} 


//Read from file and plot
void read_f()
  {

   string separator=",";
   int m_handle=-1, count, line_count = 1;
   string m_filename=file_name;
   
   m_handle=FileOpen(m_filename,FILE_CSV|FILE_ANSI|FILE_READ,separator,CP_ACP);
   if(m_handle<0)
     {
     Print("I can't open the file.");
     }
     else
     {
     Print("File successfully open.");
     while (FileIsEnding(m_handle) == false)
       {
         
       read_n_draw(m_handle);  // read the result and draw a chart
       
       count ++;
       }
       
   
      FileClose(m_handle);
      Print("read_n_draw done.File closed.");
     }
   
  }
 
 void read_n_draw(int handle_read)
{
WindowRedraw();
  PlaySound("wait.wav");
   int i=0;
   while ( !FileIsEnding(handle_read)) 
   {
    
      ExtMap[i] = FileReadNumber(handle_read);
      
      i++;     
   }
   ExtMap[i-1] = EMPTY_VALUE;
}


Reason: