//+------------------------------------------------------------------+ //| CalculateRisk.mq4 | //| | //| | //+------------------------------------------------------------------+ #property copyright "" #property link "" #property indicator_chart_window string RISK_NAME = "Максимальный риск"; string KU_NAME = "Капитал Управляющего"; string DIGITLOT_NAME = "Разрядность лота"; string SPREAD_NAME = "Запас спреда для открытия"; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { calculateRisk(); return(0); } //+------------------------------------------------------------------+ void calculateRisk() { string name; int i; string cmt = ""; double cost1, cost2; if (MarketInfo(Symbol(), MODE_TRADEALLOWED)) { cmt = "Торги разрешены\n"; } else { cmt = "Торги запрещены\n"; } cmt = cmt + "ЦЕНА ТИКА " + DoubleToStr(MarketInfo(Symbol(), MODE_TICKVALUE), 2) + "\n"; cmt = cmt + "РАЗМЕР ТИКА " + DoubleToStr(MarketInfo(Symbol(), MODE_TICKSIZE), Digits) + "\n"; Comment(cmt); if (ObjectsTotal(OBJ_FIBO) != 1) { cmt = "Неправильное количество объектов ФИБО\n"; return(0); } for (i = 0; i < ObjectsTotal(); i++) { name = ObjectName(i); if (ObjectType(name) == OBJ_FIBO) { if (ObjectGet(name, OBJPROP_TIME1) == Time[0]) { ObjectSet(name, OBJPROP_PRICE1, Close[0]); } cost1 = ObjectGet(name, OBJPROP_PRICE1); cost2 = ObjectGet(name, OBJPROP_PRICE2); break; } } string commentString = ""; double lotSize = 0; double spread = Ask - Bid; double advSpread = getSpread(); double maxPrice = NormalizeDouble(MathMax(cost1, cost2) + (1.0 + advSpread) * spread, Digits); double minPrice = NormalizeDouble(MathMin(cost1, cost2) - advSpread * spread, Digits); double balans = getKU(); double costTick = getCostTick(Symbol()); double maxRisk = balans * getRisk() / 100; double sizeRisk = costTick * (maxPrice - minPrice) / MarketInfo(Symbol(), MODE_TICKSIZE); if (NormalizeDouble(sizeRisk - 0, 2) == 0) sizeRisk = 1; int DigitLot = getDigitLot(); lotSize = NormalizeDouble(maxRisk / sizeRisk, DigitLot);// * MarketInfo(Symbol(), MODE_LOTSIZE); lotSize = MathMax(lotSize, MarketInfo(Symbol(), MODE_MINLOT)); lotSize = MathMin(lotSize, MarketInfo(Symbol(), MODE_MAXLOT)); //commentString = commentString + DoubleToStr(DigitLot , 4); double factRisk = lotSize * sizeRisk; double take; //if (ShowCost) commentString = commentString + "ЦЕНА ТИКА " + " " + DoubleToStr(costTick, 5) + "\n"; commentString = commentString + Symbol() + "\n"; if (cost1 > cost2) { commentString = commentString + "\nBuyStop: " + DoubleToStr(maxPrice, Digits) + "\nStopLoss: " + DoubleToStr(minPrice, Digits); take = maxPrice + 2.5 * (maxPrice - minPrice); } else { commentString = commentString + "\nSellStop: " + DoubleToStr(minPrice, Digits)+ "\nStopLoss: " + DoubleToStr(maxPrice, Digits); take = minPrice - 2.5 * (maxPrice - minPrice); } commentString = commentString + "\nTakeProfit: " + DoubleToStr(take, Digits); commentString = commentString + "\nLot: " + DoubleToStr(lotSize , DigitLot); commentString = commentString + "\nRisk: ~" + DoubleToStr(factRisk, 2) + " (" + DoubleToStr(factRisk * 100 / balans, 2) + "%)\n"; Comment(cmt + "\n" + commentString); } double getSpread() { if (!GlobalVariableCheck(SPREAD_NAME)) { GlobalVariableSet(SPREAD_NAME, 0); } return (GlobalVariableGet(SPREAD_NAME)); } double getBalans() { if (NormalizeDouble(AccountBalance(), 2) == 0) { return(100); } return (AccountBalance()); } double getKU() { if (!GlobalVariableCheck(KU_NAME)) { GlobalVariableSet(KU_NAME, 150); } return (GlobalVariableGet(KU_NAME)); } double getCostTick(string sym) { return (MarketInfo(sym, MODE_TICKVALUE)); } int getDigitLot() { if (!GlobalVariableCheck(DIGITLOT_NAME)) { GlobalVariableSet(DIGITLOT_NAME, 0); } return (GlobalVariableGet(DIGITLOT_NAME)); } double getRisk() { if (!GlobalVariableCheck(RISK_NAME)) { GlobalVariableSet(RISK_NAME, 1); } return (GlobalVariableGet(RISK_NAME)); }