Library of trading functions (position size, open, close position etc.) from the EAs “Ocean Breeze” and “Graalino-Pro”.
It can be of big help for novice traders as an example how to handle trading events.
It calculates the size of position from the RISK (%) given. Forget about pips, lots, ticks, digits etc. Take a pro-approach. It works on any financial instrument regardless of currency’s name, leverage, lot digits, account’s currency, broker etc.
The library works for several years at several different brokers on more than 50 currency pairs, metals, etc. This library is a Public Domain.
Example:
//_________________________________________________ // | // Calc_Optimal_Pos_Size () | //_________________________________________________| double Calc_Optimal_Pos_Size(string Bond_Name, int Time_Frame, double Max_Risk, int Portfolio_Bonds, int Full_Expert_ID, bool Use_Adaptive_Pos_Size_Param, double First_Loss_Drop_Param, double Losses_Drop_Slope_Param, double Second_Profit_Drop_Param, double Recovery_Slope_Param, double Max_Drop_Param) { double Pos_Size; double Pos_Size_Portfolio; double Pos_Size_Risk; //................................................................. Pos_Size_Risk=Calc_Pos_Size_To_Risk(Bond_Name, Max_Risk); Pos_Size_Portfolio=Calc_Pos_Size_To_Portfolio(Bond_Name, Portfolio_Bonds); Pos_Size=MathMin(Pos_Size_Risk,Pos_Size_Portfolio); // ======================= USE ADAPTIVE POS SIZE =================================== if(Use_Adaptive_Pos_Size_Param==TRUE) { // Print ("Adaptive: risk init=" + DoubleToStr (Pos_Size, 4)); Pos_Size=Adjust_Pos_Size_To_History(Pos_Size, Bond_Name, Full_Expert_ID, First_Loss_Drop_Param, Losses_Drop_Slope_Param, Second_Profit_Drop_Param, Recovery_Slope_Param, Max_Drop_Param); // Print ("Adaptive: out=" + DoubleToStr (Pos_Size, 4)); } // ============== ADJUST POS SIZE TO BROKER's LIMITS ==================================== // Print ("Optim Pos size=", DoubleToStr (Pos_Size, 4)); Pos_Size=Adjust_Pos_Size_To_Broker(Bond_Name,Pos_Size); return (Pos_Size); } //__________________________________________________ // | // Calc_Pos_Size_To_Risk () | //_________________________________________________| double Calc_Pos_Size_To_Risk(string Bond_Name, double Max_Risk) { double Equity_Drawdown; double Daily_Volatility; double Pos_Size; int Lot_Digits; double Stop_Loss_Points; double One_Point_Cost; double Tick_Value; double Margin_Init; double Min_Lot_Size; //........................................................................ if(TerminalInfoInteger(TERMINAL_CONNECTED)!=TRUE) { Pos_Size=0.0; return (Pos_Size); } //........................................................................ RefreshRates(); //................................................................. Min_Lot_Size=MarketInfo(Bond_Name,MODE_MINLOT); Tick_Value=MarketInfo(Bond_Name,MODE_TICKVALUE); Margin_Init=MarketInfo(Bond_Name,MODE_MARGININIT); if((MarketInfo(Bond_Name,MODE_MARGINCALCMODE)==2) && (Margin_Init!=0.0)) { Tick_Value=Tick_Value *MarketInfo(Bond_Name,MODE_MARGINREQUIRED)/Margin_Init; } // Print ("Tick val=", DoubleToStr(Tick_Value, 6)); //.......................................................................... Equity_Drawdown=MathMin(AccountEquity(),AccountBalance())*Max_Risk/100.0; //percent Daily_Volatility=Get_Daily_Volatility(Bond_Name); // Print ("Daily volat=", DoubleToStr (Daily_Volatility,5)); //.......................................................................... Stop_Loss_Points=MarketInfo(Bond_Name,MODE_BID)*Daily_Volatility /MarketInfo(Bond_Name,MODE_POINT); // Print ("Stop_Loss_Points=", DoubleToStr (Stop_Loss_Points, 2)); //.......................................................................... One_Point_Cost=Tick_Value/(MarketInfo(Bond_Name,MODE_TICKSIZE) /MarketInfo(Bond_Name,MODE_POINT)); // Print (" One_Point_Cost =", DoubleToStr (One_Point_Cost, 4)); //.......................................................................... if(One_Point_Cost==0.0) { Print("Error: One_Point_Cost=0.0: ",DoubleToStr(One_Point_Cost,6)); Print("Tick_Value= ",DoubleToStr(Tick_Value,6)); Print("Equity_Drawdown= ",DoubleToStr(Equity_Drawdown,6)); Print("Daily_Volat= ",DoubleToStr(Daily_Volatility,5)); Print("Stop_Loss_Points= ",DoubleToStr(Stop_Loss_Points,3)); Print("One_Point_Cost= ",DoubleToStr(One_Point_Cost,6)); return (0.0); } //.......................................................................... Pos_Size=Equity_Drawdown/(One_Point_Cost*Stop_Loss_Points); //.......................................................................... if(Pos_Size<Min_Lot_Size) { Pos_Size=Min_Lot_Size; } //............................................................................. Lot_Digits=Get_Lot_Digits(Bond_Name); // Print (" Lot_Digits =" + Lot_Digits); Pos_Size=NormalizeDouble(Pos_Size,Lot_Digits); // Print (" Pos_Size =", DoubleToStr (Pos_Size, 4)); //.......................................................................... return (Pos_Size); } //______________________________________________________________ // | // Adjust_Pos_Size_To_History () | //______________________________________________________________| double Adjust_Pos_Size_To_History(double Pos_Size, string Bond_Name, int Full_Expert_ID, double First_Loss_Drop_Param, double Losses_Drop_Slope_Param, double Second_Profit_Drop_Param, double Recovery_Slope_Param, double Max_Drop_Param) { double PL_History; // positive profits OR negative losses PL_History=Calc_PL_History_Factor(Bond_Name,Full_Expert_ID); Pos_Size=Reduce_Pos_Size_To_PL_History(PL_History, Pos_Size, Bond_Name, First_Loss_Drop_Param, Losses_Drop_Slope_Param, Second_Profit_Drop_Param, Recovery_Slope_Param, Max_Drop_Param); return (Pos_Size); }