Specific questions about close command and order open command with supertrend indicator

 

Dear Traders,

 I have a question about the attached EA i created. As you can see, the attached EA works with supertrend and halftrend indicator. Whenever supertrend says "TrendUp" and an up arrow occurs, a buy Order will be set, and the same with sell positions in the opposite. Now i have problems in two areas.

 First i want the EA  to open positions, as soon as the bar crosses the the SuperTrend line. So my idea is to program, if the ask price movs above the supertrend value, an order will be open. I would prefer the most if the condition would be with the current price value but i dont know which variable it is ? (Maye Mode_Point ?) You can see what i mean in the attached picture !!! Therefore i programmed the following sellstop but i doesnt work !

 

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


double up = iCustom(NULL,0,"HalfTrend_1",5,1);
double down = iCustom(NULL,0,"HalfTrend_1",4,1);
  double tu = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,0,0);
double td = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,1,0);

   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 ( down>0.000000001 && down<9999999 && tu>0.000000001 && tu<9999999 && 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 ( tu>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 ( up>0.000000001 && up<9999999 && td>0.000000001 && td<9999999 && 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 ( td<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
      }
      
   }

return ( 0 );
}

 

 The second question is about the closing condition. I want the current Position to be closed, at the point where TrendUp changes to TrendDown, and the opposite. when i look up the values in the data window, i see that at this point the TrendUp and the TrendDown have the same values !! But if i put "tu==td" into the close condition nothing happens, as well as you see with  "tu!=EMPTY_VALUE && td!=EMPTY_VALUE". Its only working with with "td>0.000000001 && td<9999999" but this value of course occurs 1 bar later. The idea is that i want to close the positions as soon as possible ! You can see my close command code below:

double tu = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,0,0);
double td = iCustom(NULL,0,"super-trend",Nbr_Periods,Multiplier,1,0);
   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 ( tu!=EMPTY_VALUE && td!=EMPTY_VALUE ) {
            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 ( tu!=EMPTY_VALUE && td!=EMPTY_VALUE ) {
            Ticket = OrderClose ( Ticket, OrderLots(), OrderClosePrice(), Slippage, Red );
         }    
          
      }    
      
  
  
return ( 0 );
}


 In the attachement you find my EA as well as the super-trend indicator i use ! I am not the most experienced coder, but i dont see why its not working !! I would be very thankful for any help or suggestions !!!

 

cheers

 

akuh 

Reason: