#property copyright "porquilho" #property link "porquilho@gmail.com" #property version "1.00" #property description "Script to set Stop Loss" // Include the Trade.mqh file to use CTrade class #include #include #include //---+ #include "AccountInfoSampleInit.mqh" // Input parameters input double RiskReqwardRatio = 1; // Risk:Reward Ratio (e.g., 2.0 for 1:2) input double StopLossPips = 1; // Stop Loss in pips //+------------------------------------------------------------------+ //| Account Info Sample script class | //+------------------------------------------------------------------+ class CAccountInfoSample { protected: CAccountInfo m_account; //--- chart objects CChartObjectLabel m_label[19]; CChartObjectLabel m_label_info[19]; public: CAccountInfoSample(void); ~CAccountInfoSample(void); //--- bool Init(void); void Deinit(void); void Processing(void); private: void AccountInfoToChart(void); }; CAccountInfoSample ExtScript; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CAccountInfoSample::CAccountInfoSample(void) { } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ CAccountInfoSample::~CAccountInfoSample(void) { } //+------------------------------------------------------------------+ //| Method Init. | //+------------------------------------------------------------------+ bool CAccountInfoSample::Init(void) { int i,sy=90; int dy=16; color color_label; color color_info; //--- tuning colors color_info =(color)(ChartGetInteger(0,CHART_COLOR_BACKGROUND)^0xFFFFFF); color_label=(color)(color_info^0x202020); //--- if(ChartGetInteger(0,CHART_SHOW_OHLC)) sy+=16; //--- creation Labels[] for(i=0;i<19;i++) { m_label[i].Create(0,"Label"+IntegerToString(i),0,20,sy+dy*i); m_label[i].Description(init_str[i]); m_label[i].Color(color_label); m_label[i].FontSize(8); //--- m_label_info[i].Create(0,"LabelInfo"+IntegerToString(i),0,120,sy+dy*i); m_label_info[i].Description(" "); m_label_info[i].Color(color_info); m_label_info[i].FontSize(8); } AccountInfoToChart(); //--- re\\\\draw chart ChartRedraw(); //--- return(true); } //+------------------------------------------------------------------+ //| Method Deinit. | //+------------------------------------------------------------------+ void CAccountInfoSample::Deinit(void) { } //+------------------------------------------------------------------+ //| Method Processing. | //+------------------------------------------------------------------+ void CAccountInfoSample::Processing(void) { AccountInfoToChart(); //--- redraw chart ChartRedraw(); Sleep(50); } //+------------------------------------------------------------------+ //| Method InfoToChart. | //+------------------------------------------------------------------+ void CAccountInfoSample::AccountInfoToChart(void) { m_label_info[0].Description(IntegerToString((int)(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*500))); m_label_info[1].Description(m_account.TradeModeDescription()); m_label_info[2].Description((string)m_account.Leverage()); m_label_info[3].Description(m_account.MarginModeDescription()); m_label_info[4].Description((string)m_account.TradeAllowed()); m_label_info[5].Description((string)m_account.TradeExpert()); m_label_info[6].Description(DoubleToString(m_account.Balance(),2)); m_label_info[7].Description(DoubleToString(m_account.Credit(),2)); m_label_info[8].Description(DoubleToString(m_account.Profit(),2)); m_label_info[9].Description(DoubleToString(m_account.Equity(),2)); m_label_info[10].Description(DoubleToString(m_account.Margin(),2)); m_label_info[11].Description(DoubleToString(m_account.FreeMargin(),2)); m_label_info[12].Description(DoubleToString(m_account.MarginLevel(),2)); m_label_info[13].Description(DoubleToString(m_account.MarginCall(),2)); m_label_info[14].Description(DoubleToString(m_account.MarginStopOut(),2)); m_label_info[16].Description(m_account.Server()); m_label_info[17].Description(m_account.Currency()); m_label_info[18].Description(m_account.Company()); } void OnStart() { int positionsalreadyopen=PositionsTotal(); int totalpositions=0; ExtScript.Init(); //--- cycle until the script is not halted while(!IsStopped()) { ExtScript.Processing(); if(totalpositions!=PositionsTotal() && positionsalreadyopen!=PositionsTotal()) { totalpositions=PositionsTotal(); positionsalreadyopen=PositionsTotal(); // Get current symbol string symbol = Symbol(); double point = SymbolInfoDouble(symbol, SYMBOL_POINT); double pip_value = point * SymbolInfoDouble(symbol, SYMBOL_BID);; // Adjust for 5-digit brokers // Get current market price double bid = SymbolInfoDouble(symbol, SYMBOL_BID); double ask = SymbolInfoDouble(symbol, SYMBOL_ASK); //Get minimum stop loss in points // Variable to store the stops level int stops_level; // Retrieve the minumum in _Point where you are allowed to set stop loss's for the specified symbol stops_level = (int)SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL); // Retrieve spread double spread=ask-bid; // Calculate Stop Loss and Take Profit in price units double sl = 0; // Create a CTrade object CTrade trade; // Example: Modify an open position (assumes one open position for simplicity) ulong ticket = PositionGetTicket(totalpositions-1); if(PositionSelectByTicket(ticket)) { if(PositionGetString(POSITION_SYMBOL) == symbol) { double current_sl = PositionGetDouble(POSITION_SL); double current_tp = PositionGetDouble(POSITION_TP); double open_price = PositionGetDouble(POSITION_PRICE_OPEN); double val=0; // Set SL and TP based on position type (Buy or Sell) if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) //--- cálculo do valor minimo do preço Close em 0 barras consecutivas a partir do índice 0 e terminando no índice 5. val = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, 5, 0)) - spread - stops_level; else //--- cálculo do valor maxido do preço Close em 0 barras consecutivas a partir do índice 0 e terminando no índice 5. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) val = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, 5, 0)) + spread + stops_level; // Modify position trade.PositionModify(ticket, val, current_tp); Print("SL and TP set for ticket #", ticket, ": SL=", val, ", TP=", current_tp); Sleep(500); } } } } ExtScript.Deinit(); } /* /* // In MQL5, `SYMBOL_TRADE_STOPS_LEVEL` is a predefined constant used with the `SymbolInfoInteger` function to retrieve the minimum distance in points from the current price at which stop orders (such as Stop Loss and Take Profit) can be placed for a specific symbol. This is important for traders to know because it defines the closest level at which they can set their stop orders relative to the current market price. // // Here's a basic example of how you might use `SYMBOL_TRADE_STOPS_LEVEL` in an MQL5 script: // //+------------------------------------------------------------------+ //| StopsLevel.mq5 | //| Copyright 2023, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ void OnStart() { // Define the symbol you are interested in string symbol = _Symbol; // Variable to store the stops level int stops_level; // Retrieve the stops level for the specified symbol stops_level = (int)SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL); // Check if the retrieval was successful if(stops_level != -1) { // Print the stops level in points Print("The stops level for ", symbol, " is ", stops_level, " points."); } else { // Print an error message if the retrieval failed Print("Failed to retrieve the stops level for ", symbol); } } // // ### Explanation: // // - **SymbolInfoInteger**: This function is used to get various properties of a trading symbol. When you pass `SYMBOL_TRADE_STOPS_LEVEL` as the second argument, it returns the stops level in points. // // - **stops_level**: This variable stores the result of the `SymbolInfoInteger` function call. If the function call is successful, `stops_level` will contain the minimum distance in points for stop orders. // // - **Error Handling**: The script checks if `stops_level` is not equal to `-1`, which would indicate a successful retrieval. If it equals `-1`, it means the retrieval failed, and an error message is printed. // // This script can be used in an MQL5 environment to check the stops level for any symbol you are interested in. Always ensure that the symbol you are querying is available in your trading platform. // */