MQL4 - automated forex trading   /  

Forum

Percentage Based StopLoss

Back to topics list To post a new topic, please log in or register

avatar
115
kiwi06 2010.09.19 01:32 

Hi

I have this bit of code

extern double Ratio=1.10;

double StopLoss = (AccountEquity()/AccountBalance()>Ratio) *Point;

I am trying to have a 10% StopLoss based on 10% of the equity.

Am I on the right track or way off based to get this right.

Any help would be great.

Genetic Algorithms vs. Simple Search in the MetaTrader 4 Optimizer

Genetic Algorithms vs. Simple Search in the MetaTrader 4 Optimizer

The article compares the time and results of Expert Advisors' optimization using genetic algorithms and those obtained by simple search.


avatar
1971
ubzen 2010.09.19 02:54 
There are allot of examples Here.

avatar
115
kiwi06 2010.09.20 20:01 

Not quite what I am looking for I am using a fixed lot.

Balance 100
10% SL 10
TickValue 9.5
Lot Size 0.01
TickSize 0.095

SL in pips 105.2632

I think the code would go like this

double Cash= (AccountBalance()*0.1 ) / MarketInfo(Symbol(),MODE_LOTSIZE)/MarketInfo(Symbol(), MODE_TICKVALUE)/MarketInfo(Symbol(),MODE_TICKSIZE);

Or something like that



avatar
1675
qjol 2010.09.20 20:18 

you should use

NormalizeDouble(...,...)

avatar
159
hasayama 2010.09.20 20:26 

Let us think a little:

1. We want to risk 10% of our balance, so: risk = AccountBalance()*0.1;

2. We want to risk it with constant lot size: Lot = 0.01.

3. 1 Lot costs: lot_cost = MarketInfo( Symbol(), MODE_TICKVALUE );

4. So, our lot costs: our_lot_cost = Lot*lot_cost = 0.01*lot_cost;

5. Basicaly, every pip that goes in the opposite direction we lose our_lot_cost amout.

6. So, 10% of balance will be lost in SL = risk/our_lot_cost pips.

Or:

extern int Risk_Percentage = 10;
extern double Lot = 0.01;

int GetSLinPips()
{
   double risk = AccountBalance()*Risk_Percentage/100.0; //get how much it is ok to lose
   int SL = NormalizeDouble( risk/(Lot*MarketInfo( Symbol(), MODE_TICKVALUE )), 0 ); 

   return(SL);
}

Hope I'm right=)


avatar
115
kiwi06 2010.09.20 21:33 
Well I will give it ago

avatar
716
1005phillip 2010.09.21 05:43 
hasayama:

3. 1 Lot costs: lot_cost = MarketInfo( Symbol(), MODE_TICKVALUE );


That is true for only those few currency pairs for which the counter currency is also the account's denomination.

EURUSD for a USD based account for example.

Would be wrong for USDJPY with a USD based account. Would be wrong for all crosses.

avatar
159
hasayama 2010.09.21 11:30 
1005phillip:

That is true for only those few currency pairs for which the counter currency is also the account's denomination.

EURUSD for a USD based account for example.

Would be wrong for USDJPY with a USD based account. Would be wrong for all crosses.
Ok, could you please recommend how to turn it into universal solution?

avatar
716
1005phillip 2010.09.21 15:57 
hasayama:
Ok, could you please recommend how to turn it into universal solution?


Actually I've posted the code for the universal solution it numerous times (ubzen even linked to one of those occasions above in this thread).

http://forum.mql4.com/34216/page2#356736

http://forum.mql4.com/32653/page5#331819

http://forum.mql4.com/32653/page6#332640

(the last link above is a thread that specifically discusses how to compute the price per pip)

Universally there are 5 types of currency pairs. The codes I've uploaded will analyze Symbol() and AccountCurrency() to determine which type of currency pair Symbol() represents. The currency type is referred to as its SymbolType.

//+--------------------
//|   SymbolType()
//|====================
//|   int SymbolType(bool verbose=false)
//|
//|   Analyzes Symbol() to determine the SymbolType for use with Profit/Loss and lotsize calcs.
//|   The function returns an integer value which is the SymbolType.
//|   An integer value of 6 for SymbolType is returned in the event of an error
//|
//|   Parameters:
//|
//|      verbose     -  Determines whether Print messages are committed to the experts log. By default, print messages are disabled.
//|
//|   Examples:
//|
//|      SymbolType 1:  Symbol() = USDJPY
//|
//|                     Base = USD                                    
//|                     Counter = JPY                                 
//|
//|      SymbolType 2:  Symbol() = EURUSD                             
//|
//|                     Base = EUR                                    
//|                     Counter = USD                                 
//|
//|      SymbolType 3:  Symbol() = CHFJPY                             
//|
//|                     Base = CHF                                    
//|                     Counter = JPY                                 
//|
//|                     USD is base to the base currency pair - USDCHF
//|
//|                     USD is base to the counter currency pair - USDJPY
//|
//|      SymbolType 4:  Symbol() = AUDCAD                             
//|
//|                     Base = AUD                                    
//|                     Counter = CAD                                 
//|
//|                     USD is counter to the base currency pair - AUDUSD
//|
//|                     USD is base to the counter currency pair - USDCAD
//|
//|      SymbolType 5:  Symbol() = EURGBP                             
//|
//|                     Base = EUR                                    
//|                     Counter = GBP                                 
//|
//|                     USD is counter to the base currency pair - EURUSD
//|
//|                     USD is counter to the counter currency pair - GBPUSD
//|
//|      SymbolType 6:  Error occurred, SymbolType could not be identified 
//|
//+--------------------

As you can see there are two currency types that involve the account's denomination - those for which the account's currency represents the base in the currency pair (USDJPY, USDCHF) and those for which is represents the counter-currency (EURUSD, GBPUSD).

And then there are three types of cross-currency pairs.

Once your EA knows the symboltype for the specific currency pair in question then it can compute the proper valuation changes that will come from the currency pair declining in value from one price point to another price point.

(detailed here http://forum.mql4.com/34216/page2#356736)

Back to topics list  

To add comments, please log in or register