Could someone help on my code? PLS. I am newbie. - page 2

 
William Roeder #:
“Can use” doesn't mean you should. It's your money, control your risk.

Why do you discuss edge cases? We are exclusively talking about lot size. 

The lot size determines your risk of course, but the risk taken is calculated after the chosen lot size, we are talking about calculating a lot size, and like I said.... a table is better than a calculator for this use case.


Lot size = subjective 

Risk percentage = objective after a percentage is chosen

SL = can be objective if it is trailing based on risk percentage

TP = can be objective if based on fixed pips amount


Now, you tell me how you make a calculator for a subjective number. Like I said, it's not going to make sense.

 
Fernando Carreiro #:

You are incorrect or misinformed. Please read William's post again in detail and apply it.

I find it humorous that you think I am misinformed, when William went way offtopic here.

Can we please keep it ontopic? The post is about making a Lot Size Calculator.


My argument is that a money management table is more fruitful than a lot size calculator, and I can post a table as an example if it helps people to understand.

 
phade #:He misinterpreted what I was saying. The discussion was not about risk, it was only about lot size.

No, it was not misinterpreted. The volume (lots) is your exposure, hence your risk.

 
Fernando Carreiro #:

No it was not misinterpreted. The volume (lots) is your exposure, hence your risk.

PnL is never fixed, profit and loss is constantly dynamic regardless of what lot size you use.

Lot size comes first, the rest (risk and such) must be calculated after lot size is chosen. Lot size is pointless to be "calculated".

 
phade #: PnL is never fixed, profit and loss is constantly dynamic regardless of what lot size you use. Lot size comes first, the rest (risk and such) must be calculated after lot size is chosen. Lot size is pointless to be "calculated".

Incorrect! It is never subjective. It is an objective value and it is only one element of your risk that needs to be evaluated in conjunction with other variables. not separately.

 
poogun.aph: I try to writing indicator which try to help me calculate lot size for my position on chart. this is my code. i compile it and error. idk y. Thank for you help.

try this script for now , also market info is not available anymore in mt5 i think .

you still need to check for whether or not your account will sustain opening the trade thought

#property script_show_inputs
#include <PlugAndPlay\trading\symbolTradingCosts.mqh>
input ENUM_POSITION_TYPE tradeType=POSITION_TYPE_BUY;// Position type
input double RiskPercentage = 1.0;  // Risk percentage per trade
input int StopLoss = 500;   // Stop loss in points (forget pips)
input double EntryPrice = 1.2000;   // Example entry price
input double marginSelfLimit=10.0;//Margin self limit % (when i have time i'll add margin checks in the library)

void OnStart()
  {
  ENUM_ORDER_TYPE o_type=ORDER_TYPE_BUY;
  if(tradeType==POSITION_TYPE_SELL){o_type=ORDER_TYPE_SELL;}
  //first calculate the tick value for one lot
    double tvol=0.0;
    if(get_tick_value_for_one_lot(_Symbol,o_type,tvol)){
    //then calculate the risk amount 
      ResetLastError();
      double risk_amount=(AccountInfoDouble(ACCOUNT_BALANCE)/100.0)*RiskPercentage;
      if(_LastError==0){
      /*
      now you know the cost per tick for 1 lot
          you know the amount you want to risk
          and you know what the size is of your stop loss
      so , cost of your stop loss for one lot would be 
      */
      double cost_for_one_lot=((double)StopLoss)*tvol;
        if(cost_for_one_lot>0.0){
      //and then dividing the rix amount by the cost for one lot you get the lot
        double the_lot=risk_amount/cost_for_one_lot;
        /*
        so now :
        a.is the lot valid ? 
        b.do you have enough margin for your lot ? 
        */
        //get the basics for the symbol
          double minlot=0.0,maxlot=0.0,steplot=0.0,limitlot=0.0;
          int lot_digits=0;
          if(get_lot_limits(_Symbol,minlot,maxlot,limitlot,steplot)){
          the_lot=checkLot(the_lot,minlot,maxlot,steplot,lot_digits);
          double margin_lot=0.0;
          if(get_margin_for_one_lot(_Symbol,o_type,margin_lot)){
            margin_lot*=the_lot;
            ResetLastError();
            double free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
            if(_LastError==0){
            double marginusedPCT=(margin_lot/free_margin)*100.0;
            if(marginusedPCT<=marginSelfLimit){
              Alert("Lot to use : "+DoubleToString(the_lot,lot_digits));
              }else{
              Alert("Order rejected for margin");
              }
            }else{
            Alert("Cannot get free margin");
            }
            }else{
            Alert("Cannot get margin required for one lot");
            }
          }else{
          Alert("Cannot get lot limits");
          }
        }else{
        Alert("Lol");
        }
      }else{
      Alert("Cannot get account balance");
      }
    }else{
    Alert("Cannot get tick value for one lot"); 
    }
  }
Files:
 

I recommend to OP to make an excel table with a mathematical formula to work out everything for the given currency pair, and start the table from the minimum volume that you are comfortable to start trading with.

EURUSD pip size = 0.0001

Pip Value = Pip Size * Contract Size

common contract sizes for EURUSD:

  1. Standard Lot: 100,000 EUR (base currency)
  2. Mini Lot: 10,000 EUR (base currency)
  3. Micro Lot: 1,000 EUR (base currency)

A one-pip movement will be priced at following amounts for each lot size when trading EURUSD:

  • A standard lot (1 lot) = $10
  • A mini lot (0.1 lots) = $1
  • A micro lot (0.01 lots) = $0.10
  • A nano lot (0.001 lots) = $0.01

Profit = (Value (in dollars) of lot size * number of pips)

I made a table which started at lot size 0.01

 
phade #: He misinterpreted what I was saying. The discussion was not about risk, it was only about lot size.

Wrong. Lot size and stop loss distance determines your risk. Reread #6.2.  Each is proportional to each other. You can not discuss one without discussing the other.

 
William Roeder #:

Wrong. Lot size and stop loss distance determines your risk. Reread #6.2.  Each is proportional to each other. You can not discuss one without discussing the other.

Stop loss can be fully based on the risk percentage.
A stop loss is discretionary as is a lot size.
A value-per-lot calculator makes more sense to me if anything at all. Otherwise a money management table is perfect, and makes it easier than having to plug in values every time