Long Trade on Active Bar

 

Hello,

I was experimenting around with building an EA and had a couple of coding questions.


With the following code,

if(Low[0]>Low[1] && Low[1]>Low[2] && low[2]>Low[3]....

Seeing that this would be trading on the movement of the active bar, what code would I use so that it would only take one trade on the active bar.  Currently it's taking multiple trades because each tick movement is generating an additional trade.  Also, I would want it to continue to take trades on future bar setups even if the original trade is still open.  This EA would just be taking long trades.

One more quick question.  Referring to the middle part of the above code, Low[1]>Low[2], if I wanted to specify that the low[1] needs to be 5 pips higher than the Low[2], would I set up a something like

input double PipSpread = 5;
Low[1] >Low[2]+PipSpread *_Point   (I wasn't able to get this to work, so I'm not sure if I'm moving in the right direction or way off base.

Thanks for the help!!!

Mike

 
mcassara:


Currently it's taking multiple trades because each tick movement is generating an additional trade. 

I wanted to specify that the low[1] needs to be 5 pips higher than the Low[2], would I set up a something like

input double PipSpread = 5;
Low[1] >Low[2]+PipSpread *_Point  

  1. Don't look at Low[0] on a forming bar. Add a new bar check.
  2. That results in 5 points not 5 pips.adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
 

Thank you for the help.  I'm not familiar with a new bar check and was unable to find it in the documentation.  Do you have an example that I can take a look at?

Thanks,

Mike

 
  1. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
 
whroeder1:
  1. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

Thanks whroeder!!

I was wondering if you by chance have a sample EA that I can look at that uses the New candle syntax.  I was getting some odd results when trying to use the code and I am certain that it is something that I am doing incorrectly. 

Thanks again,
Mike 

void OnTick(){    static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){
     : // Once per bar
   }
   : // every tick
}

 
mcassara I was getting some odd results when trying to use the code and I am certain that it is something that I am doing incorrectly.
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. That is correct code. Print out your variables, and find out why.
 

whroeder,

I know your code is correct, it's the logic that I am using around your code, that is giving me incorrect results.  I wasn't sure how to print the variables, so I am attaching my mq4 file.  This is all very new to me and I'm just trying to learn.  I think that once I am able to get this EA working, the logic will begin to make more sense to me.  I am running this through the strategy tester on a 5 min chart on EURCAD.  The date range that I am using is 4/3/16 t0 4/16/17.  Any advice that you can provide will be greatly appreciated.

Thanks again for your time.

Mike

Files:
Bar_Test4.mq4  2 kb
 
double FastMa = iMA(_Symbol,_Period,FastMaPeriod,0,FastMaMethod,FastMaPrice,1);
double SlowMa = iMA(_Symbol,_Period,SlowMaPeriod,0,SlowMaMethod,SlowMaPrice,1);
double close = Close[1];
These variables will never change. Assign them in OnTick/once per bar.
 

Got it, I assigned that code to the OnTick event handler.

I think my logic is still incorrect in the following code segment. I'm seeing multiple buy orders placed on a single bar.

   { (FastMa>SlowMa && Low[1]>SlowMa && Low[1]>Low[2] && Low[0]>Low[1] && High[0]>High[1]);
         // Once per bar
  }
   // every tick


 static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar)

  { (FastMa>SlowMa && Low[1]>SlowMa && Low[1]>Low[2] && Low[0]>Low[1] && High[0]>High[1]);
         // Once per bar
  }
   // every tick
 
mcassara: I think my logic is still incorrect in the following code segment. I'm seeing multiple buy orders placed on a single bar.
  { 
     // Once per bar
     (FastMa>SlowMa && Low[1]>SlowMa && Low[1]>Low[2] && Low[0]>Low[1] && High[0]>High[1]);
  }
   // every tick
  1. Your posted code doesn't contain a OrderSend. You cant open any orders there. There are no mind readers here, post the real code.
  2. What does you think that line does? Why do you ignore the warning?
 

I have the warnings fixed.  Here is the entire code.  Does the logic look correct?

#property description   "Bar Test with Moving Average"

// Input variables
input int MagicNumber = 101;
input int Slippage = 10;

input double LotSize = 0.1;
input int StopLoss = 500;
input int TakeProfit = 10;

input int FastMaPeriod = 14;
input ENUM_MA_METHOD FastMaMethod = MODE_SMA;
input ENUM_APPLIED_PRICE FastMaPrice = PRICE_CLOSE;

input int SlowMaPeriod = 30;
input ENUM_MA_METHOD SlowMaMethod = MODE_SMA;
input ENUM_APPLIED_PRICE SlowMaPrice = PRICE_CLOSE;
int BuyTicket;

// OnTick() event handler

void OnTick()

{

// Moving average and close price from last bar
double FastMa = iMA(_Symbol,_Period,FastMaPeriod,0,FastMaMethod,FastMaPrice,1);
double SlowMa = iMA(_Symbol,_Period,SlowMaPeriod,0,SlowMaMethod,SlowMaPrice,1);
double close = Close[1];



   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar)
   
  if(FastMa>SlowMa && Low[1]>SlowMa && Low[1]>Low[2] && Low[0]>Low[1] && High[0]>High[1])
         // Once per bar
 
   // every tick

    { 
      // Open buy order
     BuyTicket= OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"Buy order",MagicNumber,0,clrGreen);
    }  
}
Reason: