Multiple orders problem - page 2

 

Hi Raptor;

While in Tester all orders are closed when the TrailingStop closes, but on Demo only the order closed by mean of TrailingStop closes, any order that is open still open.

Using the following code  on top of the one from your advise the logic is;

"Find what is the condition of the last order, if was a close then perform the code to start closing all remaining open orders."  

Any clue why this happen ?

Best Regards

Luis 

int OrdType, GLError;
   double OrderClosed;
   RefreshRates();
   
    for(int Closed = OrdersTotal()-1;Closed >= 0; Closed--) 
      if(OrderSelect(Closed,SELECT_BY_POS,MODE_HISTORY)
        && OrderMagicNumber()== MagicNumber
        && OrderSymbol()== Symbol())
        {//29
        OrderClosed = OrderCloseTime();
        if(OrderClosed!=0)
           {//30                                  
   for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--)       
      if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber()== MagicNumber 
         && OrderSymbol()== Symbol())                                       
         {//31
         OrdType = OrderType();
         if(OrdType == OP_BUY || OrdType==OP_SELL)
           {//32
           if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),RealSlippage, Yellow))
               GLError = GetLastError();
           }//32                                         
          }//31
       }//30
     }//29 
    
  if(GLError > 0) Print("Error Closing/Deleting Order, error # ", GLError, " for ticket: ", OrderTicket());           
  return(0);
  }//0 
 
luisneves:

Hi Raptor;

While in Tester all orders are closed when the TrailingStop closes, but on Demo only the order closed by mean of TrailingStop closes, any order that is open still open.

Using the following code  on top of the one from your advise the logic is;

"Find what is the condition of the last order, if was a close then perform the code to start closing all remaining open orders."  

Any clue why this happen ?

