Trying to wite a MQL4 code

 

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 

 
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.
 
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

 
//+------------------------------------------------------------------+
//|                                            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;
}
 
as it is now it only opens buy positions, cant figure out why
 
//+------------------------------------------------------------------+
//|                                            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);
}

}
}
 

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.... 


 
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>"
 
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);
      }
 

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.

 
Your last posed coded returns 0/1 for the highest positioned order in history. Do not assume history is sorted.
Reason: