Download MetaTrader 5
To add comments, please log in or register
Unable to find the answer to your question? Use the search!
ruth26
21
ruth26 2016.08.24 15:24 

Hello, fellow coders!

Recently Ive been using buy/sell scripts in my trading where you can set up the % of account balance you are willing to risk and how many pips will this % represent.

Code for this script where you input the variables -

#property show_inputs
 
       double Lots              =          0.01;
       bool   UseMoneyMgmt      =          True;
extern double RiskPercent       =             5;
extern double StopLoss          =            100;
       string Input             = " Buy Price ";
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  { 
  double Risk = RiskPercent / 100.0;
  if (UseMoneyMgmt)  
   Lots = NormalizeDouble( AccountBalance()*Risk/StopLoss/(MarketInfo(Symbol(), MODE_TICKVALUE)),2); 
  if(Lots > 0) GlobalVariableSet("ordersize",Lots);
 
   return(0);
  }


Using this script pops up a window where you set the two forementioned variables ( % of account balance you are willing to risk (RiskPercent) and how much will it represent in pips (StopLoss) ).

Then there are these two scripts for buy and sell and their respective codes - for BUY script

//#property copyright ""
//#property link      ""


//#property show_inputs

extern double StopLoss = 100;
extern double TakeProfit = 100;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
 
   double lots = GlobalVariableGet("ordersize");
//----
 int ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",255,0,Green);
   return(0);
  }
//+------------------------------------------------------------------+

and for SELL script -

//#property copyright ""
//#property link      ""


//#property show_inputs

extern double StopLoss = 100;
extern double TakeProfit = 100;


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
 
   double lots = GlobalVariableGet("ordersize");
//----
 int ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",255,0,Green);
   return(0);
  }
//+------------------------------------------------------------------+

So ideally, with this setup either of the two scripts should open a position where the stop loss is 10 pips and those 10 pips represent 5% of you account balance and likewise the take profit is 10 pips and those 10 pips represent 5% of your account balance as well... ideally..

But the problem is, eventhough most of the time both take profit and stop loss are indeed 10 pips, the lot size opened according to this stop loss/take profit isnt 5% of account balance as it should be.

For example: I had 3233,37€ in my trading account and opened a position with settings: risk 5% of balance/ 10pip stop loss/ 10pip take profit, to my knowledge the lot size of the upcoming position should have been 2.02 lot but instead it opened a position with the lot size of 1.39 lot which meant each side of the trade was worth about 127.67€ ( about 3.94% of account balance ) instead of 161.60€ ( representing the set 5% risk of account balance ).

This leaves me puzzled as to why did it open such lot size and I think the answer lies within the code but I cannot find out myself. According to my logic, it should work, in theory.

I would appreciate greatly if anyone was so kind and could point me in the right direction.

All kind of help is highly appreciated!

Best regards,

ruth

whroeder1
13641
whroeder1 2016.08.24 15:38  
ruth26: Ive been using buy/sell scripts in my trading where you can set up the % of account balance you are willing to risk and how many pips will this % represent.
  1. You ask about code that you don't show. No mind readers here.
  2. You buy at the Ask and sell at the bid. For a sell the SL/TP is relative to the Ask.
    • 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.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerlot + CommissionPerLot) (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  3. See also my GUI at Indicators: Money Manager Graphic Tool - MQL5.community traders' Forum - Page 5 'Money Manager Graphic Tool' indicator by 'takycard'
/
To add comments, please log in or register