Download MetaTrader 5

Read out specific value from indicator

To add comments, please log in or register
akuh
56
akuh  

Hi all,

 

For my EA i want to read out a specific value from an indicator. For my case, i want to read out the int "changeofTrend" from the super-trend indicator. So whenever "changeofTrend ==1" occurs or is true, i want to the close the last order! The super-trend indicator i use is in the attachement.

 Here you find my code for closing buy and sell positions:

 

I would be very thankful for a hint or advice ! 

 

if ( OrderType() == OP_BUY ) {
    
         RefreshRates(); // RefreshRate() update Bid and Ask value.
         // Calculate Trailing Stop.
         if ( Bid >= OrderOpenPrice() + TrailingStart * Point && OrderStopLoss() < Bid - ( TrailingStop * Point ) ) {
            Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), Bid - ( TrailingStop * Point ), OrderTakeProfit() , 0 );
         }    
         // Calculate BA.          
         if ( Bid >= OrderOpenPrice() + Ba * Point && Bid < OrderOpenPrice() + ( Ba ) * Point ) {
            Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + ( failsafe * Point ), OrderTakeProfit() , 0 );                
         }
        
         // Replace 1 == 0 to conditions to close Buy order.
         if ( 1==0 ) {
            Ticket = OrderClose ( Ticket, OrderLots(), OrderClosePrice(), Slippage, Green );
         }    
          
      }
  
   if ( OrderType() == OP_SELL ) {
    
         RefreshRates(); // RefreshRate() update Bid and Ask value.
         // Calculate Trailing Stop.
         if ( Ask <= OrderOpenPrice() - TrailingStart * Point && OrderStopLoss() > Ask + ( TrailingStop * Point ) ) {
            Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), Ask + ( TrailingStop * Point ), OrderTakeProfit(), 0 );
         }  
         // Calculate BA.
         if ( Ask <= OrderOpenPrice() - Ba * Point &&  Ask > OrderOpenPrice() - ( Ba  ) * Point  ) {
            Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - ( failsafe * Point ) , OrderTakeProfit(), 0 );
         }
        
         // Replace 1 == 0 to conditions to close Sell order.
         if ( 1 == 0 ) {
            Ticket = OrderClose ( Ticket, OrderLots(), OrderClosePrice(), Slippage, Red );
         }    
          
      }    
      
Files:
whroeder1
17110
whroeder1  
akuh: So whenever "changeofTrend ==1" occurs ...
if ( 1 == 0 ) {
  1. Do not use zero and one for booleans. Use true or false and rarely. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  2. If you want if(changeofTrend) why did you write if(1 == 0) ?
  3. Where is your iCustom code? Detailed explanation of iCustom - MQL4 forum
  4. Where do you compute changeofTrend?
  5. Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), Ask + ( TrailingStop * Point ), OrderTakeProfit(), 0 );
    OrderModify does not return a Ticket. You are not adjusting SL, TP, and slippage for 4/5 digit brokers/JPY pairs.
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      pip_digits = (int)MathLog10(pip/_Point);
    int      slippage   = 3 * int(pip / _Point);
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
akuh
56
akuh  
whroeder1:
  1. Do not use zero and one for booleans. Use true or false and rarely. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  2. If you want if(changeofTrend) why did you write if(1 == 0) ?
  3. Where is your iCustom code? Detailed explanation of iCustom - MQL4 forum
  4. Where do you compute changeofTrend?
  5. Ticket = OrderModify ( OrderTicket(), OrderOpenPrice(), Ask + ( TrailingStop * Point ), OrderTakeProfit(), 0 );
    OrderModify does not return a Ticket. You are not adjusting SL, TP, and slippage for 4/5 digit brokers/JPY pairs.
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      pip_digits = (int)MathLog10(pip/_Point);
    int      slippage   = 3 * int(pip / _Point);
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

Hi whroeder1,

 

i just checked the return values from and got it right right now, without using changeofTrend.

 " 

  1. If you want if(changeofTrend) why did you write if(1 == 0) ?"

if(1 ==0) is just a there to let the EA execute without sending an error. Its just a spaceholder for a closing condition which can be set by the programmer. Thats why its said above"replace 1==0 to your conditions". Below you see my condition which works well.

