Download MetaTrader 5

1 Trade per Cross

To add comments, please log in or register
Justin
8
Justin  

Hi

 I am new to MQL4 and have only been codding for about 1 Week.

First of thank you for all the advice, I have learnt a lot from reading postis in this forum.

I have been tying to code a EA and thus far I have managed to get it to a point where it uses custom indicators and based on the info it oppens a long or short possition with stoploss and take proffit.

How do I do the following:

Indicator crosses up though MA - long position is opened (Done)

Indicator crosses down though MA - long position is closed and short is oppened (Done)

After indicator crosses and opens long then take profit is triggered (Done) - this is where I need help once the take profit is triggered how do I tell it not to open another possion untill the indicators have  crossed down again. 

 

I hope I have explained myself propperly.

 

Thanks 

whroeder1
15053
whroeder1  

DoctorX:

After indicator crosses and opens long then take profit is triggered (Done) - this is where I need help once the take profit is triggered how do I tell it not to open another possion untill the indicators have  crossed down again.
Check for a change in signal, not a signal.
static bool isUp;
bool wasUp = isUp;
isUp = indicator1 > indicator2;
if(wasUp != isUp){ // NEW cross.
Keith Watford
Moderator
10001
Keith Watford  

Usually, these type of EAs are coded to only work on closed bars. That is because there can be many crosses during the life of a single bar.

For a buy, check that Close[1] is above the MA & Close[2] is below the MA

Opposite for a Sell 

Justin
8
Justin  

Hi WHRoeder

Sorry, I just cant seem to figure out how or where to implement the code.

Here is the code in my EA that opens orders.

// BUY/SELL 
   if (C_Current>=T_Current)
     {
      Opn_B=true;                               // Criterion for opening Buy
      Cls_S=true;                               // Criterion for closing Sell
      Opn_S=false;                               
      Cls_B=false;
                   
     }
   if (C_Current<=T_Current)
     {
      Opn_S=true;                               // Criterion for opening Sell
      Cls_B=true;                               // Criterion for closing Buy
      Opn_B=false;                               
      Cls_S=false;
       
     }


// Open Position

 if (Opn_B==true && Total==0 )           //BUY
     {
     
         RefreshRates();   
         SL=Bid-New_Stop(StopLoss)*Point;     // Calculating SL of opened
         TP=Bid+New_Stop(TakeProfit)*Point;   // Calculating TP of opened
         Alert("Attempt to open Buy. Waiting for response..");
         Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,SL,TP,"Order",Magic);//Opening Buy
         if (Ticket < 0)                        // Success :)
           {
            Alert ("Opened oredr Buy ",Ticket);
                                         // Exit start()
           }
         else
            Print("OrderSend placed successfully");

      }
      
if (Opn_S==true && Total==0)             //SELL
     {
     
         RefreshRates();                        // Refresh rates
         SL=Ask+New_Stop(StopLoss)*Point;     // Calculating SL of opened
         TP=Ask-New_Stop(TakeProfit)*Point;   // Calculating TP of opened
         Alert("Attempt to open Sell. Waiting for response..");
         Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,SL,TP,"Order",Magic);//Opening Sels
         
         if (Ticket<0)                        // Success :)
           {
            Alert ("Opened order Sell ",Ticket);
                                      // Exit start()
           }
         else
            Print("OrderSend placed successfully");

      }

How do I need to change this to get this to work, also if I have made some errors please let me know.

To add comments, please log in or register