Help need add sl tp in my script

 

Please can anyone add sl tp in my script


#property copyright "Copyright © 2014, Zulutrade Inc"
#property link      ""

extern int BuyThreshold = 40;
extern int SellThreshold = 30;
extern double Lots = 1;
extern int MagicNumber = 33;
extern string comment = "by example script";
extern int maxOpenPositions = 3;
extern int shift = 1;

int RSILength = 14;
int TimeOfFirstBar = 0;

int init() {
}

int start() {
    double RSI = 0.0;
    
    if (isFirstTickOfCurrentBar()) {
        RSI = iRSI(NULL, PERIOD_H1, RSILength, PRICE_CLOSE, shift);
        Print("Got RSI value for period H1 and shift ", shift, " equals to ", RSI);
        // Buy Condition
        if ( RSI >= BuyThreshold && OrdersTotal() < maxOpenPositions) {
            if (doBuy() == false) { 
                return (0);
            }
        }
        // Close condition
        if ( RSI <= SellThreshold && OrdersTotal() > 0) {
           if(doClose() == false) {
               return(0);
           }
        }
    }
}

// Figures out the first tick of a new bar
bool isFirstTickOfCurrentBar() {
    if (TimeOfFirstBar != Time[1]) {
        TimeOfFirstBar = Time[1];
        return (true);
    }
    return (false);
}

// Close an order checking magic number to make sure it is generated from current script
bool doClose() {
    OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
    if (OrderClose( OrderTicket(), OrderLots(), Ask, 0, White) == -1) {
        Print ("Failed to close trade ", OrderTicket(),", error # ", GetLastError());
        return(false);
    }
    Print ("Successfully closed trade ", OrderTicket(),", error # ", GetLastError());
    return(true);
}

// Open a new order
bool doBuy() {
    int ticket = OrderSend( Symbol(), OP_BUY, Lots, Ask, 0, 0.0, 0.0, comment, MagicNumber, 0, Lime);
    if (ticket < 0) {
      Print ("Failed to open trade, error # ", GetLastError());
      return (false);
    }
    Print ("Successfully opened ticket ", ticket);
    return (true);
}
 
saqib saeed: Please can anyone add sl tp in my script
  1. No one can with such a vague want. Where does the SL go? What is the RRR of the TP? Etc.

  2. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

 
saqib saeed:

Please can anyone add sl tp in my script


#property copyright "Copyright © 2014, Zulutrade Inc"
#property link      ""

extern int BuyThreshold = 40;
extern int SellThreshold = 30;
extern int SL = 400;
extern int TP = 300;

extern double Lots = 1;
extern int MagicNumber = 33;
extern string comment = "by example script";
extern int maxOpenPositions = 3;
extern int shift = 1;

int RSILength = 14;
int TimeOfFirstBar = 0;

int init() {
return(0);
}

int start() {
    double RSI = 0.0;
    
    if (isFirstTickOfCurrentBar()) {
        RSI = iRSI(NULL, PERIOD_H1, RSILength, PRICE_CLOSE, shift);
        Print("Got RSI value for period H1 and shift ", shift, " equals to ", RSI);
        // Buy Condition
        if ( RSI >= BuyThreshold && OrdersTotal() < maxOpenPositions) {
            if (doBuy() == false) { 
                return (0);
            }
        }
        // Close condition
        if ( RSI <= SellThreshold && OrdersTotal() > 0) {
           if(doClose() == false) {
               return(0);
           }
        }
    }
   return(0);  
}

// Figures out the first tick of a new bar
bool isFirstTickOfCurrentBar() {
    if (TimeOfFirstBar != Time[1]) {
        TimeOfFirstBar = Time[1];
        return (true);
    }
    return (false);
}

// Close an order checking magic number to make sure it is generated from current script
bool doClose() {
    bool durum=OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
    if (OrderClose( OrderTicket(), OrderLots(), Ask, 0, White) == -1) {
        Print ("Failed to close trade ", OrderTicket(),", error # ", GetLastError());
        return(false);
    }
    Print ("Successfully closed trade ", OrderTicket(),", error # ", GetLastError());
    return(true);
}

// Open a new order
bool doBuy() {
     int ticket = OrderSend( Symbol(), OP_BUY, Lots, Ask, 0, 0.0, 0.0, comment, MagicNumber, 0, Lime);
     ticket= OrderModify(OrderTicket(), OrderOpenPrice(),
             NormalizeDouble(OrderOpenPrice()-SL*MarketInfo(OrderSymbol(),MODE_POINT) ,int(MarketInfo(OrderSymbol(),MODE_DIGITS))),
             NormalizeDouble(OrderOpenPrice()+TP*MarketInfo(OrderSymbol(),MODE_POINT) ,int(MarketInfo(OrderSymbol(),MODE_DIGITS))),
             0, CLR_NONE);
         
    
    if (ticket < 0) {
      Print ("Failed to open trade, error # ", GetLastError());
      return (false);
    }
    Print ("Successfully opened ticket ", ticket);
    return (true);
}