Partial exit depending on fibanacci levels.

 

This is my code. I want to partial exit on Fibonacci 38.2, 61.8 and 127.2%.

After TP1 partial close, my tp2 ant tp3 values becomes 0 and below if conditions becomes true because Bid> tp2 and tp3. But really Bid is < tp2 and tp3 and he have to wait while second condition would be true.

So, how to get tp2 and tp3 values from Fibonacci, that after first close order function they don't be zero?

Thank you.

if(OrderSymbol() == Symbol() && type==OP_BUY && Bid >= tp2 && OrderLots()==0.02)
//and
 
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp3 && OrderLots()==0.01)
int start()
  {
  double p1,p2,tp1,tp2,tp3;
  string sp2,sp3;
  datetime h = TimeCurrent();
  string FibName = "a";

 //--------------------------fibonacci creates levels
  ObjectSet(FibName,OBJPROP_FIBOLEVELS,10); 
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+0,0.000); 
  ObjectSetFiboDescription(FibName,0,"0 %$");
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+1,0.382);
  ObjectSetFiboDescription(FibName,1,"38.2 %$");
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+2,0.5); 
  ObjectSetFiboDescription(FibName,2,"50 %$");
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+3,0.618); 
  ObjectSetFiboDescription(FibName,3,"61.8 %$");
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+4,0.786); 
  ObjectSetFiboDescription(FibName,4,"78.6 %$");
  ObjectSet(FibName,OBJPROP_FIRSTLEVEL+5,0.886); 
  ObjectSetFiboDescription(FibName,5,"88.6 %$");
    ObjectSet(FibName,OBJPROP_FIRSTLEVEL+6,1); 
  ObjectSetFiboDescription(FibName,6,"100 %$");
    ObjectSet(FibName,OBJPROP_FIRSTLEVEL+7,1.272); 
  ObjectSetFiboDescription(FibName,7,"127.2 %$");
    ObjectSet(FibName,OBJPROP_FIRSTLEVEL+8,1.414); 
  ObjectSetFiboDescription(FibName,8,"141.4 %$");
    ObjectSet(FibName,OBJPROP_FIRSTLEVEL+9,1.618); 
  ObjectSetFiboDescription(FibName,9,"161.8 %$");

//----------------------------- fibonacci actions
 p1=ObjectGet(FibName,OBJPROP_PRICE1);    //100% price
 p1=NormalizeDouble(StrToDouble(sp1),Digits);
  p2=ObjectGet(FibName,OBJPROP_PRICE2);    //0 % price
 sp2=DoubleToStr(p2,5);
 p2=NormalizeDouble(StrToDouble(sp2),Digits);
 tp1=(p2-p1)*0.618+p1;                  //38.2% price
 tp1=NormalizeDouble(tp1,Digits);

 //---------------Close TP1 on 38.2% fibonacci
int total = OrdersTotal(); 
for(int i=total-1;i>=0;i--)   
{  //1   
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1 && OrderLots()==0.03)

{//2

OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 
 p1=ObjectGet(FibName,OBJPROP_PRICE1);    //100% 
 sp1=DoubleToStr(p1,Digits);
 p1=NormalizeDouble(StrToDouble(sp1),Digits);
 p2=ObjectGet(FibName,OBJPROP_PRICE2);    //0 % 
 sp2=DoubleToStr(p2,Digits);
 p2=NormalizeDouble(StrToDouble(sp2),Digits);
 tp2=(p2-p1)*0.382+p1;                  //61.8 -TP2
 tp2=NormalizeDouble(tp2,Digits);            
 tp3=p1-((p2-p1)*0.272);               //127.2% - TP3
 tp3=NormalizeDouble(tp3,Digits);
 
 break;   //after this if condition tp2 and tp3 becomes 0 and other if conditions becomes true and closes my all orders.

   }  //2  
//---------------Close TP2 on 61.8% fibonacci
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid >= tp2 && OrderLots()==0.02)
       
{//4
 p1=ObjectGet(FibName,OBJPROP_PRICE1);    //100% 
 sp1=DoubleToStr(p1,Digits);
 p1=NormalizeDouble(StrToDouble(sp1),Digits);
 p2=ObjectGet(FibName,OBJPROP_PRICE2);    //0 % 
 sp2=DoubleToStr(p2,Digits);
 p2=NormalizeDouble(StrToDouble(sp2),Digits);
 tp2=(p2-p1)*0.382+p1;                  //61.8 fib 
 tp2=NormalizeDouble(tp2,Digits);          
 tp3=p1-((p2-p1)*0.272);               //127.2% 
 tp3=NormalizeDouble(tp3,Digits);

OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 

 break; }  //4 
//---------------Close TP3 on 127.2% fibonacci
 if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp3 && OrderLots()==0.01)
      
{//6
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
  break;
}//6
    }//1
//----
   return(0);
  }
 
Eggo:

This is my code. I want to partial exit on Fibonacci 38.2, 61.8 and 127.2%.

After TP1 partial close, my tp2 ant tp3 values becomes 0 and below if conditions becomes true because Bid> tp2 and tp3. But really Bid is < tp2 and tp3 and he have to wait while second condition would be true.

So, how to get tp2 and tp3 values from Fibonacci, that after first close order function they don't be zero?

Thank you.

You already have a thread about this, please use it: https://www.mql5.com/en/forum/141265