Download MetaTrader 5
To add comments, please log in or register
Have you found a good programmer? Make a targeted order!
trading009
12
trading009 2016.06.10 08:14 

Hey Readers !

I have initialized two variables  initial_balance    // Account balance before trade

                                             final_balance     // Account balance after trades are closed

my idea is that if was loss last day than my lotsize for today trade must be increase.But i am not understanding that where should i place those two variables to get account balance and check that.

I searched online for it but i was not able to find any good solution .

trading009
12
trading009 2016.06.10 09:49  

Can any one guide me to the right path

Fernando Carreiro
1502
Fernando Carreiro 2016.06.10 10:13  
trading009:

my idea is that if was loss last day than my lotsize for today trade must be increase.But i am not understanding that where should i place those two variables to get account balance and check that. Can any one guide me to the right path

You don' have to store those variables because each Order already provides information about its own Profit or Loss. You also have the account details at all times and can calculate your risk based on current balance, equity and free margin.

Just read the documentation in detail or follow the online MT4 tutorial/book. Have a look at functions such as Account Information and Trade Functions, specially OrderProfit().

Also, do a search on the forum. Many users have provided explanations and links to code on how to calculate risk on many occasions. Here is a recent quote from one of WHRoeder  which is also relevant (all credit goes to him):

WHRoeder:
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  • Do NOT use TickValue by itself - DeltaPerlot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
trading009
12
trading009 2016.06.10 12:03  
FMIC:

You don' have to store those variables because each Order already provides information about its own Profit or Loss. You also have the account details at all times and can calculate your risk based on current balance, equity and free margin.

Just read the documentation in detail or follow the online MT4 tutorial/book. Have a look at functions such as Account Information and Trade Functions, specially OrderProfit().

Also, doa search on the forum. Many users have provided explanations and links to code on how tocalculate risk on many occasions. Here is a recent quote from one of WHRoeder  which is also relevant (all credit goes to him):

WHRoeder:
  • You place the stop where it needs to be - where the reason for thetrade is no longer valid. E.g. trading a support bounce the stop goesbelow the support.
  • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  • Do NOT use TickValue by itself - DeltaPerlot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out

Thanks for guiding me !!

But still i am confused " the final balance problem is solved " but how will i check the highest balance of account .

Should i have to loop through the recent history of all closed trades.

If yes than how should i define its code.

Fernando Carreiro
1502
Fernando Carreiro 2016.06.10 12:55  
trading009:

Thanks for guiding me !!

But still i am confused " the final balance problem is solved " but how will i check the highest balance of account .

Should i have to loop through the recent history of all closed trades.

If yes than how should i define its code.

You only need to loop through the History of Closed and Open Orders during Initialisation and/or Recovery, but you don't need to go to far back (just enough for you to synchronise your trading states). After that, you can just keep a running tab of the orders as they progress.

As for how to code it, you will have to reason it out. That is what coding is about - defining the steps you require to do something. Once you have outlined your coding attempt for it, then post it here for advice if you are still having trouble.

Fernando Carreiro
1502
Fernando Carreiro 2016.06.10 13:09  

One more thing! You should not use the balance or the equity to keep track of profit (or loss), because that would make your EA incompatible with other running EAs or even itself if it is running on another chart as well.

You should keep track of profit based only on the orders themselves as identified by its unique magic number and that are specific to the current symbol.

whroeder1
13627
whroeder1 2016.06.11 13:04  

trading009:

But still i am confused " the final balance problem is solved " but how will i check the highest balance of account .

Should i have to loop through the recent history of all closed trades.

If yes than how should i define its code.

Yes and you can't assume that history is ordered. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum and Array or not to array (Mike Tanton) - MQL4 forum - Page 2
/
To add comments, please log in or register