How to put a stop loss at the moving average price?

 

Hi, I'm strating to code my first eas and I have a question.

I want to put a stop loss at the moving average price (a static stop loss, not tralling stop). I know it's a very basic question, but I've been looking for some tutorials for this and I haven't found anything.


Thanks

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
 

Get the MA. Modify the order.

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

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

 
//+------------------------------------------------------------------+
//|                                                  prueba MA 2.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
datetime dt;


extern int TakeProfit = 50;
extern int StopLoss = 30;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   dt = Time[0];
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
   
   double TakeProfitLevel;
   double StopLossLevel;
   
   TakeProfitLevel = Bid + TakeProfit*Point;
   StopLossLevel = Bid - StopLoss*Point;

   if (isnewbar ()){
      double prev_ma10 = iMA (_Symbol, PERIOD_CURRENT,  50, 0, MODE_SMA, PRICE_CLOSE, 2);
      double prev_ma5 = iMA (_Symbol, PERIOD_CURRENT,  14, 0, MODE_SMA, PRICE_CLOSE, 2);
      double ma10 = iMA (_Symbol, PERIOD_CURRENT,  50, 0, MODE_SMA, PRICE_CLOSE, 1);
      double ma5 = iMA (_Symbol, PERIOD_CURRENT,  14, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      
      double PREV = iMA (_Symbol, PERIOD_CURRENT,  200, 0, MODE_SMA, PRICE_CLOSE, 20);
      
      if (prev_ma10>prev_ma5 && ma10<ma5){
         if (OrdersTotal() !=0) closeexisting();
         
         int buyticket;
         buyticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 10, 0, 0);
         OrderModify(buyticket, 0, PREV*Point, Bid+TakeProfit*Point, 0);
      
      }
      else if (prev_ma10<prev_ma5 && ma10>ma5){
         if (OrdersTotal() !=0) closeexisting();
         
         int sellticket;
         sellticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 10, 0, 0);
         OrderModify(sellticket, 0, Bid-StopLoss*Point, Bid+TakeProfit*Point, 0);
      
      }
      
      
    

   
   }
   
   
  }
//+------------------------------------------------------------------+

void closeexisting(){
   OrderSelect(0, SELECT_BY_POS);
   OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);

}


bool isnewbar () {
   if (Time[0] != dt){
      dt = Time[0];
      return true;
   }
   return false;
}
This is what I got right now
 
  1. Your code
    Documentation
    OrderModify(
    buyticket, 
    0,                   
    PREV*Point,          
    Bid+TakeProfit*Point, 
    0
                         
    );
    bool  OrderModify(
       int        ticket,      // ticket
       double     price,       // price
       double     stoploss,    // stop loss
       double     takeprofit,  // take profit
       datetime   expiration,  // expiration
       color      arrow_color  // color
       );
    You can't modify the open price. Select the order and use OrderOpenPrice. You can not use any Trade Functions until you first select an order.
  2. PREV is a price. Times Point is bogus.
  3. Missing argument; does your code even compile?
  4. Check your return codes for errors, and report them including GLE/LE, your variable values, and the market. That way we would know that at least you are calling your code.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  5.    OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);
    No need for that garbage; just use OrderClosePrice. You can not use any Trade Functions until you first select an order.

  6. OrderSelect(0, SELECT_BY_POS);
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

 
William Roeder:
  1. Your code
    Documentation
    You can't modify the open price. Select the order and use OrderOpenPrice. You can not use any Trade Functions until you first select an order.
  2. PREV is a price. Times Point is bogus.
  3. Missing argument; does your code even compile?
  4. Check your return codes for errors, and report them including GLE/LE, your variable values, and the market. That way we would know that at least you are calling your code.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  5. No need for that garbage; just use OrderClosePrice. You can not use any Trade Functions until you first select an order.

The code is working fine, it does what I need. The only problem is that I don't know how can I add a stop loss at the PREV moving average price.