[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 122

 

Hello ! Could you please help me complete the Trailing Stop and Trailing Step for a year and I can't find it in my head !

Files:
udf.mqh  48 kb
 

where to add trailing stop and breakeven output ? and it does not increase the lot when you increase the deposit

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int Buy (int StopLoss = 0, int TakeProfit = 0, double Lot = 0)

{

int Ticket = 0;

double SL = 0;

double TP = 0;

double VolumeLot = 0;

RefreshRates();

if (StopLoss != 0)

SL = NormalizeDouble(Bid-StopLoss*Point, Digits);

if (TakeProfit != 0)

TP = NormalizeDouble(Ask+TakeProfit*Point, Digits);

if (Lot == 0)

VolumeLot = GetLot ();

else

VolumeLot = Lot;

Ticket = OrderSend(Symbol(), OP_BUY, VolumeLot, NormalizeDouble(Ask, Digits), 20, SL, TP, NULL, AccountNumber(), 0, Green);

if (Ticket == -1)

{

return (GetLastError ();

}

else

{

return (Ticket);

}

}

int Sell (int StopLoss = 0, int TakeProfit = 0, double Lot = 0)

{

int Ticket = 0;

double SL = 0;

double TP = 0;

double VolumeLot = 0;

RefreshRates();

if (StopLoss != 0)

SL = NormalizeDouble(Ask+StopLoss*Point, Digits);

if (TakeProfit != 0)

TP = NormalizeDouble(Bid-TakeProfit*Point, Digits);

if (Lot == 0)

VolumeLot = GetLot ();

else

VolumeLot = Lot;

Ticket = OrderSend(Symbol(), OP_SELL, VolumeLot, NormalizeDouble(Bid, Digits), 20, SL, TP, NULL, AccountNumber(), 0, Red);

if (Ticket == -1)

return (GetLastError ();

else

return (Ticket);

}

double GetLot ()

{

int lot;

if (AccountBalance()>=300) lot=0.01;

if (AccountBalance()>=500) lot=0.02;

if (AccountBalance()>=800) lot=0.03;

return (lot);

}

 
mahla:

where to add trailing stop and breakeven output ? and it does not increase the lot when you increase the deposit

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

In the advisor

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

function lot increase...... is probably better to change

example:

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;
//////
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }

Look at Moving Average.mq4

 

Can you please tell me how to use bollinger bands for momentum, not for price? I tried it like this:

BandOPeriodT - external variable, period of bollinger bands.

MomentumPeriodT - period of momentum, respectively.

BandOBottom=iBands (NULL,0,BandOPeriodT,1,0,iMomentum(NULL,0,MomentumPeriodT,PRICE_OPEN,0),MODE_LOWER,0);

and also like this:

MomO=iMomentum(NULL,0,MomentumPeriodT,PRICE_OPEN,0); - internal variable which returns the value

BandOBottom=iBands(NULL,0,BandOPeriodT,1,0,MomO,MODE_LOWER,0); - the variable should return the value of the lower Bollinger line for the previous variable value.

Also, am I right in assuming that MODE_LOWER means I get the value of BOLLINGER BOLLINGER in a variable? If not, how do I get it exactly?

 
Ovari:

Can you please tell me how to use bollinger bands for momentum, not for price? I tried it like this:

BandOPeriodT - external variable, period of bollinger bands.

MomentumPeriodT - period of momentum, respectively.

BandOBottom=iBands (NULL,0,BandOPeriodT,1,0,iMomentum(NULL,0,MomentumPeriodT,PRICE_OPEN,0),MODE_LOWER,0);

and also like this:

MomO=iMomentum(NULL,0,MomentumPeriodT,PRICE_OPEN,0); - internal variable which returns the value

BandOBottom=iBands(NULL,0,BandOPeriodT,1,0, MomO,MODE_LOWER,0); - the variable should return the value of the lower Bollinger line for the previous variable value.

Also, am I right in assuming that MODE_LOWER means I get the value of BOLLINGER BOLLINGER in a variable? If not, how do I get it exactly?


See trailer - there Bands for RSI - do the same for Momentum.

Description - in the log - see code at the beginning.

 
Thank you, because I'm very new, and not much of a programmer either:)
 

Hello! Please help! I want to try the PVT indicator:

double PVT = iCustom(NULL,240,"PVT",PRICE_CLOSE,1,1)

Can you please tell me what is missing?

It says: 2012.05.14 19:34:46 Cannot open file 'C:\Program Files\Alpari NZ MT4\experts\indicators\PVT.ex4' on the EURUSD,H4

Thank you!

 
mahla:

where to add trailing stop and breakeven output ? and it does not increase the lot when you increase the deposit

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int Buy (int StopLoss = 0, int TakeProfit = 0, double Lot = 0)

{

int Ticket = 0;

double SL = 0;

double TP = 0;

double VolumeLot = 0;

RefreshRates();

if (StopLoss != 0)

SL = NormalizeDouble(Bid-StopLoss*Point, Digits);

if (TakeProfit != 0)

TP = NormalizeDouble(Ask+TakeProfit*Point, Digits);

if (Lot == 0)

VolumeLot = GetLot ();

else

VolumeLot = Lot;

Ticket = OrderSend(Symbol(), OP_BUY, VolumeLot, NormalizeDouble(Ask, Digits), 20, SL, TP, NULL, AccountNumber(), 0, Green);

if (Ticket == -1)

{

return (GetLastError ();

}

else

{

return (Ticket);

}

}

int Sell (int StopLoss = 0, int TakeProfit = 0, double Lot = 0)

{

int Ticket = 0;

double SL = 0;

double TP = 0;

double VolumeLot = 0;

RefreshRates();

if (StopLoss != 0)

SL = NormalizeDouble(Ask+StopLoss*Point, Digits);

if (TakeProfit != 0)

TP = NormalizeDouble(Bid-TakeProfit*Point, Digits);

if (Lot == 0)

VolumeLot = GetLot ();

else

VolumeLot = Lot;

Ticket = OrderSend(Symbol(), OP_SELL, VolumeLot, NormalizeDouble(Bid, Digits), 20, SL, TP, NULL, AccountNumber(), 0, Red);

if (Ticket == -1)

return (GetLastError ();

else

return (Ticket);

}

double GetLot ()

{

int lot;

if (AccountBalance()>=300) lot=0.01;

if (AccountBalance()>=500) lot=0.02;

if (AccountBalance()>=800) lot=0.03;

return (lot);

}

Let us assume that the open position has already been selected and we know for sure that this position is opened
for the symbol to which the Expert Advisor is attached. Also assume that the value of trailing stop
in pips is contained in the TrailingStop variable.

   int err;
   if (OrderType() == OP_BUY)
     {
       // позиция на покупку
       if ((Bid-OrderOpenPrice())>=(TrailingStop*Point))
         {
           // выставляем Stop Loss
           if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point,
                                    OrderTakeProfit(), 0))
             Print("#", OrderTicket(),": trailing stop ", Bid-TrailingStop*Point);
           else
             {
              err = GetLastError();
              Print("#", OrderTicket(),": trailing stop error ", err);
             }
         }
     }
   else
     {
       // позиция на продажу
       if ((OrderOpenPrice()-Ask)>=(TrailingStop*Point))
         {
           // выставляем Stop Loss
           if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point,
                                    OrderTakeProfit(), 0))
             Print("#", OrderTicket(),": trailing stop ", Ask+TrailingStop*Point);
           else
             {
              err = GetLastError();
              Print("#", OrderTicket(),": trailing stop error ", err);
             }
         }
     }
The OrderProfit() function returns the profit for the order selected using the OrderSelect() function.

Suppose that we want to calculate the profit that we have gained from closed positions.

   int profit = 0;
   int pos;
   for ( pos = 0; pos<HistoryTotal(); pos++ )
     {
       // выделим позицию
       if (OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY) == true)
         {
           // это не отложенный ордер?
           if ( OrderType() <= OP_SELL) profit += OrderProfit();            
         }
       else
           Print("Ошибка ", GetLastError(), " при выделении ордера ", OrderTicket());
     }
   Print("Суммарный профит по закрытым позициям = ", profit);
And the full code that calculates the profit for all closed positions,
that are opened today, will be:

   //---- вычислим начало дня - переменная day_start
   int c_time = CurTime();  
   datetime day_start;
   day_start=c_time-TimeHour(c_time)*60*60-TimeMinute(c_time)*60-TimeSeconds(c_time);
   //---- подсчитаем прибыль
   int profit = 0;
   int pos;
   for ( pos = 0; pos<HistoryTotal(); pos++ )
     {
       // выделим позицию
       if (OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY) == true)
         {
           // это не отложенный ордер?
           if ( OrderType() <= OP_SELL)
             {
               // ордер был открыт сегодня?
               if (OrderOpenTime()>=day_start) profit += OrderProfit();
             }
         }
       else
           Print("Ошибка ", GetLastError(), " при выделении ордера ", OrderTicket());
     }
   Print("Суммарный профит по закрытым позициям = ", profit);
 
mahla:

? And it doesn't increase the lot size when you increase the deposit

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Maybe there is something else missing in the code:

double OrderLots( )
Returns the number of lots for the selected order.
The order must be pre-selected using OrderSelect().
Example:
 if(OrderSelect(10,SELECT_BY_POS)==true) Print("lots for the order 10 ",OrderLots()); else Print("OrderSelect() has returned an error - ",GetLastError());
 
borilunad:

Hello! Please help! I want to try the PVT indicator:

Can you please tell me what is missing?

It says: 2012.05.14 19:34:46 Cannot open file 'C:\Program Files\Alpari NZ MT4\experts\indicators\PVT.ex4' on the EURUSD,H4

Thank you!

The PVT indicator itself is missing (are you sure you have one?).