Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Script | Auto SL TP by Risk Reward Ratio

MetaTrader Experts, Indicators, Scripts and Libraries
Features
  • Customizable Risk:Reward Ratio: Set your preferred Risk:Reward ratio (e.g., 1:2, 1:3) to align with your trading strategy.
  • Flexible Stop Loss Input: Define Stop Loss in pips for precise risk control.
  • Automatic SL/TP Calculation: The script calculates and applies SL and TP levels for open positions based on the position type (Buy or Sell).
  • User-Friendly: Easy-to-use interface with input parameters for quick setup.
  • Error Handling: Ensures reliable operation across different market conditions and symbols.

How to Use

  1. Attach the Script:
    • Drag and drop the script onto your MetaTrader 5 chart or double-click it in the Navigator.
  2. Configure Parameters:
    • RiskRewardRatio: Enter your desired Risk:Reward ratio (default: 2.0 for 1:2).
    • StopLossPips: Specify the Stop Loss distance in pips (default: 20 pips).
  3. Run the Script:
    • The script will automatically detect open positions for the current symbol and apply the calculated SL and TP levels.
  4. Check Results:
    • A confirmation message will appear in the MetaTrader 5 terminal, showing the modified SL and TP for each position.

Example

  • If you set StopLossPips = 20 and RiskRewardRatio = 2.0 , the script will:
    • Set a Stop Loss 20 pips away from the entry price.
    • Set a Take Profit 40 pips away from the entry price (based on a 1:2 ratio).
  • For a Buy position at 1.2000:
    • SL = 1.1980
    • TP = 1.2040

Why Use This Script?

  • Time-Saving: Eliminates manual SL/TP calculations.
  • Consistency: Ensures your trades follow a disciplined Risk:Reward strategy.
  • Versatility: Works with any symbol (Forex, commodities, indices) and account type.

Installation

  1. Download the script from the MQL5 Codebase.
  2. Place the .mq5 file in the MQL5/Scripts folder of your MetaTrader 5 directory.
  3. Compile the script in MetaEditor.
  4. Restart MetaTrader 5 and find the script in the Navigator under "Scripts."

Notes

  • Ensure you have open positions on the chart's symbol before running the script.
  • Test the script on a demo account first to familiarize yourself with its functionality.
  • For 4-digit brokers, adjust the StopLossPips value accordingly (e.g., use 2 instead of 20 pips).

Feedback

If you have suggestions or encounter issues, please leave a comment on the MQL5 CodeBase page or contact me through the MQL5 community. Your feedback is valuable for improving this tool!

Contact me: MQL5 Link

Happy trading!



59610