How To Use Lot Size Base On One Click Trading?

 

I am not well verse in mql, recently pickup after got lay off so I tot why not.

This is a code I mix and match

//+------------------------------------------------------------------+

//|                                                   BuyStop105.mq4 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

double   newopen;

double _50Pips;

double   buffer_entry;

double   buffer_exit;



// We define the basic data.

double StopLoss=50;     // Stop-Loss in pips 3 digits because of decimals

double TakeProfit=130;   // Take-Profit in pips. 3 digits because decimals.

double LotSize=0.1;    // Lot size in lots

double Slippage=5;      // Slippage in pips

double Command=OP_BUYSTOP;  // Type of order, BUY

int MagicNumber=16384;



double CalculateNormalizedDigits()

{

   if(Digits<=3){

      return(0.01);

   }

   else if(Digits>=4){

      return(0.0001);

   }

   else return(0);

}

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---



// for currency pairs that has 3 decimals place

   if (Digits ==2)

   {

   //--- this formula round of to decimal place. 1000 = 0.001 while 100 = 0.01 and 10 = 0.1

   newopen = MathRound(High[0] * 1) /1;

   _50Pips = newopen - 0.0005; 

      

   if (High[0] > newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + (newopen+0.5) + " SL is " + newopen);

   buffer_entry = newopen+0.5;

   buffer_exit = newopen;

   }

   

   if (High[0] <= newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + newopen + " SL is " + _50Pips);

   buffer_entry = newopen;

   buffer_exit = _50Pips;

   }

   }



   // for currency pairs that has 5 decimals place

   if (Digits ==5)

   {

   //--- this formula round of to decimal place. 1000 = 0.001 while 100 = 0.01 and 10 = 0.1

   newopen = MathRound(High[0] * 1000) /1000;

   _50Pips = newopen - 0.0005; 

      

   if (High[0] > newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + (newopen+0.0005) + " SL is " + newopen);

   buffer_entry = newopen+0.0005;

   buffer_exit = newopen;

   }

   

   if (High[0] <= newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + newopen + " SL is " + _50Pips);

   buffer_entry = newopen;

   buffer_exit = _50Pips;

   }

   }

     

   // for currency pairs that has 3 decimals place

   if (Digits == 3)

   {

   

   //--- this formula round of to decimal place. 1000 = 0.001 while 100 = 0.01 and 10 = 0.1

   newopen = MathRound(High[0] * 10) /10;

   _50Pips = newopen - 0.05; 

   

   

   if (High[0] > newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + (newopen+0.05) + " SL is " + newopen);

   buffer_entry = newopen+0.05;

   buffer_exit = newopen;

   }

   

   if (High[0] <= newopen)

   {

   Alert("High is " + High[0] + " Entry price should be " + newopen + " SL is " + _50Pips);

   buffer_entry = newopen;

   buffer_exit = _50Pips;

   }

   }

   

   

   

   //--- get minimum stop level

   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);

   Print("Minimum Stop Level=",minstoplevel," points");

   double price=buffer_entry;

   

//--- calculated SL and TP prices 

   double stoploss=buffer_entry - StopLoss*Point;

   double takeprofit=buffer_entry + TakeProfit*Point;

//--- place market order to buy 1 lot

   

   int ticket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,buffer_entry,Slippage,stoploss,takeprofit,"My order",MagicNumber,0,clrGreen);

   if(ticket<0)

     {

      Print("OrderSend failed with error #",GetLastError());

     }

   else

      Print("OrderSend placed successfully");

//---

   



  }

//+------------------------------------------------------------------+

This is the lot size I'm trying to match


How do I or pull this lot size from? The variable from my code is fixed. I want to make it dynamic?

Please advise?

 
  1. David Aw: How do I or pull this lot size from?

    You don't. Read an input variable or use dynamic sizes:

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. 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.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

  2.    double stoploss=buffer_entry - StopLoss*Point;
    
       double takeprofit=buffer_entry + TakeProfit*Point;

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: