script help clear all TP and SL

To add comments, please log in or register
chizzymamma
10
chizzymamma  
//|                                             RoNz Auto SL n TP.mq4|
//|                                   Copyright 2014, Rony Nofrianto |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Rony Nofrianto"
#property link      ""
#property version   "1.00"
#property strict

int CalculateCurrentOrders()
  {
   int buys=0,sells=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderType()==OP_BUY)
        {
         buys++;
        }
      if(OrderType()==OP_SELL)
        {
         sells++;
        }
     }

   if(buys>0) return(buys);
   else       return(-sells);

  }
bool ClearSLnTP()
  {
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
       bool res=OrderModify(OrderTicket(),OrderOpenPrice(),0,0,0,Blue);

     }
   return false;
  }
void OnStart()
  {
   if(CalculateCurrentOrders()!=0)
      ClearSLnTP();
  }

I found this script, but when i run it clears for all open charts.. how do i do it so it only works on the chart i drag it on?

William Roeder
20208
William Roeder  
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Filter by the chart's symbol.
    Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect/Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

Keith Watford
Moderator
20378
Keith Watford  
chizzymamma:

I found this script, but when i run it clears for all open charts.. how do i do it so it only works on the chart i drag it on?

The code that you show is very strange. Unless i am missing something, I don't see any reason to count the trades.

Not tested.....

#property strict

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   for(int i=OrdersTotal()-1; i>=0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS)
            && OrderSymbol()==_Symbol //This line will only modify trades of the chart symbol- Remove if you don't want this filter
            && OrderMagicNumber()==0  //This line will only modify manual trades- Remove if you don't want this filter (assuming EA trades have a magic number)
        )
         if(OrderTakeProfit()!=0 || OrderStopLoss()!=0)
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),0,0,0,clrNONE))
               Print("Error with Order Modify. Ticket# ",OrderTicket(),". ",ErrorDescription(GetLastError()));
      }
}
//+------------------------------------------------------------------+
To add comments, please log in or register