Trying to combine code for lot size into existing trailing stop EA

 

Hi

 

I have tried everything, but I am still unable to get this to work.  I would like to add the following code that I found elsewhere on this forum

extern double LotPerThousand=0.1;  // If you had 1000 balance what lot size would you use

int start()
{

double lots;

lots=NormalizeDouble((LotPerThousand/1000)*AccountBalance(),2);
if(lots<MarketInfo(Symbol(),MODE_MINLOT)){ lots=MarketInfo(Symbol(),MODE_MINLOT); }
if(lots>MarketInfo(Symbol(),MODE_MAXLOT)){ lots=MarketInfo(Symbol(),MODE_MAXLOT); }

...
OrderSend(Symbol(),OP_BUY,lots,...)
...

return 0;
}

 into this existing trailing stop EA

#property copyright "Copyright © 2009-2015, EarnForex.com"
#property link      "http://www.earnforex.com"
 
/*
   Kicks in when position reaches at least TrailingStop pips of profit.
*/
 
extern double TrailingStop = 10.0;
 
// Set it to some value above 0 to activate stop-loss
extern double StopLoss = 0; 
 
int init()
{
   return(0);
}
 
int deinit()
{
   return(0);
}
 
int start()
{
  double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol()) continue; // Skipping positions in other currency pairs
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = TrailingStop * PointValue;
 
      if (OrderType() == OP_BUY)
      {
         if (Bid - OrderOpenPrice() > TSTP)
         {
            if (OrderStopLoss() < Bid - TSTP)
            {
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
                  Print("Error setting Buy trailing stop: ", GetLastError());
            }
         }
         else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0))
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Buy stop-loss: ", GetLastError());
      }
      else if (OrderType() == OP_SELL)
      {
         if (OrderOpenPrice() - Ask > TSTP)
         {
            if ((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0))
            {
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
                  Print("Error setting Sell trailing stop: ", GetLastError());
            }
         }
         else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0))
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Sell stop-loss: ", GetLastError());
      }
        }
 
   return(0);
}

 Since I am manually entering the first trade, I then want the EA to take over and calculate the position (lot) size based on account balance.  As such, I removed the section regarding SendOrder in the lotsize code and came up with this, but when I place an order it ignores the lotsize calculation and place the order at whatever lotsize setting there is on the MT4 terminal

#property copyright "Copyright © 2009-2015, EarnForex.com"
#property link      "http://www.earnforex.com"
 
/*
   Kicks in when position reaches at least TrailingStop pips of profit.
*/
 
extern double TrailingStop = 10.0;
extern double LotPerThousand=0.1;  // If you had 1000 balance what lot size would you use
 
// Set it to some value above 0 to activate stop-loss
extern double StopLoss = 0; 
 
int init()
{
   return(0);
}
 
int deinit()
{
   return(0);
}
 
int start()
{

double lots;

lots=NormalizeDouble((LotPerThousand/1000)*AccountBalance(),2);
if(lots<MarketInfo(Symbol(),MODE_MINLOT)){ lots=MarketInfo(Symbol(),MODE_MINLOT); }
if(lots>MarketInfo(Symbol(),MODE_MAXLOT)){ lots=MarketInfo(Symbol(),MODE_MAXLOT); }

  double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol()) continue; // Skipping positions in other currency pairs
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = TrailingStop * PointValue;
 
      if (OrderType() == OP_BUY)
      {
         if (Bid - OrderOpenPrice() > TSTP)
         {
            if (OrderStopLoss() < Bid - TSTP)
            {
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
                  Print("Error setting Buy trailing stop: ", GetLastError());
            }
         }
         else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0))
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Buy stop-loss: ", GetLastError());
      }
      else if (OrderType() == OP_SELL)
      {
         if (OrderOpenPrice() - Ask > TSTP)
         {
            if ((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0))
            {
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
                  Print("Error setting Sell trailing stop: ", GetLastError());
            }
         }
         else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0))
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Sell stop-loss: ", GetLastError());
      }
        }
 
   return(0);
}

 Will someone please have a look and explain what I need to do so that the EA will automatically calculate my lotsize of my manually entered trades?  Thank you.  Gary

 

If you want to calculate the lot size for your manual trades, you will need to write a separate script that calculates and sends the order.

Reason: