Trailing stop wont move

 
//Input Variable for StopLoss%
input double StopLoss = 0.015;

//Risk%
input double RiskP = .015;

//Short Term MA Period
input int ShortTermMovingAverage = 2;

//Medium Term MA Period
input int MediumTermMovingAverage = 10;

//Long Term MA Perios
input int LongTermMovingAverage = 20;

//Configure ADX Value
input double VADXValue = 25;

//Input Variable for Buffer
input double Buffer =.001;

// Every Tick Code Starts here
void OnTick()
  {
//Create the Short Term MA
double ShortTermMALine = iMA(_Symbol,_Period,ShortTermMovingAverage,0,MODE_SMA,PRICE_CLOSE,1);

//Create the Medium Term MA
double MediumTermMALine = iMA(_Symbol,_Period,MediumTermMovingAverage,0,MODE_SMA,PRICE_CLOSE,1);

//Create Long Term MA
double LongTermMALine = iMA(_Symbol,_Period,MediumTermMovingAverage,0,MODE_SMA,PRICE_CLOSE,1);

//Create Upper BB
double UpperBB = iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_UPPER,1);

//Create Moving Average Line for BB
double BBMA = iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE,1);

//Create Lower BB
double LowerBB = iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_LOWER,1);

//Create ADX Value to get Sideways Trend
double ADXValue = iADX(_Symbol,_Period,14,PRICE_CLOSE,MODE_MAIN,0);

//If Uptrend Buy
if (((Close[1]>UpperBB)&&(ShortTermMALine>LongTermMALine)&&(ADXValue<VADXValue)&&(OrdersTotal()<1)))

//Send Uptrend BUYStop Order
{
 //Buy Price
double BuyPrice = iHigh(_Symbol,_Period,1);

//Create Buffer to let trades run
double BufferValue = (BBMA-Buffer);

//StopLoss
double BuyStopLoss = ((BuyPrice-(BuyPrice*StopLoss))-BufferValue);

//Buy Position Size
double BuyPositionSize = NormalizeDouble((AccountBalance()*RiskP) / (BuyPrice-LowerBB) / 100000,2);

//Actuall Order Send
int UpTrendBuyOrder = OrderSend(_Symbol,OP_BUYSTOP,BuyPositionSize,BuyPrice,5,LowerBB,0,NULL,0,0,Blue);
}

//Order Counter
for (int b= OrdersTotal()-1;b>=0;b--)
{
   //We select the order
   if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      //We Check to see if the Symbol Matches
      if (OrderSymbol()==_Symbol)
         //If Order type Matches
         if(OrderType()==OP_BUYSTOP)
        {
             //If the stop loss is below the BBMA
             if (OrderOpenPrice()<BBMA)
                 //We Modify the order
                bool modify= OrderModify(
                                 OrderTicket(),                                     //Current Order
                                 OrderOpenPrice(),                                  //Open Price
                                 iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE,0),  //Stop Loss
                                 OrderTakeProfit(),                                 //Take Profit
                                 0,                                                 //Exp
                                 CLR_NONE                                           //No Color
                                 );
                                 
                                 }
                                 }//End For loop


}

I cant figure this one out.


No Errors or anything on this one but the trailing stop wont move and I cant figure it out.  Any ideas?

Moving Average - Trend Indicators - MetaTrader 5 Help
Moving Average - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
Mikeel1987:

I cant figure this one out.


No Errors or anything on this one but the trailing stop wont move and I cant figure it out.  Any ideas?

Check the modify variable and if it's false, use

Print("Error = ", GetLastError());


to see the error

 
update it was because ordertype was OP_BUYSTOP instead of _OPBUY.  The order in question had been filled and no longer was a pending order.
 
double ShortTermMALine = iMA(_Symbol,_Period,ShortTermMovingAverage,0,MODE_SMA,PRICE_CLOSE,1);
double MediumTermMALine = iMA(_Symbol,_Period,MediumTermMovingAverage,0,MODE_SMA,PRICE_CLOSE,1);

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Reason: