Non-static Take Profit

 

Hi,

I would like to know if the function OrderSend accepts inside the field "Take Profit" a variable value.


For example: I want to close a buy order if price is greater than a moving average. I did like this:

I read that in an EA the function IndicatorCounted() is not allowed but I must do a lopping to recalculate the new take profit. So, I think that is my problem.


extern double Lots = 0.1;
extern double TrailingStop = 30;

//---- Expert Options ----//

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


int start()
  {
   double Compra, Venda, TakeProfit_Buy[],TakeProfit_Sell[];
   int total, ticket, cnt;
   
//---- initial data checks----//

if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   
//----Indicator Reference----//

Compra=iCustom(Symbol(),0," DivBands",1,0);
Venda=iCustom(Symbol(),0," DivBands",0,0);

total=OrdersTotal();

//----+ INSERTION OF A STATIC INTEGER MEMORY VARIABLE
   static int IndCounted;
//----+ Insertion of variables with a floating point
   double Resalt0, Resalt1, Resalt2;
//----+ Insertion of integer variables and getting calculated bars
   int limit, MaxBar, bar, counted_bars = IndCounted;
//---- checking for possible errors
   if(counted_bars < 0)
       return(-1);
//---- the last calculated bar must be recalculated 
   if(counted_bars > 0) 
       counted_bars--;
//----+ REMEMBERING THE AMOUNT OF ALL BARS OF THE CHART
   IndCounted = Bars - 1;
//---- defining the number of the oldest bar, 
//     starting from which new bars will be recalculated
   limit = Bars - counted_bars - 1; 
   
//---- Checking for an exit ----//

   for(int i=limit-1;i>=0;i--) // Count down for one pass
      TakeProfit_Buy[i]=(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point)-(Ask*Point);   
      TakeProfit_Sell[i]=(Bid*Point)-(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point); 

//---- Checking for orders ----//

if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
//---- check for long position (BUY) possibility ----//
        if(Compra==-1)
        {
         for(i=limit-1;i>=0;i--) // Count down for one pass
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,TakeProfit_Buy[i],"Rodrigo Especulação",0001,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
//---- check for short position (SELL) possibility ----//
       if(Venda==1)
         {
         for(i=limit-1;i>=0;i--) // Count down for one pass
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,TakeProfit_Sell[i],"Rodrigo Especulação",0002,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
//---- Check for an exit ----//
         for(cnt=0;cnt<total;cnt++)
         {
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
            if(OrderType()<=OP_SELL &&   // check for opened position 
               OrderSymbol()==Symbol())  // check for symbol
            {
               if(OrderType()==OP_BUY)   // long position is opened
               {
               // should it be closed?
                  if(Bid==TakeProfit_Buy[i])
                  {
                  OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                  return(0); // exit
                  }
               }   
            }
         }   
//---- 
      }
//---- 

return(0);      
   }
   
// the end

//+------------------------------------------------------------------+ 
 

y * Point

      TakeProfit_Buy[i]=(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point)-(Ask*Point);   
      TakeProfit_Sell[i]=(Bid*Point)-(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point); 


Besides

u don't need to loop + using array

jost simple

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0),"Rodrigo Especulação",0002,0,Red);
 
qjol:

y * Point

also

y not


qjol, sorry, but I didn't understand what you are trying to say.
 

i said why you using by this lines *Point.

      TakeProfit_Buy[i]=(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point)-(Ask*Point);   
      TakeProfit_Sell[i]=(Bid*Point)-(iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i)*Point); 

and why looping and use an array at all you can simple use OrderSend() like this

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0),"Rodrigo Especulação",0002,0,Red);
 

Great, you are absolutely right.

I'm beginner, I'm not a programmer. So I have a lot of questions about programming, and it results in bugs that I don´t know how to solve. But, fortunately, some guys like you has been helping me to learn faster.


Thanks again.

 
qjol:

i said why you using by this lines *Point.

and why looping and use an array at all you can simple use OrderSend() like this


There is a problem.... It isn't doing the looping. The take profit is static at the point where were my moving average when the buy or sell event happened.


I need a recalculation for the iMA value.

 
rodrigosm:

There is a problem.... It isn't doing the looping. The take profit is static at the point where were my moving average when the buy or sell event happened.


I need a recalculation for the iMA value.

You want to move your TP when MA changes value? In such case you need to use OrderModify function.

 

I still with this problem.... I changed this part of code, but the take profit still static.


//---- Check for an exit ----//
         for(cnt=0;cnt<total;cnt++)
         {
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
            if(OrderType()<=OP_SELL &&   // check for opened position 
               OrderSymbol()==Symbol())  // check for symbol
            {
               if(OrderType()==OP_BUY)   // long position is opened
               {
               // should it be closed?
                  if(Bid>=TakeProfit)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,TakeProfit,0,Red); // new close position which recalculate the TakeProfit based on MA 
                  //OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position ---- I took out this part
                  return(0); // exit
                  }
               }   
            }
         }   
 
//---- Check for an exit ----//
void CheckForClose()
   {
   double TakeProfit = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0);
   //-----
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
      OrderSymbol()==Symbol())  // check for symbol
         {
         if(OrderType()==OP_BUY)   // long position is opened
            {
         // should it be closed?
            if(Bid >= TakeProfit)
               {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position ---- I took out this part
               }
            }   
         if(OrderType()==OP_SELL)   // short position is opened
            {
            if(Ask <= TakeProfit)
               {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position ---- I took out this part
               }
            }
         }
      }
   }

//or u can use like this
void CheckForClose()
   {
   double TakeProfit = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0);
   //-----
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
      OrderSymbol()==Symbol())  // check for symbol
         {
         OrderModify(OrderTicket(),OrderOpenPrice(),0,TakeProfit,0,Red); // new close position which recalculate the TakeProfit based on MA 
         }
      }
   }
didn't checked i hope it's what u want
 

Thanks again for your help. But, there is one error message - '(' - function definition unexpected. I only replace this part of code.

One more question. Do You usually type the code, or only copy some code and make changes on it?

 
rodrigosm:

Thanks again for your help. But, there is one error message - '(' - function definition unexpected. I only replace this part of code.

One more question. Do You usually type the code, or only copy some code and make changes on it?


1) You should put this at the end of the EA after the last parentheses and u should call the function

2) I took your code & i made some changes

edit: like this

int start()
  {
//----
   your code
   ........
   
   CheckForClose();

//----
   return(0);
  }
//+------------------------------------------------------------------+

void CheckForClose()
   {
   double TakeProfit = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0);
   //-----
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
      OrderSymbol()==Symbol())  // check for symbol
         {
         OrderModify(OrderTicket(),OrderOpenPrice(),0,TakeProfit,0,Red); // new close position which recalculate the TakeProfit based on MA 
         }
      }
   }

//or the other function

void CheckForClose()
   {
   double TakeProfit = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,0);
   //-----
   for(int cnt=0; cnt < OrdersTotal(); cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
      OrderSymbol()==Symbol())  // check for symbol
         {
         if(OrderType()==OP_BUY)   // long position is opened
            {
         // should it be closed?
            if(Bid >= TakeProfit)
               {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position ---- I took out this part
               }
            }   
         if(OrderType()==OP_SELL)   // short position is opened
            {
            if(Ask <= TakeProfit)
               {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position ---- I took out this part
               }
            }
         }
      }
   }
Reason: