below 1% margin

 

is there a Library or some way to set my RISK % PRE TRADE below 1% such as 0.10% ?

Im trying it in Three Neural Networks

Thank you

 
kaneneal2023: is there a Library or some way to set my RISK % PRE TRADE below 1% such as 0.10% ? Im trying it in Three Neural Networks Thank you

It has to be coded according to the requirement, but there are some support functions and class methods.

Risk depends on the size of the stop-loss, which then has to be adjusted depending on the % margin you want to apply.

Search the forum, the CodeBase and Articles, as there are many examples and explanations.

Here are a few of some of my related posts:

Forum on trading, automated trading systems and testing trading strategies

Confused about tickvalue

Fernando Carreiro, 2023.04.26 13:27

This site has something called "search" ... https://www.mql5.com/en/search#!keyword=OrderCalcProfit%20calculate%20volume&module=mql5_module_forum

To calculate the correct volume for your trade, you should ...

  1. Use the OrderCalcProfit to first obtain the correct volume based on your stop-loss size and percentage risk.
  2. Adjust the volume, based on the contract specifications
  3. Use the OrderCalcMargin to verify that the volume is within the margin limits and requirements, and if not, then either reduce the volume or abort the trade.
  4. And finally, before placing the order, use OrderCheck to make sure everything is in valid.

Forum on trading, automated trading systems and testing trading strategies

P&L calculation formula for different traded assets

Fernando Carreiro, 2023.05.04 14:53

I suggest reading up on the following two MQL5 trade functions ...

OrderCalcMargin

Calculates the margin required for the specified order type, in the deposit currency

OrderCalcProfit

Calculates the profit based on the parameters passed, in the deposit currency

They calculate the margin and the profit/loss respectively, adjusting automatically for the asset type, making it unnecessary to calculate it manually using the methods you have described.

They do however, not include Commission or Swaps. Those are only provided after the fact and are accessible via other functionality — Documentation on MQL5: Trade Functions

Forum on trading, automated trading systems and testing trading strategies

How to calculate take profit from currency

Fernando Carreiro, 2022.09.05 17:00

These are all the same equation written in different ways ...

[Volume]      = [Money Value] * [Tick Size] / ( [Tick Value] * [Stop Size] )
[Stop Size]   = [Money Value] * [Tick Size] / ( [Tick Value] * [Volume]    )
[Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]

[Volume] in lots
[Stop Size] in quote price change
[Money Value] in account currency

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};

Forum on trading, automated trading systems and testing trading strategies

Market Registration of EA Unable to Validate

Fernando Carreiro, 2022.08.30 14:20

For your screenshot with an Error 131 and it also has a link on "How to fix it". So follow up on it and fix your EA accordingly.

In regards to volume, you have to check the contract specification of the symbol and limit your volume to the minimum, maximum and step that is allowed for the symbol.

// Variables for symbol volume conditions
   double
      dbLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
      dbLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
      dbLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
       
// Adjust volume for allowable conditions
   dbLots = fmin(  dbLotsMaximum,                           // Prevent too greater volume
            fmax(  dbLotsMinimum,                           // Prevent too smaller volume
            round( dbLots / dbLotsStep ) * dbLotsStep ) );  // Align to step value
Documentation on MQL5: Trade Functions
Documentation on MQL5: Trade Functions
  • www.mql5.com
Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
This topic has been moved to the section: Expert Advisors and Automated Trading