You cannot do this . . .

    for(int Closed = OrdersTotal()-1;Closed >= 0; Closed--)    // OrdersTotal is the total of open orders
      if(OrderSelect(Closed,SELECT_BY_POS,MODE_HISTORY)        //  this is looping through the closed orders

  . . .  it makes no sense to use the number of Open Orders as the loop starting condition to loop through the Closed Orders.  Maybe you meant to use  OrderssHistoryTotal() ?  but the OrderCloseTime() of a closed Order will never == 0

 
luisneves:

.....

 I am using BuyTicket and SellTicket in the code to avoid the opening of multiple orders but seems that is not the right method to go... 

.....

A problem comes when a condition to open a third order comes and so on. Here nevertheless the conditions to open are there and the code have deal fine with the second open why doesn't the code work here ? 

I understand you have slightly changed the initial topic and maybe the first issue is discussed to its end (it's ok for me)
If you still also want follow up the inital issue, you have to explain it a bit else, because I dont really understand what you are looking for.

The first snippet I've adjusted prevents the code from opening multiple orders. You seemed not happy with it, therefore you removed (or just not added) it, but doing the similar thing by using BuyTicket and SellTicket.
Then you are talking about a third order... Do you look for something like this?

Buy->Sell->Buy->Sell->Buy
but prevent
Buy->Buy->Sell->Buy->Buy

In other words, if the last opened order is a buy order, the next must be a sell and vice versa?

Or what is the max amount of orders your EA should open? Is it 2, one sell and if condition for an opposite order is met, a buy, but not anymore a sell if it bounces back from the buy trigger again?
If this is true, what was the issue with the OpenOpposite counter I added to your inital code? 

edit:

The third option I can imagine is you want to open another opposite order if the first opposite order has been stopped out?
So like this:

Buy->Sell->if Sell has been stopped out->Sell->if Sell has been stopped out->Sell 

 
kronin:

I understand you have slightly changed the initial topic and maybe the first issue is discussed to its end (it's ok for me)
If you still also want follow up the inital issue, you have to explain it a bit else, because I dont really understand what you are looking for.

The first snippet I've adjusted prevents the code from opening multiple orders. You seemed not happy with it, therefore you removed (or just not added) it, but doing the similar thing by using BuyTicket and SellTicket.
Then you are talking about a third order... Do you look for something like this?

Buy->Sell->Buy->Sell->Buy
but prevent
Buy->Buy->Sell->Buy->Buy

In other words, if the last opened order is a buy order, the next must be a sell and vice versa?

Or what is the max amount of orders your EA should open? Is it 2, one sell and if condition for an opposite order is met, a buy, but not anymore a sell if it bounces back from the buy trigger again?
If this is true, what was the issue with the OpenOpposite counter I added to your inital code? 

edit:

The third option I can imagine is you want to open another opposite order if the first opposite order has been stopped out?
So like this:

Buy->Sell->if Sell has been stopped out->Sell->if Sell has been stopped out->Sell 


Hi Kronin,

Tank you in advance for your time to support me in this issue. 

  The strategy observe this logic;

Say that the first order to open is a buy and then it looks to close by mean of TrailingStop, but if  Bid bounce down some pips under the OrderOpenPrice  (ReturnDistance), a Sell will open and looks to close by mean of TrailingStop and again if the Ask bounce up some pips above OrderOpenPrice then a buy will open. This process of ping pong will end when the last order to open closes by mean of TrailingStop or reach the maximum orders settings which is 7 (one could adjust this by external).

The problem with the multiple openings happens when price goes up and down crossing the OrderOpenPrice. so, if we have a buy the next order just can be a sell an so on.

Regarding your previous help perhaps I have not explain well what my issue was. Any help provided have for me the up most of value.  

 Best Regards

Luis 

 

Luis, I spent quite a lot of time on your code, but honestly I am not really sure I does what you want. 

See attached. Finalize/Change it, test it, understand it.... and tell me at least it is working close, similar or complete else, to what you are looking for. I am not quite sure about the strategy yet.
Please don't add new function yet (don't re-add martingale). The code is big enough and you have still a lot of work in front of you, to get it reliable running.
I have to say the code is not much more clearly arranged. I did not want to change your running parts (even you need a better errorhandling). I have commented parts and moved parts, but it is all still there...


Have fun...

 
kronin:

Luis, I spent quite a lot of time on your code, but honestly I am not really sure I does what you want. 

See attached. Finalize/Change it, test it, understand it.... and tell me at least it is working close, similar or complete else, to what you are looking for. I am not quite sure about the strategy yet.
Please don't add new function yet (don't re-add martingale). The code is big enough and you have still a lot of work in front of you, to get it reliable running.
I have to say the code is not much more clearly arranged. I did not want to change your running parts (even you need a better errorhandling). I have commented parts and moved parts, but it is all still there...


Have fun...


Hi Kronin,

Before all my up most thank you for time spent in support of my issues.

While I believe that you have done your best to understand the strategy some things are not quite respond  (due of course of my lack of understanding in this matter).

I have done some modifications buy your advise,but not sure if done correctly.

Two issues;

1 - The idea is once the trade have been closed by mean of TrailingStop all remaining open orders should close. The orders are not to be closed on TakeProfit (that TakeProfit is just there because I want be sure that is put of Freeze Zone). So I thought to do that using the close of the last order to run the CloseAll function (some stupid things comes up to try do it...). You are using Last Closed Ticket to run the close of reaming open orders, but I do not understand if that happens when trade closes by mean of TrailingStop...

2 - The "ping pong" is not working, at least in Tester.

In attach is the file where the modifications had been made as far I understood them.

Thank you in advance for you patience and time spent. (more in private message)

Best Regards

Luis 

 

Ok, I have changed the algorithm to close all orders on SL insted of TP. (The change was '<' replaced by '>' - you should find out where) 

The ping pong does work for me and I have only let it run in the tester in Visual Mode. But I have adjusted your input params to have not all orders open at pretty much the same time. Maybe you have to verify the default params. 

It starts with the inital order (I have changed the print() statement) and then does opposite orders. 

EURUSD,M1: open #1 buy 0.01 EURUSD at 1.43310 ok
EURUSD,M1: Inital Buy order placed # 1
EURUSD,M1: modify #1 buy 0.01 EURUSD at 1.43310 sl: 1.42810 tp: 1.43510 ok
EURUSD,M1: open #2 sell 0.01 EURUSD at 1.43200 ok
EURUSD,M1: Opposite Sell order placed # 2
EURUSD,M1: modify #2 sell 0.01 EURUSD at 1.43200 sl: 1.43700 tp: 1.43000 ok
EURUSD,M1: open #3 buy 0.01 EURUSD at 1.43300 ok
EURUSD,M1: Opposite Buy order placed # 3
EURUSD,M1: modify #3 buy 0.01 EURUSD at 1.43300 sl: 1.42800 tp: 1.43500 ok

 

I have added return to the OpenOppositeOrder() when i did open an order. Together with the settings it was possible to open a buy order and in the same tick a sell order. This lead into MaxOrder is not reliable.
Maybe a better approach is, to split it in 2 functions or give the function a parameter to only run for sell orders or buy orders.

btw. the code you uploaded did not trade! All trades failed because of 'invalid LotSize'....   

 

Hi Kronin,

  Thank you for time spent in support me.

  Regarding the issue " the code you uploaded did not trade! All trades failed because of 'invalid LotSize'.... ", that happened after moved the MM code to the end of file. I've done the call of the function using MM(); at the start of code, seems that this kind of action doesn't work, but the last code you sent is working and the MM() function is still at same place and is working, so here I'm lost....

  About the order opening at same tick; 

 When EA is get into the chart it must way to the price is more (or less) than the price at that moment, meaning when price goes up (or down) the OpenDistance an buy (or sell) order take place. From here the next opening just can happen when price bounce down (if the last order is a buy) from the OrderOpenPrice minus ReturnDistance.Same logic in case if sell is the last order. Perhaps a limit have to be in place to avoid any open of order out of this logic.

 Best regard's

Luis 

 
//mine
LotSize = (RiskAmount / StopLoss) / TickValue;              //Phil: LotSize is defined in global scope

//yours
double LotSize = (RiskAmount / StopLoss) / TickValue;


You defined LotSize in global scope and initalized it with 0. In the void MM() function you calculate a LotSize only valid in that function. I have only removed the initialization, so the variable in global scope is updated.

Could you comment for each of these values, the value in Pips, please? 

extern double StopLoss       =  50;
extern double TakeProfit     =  20;
extern double TrailingStop   =   2;
extern int    MinimumProfit  =   3;
extern int    Slippage       =   3;
extern double OpenDistance   =   2;
extern double ReturnDist     =   1;
extern double MinStop        =   1;

What is the spread for the Symbol you want to run the EA?

But I have adjusted your input params to have not all orders open at pretty much the same time. Maybe you have to verify the default params.  

Try and do the same and let it run in the tester in visual mode.  

 
kronin:


You defined LotSize in global scope and initalized it with 0. In the void MM() function you calculate a LotSize only valid in that function. I have only removed the initialization, so the variable in global scope is updated.

Could you comment for each of these values, the value in Pips, please? 

What is the spread for the Symbol you want to run the EA?

But I have adjusted your input params to have not all orders open at pretty much the same time. Maybe you have to verify the default params.  

Try and do the same and let it run in the tester in visual mode.  


Hi Kronin,

Yes, I've a lot to learn....Now understand that when there is a need of a value to get accessed from outside a function that must be at Global. 

The values that are on external are multiplied by 10 because the EA must also run on 5 digits brokers. I'm using this block of code to get that automatically, but get an advise from WHRoeder that is not compatible with metals.  

int init ()// Adjust for 4 or 5 digits.
   {
   if (Digits == 2 || Digits == 4) <------- not compatible with metals
      {
      pt = Point;
      RealSlippage = Slippage;
      }    
   if (Digits == 3 || Digits == 5) 
      {
      pt = Point * 10;
      RealSlippage = Slippage * 10;
      }

 The spread of pair could be variable. That's why I'm using code to get out from the Stop Level.

Regarding this issue " But I have adjusted your input params to have not all orders open at pretty much the same time. Maybe you have to verify the default params."

As far I can see (sorry if am not...), OpenDistance maintain as 2 pips and ReturnDistance is now with 2 as well. What I'm see right now is that the order opens but not at 2 pips difference. This is running with a tester from in a platform of an ECN broker (IC Markets). Could this have some importance ?

In fact the orders do not open at same time but  seems that Open Distance and ReturnDist are not take in account to get the right distance to open orders.

You have in code that;

 

OTLastTick=OTCurrentTick;                      //shift OrderTotal
  OTCurrentTick=OrdersTotal();                   //reinit OrderTotal
  if(OTCurrentTick>0)Trail();                    //Trail
  if(OTLastTick>=2                               //if OrderTotal has changed
     &&OTCurrentTick<OTLastTick
     &&OTCurrentTick>0){CloseAllOnSL();return;}  //Check order closed on SL level
  if(OTCurrentTick>=MaxOrders)return;            //Dont open more orders. Trail and return.
                                                 //Actually we have nothing more to do.
                                                 //Only call opposite if the initial order of the serie is open
  if(OTCurrentTick>0)OpenOppositeOrder(); //<--------------------- include this line to call function (not sure if this the right method to do it...)
  MM();                //<--------------------- include this line to call function (not sure if this the right method to do it...)
                        
  if(OTCurrentTick==0){//init serie
     BuyAllowed=true;
     SellAllowed=true; 

 

 I've include the line in bold to call the function OpenOppositeOrder  and here not sure if this is right. For other side can't see where is the comparison of present tick with last tick that happened 2 pips before (OpenDistance).

Sorry if i start to bore you with my issues.

Best regard's

Luis 

Reason: