Problem with EA code 2 (SL manager)

 

Hello,

Thanks again with your help while I am learning MQL.

Here is my next problem.... I am SOOOO close on this one.... I have it doing everything that I want it to... However it is modifying the SLs on EVERYTICK if the GAP is over this criteria, rather than just EXECUTING ONCE...

It is a very simple code, and a very simple problem that I am unable to work out at this stage, and any help would assist my learning..

This is just the BUY SIDE atm, however I have wrote allowances for the IFGAP for OP_SELL side for later.


The function of this EA is to:

1. initiate a buy trade if one not exist.

2. at TP level 1, move SL up.... at TP level 2, move SL up again.... at TP level 3, move SL up yet again last time. Nice and simple really..


The noob problem I am having is:

1. It executes my OrderModify EVERYTICK if over GAP... I can't think how to change the logic, but once I am shown, I will know for future...


Thanks in advance.

Kind Regards and happy pips everyone.


int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
    
    OnEveryTick();
    
}

void OnEveryTick()
{
    if (DetectBrokerDigits == false && FiveDigitBroker) PipValue = 10;
    if (DetectBrokerDigits && (NDigits == 3 || NDigits == 5)) PipValue = 10;
    
    IfGap_Level_1();
    IfGap_Level_2();
    IfGap_Level_3();
    IfBUYOrderDoesNotExist();
    
}

void IfGap_Level_1()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            if ((Gap_Level_1 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_1*PipValue*Point))) ||
            ((Gap_Level_1 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
            {
                BuyOrderModifyLevel_1();
                
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void BuyOrderModifyLevel_1()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            double price = Ask;
            if (UseAskLineorNot == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void IfGap_Level_2()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            if ((Gap_Level_2 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_2*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_2*PipValue*Point))) ||
            ((Gap_Level_2 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_2*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_2*PipValue*Point)))))
            {
                BuyOrderModifyLevel_2();
                
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void BuyOrderModifyLevel_2()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            double price = Ask;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_2*PipValue*Point, price + BuyTakeprofit_Level_2*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void IfGap_Level_3()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            if ((Gap_Level_3 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_3*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_3*PipValue*Point))) ||
            ((Gap_Level_3 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_3*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_3*PipValue*Point)))))
            {
                BuyOrderModifyLevel_3();
                
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void BuyOrderModifyLevel_3()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            double price = Ask;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_3*PipValue*Point, price + BuyTakeprofit_Level_3*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void IfBUYOrderDoesNotExist()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == InitialOrderType && OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        Initial_BuyOrder();
        
    }
}

void Initial_BuyOrder()
{
    double SL = Ask - InitialBuyStoploss*PipValue*Point;
    if (BuyStoploss_Level_3 == 0) SL = 0;
    double TP = Ask + Initial_Takeprofit*PipValue*Point;
    if (Initial_Takeprofit == 0) TP = 0;
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, InitialBuyLots, Ask, 4, 0, 0, "My Expert", BUY_Magic, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, InitialBuyLots, Ask, 4, SL, TP, "My Expert", BUY_Magic, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
            
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}



int deinit()
{
    if (ClearAllObjects) ObjectsDeleteAll();
    
    
}

 
Funky:

The noob problem I am having is:

1. It executes my OrderModify EVERYTICK if over GAP... I can't think how to change the logic, but once I am shown, I will know for future..

If you don't want to execute it for every tick when do you want to execute it ? do you mean every tick but only once ? to do that just check if the SL & TP have already been adjusted . . .

One other comment . . . IfGap_Level_1() loops through all open orders looking for those that match Symbol and Magic number, then checks if the order is in profit a certain amount, etc . . . if it is it calls BuyOrderModifyLevel_1(), this function loops through all open orders looking for those that match Symbol and Magic number and is a Buy . . do see a little repetition here ? why don't you just pass the Ticket number from IfGap_Level_1() to BuyOrderModifyLevel_1() ?

 
Funky:

Hello,

Thanks again with your help while I am learning MQL.

Here is my next problem.... I am SOOOO close on this one.... I have it doing everything that I want it to... However it is modifying the SLs on EVERYTICK if the GAP is over this criteria, rather than just EXECUTING ONCE...

It is a very simple code, and a very simple problem that I am unable to work out at this stage, and any help would assist my learning..

This is just the BUY SIDE atm, however I have wrote allowances for the IFGAP for OP_SELL side for later.


The function of this EA is to:

1. initiate a buy trade if one not exist.

2. at TP level 1, move SL up.... at TP level 2, move SL up again.... at TP level 3, move SL up yet again last time. Nice and simple really..


The noob problem I am having is:

1. It executes my OrderModify EVERYTICK if over GAP... I can't think how to change the logic, but once I am shown, I will know for future...


Thanks in advance.

Kind Regards and happy pips everyone.


You are not a NOOB. Judging from elegant ability to be able to write and then call functions, the way you did. You should be able to see for yourself, that your function calls (depth of logic, etc) can be simplfied perhaps to 1-2 functions.

Instead of:

OrderOpenPrice() 

in your condition:

if ((Gap_Level_1 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_1*PipValue*Point) ||

have you considered/tried using OrderStopLoss() ?

 
RaptorUK:

If you don't want to execute it for every tick when do you want to execute it ? do you mean every tick but only once ? to do that just check if the SL & TP have already been adjusted . . .

One other comment . . . IfGap_Level_1() loops through all open orders looking for those that match Symbol and Magic number, then checks if the order is in profit a certain amount, etc . . . if it is it calls BuyOrderModifyLevel_1(), this function loops through all open orders looking for those that match Symbol and Magic number and is a Buy . . do see a little repetition here ? why don't you just pass the Ticket number from IfGap_Level_1() to BuyOrderModifyLevel_1() ?

Hello again Raptor, thank you again for your time. The way that I put it, it does sound confusing. I am having trouble joining all of these components together properly I guess. At the moment the main problem I am having is 'it is modifying the orders on every tick', however I can now see some other problems too after your comments. With the last part of what you said, if I understand you, do you mean to insert the BuyOrderModify code inside the IfGap_Level_1 inside a {} after checking if the Gap was reached rather than in its own {}? Like in this example below?


void OverGap1_soModify()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            if ((Gap_Level_1 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_1*PipValue*Point))) ||
            ((Gap_Level_1 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
               {
                double price = Ask;
                if (UseAskLineorNot == false)
                {
                 price = OrderOpenPrice();
                 }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

And you know, I am feeling like dejavue with my other EA in a way now that box is taking form (here https://forum.mql4.com/42870 - non intentional similarity, as it is a different EA with a different purpose, however this is pointing to same areas I need to learn such as including two functions together, I promise I wasn't trying to fill the forum up with redundancy)...

Can I therefore ask on the offchance if would a variable IFNEWORDEREXIST = TRUE in the same spot would solve another issue. See I want the last Open Trade that was made in ModeTrades to be the one that I am comparing to the IfGap_Level_X at all times (I guess that part is similiar to my other).


Once I fix that error with OrderModify happening on EVERYTICK while it is stuck inside that {} if that box above is not correct, and deal with the Latest Order Select Issue, I will be then faced with the next drama that while IfGap_Level_3 is satisfied, so is IfGap_Level_2, and IfGap_Level_1.

I sort of want the EA to act as a SL manager, and (despite the initial single buy order) I am going to use it to manage multiple trades. The first buy was just so that I could backtest it ok. I would like this code to:

If Order's Pips gets to IfGap_Level_1, I want it to go to BuyOrderModifyLevel_1, then modify existing orders, and forget this part of the code.

IfOrder's Pips gets to IfGap_Level_2, I want it to go to BuyOrderModifyLevel_2, then modify existing orders, and forget this part of the code

If Order's Pips gets to IfGap_Level_3, I want it to go to BuyOrderModifyLevel_3, then modify existing orders, and forget this part of the code

I hope I am not too far off, it felt simple at first.

I was hoping I could just insert a "break;" or something at the end of each block and get away with this code...
diostar:

You are not a NOOB. Judging from elegant ability to be able to write and then call functions, the way you did. You should be able to see for yourself, that your function calls (depth of logic, etc) can be simplfied perhaps to 1-2 functions.

Instead of:

in your condition:

have you considered/tried using OrderStopLoss() ?

Ah thanks diostar, you are too kind with your comments. I am ok at writing basic scripts, however I am not far from that level, and now joining it all together is causing me a headache. Thank you for the encouragement though, the forumulas and things all make sense to me, not so much the program grammer/syntax.

I believe that you might be correct what you are saying, by condensing the code down to 1-2 functions, I am just unsure how...

If I had the experience, I would have the IFGAP and MODIFY ORDER in the one block as I think both you and Raptor are suggesting, however I was playing it safe at this stage... I guess if I was a pro, I would have the TPs on an array or switch, and set old TP=oldTP+TPincrement_Level, and could have it all in one block in about 10 lines of code... Eventually I will do this in the very last If Gap & ModifyOrder {} so that the SL increment is stepped up at programmable intervals, however the first few I want to leave to set manually depending on the market conditions...

I haven't considered using OrderStopLoss() in the formula. I guess as I will be varying the SLs a little for the first few moves, I really want it based of OrderOpenPrice and the Pips it has increased since being opened. I hadn't explained the EA very well, apologies. Thank you for your time too mate, much appreciated.

 
Funky:

Hello again Raptor, thank you again for your time. The way that I put it, it does sound confusing. I am having trouble joining all of these components together properly I guess. At the moment the main problem I am having is 'it is modifying the orders on every tick', however I can now see some other problems too after your comments. With the last part of what you said, if I understand you, do you mean to insert the BuyOrderModify code inside the IfGap_Level_1 inside a {} after checking if the Gap was reached rather than in its own {}? Like in this example below?

You could do that but it isn't really what I meant . . .

Have a look at this see if it makes sense . . .

          (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
            {
                BuyOrderModifyLevel_1(OrderTicket() );    //  <-----  pass the ticket number to the function
                
            }
.
.
.


void BuyOrderModifyLevel_1(int Ticket)
{
    if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES))
    {
       double price = Ask;
       if (UseAskLineorNot == false)
       {
          price = OrderOpenPrice();
       }
       bool ret = OrderModify(Ticket, OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
       if (ret == false)
       Print("OrderModify() error - ", ErrorDescription(GetLastError()));
     
    }
    
}

The other thing I mentioned was about checking if the Order had already been modified by looking to see what it's SL & TP were, in other words, if you have already modified the order on the last tick don't do it again on this tick . . . is that what you were looking to do ?

 
RaptorUK:

You could do that but it isn't really what I meant . . .

Have a look at this see if it makes sense . . .

The other thing I mentioned was about checking if the Order had already been modified by looking to see what it's SL & TP were, in other words, if you have already modified the order on the last tick don't do it again on this tick . . . is that what you were looking to do ?


This is great stuff for my learning... I did not realise that one could put (OrderTicket()) after a function name in the first place, cool. So that attributes my latest Open Order's Ticket Number, cool. This is a very very handy tip for the things I am into atm, thank you.

(OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
            {
                BuyOrderModifyLevel_1(OrderTicket() );    //  <-----  pass the ticket number to the function
                
            }

and here I see you made the variable Ticket. I actually didn't know one could declare a variable in this part of the code. Can this be done because it was given a value in that last part above? I undertstand how it could work, then, it uses this last Trade's Order Ticket number here instead of my 'unspecific' OrderTicket(). It makes sense. It will be a lot more accurate than OrderSelect(i++.. etc for me.

void BuyOrderModifyLevel_1(int Ticket)
{
    if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES))
    {
       double price = Ask;
       if (UseAskLineorNot == false)
       {
          price = OrderOpenPrice();
       }
       bool ret = OrderModify(Ticket, OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
       if (ret == false)
       Print("OrderModify() error - ", ErrorDescription(GetLastError()));
     
    }
    
}


It is nearly 1am here so I will have a closer look tomorrow, thank you heaps for your time.... I have a feeling that I might have to insert this logic into the "If_Gap_Levels{}" instead of the "BuyModify{}" block. I hadn't explained it overly clearly, apologies. I think this tip will help me check that latest ticket for GAP, and then MODIFY ALL previous orders with that OP_BUY and BUY_Magic etc.

The other thing I mentioned was about checking if the Order had already been modified by looking to see what it's SL & TP were, in other words, if you have already modified the order on the last tick don't do it again on this tick . . . is that what you were looking to do ?

Yes, this is exactly where I am stuck on actually... I am just not sure how to specify this... Do I make a bool value of NeedsModifying = false, and then convert it to True when IfGap criteria is met? I can think of the logic, just not the syntax, however I learned from your tips for me in my last EA to even to be able to suggest my last sentence. Thanks again Raptor.

 
Funky:

This is great stuff for my learning... I did not realise that one could put (OrderTicket()) after a function name in the first place, cool. So that attributes my Ticket that meets the Gap criterea to that function BuyOrderModifyLevel_1? This is a very very handy tip for the things I am into atm, thank you.

and here I see you made the variable Ticket. I actually didn't know one could declare a variable in this part of the code. Can this be done because it was given a value in that last part above? I undertstand how it could work, then, it uses this last Trade's Order Ticket number here instead of my 'unspecific' OrderTicket(). It makes sense. It will be a lot more accurate than OrderSelect(i++.. etc for me.

It's part of the Function definition . . . just like any of the standard Functions, e.g. OrderModify ( https://docs.mql4.com/trading/OrderModify ) OrderModify is a function and you pass variables to it, a minimum of 5 variables, the 6th is optional. you can do exactly the same with your own Functions . . .

You are welcome. Night.

 
RaptorUK:

It's part of the Function definition . . . just like any of the standard Functions, e.g. OrderModify ( https://docs.mql4.com/trading/OrderModify ) OrderModify is a function and you pass variables to it, a minimum of 5 variables, the 6th is optional. you can do exactly the same with your own Functions . . .

You are welcome. Night.


Thanks Raptor.... ah you caught me while I was editing the last sentence into that post, as yeah you were right with what I was after...

Thanks for reminding me of that .doc again... it is funny that I have read that particular .doc on OrderMofidy so many times... It used to look chinese to me that manual, however as I progress, more advanced tips stand out. I will read it again now, and I bet it will be as clear as daylight that feature you mentioned for me, however thanks for pointing it out to me so I knew to look for it.

--copied from above, as I had edited that last part in unknowingly while you were writing....

The other thing I mentioned was about checking if the Order had already been modified by looking to see what it's SL & TP were, in other words, if you have already modified the order on the last tick don't do it again on this tick . . . is that what you were looking to do ?

Yes, this is exactly where I am stuck on actually... I am just not sure how to specify this... Do I make a bool value of NeedsModifying = false, and then convert it to True when IfGap criteria is met? I can think of the logic, just not the syntax, however I learned from your tips for me in my last EA to even to be able to suggest my last sentence. Thanks again Raptor.

G'night from Downunder.

 
RaptorUK:

It's part of the Function definition . . . just like any of the standard Functions, e.g. OrderModify ( https://docs.mql4.com/trading/OrderModify ) OrderModify is a function and you pass variables to it, a minimum of 5 variables, the 6th is optional. you can do exactly the same with your own Functions . . .

You are welcome. Night.


Hi Raptor,

I took things a little further using the ideas you have shown me. I realised that your code would of fixed my problem, but then once the orders hit my second TP level, it will be hard to manage..

I didn't realise I could drop the OrderSelect i++ and just choose my own Ticket in that equation, so I have tried this as it will solve all my problems with multiple trades... would this work?

If someone can confirm for me that this code would work, I would be delighted... thanks again for support ;)

void BuyPendingOrder_1()
{
    int expire = TimeCurrent() + 60 * 0;
    double price = NormalizeDouble(Ask, NDigits) + PriceOffset_1*PipValue*Point;
    double SL = price - BuyStoploss_1*PipValue*Point;
    if (BuyStoploss_1 == 0) SL = 0;
    double TP = price + BuyTakeprofit_1*PipValue*Point;
    if (BuyTakeprofit_1 == 0) TP = 0;
    if (0 == 0) expire = 0;
    int PendingOrderTicket_1 = OrderSend(Symbol(), OP_BUYSTOP, BuyLots_1, price, 4, SL, TP, "EA_Pending_Order1", BUY_Magic, expire, Blue);
     ///--^^^----^^^ allocate this ticket number to PendingOrderTicket_1 above
    if (PendingOrderTicket_1 == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void IfGap_Level_1()
{
    if (OrderSelect(PendingOrderTicket_1,SELECT_BY_POS,MODE_TRADES)) ///--<<<---- select ticket number from PendingOrderTicket_1
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            if ((Gap_Level_1 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_1*PipValue*Point))) ||
            ((Gap_Level_1 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
            {
                BuyOrderModifyLevel_1; ///--<<<----if PendingOrderTicket_1 is over Gap_Level_1, then proceed to BuyOrderMoidfyLevel_1 section etc
                
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void BuyOrderModifyLevel_1() 
{
    if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)  ///--<<<----select all trades of that pair and magic
    {
        
            double price = Ask;
            if (UseAskLineorNot == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
             ///--^^^^^^---- modify all trades of that pair and magic if it made it this far
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            
    }
    
}
 

There is a problem doing what you are suggesting . . . .

Imagine your EA is running . . . . then your MT4 is interrupted for whatever reason, power failure, MT4 crash, PC needs a reboot, etc . . . your code can't pickup from where it left off because it no longer has the correct value for PendingOrderTicket_1

 
RaptorUK:

There is a problem doing what you are suggesting . . . .

Imagine your EA is running . . . . then your MT4 is interrupted for whatever reason, power failure, MT4 crash, PC needs a reboot, etc . . . your code can't pickup from where it left off because it no longer has the correct value for PendingOrderTicket_1


Hi Raptor, thanks again.

Is that the only problem with the above code??? I think worst case the PendingOrderTicket_1 would not get modified, and would close at its SL.

Is it ok how I used the OrderSelect function?

 if (OrderSelect(PendingOrderTicket_1,SELECT_BY_POS,MODE_TRADES)) ///--<<<---- select ticket number from PendingOrderTicket_1

The big problem I think I will have, is that when OrderTicket_1 is over the first Gap_Level1, it will still modify the order ON EVERYTICK that this criterea is met.

Is that the case? If so, would the below code fix it?

bool HasBeenExecuted = false;
/// (further up in declared variables^^^)

void BuyOrderModifyLevel_1()
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
      {
        
            double price = Ask;
            if (UseAskLineorNot == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss_Level_1*PipValue*Point, price + BuyTakeprofit_Level_1*PipValue*Point, 0, Modify_Order_Colour);
            HasBeenExecuted = false;
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            {
            HasBeenExecuted = true;
            }
     }    
   
}


Or would I do this with the CheckGapLevel_1, so that it only checks there once? And if so, did I do it correctly below???? Is there somewhere I should be putting a "break"?

bool HasBeenExecuted = false;
/// (further up in declared variables^^^)

void IfGap_Level_1()
{
    if (OrderSelect(PendingOrderTicket_1,SELECT_BY_POS,MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == BUY_Magic)
        {
            if ((Gap_Level_1 < 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() < Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_1*PipValue*Point))) ||
            ((Gap_Level_1 > 0 && ((OrderType() == OP_BUY && Ask - OrderOpenPrice() > Gap_Level_1*PipValue*Point) ||
            (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_1*PipValue*Point)))))
            HasBeenExecuted = false;
            {
            HasBeenExecuted = true;                
            BuyOrderModifyLevel_1;
               
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

Thanks again ;)

Reason: