How to automatically calculate risk level and order lots?

 

Hello, experts,

I am a brand new EA learner. I am thinking how to write a small EA to automatically calculate risk level and order lots. Let's say I have opened 10 charts. At some certain time, for example 0:00 Monday, while some entry signals=true, before opening orders, I want the EA check and count how many charts getting true entry signal, and divide a total risk level (let’s say 30% of balance), get a average risk level for each chart which has got true signal to open orders (if 3 of 10 charts get true signal, each chart of these 3 should use 10% risk level to open a position, then the lots can be calculated). I guess this can maximize the capital use efficiency.

I am trying to use fileread and filewrite functions to write this EA, but it does not work. Do you have any idea on this?

Many Thanks

DOND_i=magicnumber;              //00, 01, 02, ...29 etc.
   if (Open_Signal==true)         Deal_Or_No_Deal=1;
   else                           Deal_Or_No_Deal=0;
 
   for (i=0; i<30; i++)           DOND[i]=0;
 
   DOND[DOND_i]=Deal_Or_No_Deal;
 
     handle=FileOpen("open_data.dat",FILE_BIN|FILE_WRITE);
       if(handle>0)
         { FileWriteArray(handle, DOND, DOND_i, 1);
           FileClose(handle);
         }
 
 
     handle=FileOpen("open_data.dat",FILE_BIN|FILE_READ);
       if(handle>0)
         { DOND_Number=0;
           for (i=0; i<30; i++)
               { FileReadArray(handle, New_DOND, i, 1);
                 DOND_Number+=New_DOND[Counter];
               }
           FileClose(handle);
         }       
 
   Risk_Level=30/DOND_Number;		// let's say total risk level is 30% of balance
   Lots=(AccountBalance()*(Risk_Level/100)/(CurrencyLotSize/100))/(Margin_Per_Lot/1000);
 

"I am trying to use fileread and filewrite functions to write this EA, but it does not work"

That's not very descriptive, so I'll guess.

You don't get much of a file written?

You are not using FILE_READ in combination with FILE_WRITE, so the file is not appended.

Use FileSeek() to go to the end of the file before writing to it.

Did I guess wrong?

 

FileReadArray, FileWriteArray are designed to read/write whole array at once.

Take them outside the loop:

FileWriteArray(handle, DOND,0, 30);

FileReadArray(handle, New_DOND, 0, 30);

for (i=0; i<30; i++)
DOND_Number+=New_DOND[i]; // i don''t know where that counter comes from

 

OK, my fault. Let me explain it more clearly.

1, I opened 10 charts, such as: EUR/USD, GBP/USD , USD/JPY…, AUD/JPY etc., their magicnumber as 00, 01, 02, … 29

2, I enabled this auto-count-risk EA on all these 10 charts. While buy or sell signal appears, a 1 will be assigned to an element of an array. For example, chart 1 EUR/USD will open a buy order, chart 2 GBP/USD no deal , chart 3 USD/JPY will open a sell order…, chart 30 AUD/JPY no deal. Then the EA on chart 1 will write a 1 to an array DOND[0], chart 2 will write an o to DOND[1], etc, chart 30 will write a 1 to DOND[29]. Then one by one, all this elements form a file named as open_data.dat on disk.

3, Open and read this file, by a loop, EA will count the sum of 1 from this file. Let’s say we get 12 buy or sell signals from 12 individual charts, so we will get 12*1=12.

4, OK, I planned put 30% of balance for total risk level to trade, divided by this 12, each chart will use 30/12=2.5% capital to open deals.

5, While next buy or sell chance come, the EA will count the risk level automatically again, no need to adjust it by hand, only set the total risk level, then let it tun. If this time we get 15 signals, htis EA will calculate and arrange 2% risk level to each chart which signal appears.

So, that is what I want, but fail to do. There must be something wrong in my code.

Thanks for your help.

 

Each chart increments the counter of dCharts and decements it when logging out.

double dCharts;

init() {
if(!GlobalVariableCheck("Charts")) GlobalVariableSet("Charts", 1);
else {
dCharts=GlobalVariableGet("Charts");
GlobalVariableSet("Charts",dCharts+1);
}
return(0);
}

deinit() {
dCharts=GlobalVariableGet("Charts");
GlobalVariableSet("Charts",dCharts-1);
}

start() {
dCharts=GlobalVariableGet("Charts");
Risk_Level=30/dCharts; // let's say total risk level is 30% of balance
Lots=(AccountBalance()*(Risk_Level/100)/(CurrencyLotSize/100))/(Margin_Per_Lot/1000);
}

 
sxTed, Thanks a lot! Thank you so much! You saved me. Global Variable will work in deed. Many thanks again!
Reason: