1. Please insert code correctly: when editing a post, click and paste your code in the popup window
2. Study the example from the documentation: Trade Operation Types ( Example of the TRADE_ACTION_SLTP trade operation for modifying the Stop Loss and Take Profit values of an open position )
#define EXPERT_MAGIC 123456 // MagicNumber of the expert //+------------------------------------------------------------------+ //| Modification of Stop Loss and Take Profit of position | //+------------------------------------------------------------------+ void OnStart() { //--- declare and initialize the trade request and result of trade request MqlTradeRequest request; MqlTradeResult result; int total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=0; i<total; i++) { //--- parameters of the order ulong position_ticket=PositionGetTicket(i);// ticket of the position string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position double volume=PositionGetDouble(POSITION_VOLUME); // volume of the position double sl=PositionGetDouble(POSITION_SL); // Stop Loss of the position double tp=PositionGetDouble(POSITION_TP); // Take Profit of the position ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the position //--- output information about the position PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]", position_ticket, position_symbol, EnumToString(type), volume, DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits), DoubleToString(sl,digits), DoubleToString(tp,digits), magic); //--- if the MagicNumber matches, Stop Loss and Take Profit are not defined if(magic==EXPERT_MAGIC && sl==0 && tp==0) { //--- calculate the current price levels double price=PositionGetDouble(POSITION_PRICE_OPEN); double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID); double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK); int stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL); double price_level; //--- if the minimum allowed offset distance in points from the current close price is not set if(stop_level<=0) stop_level=150; // set the offset distance of 150 points from the current close price else stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability //--- calculation and rounding of the Stop Loss and Take Profit values price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT); if(type==POSITION_TYPE_BUY) { sl=NormalizeDouble(bid-price_level,digits); tp=NormalizeDouble(ask+price_level,digits); } else { sl=NormalizeDouble(ask+price_level,digits); tp=NormalizeDouble(bid-price_level,digits); } //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); //--- setting the operation parameters request.action =TRADE_ACTION_SLTP; // type of trade operation request.position=position_ticket; // ticket of the position request.symbol=position_symbol; // symbol request.sl =sl; // Stop Loss of the position request.tp =tp; // Take Profit of the position request.magic=EXPERT_MAGIC; // MagicNumber of the position //--- output information about the modification PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type)); //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); } } } //+------------------------------------------------------------------+
- www.mql5.com
1. Please insert code correctly: when editing a post, click and paste your code in the popup window
2. Study the example from the documentation: Trade Operation Types ( Example of the TRADE_ACTION_SLTP trade operation for modifying the Stop Loss and Take Profit values of an open position )
Thank you Very Much for your reply and explaination.
I will check your answer
And here is the general scheme of how to set Stop Loss and Take Profit:
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2021.11.24 16:04
Opening and modifying positions:
Excellent Answer, Thank you.
If I need to repeat the execution of the Buy order 4 times, each one if after closing the previous one. I tries this code but it works only one time.
#define EXPERT_MAGIC 123456 //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int Countss =4; int J ; void OnStart() { J = 0; while( J<Countss && !IsStopped()); { MqlTradeRequest request; MqlTradeResult result; request.action =TRADE_ACTION_DEAL; // type of trade operation request.symbol =Symbol(); // symbol request.volume =0.2; // volume of 0.2 lot request.type =ORDER_TYPE_BUY; // order type request.price =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // price for opening request.magic =EXPERT_MAGIC; // MagicNumber of the order if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); int total=PositionsTotal(); // number of open positions for(int i=0; i<total; i++) { //--- parameters of the order ulong position_ticket=PositionGetTicket(i);// ticket of the position string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position double volume=PositionGetDouble(POSITION_VOLUME); // volume of the position double sl=PositionGetDouble(POSITION_SL); // Stop Loss of the position double tp=PositionGetDouble(POSITION_TP); // Take Profit of the position ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the position PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]", position_ticket, position_symbol, EnumToString(type), volume, DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits), DoubleToString(sl,digits), DoubleToString(tp,digits), magic); if(magic==EXPERT_MAGIC && sl==0 && tp==0) { //--- calculate the current price levels double price=PositionGetDouble(POSITION_PRICE_OPEN); double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID); double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK); double stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL); double price_level; //--- if the minimum allowed offset distance in points from the current close price is not set if(stop_level<=0) stop_level=150; // set the offset distance of 150 points from the current close price else stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability //--- calculation and rounding of the Stop Loss and Take Profit values price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT); if(type==POSITION_TYPE_BUY) { sl=NormalizeDouble(bid-price_level,digits); tp=NormalizeDouble(ask+price_level,digits); } else { sl=NormalizeDouble(ask+price_level,digits); tp=NormalizeDouble(bid-price_level,digits); } //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); //--- setting the operation parameters request.action =TRADE_ACTION_SLTP; // type of trade operation request.position=position_ticket; // ticket of the position request.symbol=position_symbol; // symbol request.sl =sl; // Stop Loss of the position request.tp =tp; // Take Profit of the position request.magic=EXPERT_MAGIC; // MagicNumber of the position //--- output information about the modification PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type)); //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); } } } J = J+1; } //+------------------------------------------------------------------+
You are using a "script" - a "script" is a one-time program. You need to use "advisor".
//+------------------------------------------------------------------+ //| BUY__ADVISOR.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #define EXPERT_MAGIC 123456 int Countss =4; int J = 0 ; int OnInit() { do { MqlTradeRequest request; MqlTradeResult result; request.action =TRADE_ACTION_DEAL; // type of trade operation request.symbol =Symbol(); // symbol request.volume =0.2; // volume of 0.2 lot request.type =ORDER_TYPE_BUY; // order type request.price =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // price for opening request.magic =EXPERT_MAGIC; // MagicNumber of the order if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); int total=PositionsTotal(); // number of open positions for(int i=0; i<total; i++) { //--- parameters of the order ulong position_ticket=PositionGetTicket(i);// ticket of the position string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position double volume=PositionGetDouble(POSITION_VOLUME); // volume of the position double sl=PositionGetDouble(POSITION_SL); // Stop Loss of the position double tp=PositionGetDouble(POSITION_TP); // Take Profit of the position ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the position PrintFormat("#%I64u %s %s %.2f %s sl: %s tp: %s [%I64d]", position_ticket, position_symbol, EnumToString(type), volume, DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits), DoubleToString(sl,digits), DoubleToString(tp,digits), magic); if(magic==EXPERT_MAGIC && sl==0 && tp==0) { //--- calculate the current price levels double price=PositionGetDouble(POSITION_PRICE_OPEN); double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID); double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK); double stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL); double price_level; //--- if the minimum allowed offset distance in points from the current close price is not set if(stop_level<=0) stop_level=150; // set the offset distance of 150 points from the current close price else stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability //--- calculation and rounding of the Stop Loss and Take Profit values price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT); if(type==POSITION_TYPE_BUY) { sl=NormalizeDouble(bid-price_level,digits); tp=NormalizeDouble(ask+price_level,digits); } else { sl=NormalizeDouble(ask+price_level,digits); tp=NormalizeDouble(bid-price_level,digits); } //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); //--- setting the operation parameters request.action =TRADE_ACTION_SLTP; // type of trade operation request.position=position_ticket; // ticket of the position request.symbol=position_symbol; // symbol request.sl =sl; // Stop Loss of the position request.tp =tp; // Take Profit of the position request.magic=EXPERT_MAGIC; // MagicNumber of the position //--- output information about the modification PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type)); //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); } } J = J+1; } while (J<Countss ); return(INIT_SUCCEEDED); }
Create the correct template - use the 'MQL5 Wizard' program:
//+------------------------------------------------------------------+ //| Trailing.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Everyone,
the following Code does not work with Take profit and Stop Lose. If i delete them ,it works fine.
What is the expected reasons for this?
***