I need to add stop loss!

Hi

I’m working on making my first Expert Advisor and I want to add stop loss and trailing stop to my script to make my profits the maximum. Anyone that can help me?

Here is my script so far:


extern int     Magic = 1;
extern int     Slippage=1;
extern int     ProfitTarget=1;
extern int     StopLoss=1;
extern double  Lots=0.01;

static int     Dig;
static double  Points;
static bool    Initialized = FALSE;
static bool    Running = FALSE;
static int     OrderNumber;
static int     TargetOrder;
static int     StopOrder;
static double  LastBid;
static double  LastAsk;
static color   clBuy = DodgerBlue;
static color   clSell = Crimson;

#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>

int Order(string symbol, int Type, double Entry, double Quantity,
          double TargetPrice, double StopPrice, string comment="")
{
   string TypeStr;
   color  TypeCol;
   int    ErrorCode, Ticket;
   double Price, FillPrice;
   Price = NormalizeDouble(Entry, Dig);
   switch (Type) {
      case OP_BUY:
         TypeStr = "BUY";
         TypeCol = clBuy;
         break;
      case OP_SELL:
         TypeStr = "SELL";
         TypeCol = clSell;
         break;
      default:
         Print("Unknown order type ", Type);
         break;
   }
   Ticket = OrderSend(symbol, Type, Quantity, Price, Slippage,
              StopPrice, TargetPrice, comment, Magic, 0, TypeCol);
   if (Ticket >= 0) {
      Sleep(SLEEP_OK);
      if (OrderSelect(Ticket, SELECT_BY_TICKET) == TRUE) {
         FillPrice = OrderOpenPrice();
         if (Entry != FillPrice) {
            RefreshRates();
            Print("Slippage on order ", Ticket, " - Requested = ",
                  Entry, ", Fill = ", FillPrice, ", Current Bid = ",
                  Bid, ", Current Ask = ", Ask);
         }
      }
      else {
         ErrorCode = GetLastError();
         Print("Error selecting new order ", Ticket, ": ",
               ErrorDescription(ErrorCode), " (", ErrorCode, ")");
      }
      return (Ticket);
   }
   ErrorCode = GetLastError();
   RefreshRates();
   Print("Error opening ", TypeStr, " order: ", ErrorDescription(ErrorCode),
         " (", ErrorCode, ")", ", Entry = ", Price, ", Target = ",
         TargetPrice, ", Stop = ", StopPrice, ", Current Bid = ", Bid,
         ", Current Ask = ", Ask);
   Sleep(SLEEP_ERR);
   return (-1);
}

void InitSystem()
{
   Running = FALSE;
   RefreshRates();
   LastBid = Bid;
   LastAsk = Ask;
   Initialized = TRUE;
}

int CheckEntry(double Size)
{
   if (Ask > LastAsk) {  // Short term uptrend so GO LONG!
      Print("Going Long!");
      OrderNumber = Order(Symbol(), OP_BUY, Ask, Size, 0,0, 0.0);

   }
   else if (Bid < LastBid) {  // Short term downtrend so GO SHORT!
      Print("Going Short!");
      OrderNumber = Order(Symbol(), OP_SELL, Bid, Size, 0.0, 0.0);

   }
   return(0);
}

int CheckExit()
{
   int ErrorCode;
   if (OrderSelect(TargetOrder, SELECT_BY_TICKET) != TRUE) {
      ErrorCode = GetLastError();
      Print("Error selecting target order ", TargetOrder, ": ",
            ErrorDescription(ErrorCode), " (", ErrorCode, ")");
      return (0);
   }
   else if ((OrderType() == OP_SELL) || (OrderType() == OP_BUY)) {
      Print("Target ", TargetOrder, " reached: ", OrderOpenPrice(),
             " at ", TimeToStr(OrderOpenTime(), TIME_SECONDS));
      CloseBy(OrderNumber, TargetOrder);
      Cancel(StopOrder);
      return (1);
   }
   if (OrderSelect(StopOrder, SELECT_BY_TICKET) != TRUE) {
      ErrorCode = GetLastError();
      Print("Error selecting stop order ", StopOrder, ": ",
            ErrorDescription(ErrorCode), " (", ErrorCode, ")");
      return (0);
   }
   else if ((OrderType() == OP_SELL) || (OrderType() == OP_BUY)) {
      Print("Stop Loss ", StopOrder, " reached: ", OrderOpenPrice(),
             " at ", TimeToStr(OrderOpenTime(), TIME_SECONDS));
      CloseBy(OrderNumber, StopOrder);
      Cancel(TargetOrder);
      return (1);
   }
   return(0);
}

int init()
{
   Dig = MarketInfo(Symbol(), MODE_DIGITS);
   Points = MarketInfo(Symbol(), MODE_POINT);
   Print("Digits = ", Dig, ", Points = ", DoubleToStr(Points, 5));
   if (!IsDemo() && !IsTesting()) {
      MessageBox("Wealth Warning! This expert is for educational purposes only." +
            " It should NEVER be used on a live account." +
            " Past performance is in no way indicative of future results!");
      Print("Initialization Failure");
      return(-1);
   }
   InitSystem();
   Print("Initialized OK");
   return(0);
}

int deinit()
{
    Print("DeInitialized OK");
    return(0);
}

int start()
{
   if (!Initialized) {
      return(-1);
   }
   else if (Running) {                    // Are we in a trade at the moment?
      if (CheckExit() > 0) {              // Yes - Last trade complete?
         Initialized = FALSE;             // Yes - Indicate we need to reinitialise
         InitSystem();                    //  and start all over again!
      }
   }
   else {
      if (CheckEntry(Lots) > 0) {         // Entered a trade?
         Running = TRUE;                  // Yes - Indicate that we're in a trade
      }
   }
   return(0);
}