if ( td == EMPTY_VALUE ) {
            Ticket = OrderClose ( Ticket, OrderLots(), OrderClosePrice(), Slippage, Red );


I would like to ask another question. Whats the correct function to call up the current market price ? i havent found an example till now. My idea is, that i want to code a buystop and sellstop condition. THe buystop condition is fulfilled when the market price moves below the supertrend value (in Uptrend) and a sellstop when the market price moves above the supertrend value (in downtrend). I tried it with MarketInfo() with the bid and ask values but they dont return the right values.



double vbid    = MarketInfo("EURUSD",MODE_BID);
   double vask    = MarketInfo("EURUSD",MODE_ASK);

   // Here you can place indicators and all stuff              // For ex. if you want to play 0.5 Lot with 1000$ account you can write MathRound ( AccountBalance() / 20 ) / 100;
   // Which will calculate when script should buy or sell.
  double up = iCustom(NULL,0,"HalfTrend_1",4,1);
  //Print("vask:", vask);
double down = iCustom(NULL,0,"HalfTrend_1",5,1);
//Print("vbid:", vbid);
  double tu = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,0,0);
// Print("supertrendup:", tu);
double td = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,1,0);
  //Print("supertrenddown:", td);
double tuu = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,0,1);
double tdd = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,1,1);
   RefreshRates();  // RefreshRate() update Bid and Ask value.
  
   if ( ( Ask - Bid ) / Point < MaxSpread ) { // Checking, if spread is less than MaxSpread from inputs. If its Bigger, orders wont open

  // Replace 1 == 0 to your conditions to buy order.&& LastOT==1
      if ( 0.01<up && up<5 && tuu>0.01 && tuu<5 && OrderType()==OP_SELL && LastOrder != 1 ) { // LastOrder!= 1 prevent script from making a lot of same buy orders
         Ticket = OrderSend ( Symbol(), OP_BUY, Lots, Ask, Slippage, Ask - ( StopLoss * Point ), Ask + ( TakeProfit * Point ), NULL, Magic, 0, Green);
         LastOrder = 1; // It prevent script from making a lot of same buy orders
      }
  
      // Replace 1 == 0 to your conditions to buy order.&& LastOT==1
      if ( tuu> vask  && LastOrder != 1 )  {  // LastOrder!= 1 prevent script from making a lot of same buystop orders
         Ticket = OrderSend ( Symbol(), OP_BUYSTOP, Lots, Ask + ( Pips * Point ), Slippage, Ask + ( Pips * Point ) - ( StopLoss * Point ), Ask - ( Pips * Point ) + ( TakeProfit * Point ), NULL, Magic, TimeCurrent() + ( 60 * Mins ), Green);
         LastOrder = 1; // It prevent script from making a lot of same buy orders
      }
  
      // Replace 1 == 0 to your conditions to sell order.&&  LastOT==0
      if ( 0.01<down && down<5 && tdd>0.01 && tdd<5 && OrderType()==OP_BUY  && LastOrder != 0 ) { // LastOrder!= 0 prevent script from making a lot of same sell orders
         Ticket = OrderSend ( Symbol(), OP_SELL, Lots, Bid, Slippage, Bid + ( StopLoss * Point ), Bid - ( TakeProfit * Point ), NULL, Magic, 0, Red);
         LastOrder = 0; // It prevent script from making a lot of same sell orders
      }
  
      // Replace 1 == 0 to your conditions to sell order.&&  LastOT==0
      if ( tdd<vbid   && LastOrder != 0 ) {  // LastOrder!= 0 prevent script from making a lot of same sellstop orders
         Ticket = OrderSend ( Symbol(), OP_SELLSTOP, Lots, Bid - ( Pips * Point ) , Slippage, Bid - ( Pips * Point ) + ( StopLoss * Point ), Bid + ( Pips * Point ) - ( TakeProfit * Point ), NULL, Magic, TimeCurrent() + ( 60 * Mins ), Red);
         LastOrder = 0; // It prevent script from making a lot of same sell orders
      }
      
   }

I would be very thankful for an idea. 


cheers


akuh
 

To add comments, please log in or register