Percentage Based StopLoss

 

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.

 
There are allot of examples Here.
 

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

 

you should use

NormalizeDouble(...,...)
 

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=)

 
Well I will give it ago
 
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.
 
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?
 
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).

https://www.mql5.com/en/forum/127798/page2#356736

https://www.mql5.com/en/forum/126450/page5#331819

https://www.mql5.com/en/forum/126450/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 https://www.mql5.com/en/forum/127798/page2#356736)

Reason: