help with Small fix on this EA

 

Hi guys I have been using all your great suggestions to make this Grid EA idea I had, work properly...for the most part it works

in how it places the grid and replaces triggered orders but I am having a small problem...

I want to make sure that if an order closes in loss then the EA will cycle through the open orders and close the 2 with the highest

profits to off set the loss...with the way the grid is arranged if 1 trade hits SL then there should at least be 2 open other trades if not more...


I have attached the EA can some one help me with this please...

here is how i tried to accomplish it....I did this :


for(int k=OrdersHistoryTotal()-1;k>=0;k--)
   {
   OrderSelect(k,SELECT_BY_POS, MODE_HISTORY);
  
   if(StringFind(OrderComment(),"[sl", 0)>=0)
   {
   int ticket;// ticket of current order
   int profit;// profit of current order
  
for (int y=2;y>0;y--)  // cycle twice to close 2 highest profit orders
   {
 for (int x=OrdersTotal()-1;x>0;x--) // loop through orders
      {
   OrderSelect(x,SELECT_BY_POS);
  
       if (OrderProfit()>profit) ticket = OrderTicket();
          }
                   
OrderSelect(ticket,SELECT_BY_TICKET);
  
      if (OrderType()==OP_BUY)
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),NULL);
      if (OrderType()==OP_SELL)
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),NULL);   
            }
       }
   }  


this seems to give me a Order modify error 1 though and so doesn't work...can any one help me get this right please...


There is 1 other thing i would like to do...if the distance between the lowest BuyStop Order and the highest SellStop order exceeds 20pips then i want to delete and replace the grid ...I know how to delet the grid and replace it...I just dont know how to select the lowest buystop and the highest selltop so i can subtract the diffeence between them and check if its above 20...something like this...

lowest buystop entry price = 1.3500 - the highest sellstop entry price 1.3450 = 50pips so delete grid and replace it...

Anyone know how to select the lowest buystop and highest sellstop??


thanx guys...the EA is mine so if anyone wants to use it or play with it, feel free


Files:
grid.mq4  11 kb
 
can anybody help with this small adjustment?
 
anybody?
 
Nothing to fix, everything wrong.
 
Roger:
Nothing to fix, everything wrong.

the grid works fine...it replaces the grid everytime a pending order is converted into a market order...the only thing i can see to fix is closing the 2 highest profitable open trades when a trade hits SL...and to calculate the difference between the highest sell stop and the lowest buy stop...


why do you say its all wrong?

 
23510 wrote >>

the grid works fine...it replaces the grid everytime a pending order is converted into a market order...the only thing i can see to fix is closing the 2 highest profitable open trades when a trade hits SL...and to calculate the difference between the highest sell stop and the lowest buy stop...

why do you say its all wrong?

You will get an error if there isn't 2 orders in profit to be closed. If you want to find the highest and lowest stop orders it would be the same as finding the highest profit order. When you say difference between highest and lowest stop.. is that of one particular currency pair or of all open orders? Curious as to what is your objective.

 
mqldelta:

You will get an error if there isn't 2 orders in profit to be closed. If you want to find the highest and lowest stop orders it would be the same as finding the highest profit order. When you say difference between highest and lowest stop.. is that of one particular currency pair or of all open orders? Curious as to what is your objective.

Hi there, What I am trying to do is this:


1: If a trade is closed at SL then cycle through the open orders and close the 2 with the highest profits to cancel the loss...with the grid the EA placed if an order is closed in Sl there will always be at least 2 open orders in the opposite direction that are in profit.( I have checked this many times it is always true)


2 :the ea places a grid of 12pending orders currently...and if a pending order id triggered it will place a new pending but above the grid and a new pending sell a certain distance below the current price...but what happens is that gaps form and the gap between the lowest pending buy order and the highest pending sell order can exceed 30-50 pips so the price can range where there is no grid...


So I want to determine constantly the gap between the lowest pending buy order and the highest pending sell order and if that gap becomes bigger than 25 pips i want to delete the grid and place it again...I know how to delete the grid and place it again but cant figure how to determine the size of the gap between the lowest pending buy and the highest pending sell...


I hope this is a good explanation...

 
23510 wrote >>

Hi there, What I am trying to do is this:

1: If a trade is closed at SL then cycle through the open orders and close the 2 with the highest profits to cancel the loss...with the grid the EA placed if an order is closed in Sl there will always be at least 2 open orders in the opposite direction that are in profit.( I have checked this many times it is always true)

2 :the ea places a grid of 12pending orders currently...and if a pending order id triggered it will place a new pending but above the grid and a new pending sell a certain distance below the current price...but what happens is that gaps form and the gap between the lowest pending buy order and the highest pending sell order can exceed 30-50 pips so the price can range where there is no grid...

So I want to determine constantly the gap between the lowest pending buy order and the highest pending sell order and if that gap becomes bigger than 25 pips i want to delete the grid and place it again...I know how to delete the grid and place it again but cant figure how to determine the size of the gap between the lowest pending buy and the highest pending sell...

I hope this is a good explanation...

u have two problems u like us to solve for this grid EA. Before attempting to solve them first, can I request you describe how your grid EA works in the first place. Sure we can read your codes, but need to verify the codes against your idea of the grid strategy so that it is doing all that you want correctly before solving the two apparently harder problems.

 
ronaldosim wrote >>

u have two problems u like us to solve for this grid EA. Before attempting to solve them first, can I request you describe how your grid EA works in the first place. Sure we can read your codes, but need to verify the codes against your idea of the grid strategy so that it is doing all that you want correctly before solving the two apparently harder problems.

23510,

waited a week for you to answer but looks like u are not going to.. anyway, just want to encourage u to keep searching and trying yr EA. i tried to interpret what u want to do and got quite encouraging results. all the best to you then.

 

ronaldosim,

sorry for the silence, I have been internetless for a while...my connection was disupted and only recently fixed.

I will try explain the basic thing I am wanting to fix really...

The EA places a grid and when the price moves up an interval (8 pips) then it must place a pending buy above the grid (an interval above the grid) and a pending sell trade an interval below the price where the order was triggered...


I modified the code and got this working...but it created a problem...the problem is as price fluctuates up and down it creates gaps in the grid.

I cant figure out how to replace the pending orders so that the grid stays intact....


so as an example : if price moves up 2 intervals (16pips) 2 pending buy orders will be triggered and so the Ea will place 2 pending buy orders above the grid, one will be 8pips above(8pips=interval) and the next one will be 16pips above ...at the same time the EA will place 2 pending sell orders one 8 pips lower than the current price and one 16 pips lower than the current price...


but when the price moves up and down gaps form in the spacing of the orders....I want there to be a even spacing...

I have attached the EA the way i have now coded it.....please run it in visual mode and you will see what i mean.


thanx again for all the help, and again sorry for the silence it was not by choice.

Files:
mygridea.mq4  11 kb
Reason: