Opening only 1-2 (max) orders per bar on EA, making indicator repaint on every new bar

 

Hi guys,

This one is a 2 part question (with sub-parts). 

Part 1

1) First, regarding my EA, I want to know how I can make it open only 1-2 (verify criteria to determine how many) orders per bar and then discard the bar as verified. I was thinking of defining a variable e.g. barVerified (set at 0) and then using Bars to compare against. I need an example on this...

2) My order criteria requires a buystop or a sellstop order to be placed (if criteria confirms) on most recent closed part at the high or low. Unfortunately, my broker requires a 3 pip differential between current bid/ask and the buy/sell stop orders. What I want my EA to do is this (in this situation): switch from buy/sell stop "mode" to market "mode". So in the case of it not being able to place the buy/sell stop order, it will attempt to place the order at market (only once the current price matches the previous bar's high/low).

So here's an example to make it clear: EUR/USD currently made a low of 1.3450 and opened (on new bar) at 1.3451. The EA on the new bar can't place the the order because only 1 pip difference between the 2 prices. I need the EA to keep trying (on every tick) in case price goes up to let's say 1.3453 (or better) in which case the sellstop will be placed (with TP and SL). In the event that price, goes down to 1.3450 instead, I need the order to go through at market. Which brings me to my next question...

3) When opening a market order, I can't place a TP and an SL right away, I need to place those after the order get's filled. How do I do that? Which function do I use? (I need the SL and TP to be calculated from entry price)... I'm thinking some kind of thing that will get most recent ticket number and update TP and SL info. (That's my imagination not my knowledge)

Part 2

1) This is regarding my indicator. It currently draws objects on previous bar when new bar opens. The name of the object is e.g. "[VSA] Effort (bar number)". When a new object comes up, bar number is set to 1 (the most recent bar). As new bars keep coming, that old object will have the same name. In other words, it doesn't repaint. In my case, I need my indicator to repaint. In reality, I need to repaint only the names of the most recent 2 objects (not sure if it's possible). Otherwise, I will have to go the route of repainting the whole indicator. In this case however, I need it to repaint only once when a new bar is formed. If someone can point me in the right direction, I would appreciate it.

Here are 2 code snippets that I think might need to be slightly altered (in my indicator):

   int counted_bars=IndicatorCounted(); 
   if(counted_bars > 0) { counted_bars--; } 
   int i;
   if(counted_bars>=period) { i=Bars-counted_bars-1; }
   else { i=Bars-period-1; }
if (ObjectCreate(objprefix+" "+text[s]+" ("+bar+")", OBJ_ARROW, 0, Time[bar], dprice) == true)
      {
      ObjectSet(objprefix+" "+text[s]+" ("+bar+")", OBJPROP_ARROWCODE, code[s]);     
      ObjectSet(objprefix+" "+text[s]+" ("+bar+")", OBJPROP_COLOR, icolor);
      SetIndexArrow(3,code[s]);
      sctr++; 
      }

In the second code, you can disregard sctr++ (it's not my code and I think this isn't being referenced anywhere else, might delete it).

The 1st part counts bars and on new bar, creates object if criteria meets, but like I said it doesn't repaint. Take a look at this and if you need more clarifications, I will provide them.

Thanks in advance

 
Sageone:

Hi guys,

This one is a 2 part question (with sub-parts). 

Part 1

1) First, regarding my EA, I want to know how I can make it open only 1-2 (verify criteria to determine how many) orders per bar and then discard the bar as verified. I was thinking of defining a variable e.g. barVerified (set at 0) and then using Bars to compare against. I need an example on this...

3) When opening a market order, I can't place a TP and an SL right away, I need to place those after the order get's filled. How do I do that? Which function do I use? (I need the SL and TP to be calculated from entry price)... I'm thinking some kind of thing that will get most recent ticket number and update TP and SL info. (That's my imagination not my knowledge)

Part 2

1) This is regarding my indicator. It currently draws objects on previous bar when new bar opens. The name of the object is e.g. "[VSA] Effort (bar number)". When a new object comes up, bar number is set to 1 (the most recent bar). As new bars keep coming, that old object will have the same name. In other words, it doesn't repaint. In my case, I need my indicator to repaint. In reality, I need to repaint only the names of the most recent 2 objects (not sure if it's possible). Otherwise, I will have to go the route of repainting the whole indicator. In this case however, I need it to repaint only once when a new bar is formed. If someone can point me in the right direction, I would appreciate it.

1.)  https://www.mql5.com/en/forum/143254

 

3.) read some posts:  ECN  

 

Part 2

1)  where does s come from ?  I don't think you mean re-paint,  I think you just want more bars marked. 

 
Sageone:

Hi guys,

This one is a 2 part question (with sub-parts). 


Part 2

1) This is regarding my indicator. It currently draws objects on previous bar when new bar opens. The name of the object is e.g. "[VSA] Effort (bar number)". When a new object comes up, bar number is set to 1 (the most recent bar). As new bars keep coming, that old object will have the same name. In other words, it doesn't repaint. In my case, I need my indicator to repaint. In reality, I need to repaint only the names of the most recent 2 objects (not sure if it's possible). Otherwise, I will have to go the route of repainting the whole indicator. In this case however, I need it to repaint only once when a new bar is formed. If someone can point me in the right direction, I would appreciate it.


Instead of barnumber   use   Time[barnumber]

if (ObjectCreate(objprefix+" "+text[s]+" ("+Time[bar]+")", OBJ_ARROW, 0, Time[bar], dprice) == true)
      {
      ObjectSet(objprefix+" "+text[s]+" ("+Time[bar]+")", OBJPROP_ARROWCODE, code[s]);     
      ObjectSet(objprefix+" "+text[s]+" ("+Time[bar]+")", OBJPROP_COLOR, icolor);
      SetIndexArrow(3,code[s]);
      sctr++; 
      }

DeInit()

int deinit()
  {
//----
       int i, ot=ObjectsTotal()-1;
       string id;
     //----
       for(i=ot;i>=0;i--)
         {
          id=ObjectName(i);
          if(StringSubstr(id,0,12)== "[VSA] Effort")
             {ObjectDelete(id);}
         }  

    }   
//----
   return(0);
  }
 
RaptorUK:

1.)  https://www.mql5.com/en/forum/143254

 

3.) read some posts:  ECN  

 

Part 2

1)  where does s come from ?  I don't think you mean re-paint,  I think you just want more bars marked. 

Thanks for your replies on 1 and 3. I'm not lazy, I looked through Google a quite a bit. I'm just nearing completion of my EA and I'm getting excited I guess...

For part 2, 1) s is the name of the signal it's calcualted on prior criteria, it is an int value corresponding to a name. Whatis happening is the bar is marked as VSA Effort (1)... then when the 2nd bar comes in, it's supposed to change name to VSA Effort (2) but it just stays at VSA Effort (1). When I close MT4 and reopen, or if I change timeframe, it is renamed properly.

Thanks. Also deVries, I will check out what you wrote, currently at work.  

 
RaptorUK:

1.)  https://www.mql5.com/en/forum/143254

 

3.) read some posts:  ECN  

 

Part 2

1)  where does s come from ?  I don't think you mean re-paint,  I think you just want more bars marked. 

Regarding Part 2, number 1.

When signal appears on bar 1, it is e.g. "[VSA] Effort (1)"... then when a new bar comes in, the signal should shift to bar 2 and should change to "[VSA] Effort (2)" but instead it just stays as "[VSA] Effort (1)".... How do I make it update. Like I said, when changing timeframe or closing/reopening MT4, it becomes corrected.