How do I set "TakeProfit" at death cross?? HELP!!!

 

Hi, I am new to MQL4 coding, and I have a question.

I am coding an expert advisor which sends an Entry order at Golden Cross of Moving Average lines.

I also want to close order when the Moving Average lines cross at Death Cross.

However, when I backtest this EA, I can only send the orders and these orders never close at the death cross.

How do I code in a way that allows me to close my orders at Death Cross of Moving Averages??

My source code bellow:


//+------------------------------------------------------------------+
//|                                                   パーフェクトオーダー.mq4 |
//|                                                             Real |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Real"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include  <本番.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

  input int shortMAperiod = 20;
  input int midMAperiod = 60;
   
   int Ticket = 0;



int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---



double short_ma_before;
double mid_ma_before;

double short_ma_now;
double mid_ma_now;

   
   int ticket;
   int order_select;
   bool order_close;


   //Previous20PeriodMA
   short_ma_before = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,1);   
   //Previous60PeriodMA
   mid_ma_before = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,1);

   //20PeriodMA
   short_ma_now = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,0);
   //60PeriodMA
   mid_ma_now = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,0);



if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
   {
      Ticket = OrderSend(Symbol(),OP_BUY,0.1, Ask,0,0,0);    
      if (short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)
      {
         OrderClose(Ticket,0.1,Bid,10,clrBlue); //TakeProfit@DeathCross
      }
   }
else if(short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)//ShortEntry@GoldenCross
   {
      Ticket = OrderSend(Symbol(),OP_SELL,0.1, Bid,0,0,0); 
      if (short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)
      {
         OrderClose(Ticket,0.1,Bid,10,clrBlue); //TakeProfit@DeathCross
      }
   }
     

}
   
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Dozens of new automated trading applications appear in the MQL5 Market every day. Choose the right app among 10,000 products and forget about unnecessary routine operations of manual trading. Sell your algorithmic trading programs through the largest store of trading applications! One Click Close The script allows users to easily close...
 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 

several things wrong with your sytax there mate. not simple to answer in a short comment. But 1) you need to separate the 4 lines below your lines starting with Ticket = OrderSend, and put them outside the brackets, and add condition

if(Ticket > 0)

... if(short_ma.

sen124be:

Hi, I am new to MQL4 coding, and I have a question.

I am coding an expert advisor which sends an Entry order at Golden Cross of Moving Average lines.

I also want to close order when the Moving Average lines cross at Death Cross.

However, when I backtest this EA, I can only send the orders and these orders never close at the death cross.

How do I code in a way that allows me to close my orders at Death Cross of Moving Averages??

My source code bellow:


try this...

//+------------------------------------------------------------------+
//|                                                   パーフェクトオーダー.mq4 |
//|                                                             Real |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Real"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include  <本番.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

  input int shortMAperiod = 20;
  input int midMAperiod = 60;
   
   int Ticket = 0;



int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---



double short_ma_before;
double mid_ma_before;

double short_ma_now;
double mid_ma_now;

   
   static uint Ticket;
   int order_select;
   bool order_close;


   //Previous20PeriodMA
   short_ma_before = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,1);   
   //Previous60PeriodMA
   mid_ma_before = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,1);

   //20PeriodMA
   short_ma_now = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,0);
   //60PeriodMA
   mid_ma_now = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,0);


if(OrdersTotal() <=0)
{
if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
   {
      Ticket = OrderSend(Symbol(),OP_BUY,0.1, Ask,0,0,0);    

   }
else if(short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)//ShortEntry@GoldenCross
   {
      Ticket = OrderSend(Symbol(),OP_SELL,0.1, Bid,0,0,0); 
   }
}
     

if(Ticket > 0)
{
      if (short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)
      {
         order_close = OrderClose(Ticket,0.1,OrderClosePrice(),10,clrBlue); //TakeProfit@DeathCross
      }
else      
if (short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)
      {
         order_close = OrderClose(Ticket,0.1,OrderClosePrice(),10,clrBlue); //TakeProfit@DeathCross
      }
if(order_close)
	Ticket = 0;
}
 
Revo Trades:

several things wrong with your sytax there mate. not simple to answer in a short comment. But 1) you need to separate the 4 lines below your lines starting with Ticket = OrderSend, and put them outside the brackets, and add condition

if(Ticket > 0)

... if(short_ma.

try this...

Thank you for trying to help me out!

I tried your code, however, it still didnt work...

The orders did close only 1 and a half year after the orders were sent.

It did not close orders right when the first death cross occurred after the entry order...

I totally have no clue why this is happening, could you help me out here??

 
if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
   {
      Ticket = OrderSend(Symbol(),OP_BUY,0.1, Ask,0,0,0);    
      if (short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)      //This will never be executed
      {
         OrderClose(Ticket,0.1,Bid,10,clrBlue); //TakeProfit@DeathCross
      }
   }
if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
   {
      //Anything in this block of code will only be executed if the above condition is true
   }
if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
   {
      if (short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)    //If the above condition is true, this condition cannot be true.
   }
 
Revo Trades:

several things wrong with your sytax there mate. not simple to answer in a short comment. But 1) you need to separate the 4 lines below your lines starting with Ticket = OrderSend, and put them outside the brackets, and add condition

