I am trying to create an EA which should take only trade per crossover

 

Hello Everyone,

I am newbee in as EA developer, I am trying to create an EA which should take only trade per crossover ,,,Unfortunately I am not able to code the EA in such way as required, Its just taking trades all the way ... Pls can some one guide me through this . Orders are executing but EA should take one trade per cross over only.

Example if Bull Crossover then only one Trade of buy as same for sell.

Below is my piece of code FYR,

void OnTick()

{ 

   bool Cbuy = true;

   bool Csell = true;  

 for (int i =OrdersTotal()-1;i>0;i--)

 {

   if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == Symbol()) 

    {

         //check for any open buy orders

         if (OrderType() == OP_BUY)

            {

               Cbuy = false;

            }  

         

            //check for any open Sell orders

         if (OrderType() == OP_SELL)

            {

               Csell = false;

            }    

          }  

      }      

            

            

          if((Cbuy = true) && SignalCheck() == true)

            {

             OrderSend(NULL,OP_BUY,0.01,Ask,3,Ask-50*Point,Ask+20*Point,"",12345);

            }  

            

          if((Csell= true) && (SignalCheck() == false))

            {

             OrderSend(NULL,OP_SELL,0.01,Bid,3,Bid+50*Point,Bid-20*Point,"",12345);

            }      

}   

//+------------------------------------------------------------------+

bool SignalCheck(){

    

    double rviMain;

    double rviSignal;

    

    rviMain = iRVI(NULL,PERIOD_H1,5,MODE_MAIN,0);

    rviSignal = iRVI(NULL,PERIOD_H1,5,MODE_SIGNAL,0);    

      

    if (rviMain > rviSignal && OrdersTotal() < 0) {

    

      return(true);

    

    }   

   return(false); 
 
Use the </> button to insert your code.
 
divyacchandravg24:

Hello Everyone,

I am newbee in as EA developer, I am trying to create an EA which should take only trade per crossover ,,,Unfortunately I am not able to code the EA in such way as required, Its just taking trades all the way ... Pls can some one guide me through this . Orders are executing but EA should take one trade per cross over only.

Example if Bull Crossover then only one Trade of buy as same for sell.

Below is my piece of code FYR,

OrderSend: - Check your returned Value; if it succeeds, it will return the Ticket Number.
                  - 3 is too small as a Slippage input, Slippage is in points, not pips.

SignalCheck: - OrdersTotal should never return a negative number... why check if it's smaller then 0?

I did not run tests with your code but these are some points that should be addressed. Cheers!

 
Jeremie Courchesne #:

OrderSend: - Check your returned Value; if it succeeds, it will return the Ticket Number.
                  - 3 is too small as a Slippage input, Slippage is in points, not pips.

SignalCheck: - OrdersTotal should never return a negative number... why check if it's smaller then 0?

I did not run tests with your code but these are some points that should be addressed. Cheers!

Thakns  Jeremie for addresing it... Will rectify and will come back....Cheers!
 

Hi..it is taking trades exactly how you code it...


if (rviMain > rviSignal && OrdersTotal() < 0)

this means that it will take long trades as long as rviM > rvi Sig... you did not code the crossover  you can code crossover multiple ways...the easiest to explain is to think like that:

 When is crossing over...how the lines looked before crossing and how they are looking now... which one was bigger before and which one is bigger now... what candle is "before" and what candle is "present"   ... when do you take want to take the trade...on candle 0 ? or closing of last candle ??  

also  


if((Csell= true) && (SignalCheck() == false))

Csell = true ... this is assignment.... you assign to Csell value of true...you will have an error here ... "expression not Boolean" or something....  


also .. 

for (int i =OrdersTotal()-1;i>0;i--)

you are not counting all orders..


also... when use this 

Ask+20*Point

Normalize it 


Other then that the idea of your code is good...good luck

 
             OrderSend(NULL,OP_BUY,0.01,Ask,3,Ask-50*Point,Ask+20*Point,"",12345);
  1. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020.07.25)

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: