거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Telegram에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

LotFromRisk: position size from risk, stop and commission - MetaTrader 5용 라이브러리

Northern Lights
Northern Lights
트레이더이자 Dojiwave 브랜드로 출시한 MetaTrader 5용 RiskDeck의 개발자입니다. 계좌가 살아남을지를 결정하는 부분, 즉 포지션 크기, 손절, 손실 한도를 위한 도구를 만듭니다. 모든 버전은 Market에 올라가기 전에 MetaTrader 5 데모 계좌에서 테스트합니다. 질문이나 오류 신고는 여기로 메시지를 보내 주세요. 보통 영업일 기준 하루 안에 답변드립니다.
조회수:
22
게시됨:
\MQL5\Include\LotFromRisk\ \MQL5\Scripts\LotFromRisk\
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

LotFromRisk is an include file for EA developers. It turns a risk figure, a stop and a commission per lot into a lot size whose loss at the stop never goes over the risk you asked for.

How it counts

lots = money at risk / (loss of 1 lot at the stop + commission of 1 lot, round turn)

  • Loss of 1 lot. The terminal values the move from entry to stop with OrderCalcProfit, so contract size, profit currency and the conversion to the account currency are the terminal's own. Tick value is the fallback.
  • Rounding. The lot is always rounded down to the symbol's volume step. Rounding to the nearest step can put the loss at the stop over the risk. The code also catches the floating point case where 0.29 lots would come out as 0.28.
  • Below the minimum. When the risk does not pay for the symbol's minimum volume, the answer is 0 lots with a reason. It never falls back to the minimum, which would risk more than asked.
  • Above the maximum. The size is capped at the symbol's maximum volume and the result says so.
  • Margin. The result reports the margin for that size and whether it fits in the free margin. The lot is not changed because of it.

Not counted: slippage at the stop, price gaps and swap.

Functions

  • LfrLots(symbol, type, entry, stop, risk, mode, commissionPerLot, result). The size for a buy or a sell, market or pending. entry = 0 means the current ask or bid. Returns true when result.lots is above 0.
  • LfrSizeFromMoney(riskMoney, lossPerLot, commissionPerLot, step, min, max, result). The arithmetic alone, without the terminal.
  • LfrLossPerLot(symbol, type, entry, stop). The loss of 1 lot from entry to stop, in the account currency.
  • LfrCommissionFromHistory(symbol, days). An estimate of the round turn commission per lot from the account's deals of the last N days (90 by default).
  • LfrRoundDown, LfrStepDigits, LfrRiskMoney. Helpers.

The risk can be a percent of the balance, a percent of the equity or money in the account currency (ENUM_LFR_RISK). LfrResult holds lots, riskMoney, lossPerLot, commissionPerLot, lossAtStop, margin, marginOk, a code (ENUM_LFR_CODE) and a reason in plain English.

Example

#include <LotFromRisk\LotFromRisk.mqh>
LfrResult r;
double sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) - 300 * _Point;
if(LfrLots(_Symbol, ORDER_TYPE_BUY, 0, sl, 1.0, LFR_PERCENT_BALANCE, 7.0, r))
   trade.Buy(r.lots, _Symbol, 0, sl, 0);
else
   Print("no trade: ", r.reason);

Demo script

LotFromRiskDemo.mq5 calculates a buy and a sell on the chart symbol and shows the result in the Experts log and on the chart. It sends no orders. Inputs: risk mode, risk, stop distance in points and commission per lot (-1 takes the estimate from the last 90 days of deals).


The screenshot shows XAUUSD with the stop 500 points (5.00) away, 1% of the balance at risk (84.57 USD) and a commission of 7 per lot round turn: 0.16 lots, 81.12 lost at the stop (1 lot: 500.00 plus 7.00 commission).

The code is free to use and modify, also in your own products. It is provided as is, without warranty. Test it on a demo account first.

Files

  • MQL5\Include\LotFromRisk\LotFromRisk.mqh
  • MQL5\Scripts\LotFromRisk\LotFromRiskDemo.mq5

Tested on MetaTrader 5 build 6331, a hedging demo account in USD, with EURUSD, USDJPY, GBPJPY, EURGBP, XAUUSD, BTCUSD and USDCHF. In 4054 random cases no size went over the risk. Rounding to the nearest step instead would have gone over in 1977 of them.

Dashboard Panel Dashboard Panel

A clean information card for your EA or indicator: rows, progress bars and a status pill on one anti-aliased CCanvas bitmap, light or dark to match the chart, and foldable with a click.

Broker Clock Broker Clock

Broker server time, UTC and the local time of London, Frankfurt, New York, Tokyo and Sydney, with daylight saving time on any date, live and in the Strategy Tester.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.