Download MetaTrader 5

Trying to wite a MQL4 code

To add comments, please log in or register
Are you used to trading from your smartphone? Mobile version of MetaTrader 5 will suit you best!
Eduard de Waal
30
Eduard de Waal 2015.05.05 10:55 

Hi

I  am trying to write a expert advisor code that will open a new trade in the same direction as the previous if it was profitable and change direction if it was not.

Any ideas on how to do this?

 Thanks 

whroeder1
14504
whroeder1 2015.05.05 12:03  
Eddiedewaal: Any ideas on how to do this?
  1. Find the last trade in history, check the profit, choose your direction, open an order.
  2. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
Eduard de Waal
30
Eduard de Waal 2015.05.05 13:41  
WHRoeder:
Eddiedewaal: Any ideas on how to do this?
  1. Find the last trade in history, check the profit, choose your direction, open an order.
  2. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.

I dont expect you to code it for me. Ill attach what I have so far. IMy idea was along the same lines aswell. Just cant seem to get it right

Eduard de Waal
30
Eduard de Waal 2015.05.05 14:02  
//+------------------------------------------------------------------+
//|                                            open trade test 2.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

extern int TakeProfit = 100;
extern int StopLoss = 100;
extern int direction = 1;


//+------------------------------------------------------------------+
//| Open first trade                                                 |
//+------------------------------------------------------------------+
void OnInit()
  {
//---
      double TakeProfitLevelbuy;
      double StopLossLevelbuy;
      double TakeProfitLevelsell;
      double StopLossLevelsell;
      
     //----------------------------------------------------
      
      TakeProfitLevelbuy = Bid + TakeProfit*Point;
      StopLossLevelbuy = Ask - StopLoss*Point;
      
      TakeProfitLevelsell = Ask - TakeProfit*Point;
      StopLossLevelsell = Bid + StopLoss*Point;
      
      
      if (direction == 0)
      {
      OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Sell Trade", 11111,Blue);
      }
      else if (direction == 1)
      {
      OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue);
      }
    
      //--------------------------------------------------------------
   
  }
 

//__________________________________________________________________________________________________________________________________________    
//+------------------------------------------------------------------+
//|Open next trade                                                   |
//+------------------------------------------------------------------+
   void OnTick()
{
      double direction1;
      double TakeProfitLevelbuy;
      double StopLossLevelbuy;
      double TakeProfitLevelsell;
      double StopLossLevelsell;
      
      TakeProfitLevelbuy = Bid + TakeProfit*Point;
      StopLossLevelbuy = Ask - StopLoss*Point;
      
      TakeProfitLevelsell = Ask - TakeProfit*Point;
      StopLossLevelsell = Bid + StopLoss*Point;
      
    
       

if (AccountMargin() > 0)
{
return;
}

if (OrderType()== 0 && OrderProfit() > 0)
{
direction1 = 0;
}
else if(OrderType()==0 && OrderProfit() < 0)
{
direction1 = 1;
}
 else if (OrderType()== 1 && OrderProfit() > 0)
{
direction1 = 1;
}
else if(OrderType()==1 && OrderProfit() < 0)
{
direction1 = 0;
}
  
 //--------------------------------------------------------------------     


if (direction1 == 0)
{
{
OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue);
}
}
else if(direction1 == 1)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue);
}


if (direction1 == 0)
{
{
Alert("direction1 = 0");
}
}
else if(direction1 == 1)
{
Alert("direction1 = 1");
}

return;
}
Eduard de Waal
30
Eduard de Waal 2015.05.05 14:08  
as it is now it only opens buy positions, cant figure out why
Eduard de Waal
30
Eduard de Waal 2015.05.05 15:33  
//+------------------------------------------------------------------+
//|                                            open trade test 2.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

extern int TakeProfit = 100;
extern int StopLoss = 100;
extern int direction = 1;


//+------------------------------------------------------------------+
//| Open first trade                                                 |
//+------------------------------------------------------------------+
void OnInit()
  {
//---
      double TakeProfitLevelbuy;
      double StopLossLevelbuy;
      double TakeProfitLevelsell;
      double StopLossLevelsell;
      
     //----------------------------------------------------
      
      TakeProfitLevelbuy = Bid + TakeProfit*Point;
      StopLossLevelbuy = Ask - StopLoss*Point;
      
      TakeProfitLevelsell = Ask - TakeProfit*Point;
      StopLossLevelsell = Bid + StopLoss*Point;
      
      
      if (direction == 0)
      {
      OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Sell Trade", 11111,Blue);
      }
      else if (direction == 1)
      {
      OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue);
      }
    
      //--------------------------------------------------------------
   
  }
 

//__________________________________________________________________________________________________________________________________________    
//+------------------------------------------------------------------+
//|Open next trade                                                   |
//+------------------------------------------------------------------+
   bool OnTick()
{
      double direction1;
      double TakeProfitLevelbuy;
      double StopLossLevelbuy;
      double TakeProfitLevelsell;
      double StopLossLevelsell;
      
      TakeProfitLevelbuy = Bid + TakeProfit*Point;
      StopLossLevelbuy = Ask - StopLoss*Point;
      
      TakeProfitLevelsell = Ask - TakeProfit*Point;
      StopLossLevelsell = Bid + StopLoss*Point;
      
    
       

if (AccountMargin() > 0)
{
return;
}

bool getlastorder()
{

for(int i=OrdersTotal(); i<0; i--)
{

OrderSelect(i,SELECT_BY_POS,MODE_HISTORY );

if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()<0)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue);
}

else if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()>0)
{
OrderSend(Symbol(),OP_BUY,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue);
}

if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()<0)
{
OrderSend(Symbol(),OP_BUY,0.1,Bid,10,StopLossLevelsell,TakeProfitLevelsell,"Sell Trade", 11111,Blue);
}

else if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()>0)
{
OrderSend(OrderType()==OP_SELL,0.1,Ask,10,StopLossLevelbuy,TakeProfitLevelbuy,"Buy Trade", 11111,Blue);
}

}
}
Eduard de Waal
30
Eduard de Waal 2015.05.05 15:35  

I have tried it another way but now i get erros

  

'}' - unexpected end of program

 '{' - unbalanced parentheses

return value of 'OrderSend' should be checked

 

not sure why.... 


whroeder1
14504
whroeder1 2015.05.05 17:56  
Eddiedewaal: I dont expect you to code it for me. Ill attach what I have so far. IMy idea was along the same lines aswell. Just cant seem to get it right
  1. Where is you code that "Find the last trade in history?
  2. Where is your code that  "check the profit?"
  3. Where is your code that "choose your direction>"
Eduard de Waal
30
Eduard de Waal 2015.05.06 10:56  
for(int i=OrdersTotal(); i<0; --i)
{
   OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

   if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()>0)   
      {
      return(1);
      }
   if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderProfit()<0)
      {
      return(0);
      }  
   if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()>0)
      {
      return(0);
      }  
   if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderProfit()<0)
      {
      return(1);
      }
Eduard de Waal
30
Eduard de Waal 2015.05.06 11:00  

ok I will be more specific.

 My problem is with this part. I need  it to give a 0 value if  

 last trade was a buy order and made loss or if last trade was a sell trade and made profit. 

or 1 value if 

last trade was a buy order and made profit or if last trade was a sell trade and made a loss.

whroeder1
14504
whroeder1 2015.05.06 12:09  
Your last posed coded returns 0/1 for the highest positioned order in history. Do not assume history is sorted.
12
To add comments, please log in or register