Problem with EA code - page 2

 
Funky:


Yeah I have tried to modify old ticket first, then open new trade, then modify that ticket. It was the only way I could think of.


You are doing this:

1. Count open orders

3. loop through all open orders and modify and also 2. Decide whither to open new orders. (but without the decide bit . .)

Each time you successfully Modify an order you then automatically open a new order . . . not really sure what you are trying to do but this is what your code does. Perhaps you could explain what you are trying to do . . .

 
RaptorUK:


You are doing this:

1. Count open orders

3. loop through all open orders and modify and also 2. Decide whither to open new orders. (but without the decide bit . .)

Each time you successfully Modify an order you then automatically open a new order . . . not really sure what you are trying to do but this is what your code does. Perhaps you could explain what you are trying to do . . .

Thanks Raptor.

Yeah I was hoping to avoid counting open orders. I was hoping to just look at last trade by LAST TICKET using SMYBOL and ORDERID somehow, in order to make the ORDERSEND decision. And then modify old orders of that SYMBOL ORDER ID once that even occurs.

I want it to build my trades up. If direction hits the first TradeUpPoint, it will OrderSend(), then OrderModify() old trades of that pair and direction (except the current one) to the TrailingGap Point. So if I set TradeUpPoint to 20 pips, and the TradeUpTraillingGap to 12 pips, I want it to open a new order every 20 pips in right direction, and bring the SL on old orders that are still alive up to that point minus 12 pips etc. It is a little reversed hey...

It is more the order select part of the code that I am stuck on....

 

OK I think I understand what you want to do . . . I think I do, I am not 100% sure I do . . . but I think I do.

What governs whether you open an additional order or not is the most recent order . . . not all the other older orders . . . it's easy just to place a single new trade . . .

                else
                {
                    int ticket = -1;
                    if (IsECNBroker)
                       if (!NewTradePlaced) ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, MaxSlippage, 0, 0, BuyExpertName, OrderId_BUY, 0, Colour);
                    else
                       if (!NewTradePlaced) ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, MaxSlippage, stoploss, takeprofit, BuyExpertName, OrderId_BUY, 0, Colour);
                    if (ticket > -1 && !NewTradePlaced)
                    {
                        NewTradePlaced = true;
                        if (IsECNBroker)
                        {
                            OrderSelect(ticket, SELECT_BY_TICKET);
                            bool var2 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, Colour);
                            if (var2 == false)
                            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                        }

you will need to define and set NewTradePlaced = false; at a relevant place . . .

You seem to be deciding whether you modify an order on an Order by Order basis ? is that correct ?

 
RaptorUK:

OK I think I understand what you want to do . . . I think I do, I am not 100% sure I do . . . but I think I do.

What governs whether you open an additional order or not is the most recent order . . . not all the other older orders . . . it's easy just to place a single new trade . . .

you will need to define and set NewTradePlaced = false; at a relevant place . . .

You seem to be deciding whether you modify an order on an Order by Order basis ? is that correct ?

Forgive my delayed reply, it has been one of those weeks. You are actually 'spot on' with what I am after Raptor, I really appreciated your time looking at this one. Yeah at TradeUpPoint, I want it to both make a new order and modify old stops. Sounds simple, however so close now.

I was going to report back, as coincedently I was trying this below code. I was excited for a while as it works perfectly 99% of the time.

However I am having a really wierd problem at the moment, in the sense that it now throws in an extra order randomly it seems when the market it hot. I have tried increasing slippage and quite a few things that I believe could of caused it to loop around, however I am quite lost again.

I have since taken out the ECN broker stuff, as wasn't sure if that was where I was wrong.

I will try using your code next Raptor thank you very much. If you can see where my code is getting lost, please let me know too, as it will help me in my learning. I see the subtle difference in how you approached it, I like your ideas, I will try that code and report back. Many thanks for your thoughts and regards :)

{
    double lots1 = 0;
    double takeprofit1 = 0, stoploss1 = 0;
    bool NewOrderAlreadyOpened1 = FALSE;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderIdBuy)
        {
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TradeUpPointBuy*PipValue*Point && (OrderStopLoss() < Ask-(TradeUpPointBuy+TrailingGapBuy)*PipValue*Point))
            {
                stoploss1 = Ask-TrailingGapBuy*PipValue*Point;
                takeprofit1 = Ask+NewTakeProfitBuy*PipValue*Point;
                if (NewTakeProfitBuy == 0) takeprofit1 = OrderTakeProfit();
                bool ret0 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss1, takeprofit1, OrderExpiration(), BuyStackColour);
                if (ret0 == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                else
                {
                  if (!NewOrderAlreadyOpened1)
                  {
                     int ticket1 = -1;
                     ticket1 = OrderSend(Symbol(), OP_BUY, LotSizeBuy, Ask, MaxSlippageBuy, 0, 0, ExpertNameBuy, OrderIdBuy, 0, BuyStackColour);
                     if (ticket1 > -1)
                     {
                         OrderSelect(ticket1, SELECT_BY_TICKET);
                         bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss1, takeprofit1, 0, BuyStackColour);
                         NewOrderAlreadyOpened1 = TRUE;
                         if (ret1 == false)
                         Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                     }
               
 

Ok I am back with results.

You code works really nicely RaptorUK, however I still have the same problem with it as mine. It is throwing in wierd orders when the market gets hot. It backtests fine and normally forwardtests fine except when the market is raging...

Could this be the deafult MT4 Order Send.mqh causing this problem? Can anyone recommend a good OrderSend Library file for me to test? I will post back if I find one and this sorts out the error too.

Thanks again, I have never seen this sort of thing.

Regards.

 
RaptorUK:

OK I think I understand what you want to do . . . I think I do, I am not 100% sure I do . . . but I think I do.

What governs whether you open an additional order or not is the most recent order . . . not all the other older orders . . . it's easy just to place a single new trade . . .

you will need to define and set NewTradePlaced = false; at a relevant place . . .

You seem to be deciding whether you modify an order on an Order by Order basis ? is that correct ?


Ok I am back with results.

You code works really nicely RaptorUK, however I still have the same problem with it as mine. It is throwing in wierd orders when the market gets hot. It backtests fine and normally forwardtests fine except when the market is raging...

Could this be the deafult MT4 Order Send.mqh causing this problem? Could my EA be returning an OrderModify value of (-1) which cycles it through another OrderSend at times when the market it hot???

Can anyone recommend a good OrderSend Library file for me to test? I will post back if I find one and this sorts out the error too.

Thanks again, I have never seen this sort of thing.

Regards.


EDIT: sorry I was unable to edit my post above out of there, as I realised that I replied to my own post when I hit the reply button, forgive me I am still getting used to this forum interface.

 
Funky:

Ok I am back with results.

You code works really nicely RaptorUK, however I still have the same problem with it as mine. It is throwing in wierd orders when the market gets hot. It backtests fine and normally forwardtests fine except when the market is raging...

Could this be the deafult MT4 Order Send.mqh causing this problem? Can anyone recommend a good OrderSend Library file for me to test? I will post back if I find one and this sorts out the error too.

Thanks again, I have never seen this sort of thing.

Regards.

The problem is the logic behind your code . . . you need to be very, very clear about what you are trying to achieve with your code. Write it down in words not code . . then work through your code and see where it doesn't meet what you are trying to do.

I had to make a guess at what you were trying to do . . . it's not the best way of helping . .