if(Ticket > 0)

... if(short_ma.

try this...

it seems like Im getting

"OrderClose error 138"...

 I so stuck with this problem..

 
Keith Watford:

Hi, Keith thank you for the help.

I have tried Revo's code, but it is still not working...


 
sen124be:

Hi, Keith thank you for the help.

I have tried Revo's code, but it is still not working...

My post had nothing to do with Revo's code.

My post was pointing out the error in your code.

You need to check and understand what I have said about your code. Then the next step will be for you to re-code and post your efforts.

If you don't understand why your code is not working, you will not learn.

I could re-code and post working code, but what will you learn from that? 

Nothing. You will keep on making the same mistakes.

Revo's code is an improvement on yours, but it still has mistakes. You cannot use OrderClosePrice() without selecting an order first.

 
Keith Watford:

My post had nothing to do with Revo's code.

My post was pointing out the error in your code.

You need to check and understand what I have said about your code. Then the next step will be for you to re-code and post your efforts.

If you don't understand why your code is not working, you will not learn.

I could re-code and post working code, but what will you learn from that? 

Nothing. You will keep on making the same mistakes.

Revo's code is an improvement on yours, but it still has mistakes. You cannot use OrderClosePrice() without selecting an order first.

So, after tons of researches and Try & Errors, I ended up with this code. I did select my order, and attempted to modify my TakeProfit everytime Death Cross occurs.

While it did work on few orders, some orders did not close at first Death Cross and some others had an Error 130.



//+------------------------------------------------------------------+
//|                                                   パーフェクトオーダー.mq4 |
//|                                                             Real |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Real"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

  input int shortMAperiod = 20;
  input int midMAperiod = 60;


int openOrderID;

int magicNB = 55555;

int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

 double short_ma_before;
double mid_ma_before;

double short_ma_now;
double mid_ma_now;

   




   //Previous20PeriodMA
   short_ma_before = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,1);   
   //Previous60PeriodMA
   mid_ma_before = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,1);

   //20PeriodMA
   short_ma_now = iMA(NULL,0,shortMAperiod,0,MODE_SMA,PRICE_CLOSE,0);
   //60PeriodMA
   mid_ma_now = iMA(NULL,0,midMAperiod,0,MODE_SMA,PRICE_CLOSE,0);
   
   if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders try to enter new position
   {
      if(short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//LongEntry@GoldenCross
      {
         Print("Golden cross, Sending buy order");
         Print("Entry Price = " + Ask);
      
         openOrderID = OrderSend(NULL,OP_BUY,0.1,Ask,10,0,0,NULL,magicNB);
         if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
      else if(short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)//ShortEntry@GoldenCross
      {
         Print("Death cross, Sending short order");
         Print("Entry Price = " + Bid);
   
        openOrderID = OrderSend(NULL,OP_SELL,0.1,Bid,10,0,0,NULL,magicNB);
          if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
   }
   
     else //else if you already have a position, update orders if need too.
   {
      if(OrderSelect(openOrderID,SELECT_BY_TICKET)==true)
      {
            int orderType = OrderType();// Short = 1, Long = 0

            double optimalTakeProfit;
            
            if(orderType == 0 && short_ma_before>mid_ma_before && short_ma_now<=mid_ma_now)//long position
            {
               optimalTakeProfit = NormalizeDouble(Ask,Digits);
               
            }
            else if (short_ma_before<mid_ma_before && short_ma_now>=mid_ma_now)//if short
            {
               optimalTakeProfit = NormalizeDouble(Bid,Digits);
            }

            double TP = OrderTakeProfit();
            double TPdistance = MathAbs(TP - optimalTakeProfit);
            if(TP != optimalTakeProfit && TPdistance > 0.0001)
            {
               bool Ans = OrderModify(openOrderID,OrderOpenPrice(),OrderStopLoss(),optimalTakeProfit,0);
            
               if (Ans==true)                     
               {
                  Print("Order modified: ",openOrderID);
                  return;                           
               }else
               {
                  Print("Unable to modify order: ",openOrderID);
               }   
            }
         }
      }

}

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


bool CheckIfOpenOrdersByMagicNB(int magicNB) //checks if there is an order by this EA
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNB) 
         {
            return true;
         }  
      }
   }
   return false;
}  
 
sen124be:

Hi, I am new to MQL4 coding, and I have a question.

I am coding an expert advisor which sends an Entry order at Golden Cross of Moving Average lines.

I also want to close order when the Moving Average lines cross at Death Cross.

However, when I backtest this EA, I can only send the orders and these orders never close at the death cross.

How do I code in a way that allows me to close my orders at Death Cross of Moving Averages??

My source code bellow:


youre closing orders by BID, while this is different for buy/sell. bid/ask

Reason: