Script does not auto update SL as required

 
Hello,

I am fairly new to script writing as a whole in this regard. Hence this may be some minor user error on my part. However the attached script is meant to auto update the SL of all my current open positions. However when using the predefined hotkey that I have assigned to it. The journal indicates that the script has run but the SL is still set to 0. I understand you get other versions of this online that I may make use of, however this is going to be further developed later on to suit my own needs. Hence, I am merely trying to get a fundamental working prototype going.

Code is as follows:

//+------------------------------------------------------------------+
//|                                                  StopAndTake.mq5 |
//|                                   Copyright 2023, MetaQuotes Ltd |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo pos;
CTrade trade;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double bid=SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double ask=SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   double buy_sl = bid - 500 * _Point;
   double sell_sl = ask + 500 * _Point;

   int    err;

   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong ticket = PositionGetTicket(i);
      if(!PositionSelectByTicket(ticket))
         continue;
      if(pos.Symbol() != Symbol())
         continue;

      if((pos.PositionType() == POSITION_TYPE_BUY) && (pos.Symbol()==_Symbol))
        {
         if(!trade.PositionModify(ticket, buy_sl, pos.TakeProfit()))
           {
            err = GetLastError();
            PrintFormat("Failed to update ts on ticket %I64u to %f, err=%i", ticket, buy_sl, err);
           }
        }

      if((pos.PositionType()==POSITION_TYPE_SELL) && (pos.Symbol()==_Symbol))
        {
         if(!trade.PositionModify(ticket, sell_sl, pos.TakeProfit()))
           {
            err = GetLastError();
            PrintFormat("Failed to update ts on ticket %I64u to %f, err=%i", ticket, sell_sl, err);
           }
        }
     }
  }



Additionally if someone could perhaps provide me with some advice. Essentially I want to auto set the SL to the current ATR value -/+ current chart price depending on the position. Would that be possible using the standard script format and if so how would I go about doing that?

I have used an atr in an EA but I don't know if it would be the exact same way in terms of creating the handles/buffers and so forth? As this is not meant to display on a chart but merely run once, it leaves me a tad bit confused.


Thanks

 
are u using the same sl and tp for the opening orders? (they both have to be different at least 1 point)
 
  1. Javier Santiago Gaston De Iriarte Cabrera #: (they both have to be different at least 1 point)

    You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)


  2. Javier Santiago Gaston De Iriarte Cabrera #: are u using

    This is an international English forum; post in English on this part of the forums.
              Please don't write ur - it's "you are" or "your" - MQL4 programming forum (2014)

 
William Roeder #:
or pending prices) closer to the market than t

yes, you are right.

use symbol trade freeze level, tick size & stop level (also change the sl`s and tp's to be different)

double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
SYMBOL_TRADE_FREEZE_LEVEL*_Point
 
Javier Santiago Gaston De Iriarte Cabrera #:
are u using the same sl and tp for the opening orders? (they both have to be different at least 1 point)

The Take Profit is going to be assigned to either the 21 or 50 EMA (Need to backtest to see which one works better first)

The SL is going to be assigned to the ATR value ultimately. I was just trying to get it to work first before proceeding further

 
William Roeder #:
  1. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)


  2. This is an international English forum; post in English on this part of the forums.
              Please don't write ur - it's "you are" or "your" - MQL4 programming forum (2014)

Thanks for all of those. I will read the attached articles.


Do you have any further advice on how to grab the ATR value using a script? Would I grab the value from the indicator by creating another handle with the relevant buffers? Seems a bit pointless as it's just going to run once. Again I stand to be corrected.