Download MetaTrader 5
To add comments, please log in or register
Have you discovered something really important? Write an article and receive 200 USD!
AussieBattler
103
AussieBattler 2014.07.09 12:44 

Can anybody suggest why the following function would randomly delete TakeProfit levels when modifying the TrailingStop.

TakeProfit, TrailingStop and TrailingStep are all controlled by this function. This function also controls these levels for two different strategies running within the EA. Both strategies have thier own block within this function and each block is then divided into a sub-block which sets the Initial StopLoss/TakeProfit, and another which adjusts the StopLoss (TrailingStop).

The problem: this function can sucessfully handle these roles for each strategy independantly. When both strategies are activated, then the TakeProfit is 'randomly' deleted in the process of setting the TrailingStop

Chhers


//---Local variables
double maOldSL,maNewSL,maNewSLf,ctOldSL,ctNewSL,ctNewSLf,maSL,maTP,
maSLTPticket,maOpen,MAcalcSL,MAcalcTP,ctSL,ctTP,ctSLTPticket,ctOpen,CTcalcSL,CTcalcTP;
int maTYPE,ctTYPE;


//---MAslope SL/TP set and TrailStop function
void maSetSTOPS(){

maSL=0;maTP=0;maSLTPticket=0;maOldSL=0;maNewSL=0;maNewSLf=0;maTYPE=0;                    
//Empty all values ready for next iteration

if(MAslopeCnt>=1){

RefreshRates();
double maLimit=MarketInfo(Symbol(),MODE_STOPLEVEL);                                      
//Current stop level for current Symbol
double maFreeze=MarketInfo(Symbol(),MODE_FREEZELEVEL);                                   
//Current freeze level for current symbol

maTYPE=MAslAr_New[1][5];
if(maTYPE==0){
      STOPLOSS=StopLoss_1;
      TAKEPROFIT=TakeProfit_1;
      TRAILINGSTEP=TrailingStep_1;
      TRAILINGSTOP=TrailingStop_1;
      TSPRICE=Bid;
      SLF=SLfactor_1;
      maSLTPticket=MAslAr_New[1][1];
      maSL=MAslAr_New[1][7];
      maTP=MAslAr_New[1][8];
      maOpen=MAslAr_New[1][3];
      MAcalcSL=maOpen-STOPLOSS;
      MAcalcTP=maOpen+TAKEPROFIT;}
     
if(maTYPE==1){
      STOPLOSS=-StopLoss_2;
      TAKEPROFIT=-TakeProfit_2;
      TRAILINGSTEP=-TrailingStep_2;
      TRAILINGSTOP=-TrailingStop_2;
      TSPRICE=Ask;
      SLF=SLfactor_2;
      maSLTPticket=MAslAr_New[1][1];
      maSL=MAslAr_New[1][7];
      maTP=MAslAr_New[1][8];
      maOpen=MAslAr_New[1][3];
      MAcalcSL=maOpen-STOPLOSS;
      MAcalcTP=maOpen+TAKEPROFIT;}

if(maTYPE<=1){      
while(maSL<=0 && maTP<=0){          
maSL=MAslAr_New[1][7];
maTP=MAslAr_New[1][8];
if(maSL<=0 && maTP<=0){
if(STOPLOSS!=0 && TAKEPROFIT!=0){
if((MathAbs(TSPRICE-STOPLOSS)>maFreeze)&&                                      
//Distance between new SL and price is greater than FreezeLevel 
   (MathAbs(TSPRICE-STOPLOSS)>maLimit)){                                       
//Distance between new SL and price is greater than StopLevel
if(orderModifyReliable(maSLTPticket,OrderOpenPrice(),MAcalcSL,MAcalcTP,0,Blue))
    {Print("MAslope TakeProfit & StopLoss Set; TakeProfit=",TAKEPROFIT/Point,
                     ", StopLoss=",STOPLOSS/Point," for order #",maSLTPticket);}
    else
    {Print("Could not set SL/TP for MAslope #",maSLTPticket," @ SL ",MAcalcSL," on ",
                     TimeToStr(Time[0],TIME_DATE|TIME_MINUTES)," due to ERROR:",GetLastError());}
    
   MAslOrderArrays();
   MAslTracking();       
  }//end of freeze/stop levels check
  }//end of TP/SL allowed
  }//end of SL/TP=0

//else

//---TrailingStop Block    
if(maSL!=0 && maTP!=0){    
if(TRAILINGSTEP!=0 && TRAILINGSTOP!=0)
   maOldSL=OrderStopLoss();
   maNewSL=TSPRICE-TRAILINGSTOP;
   maNewSLf=NormalizeDouble(maNewSL/SLF,6);{
if((maNewSLf/TRAILINGSTOP)>(maOldSL/TRAILINGSTOP))                    
//nEw  SL is in better place than old SL
if(MathAbs(maNewSLf-maOldSL)>MathAbs(TRAILINGSTEP)){                         
//distance between nEw SL and old SL is greater than step distance 
if(MathAbs(TSPRICE-maNewSLf)>maFreeze)                                       
//Distance between new SL and price is greater than FreezeLevel 
if(MathAbs(TSPRICE-maNewSLf)>maLimit){                                       
//Distance between new SL and price is greater than StopLevel
if(orderModifyReliable(maSLTPticket,OrderOpenPrice(),maNewSLf,OrderTakeProfit(),0,Cyan)) 
   {Print("MAslope TrailingStop Activated/Reset; TrailingStop=",TRAILINGSTOP/Point,
   ", TrailingStep=",TRAILINGSTEP/Point," ,NewSLf=",DoubleToStr(maNewSLf,5)," for order #",maSLTPticket);
    maOldSL=maNewSLf;}
   else
   {Print("Could not Activate/Reset MAslope SL for",maSLTPticket," @ ",maNewSLf,
   " on ",TimeToStr(Time[0],TIME_DATE|TIME_MINUTES)," due to ERROR:",GetLastError());}
   
   MAslOrderArrays();
   MAslTracking();  
   }  //end of check against market levels
   }  //end of check against old SL and step size
   }  //end of NewSL calculation and variable declarations
   }  //end of SL/TP!=0
   }  //end of order type filter
   }  //end of counter holding orders
return;                                  
}  //end of function  


//---CounterTrend SL/TP set and TrailStop function
void ctSetSTOPS(){

ctSL=0;ctTP=0;ctSLTPticket=0;ctOldSL=0;ctNewSL=0;ctNewSLf=0;ctTYPE=0;                   
 //Empty all values ready for next iteration

if(CtCnt>=1){

RefreshRates();
double ctLimit=MarketInfo(Symbol(),MODE_STOPLEVEL);                                      
//Current stop level for current Symbol
double ctFreeze=MarketInfo(Symbol(),MODE_FREEZELEVEL);                                   
//Current freeze level for current symbol

ctTYPE=CtAr_New[1][5];
if(ctTYPE==0){
      STOPLOSS=StopLoss_3;
      TAKEPROFIT=TakeProfit_3;
      TRAILINGSTEP=TrailingStep_3;
      TRAILINGSTOP=TrailingStop_3;
      TSPRICE=Bid;
      //SLF=SLfactor_3;
      ctSLTPticket=CtAr_New[1][1];
      ctSL=CtAr_New[1][7];
      ctTP=CtAr_New[1][8];
      ctOpen=CtAr_New[1][3];
      CTcalcSL=ctOpen-STOPLOSS;
      CTcalcTP=ctOpen+TAKEPROFIT;}
     
if(ctTYPE==1){
      STOPLOSS=-StopLoss_4;
      TAKEPROFIT=-TakeProfit_4;
      TRAILINGSTEP=-TrailingStep_4;
      TRAILINGSTOP=-TrailingStop_4;
      TSPRICE=Ask;
      //SLF=SLfactor_4;
      ctSLTPticket=CtAr_New[1][1];
      ctSL=CtAr_New[1][7];
      ctTP=CtAr_New[1][8];
      ctOpen=CtAr_New[1][3];
      CTcalcSL=ctOpen-STOPLOSS;
      CTcalcTP=ctOpen+TAKEPROFIT;}

if(ctTYPE<=1){      
if(ctSL<=0 && ctTP<=0){
if(STOPLOSS!=0 && TAKEPROFIT!=0){
if((MathAbs(TSPRICE-STOPLOSS)>ctFreeze)&&                                      
//Distance between new SL and price is greater than FreezeLevel 
   (MathAbs(TSPRICE-STOPLOSS)>ctLimit)){                                       
//Distance between new SL and price is greater than StopLevel
if(orderModifyReliable(ctSLTPticket,OrderOpenPrice(),CTcalcSL,CTcalcTP,0,Blue))
    {Print("Counter Trend TakeProfit & StopLoss Set; TakeProfit=",TAKEPROFIT/Point,
    ", StopLoss=",STOPLOSS/Point," for order #",ctSLTPticket);}
    else
    {Print("Could not set SL/TP for Counter Trend #",ctSLTPticket," @ SL ",CTcalcSL," on ",
    TimeToStr(Time[0],TIME_DATE|TIME_MINUTES)," due to ERROR:",GetLastError());
     return;} 
   CtOrderArrays();
   CtTracking();       
  }//end of freeze/stop levels check
  }//end of TP/SL allowed
  }//end of SL/TP=0
      
else

//---TrailingStop Block    
if(ctSL!=0 && ctTP!=0){
if(TRAILINGSTEP!=0 && TRAILINGSTOP!=0)
   ctOldSL=OrderStopLoss();
   ctNewSL=TSPRICE-TRAILINGSTOP;{
 //  ctNewSLf=NormalizeDouble(ctNewSL/SLF,6);{
if((ctNewSL/TRAILINGSTOP)>(ctOldSL/TRAILINGSTOP))                    
//nEw  SL is in better place than old SL
if(MathAbs(ctNewSL-ctOldSL)>MathAbs(TRAILINGSTEP)){                         
//distance between nEw SL and old SL is greater than step distance 
if(MathAbs(TSPRICE-ctNewSL)>ctFreeze)                                       
//Distance between new SL and price is greater than FreezeLevel 
if(MathAbs(TSPRICE-ctNewSL)>ctLimit){                                       
//Distance between new SL and price is greater than StopLevel
if(orderModifyReliable(ctSLTPticket,OrderOpenPrice(),ctNewSL,OrderTakeProfit(),0,Cyan)) 
   {Print("Counter Trend TrailingStop Activated/Reset; TrailingStop=",TRAILINGSTOP/Point,", TrailingStep=",
         TRAILINGSTEP/Point," ,NewSL=",DoubleToStr(ctNewSL,5)," for order #",ctSLTPticket);
    ctOldSL=ctNewSL;}
   else
   {Print("Could not Activate/Reset Counter Trend SL for",ctSLTPticket," @ ",ctNewSL," on ",
         TimeToStr(Time[0],TIME_DATE|TIME_MINUTES)," due to ERROR:",GetLastError());
    return;}
   CtOrderArrays();
   CtTracking();  
   }  //end of check against ctrket levels
   }  //end of check against old SL and step size
   }  //end of NewSL calculation and variable declarations
   }  //end of SL/TP!=0
   }  //end of order type filter
   }  //end of counter holding orders
return;                                  
}  //end of function  
whroeder1
13644
whroeder1 2014.07.09 13:06  
SteepCurve:

Can anybody suggest why the following function would randomly delete TakeProfit levels when modifying the TrailingStop.

if(orderModifyReliable(maSLTPticket,OrderOpenPrice(),maNewSLf,OrderTakeProfit(),0,Cyan)) 
Maybe because you are using OrderTakeProfit() without doing an OrderSelect() first.
AussieBattler
103
AussieBattler 2014.07.09 13:17  

Thanks for the reply.

This function was also an a venture into the world of arrays. This is the first time I have used them and maybe I've made some poor assumptions on what they can/can not do.

Both the MAslAr and CtAr arrays should only contain one order at any one time. Both arrays store the information for the trade open in the strategy they represent.

The reason there is no OrderSelect function is because the function first checks if there is an order open for that strategy...

if(MAslopeCnt>=1){

and

if(CtCnt>=1){

and then calls the arrays mentioned above to set a number of local variables which store information about the trade, including OrderTicketNumber().

Am I wrong in thinking that calling/storing the ticket number in this way does not replicate the OrderSelect() function?

whroeder1
13644
whroeder1 2014.07.09 14:59  
SteepCurve: Am I wrong in thinking that calling/storing the ticket number in this way does not replicate the OrderSelect() function?
You're not passing what is in your array to OrderModify, you are passing OrderTakeProfit() which has no value because you didn't select the order first. What is in your arrays is irrelevant to OTP.
Either use what is in your arrays ONLY, or select the order by ticket so you can use OTP.
AussieBattler
103
AussieBattler 2014.07.09 23:30  

"You're not passing what is in your array to OrderModify, you are passing OrderTakeProfit() which has no value because you didn't select the order first. What is in your arrays is irrelevant to OTP."

So even if the orderticket number is stored (in this case in) Array[1][1] and this value is stored in a variable like this

maSLTPticket=MAslAr_New[1][1];

, then calling maSLTPticket in OrderModify() will not select the correct order to act upon? OrderStopLoss is in Arary[1][7] and OrderTakeProfit is in Array[1][8]. Can arrays not be used for this type of operation?

If not, what can order arrays be used for (other than counting orders)?


"Either use what is in your arrays ONLY, ...."

I am a little confused by this. I am not trying to use anything that isn't stored in my array (at least I don't think I am).

whroeder1
13644
whroeder1 2014.07.11 13:22  
SteepCurve: I am a little confused by this. I am not trying to use anything that isn't stored in my array (at least I don't think I am).
if(orderModifyReliable(maSLTPticket,OrderOpenPrice(),maNewSLf,OrderTakeProfit(),0,Cyan))
AussieBattler
103
AussieBattler 2014.07.13 11:30  

I finally understand the meaning of RFTM. And make sure its read properly too. not just looked at and have the information go in one eye ball and out the other.

Sorry for the hassle. What you have been saying is blatantly obvious now. 

An OrderSelect loop on the respective arrays has solved this issue.

Thank you for your patience.

/
To add comments, please log in or register