Targets based upon order open price (conflict issue)

 

I've pasted the code below of what I think should do the job, but am not convinced I've written it properly (Just looking for someone to tell me if I am doing this wrong).

Basically I am wanting the OP_BUY / OP_SELL to close the order at a pre-defined price target which is based off of OrderOpenPrice and OrderTakeProfit (Just a little basic formula to define the target.)

However, where it is going wrong is the fact that it does not know what the correct market is, in terms of the corresponding OrderOpenPrice and OrderTakeProfit... It could be the EURUSD (correct market) but instead its pulling the USDJPY price and messing it up.

Just looking for someone to confirm to me its because I need to actually select the correct order first?

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 & 1:2 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   static datetime partclosedonce;
   static datetime partclosedtwice;
   static datetime partclosedthird;
   
   double minLot = MarketInfo(Symbol(),MODE_MINLOT);   
   double lotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
   double half_1st = MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
   double half_2nd = MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
   double Target_2 = MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/6); //<< Point of interest        

   double TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/3); //<< Point of interest.            
 
   int PositionIndex;    
   int TotalNumberOfOrders;  
   TotalNumberOfOrders = OrdersTotal();    
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
      if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES))continue;
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         
          if(OrderOpenTime() != partclosedonce)  
           if(OrderType()==OP_BUY && Bid >= FirstTarget_Buy+(Point/2) && OrderLots()>minLot) 
              {
              bool Close_Half_Order_Buy = OrderClose(OrderTicket(),half_1st,Bid,5,Blue);
              }
               
          if(Close_Half_Order_Buy==True && OrderOpenPrice() > OrderStopLoss())
              {
              MoveToBreakEven(); 
              }
              if(Close_Half_Order_Buy==True)
                 {
                 partclosedonce = OrderOpenTime();
                 } 
      
          if(partclosedonce != partclosedtwice && OrderOpenTime() != partclosedtwice)
           if(Bid - OrderOpenPrice() > Point / 2. 
            && OrderType()==OP_BUY && EMA_Bar < EMA_MA && OrderLots()>minLot)
              {
              bool EMA_Buy_Close=OrderClose(OrderTicket(),half_2nd,Bid,5,CLR_NONE);
              if(EMA_Buy_Close==True)partclosedtwice = OrderOpenTime();
              }     
                 
          if(OrderOpenTime() != partclosedthird)
           if(OrderType()==OP_BUY && Bid >= TwoRatio_Buy+(Point/2) && OrderLots()>minLot)
              {
              bool Two_Ratio_Buy=OrderClose(OrderTicket(),Target_2,Bid,5,CLR_NONE);
              if(Two_Ratio_Buy==True)partclosedthird = OrderOpenTime();
              }     
        }
     }
}
 

Would this be the correct way of writing it?

double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/6); << these two lines... 
double TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/3);  << this two lines... 

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   static datetime partclosedonce;
   static datetime partclosedtwice;
   static datetime partclosedthird;
   
   int PositionIndex;    
   int TotalNumberOfOrders;   
   TotalNumberOfOrders = OrdersTotal();   
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     bool CurrentOrder = OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES);
      if(  ! CurrentOrder ) continue;  
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         if( CurrentOrder == True)  //< If the OrderSelect returned true, then find the answer to the formula' below?
            {
               double minLot=MarketInfo(Symbol(),MODE_MINLOT);
               double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
               double half_1st=MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
               double half_2nd=MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
               double Target_2 =MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;

   
               double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
               double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
               double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/6);  

               double TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/3);     

            }
            
            
          if(OrderOpenTime() != partclosedonce)  
           if(OrderType()==OP_BUY && Bid >= FirstTarget_Buy+(Point/2) && OrderLots()>minLot) 
              {
              bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half_1st,Bid,5,Blue);
              }
               
          if(Close_Half_Order_Buy==True && OrderOpenPrice() > OrderStopLoss())
              {
              MoveToBreakEven(); 
              }
              if(Close_Half_Order_Buy==True)
                 {
                 partclosedonce = OrderOpenTime();
                 } 
      
          if(partclosedonce != partclosedtwice && OrderOpenTime() != partclosedtwice)
           if(Bid - OrderOpenPrice() > Point / 2. 
            && OrderType()==OP_BUY && EMA_Bar < EMA_MA && OrderLots()>minLot)
              {
              bool EMA_Buy_Close=OrderClose(OrderTicket(),half_2nd,Bid,5,CLR_NONE);
              if(EMA_Buy_Close==True)partclosedtwice = OrderOpenTime();
              }     
                 
          if(OrderOpenTime() != partclosedthird)
           if(OrderType()==OP_BUY && Bid >= TwoRatio_Buy+(Point/2) && OrderLots()>minLot)
              {
              bool Two_Ratio_Buy=OrderClose(OrderTicket(),Target_2,Bid,5,CLR_NONE);
              if(Two_Ratio_Buy==True)partclosedthird = OrderOpenTime();
              }     
        }
     }
}
 

You could easily put your comments in your code on a different line and then it could be read easily without scrolling left and right.

Make it easier to read and I will read it.

 
GumRai:

You could easily put your comments in your code on a different line and then it could be read easily without scrolling left and right.

Make it easier to read and I will read it.


Do you mean removing the prints, yea?
 
DomGilberto:

Do you mean removing the prints, yea?


I mean the stuff that you put after // at the end of a line of code

Important to have as they explain things, The problem is that it makes the SRC so wide that it is difficult to read and then your post, unless it has a paragraph break is also difficult to read. Because one has to keep scrolling left and right to read it. I know that it is not your fault, it is the way that the forum is set up. It just really bugs me.

 
Yea no worries, I'll replace those notes now and make it a little more compact with removing the prints (after-all they're not important for what it is I am asking anyway)
 
bump
 

What are the principle differences between the code you posted above and the code you posted in Closing out half lots at page 19 ? It was my understanding based on your previous thread that your CloseHalfOrder() function worked the way you wanted:

"Just thought it would only be fair to update this thread, as a lot of people helped me understand how to get this down and working! This is how I have it working sweet as a nut and closing at the right times. FirstTarget first, if that is true and has successfully closed, then the EMA close, and then the TwoRatio_Buy target..."

 
Thirteen:

What are the principle differences between the code you posted above and the code you posted in Closing out half lots at page 19 ? It was my understanding based on your previous thread that your CloseHalfOrder() function worked the way you wanted:

"Just thought it would only be fair to update this thread, as a lot of people helped me understand how to get this down and working! This is how I have it working sweet as a nut and closing at the right times. FirstTarget first, if that is true and has successfully closed, then the EMA close, and then the TwoRatio_Buy target..."



Well, that works if you're assuming your EA is just running on one pair... Where it won't necessarily accurately work, is when you're using multiple pairs because of the formula's below (relative to the fact that you need to select the corresponding open order to obtain which OrderOpenPrice and OrderTakeProfit prices are...) - Basically, on page 19 you can see where the two formula's are (the two below), within that code. No order is being selected and I was looking to get some confirmation that the code above ^ (the second one I posted) is the correct way to do this?

double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/6);
double TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/3); 
 
Anyone able to confirm I am doing this right between the two codes I've pasted?
 

Your code will not work within a multi_currency expert advisor. You'll need to create a global variable and/or functions to allow working on multi_currency. Here's how I do it.

string      CurSetSymbol="";
extern int  Take_Profit =20;



void start(){
    CurSetSymbol=SymbolsArray[i];
    OrderSend(Op_Buy,..., Aski() + i2p(Take_Profit) .....);

}

double i2p(double X){
// Converts Integer Pips Into Double Pips
    if(StringFind(CurSetSymbol,"JPY")>-1){
        return(X*0.01);    // Jpy_Pip
        else
        return(X*0.0001);  // Others_Pip
    }
}
Code just example, incomplete and may not compile.
Reason: