Problem with EA code 2 (SL manager) - page 3

 

This link here is 'way' above me at this stage, however I can see how I could make a cool library file to tackle my issues... https://www.mql5.com/en/articles/1390 . I am a bit nervous building ticket arrays atm and am hoping I can get around it for now.. since when I modify, it is only all of that Pair and Magic etc..

I have done everything correctly before it gets to the Modify section. It selects by ticket correctly, and then passes through to the Modify block... that part is great.

I just can't see 'if things get stuck in this modify section' or am I in trouble because it 'needs to pass through these sections to grab the next order'.

I had hoped I could send it to the modify block and get it to modify all orders of that Magic Number and Symbol. That is why I tried a little true or false switch.

If it needs to pass through these sections each tick 'to select the next order', I might be in trouble.

Maybe I could just manually name the tickets that need to be modified, but I don't think this is the problem, as it gets them correctly, it just cycles every tick, and I am a noob to know why...

Would this be another option?

 for(int i=1; i<=OrdersTotal(); i++)
     { 
      if (OrderSelect(i-1,SELECT_BY_POS)
       executedpreviously = true;
       while(true)
         {
         modify section is ok...   
          }
        executedpreviously = false;
      }
//or would I declare this back to false elsewhere... 
//I am just not sure if it needs to run here each tick to grab the next order

Would a continue;, return(), or break; be useful somewhere?

 

Ah, I saw an error in one of my previous examples...

if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && !IfGap_SELLLevel_00AlreadyExe) 

I think should be:

bool IfGap_SELLLevel_00AlreadyExe = FALSE; //(this time up in declared varibales before program start)


    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && IfGap_SELLLevel_00AlreadyExe == FALSE)
        { 
            double Level_00_SL = Bid + Stoploss_Level_00*PipValue*Point;
            if (Stoploss_Level_00 == 0) Level_00_SL = 0;
            double Level_00_TP = Bid - Takeprofit_Level_00*PipValue*Point;
            if (Takeprofit_Level_00 == 0) Level_00_TP = 0;
            bool ret00 = OrderModify(OrderTicket(), OrderOpenPrice(), Level_00_SL, Level_00_TP, 0, Modify_Order_Colour);
            IfGap_SELLLevel_00AlreadyExe = TRUE;
            if (ret00 == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
            break;  (EDIT: going to try this break in the morning, pls let me know if I am off track totally anyway)
         }
    }

That should isolate it in that section... is 5am so yet to test... (lost an hour, damn daylight savings haha)

If I let it through to this section one way, it spams order modifies on every tick with the latest order... and if I let it through the other way it scrolls orders properly, but it still modifies the order cache on every tick...

I can get the orders scrolling through backwards 5,4,3,2,1 or forwards 1,2,3,4,5, but I can't think of a way to let it through to modify all orders without spamming them.

I wonder 1) Do I need to let it through once to modify all the orders that pass through..., or 2) does it need to pass through each time that it needs to select a new order?

I am still learning that is all.. If nobody can steer me in right direction, I will try this line above, with a break and the end and see if that ends.... it has to be soemthing simple..

 
IfGap_SELLLevel_00AlreadyExe == FALSE)  this may still compile, but truly speaking, this a "mis-spelt" boolean syntax.

You did NOT make any error. The first one is GOOD to go...

if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && !IfGap_SELLLevel_00AlreadyExe) <--- this is GOOD to go..


I may had quite a similar prb before, if I read all this correctly. I was trying to create my own "modify stop/limit orders by the minimum MODE_STOP level, if Bid/Ask moves x Pips away..."

Is your case similar?

 
diostar:

You did NOT make any error. The first one is GOOD to go...


I may had quite a similar prb before, if I read all this correctly. I was trying to create my own "modify stop/limit orders by the minimum MODE_STOP level, if Bid/Ask moves x Pips away..."

Is your case similar?



Thanks Diostar, I am trying to sort of make a heap of with Pending Orders, then as it gets to the next successful Pending Order, bring up the old stops.

I will try with the ! again then... so many combinations to try... and I see, I should of used "false" for the boolean value instead of FALSE... yeah it did compile and work ok still, but same problem. I might find that draft again and try with "false".. In that example 2, it wouldn't scroll through the orders, and modified every tick, so I will try again with this variable changed.

I can get it to pass to the modify block and rotate the orders either forwards or backwards, but it modifies every tick, and if I put in a true or false filter after modified, it only modifies the first order in the cache....

to me this suggests the EA needs to get to this block each tick to get the next order (I wasn't sure if when it gets to this block, if I could just seal it in here, and get it to rotate through the orders once)...

this presents me a problem, as any true or false questions are not letting it through...

I think if I learned how to make an order cache and select through them I would still have the same problem...I have had all combinations happening, but not all at the same time. I have got it to modify once and then move on, but it only got the first order and stopped... so I am wondering if I should use that with a switch with each orderticket actually specified... I wasn't sure if my bool OrderTicket() to report my error message, messed up the declaration of a Pending_OrderTicket_XX too... It has to be something simple..

how did you solve this problem in yours?

 
void IfGap_SELLLevel_00()
{
    if (OrderSelect(FirstRunSell_ticket_00,SELECT_BY_TICKET,MODE_TRADES)) 
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01)
        {
            if ((Gap_Level_00 < 0 && (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_00*PipValue*Point))
             || (Gap_Level_00 > 0 && (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_00*PipValue*Point)))
            {
                SellOrderModifyLevel_00();
            }
        }
    }
    
    
}

void SellOrderModifyLevel_00()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && !IfGap_SELLLevel_00AlreadyExe) 
        { 
            double Level_00_SL = Bid + Stoploss_Level_00*PipValue*Point;
            if (Stoploss_Level_00 == 0) Level_00_SL = 0;
            double Level_00_TP = Bid - Takeprofit_Level_00*PipValue*Point;
            if (Takeprofit_Level_00 == 0) Level_00_TP = 0;
            OrderModify(OrderTicket(), OrderOpenPrice(), Level_00_SL, Level_00_TP, 0, Modify_Order_Colour);
            IfGap_SELLLevel_00AlreadyExe = true;
            
        }
    }
    
}


Regardless of how I declare in global variables...

bool IfGap_SELLLevel_00AlreadyExe;

..it seems to modify once, BUT only one of the orders, I assume the last order with the way I scroll orders.

I took out the ret00 before order ticket in case too...

At least it is not every tick, I could use this example IF it would still scroll through my orders when conditions for the first forumula are true (but on not every tick 'spamming modifies' after scrolling ok, like my most successful attempt does...)

This attempt didn't work either:

**sent here first from that other decision, but for Pending Order 5 instead of FirstSell 00 etc...

void SellOrderModifyLevel_05()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_TICKET))
    {
        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && !IfGap_SELLLevel_00AlreadyExe) 
        { 
            double Level_05_SL = Bid + Stoploss_Level_05*PipValue*Point;
            if (Stoploss_Level_05 == 0) Level_05_SL = 0;
            double Level_05_TP = Bid - Takeprofit_Level_05*PipValue*Point;
            if (Takeprofit_Level_05 == 0) Level_05_TP = 0;
            OrderModify(FirstRunSell_ticket_00, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            OrderModify(SellPendingOrderTicket_01, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            OrderModify(SellPendingOrderTicket_02, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            OrderModify(SellPendingOrderTicket_03, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            OrderModify(SellPendingOrderTicket_04, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            OrderModify(SellPendingOrderTicket_05, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
            IfGap_SELLLevel_00AlreadyExe = true;
            
        }
    }
    
}

I shows clearer what I am trying to do anyway, I wish this above would work, i would of been done if so...

I am hoping I am on the right track anyway.. I still not understand why my example 1 earlier was wrong, silly thing does exactly what I want, but every tick... I can't even get it to work when I program in the orders, so I am totally lost again..

Any pointers?


EDIT: I am getting really exhausted, but just thought of this, however will have to try later on as ran out of time...

void IfGap_SELLLevel_05()
{
    if (OrderSelect(SellPendingOrderTicket_05,SELECT_BY_TICKET,MODE_TRADES)) 
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01 && !IfGap_SELLLevel_00AlreadyExe)
        {
            if ((Gap_Level_05 < 0 && (OrderType() == OP_SELL && OrderOpenPrice() - Bid < Gap_Level_05*PipValue*Point))
             || (Gap_Level_05 > 0 && (OrderType() == OP_SELL && OrderOpenPrice() - Bid > Gap_Level_05*PipValue*Point)))
            
             {
              for (int i=OrdersTotal()-1; i >= 0; i--)
              if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                     {
                     if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SELL_Magic01) 
                      { 
                       double Level_05_SL = Bid + Stoploss_Level_05*PipValue*Point;
                       if (Stoploss_Level_05 == 0) Level_05_SL = 0;
                       double Level_05_TP = Bid - Takeprofit_Level_05*PipValue*Point;
                       if (Takeprofit_Level_05 == 0) Level_05_TP = 0;
                       OrderModify(FirstRunSell_ticket_00, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       OrderModify(SellPendingOrderTicket_01, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       OrderModify(SellPendingOrderTicket_02, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       OrderModify(SellPendingOrderTicket_03, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       OrderModify(SellPendingOrderTicket_04, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       OrderModify(SellPendingOrderTicket_05, OrderOpenPrice(), Level_05_SL, Level_05_TP, 0, Modify_Order_Colour);
                       IfGap_SELLLevel_00AlreadyExe = true;
            
                      }
                }
           }
       }
    
    
}
 

are you dealing with fractional point?

 
diostar:

are you dealing with fractional point?


I think I doubled the SL and TP value if that is what you mean, so that it gets assigned the price to set the SL and TP. I hope I was not wrong here doing this.

I just need MT4 to get to that block, and then forget about it.... I have a feeling the expression is going to be too complicated for MT4, but I will keep trying...

 
Funky:


I think I doubled the SL and TP value if that is what you mean, so that it gets assigned the price to set the SL and TP. I hope I was not wrong here doing this.

I just need MT4 to get to that block, and then forget about it.... I have a feeling the expression is going to be too complicated for MT4, but I will keep trying...

No what I mean is, are you dealing 4/5 digits - 0.0001, or 0.00001 Point?
 
diostar:
No what I mean is, are you dealing 4/5 digits - 0.0001, or 0.00001 Point?


Ah 5 digits... I use this earlier on...

extern bool IsFiveDigitBroker = true;
extern bool DoDetectBrokerDigits = true;
double PipValue=1;
int NDigits = 4; 
// ----

int init()
{
    NDigits = Digits;
}

// ----
init start()

int start()
{
   OnEveryTick();
}
void OnEveryTick()
{
    if (DoDetectBrokerDigits == false && IsFiveDigitBroker) PipValue = 10;
    if (DoDetectBrokerDigits && (NDigits == 3 || NDigits == 5)) PipValue = 10;



    //then I go on to check the IfGaps on every tick...

    IfGap_SELLLevel_00();
    IfGap_SELLLevel_01();
    IfGap_SELLLevel_02();
    IfGap_SELLLevel_03();
    IfGap_SELLLevel_04();
    IfGap_SELLLevel_05();

 }



And I was thinking earlier, I might try turning this Detect Digits as my first library insert, hehe, but one step at a time, I have to learn to crawl first, hehe.

I am not getting any error messages 130 price to close or anything.... I wish it were something simple like that..

Last page I had example 1 working, and lots of other similar examples, I just wish I could let it modify once per tick...

Instead of trying to get all of my previous SELL orders at once, I wonder if I should just make a new box for each of them... yeah I am really lost how to cycle through all my old SELLS in any of my attempts, and modify them once.


&& (OrderStopLoss() < Stoploss_Level_00)

instead of

!IfGap_SELLLevel_00AlreadyExe

You know you suggest something earlier Diostart that has got me thinking...

I am wondering if a !IfGap_SELLLevel_00AlreadyExe idea won't work (if it is not going to grab all of the orders and modify them in the same pass through before I lock off that block), I wonder if I use your idea before, and put a question like this in there... I will pop back if it works.

If I am on the wrong track though please let me know, cheers ;)

 
Funky:


Ah 5 digits... I use this earlier on...



And I was thinking earlier, I might try turning this Detect Digits as my first library insert, hehe, but one step at a time, I have to learn to crawl first, hehe. <-- wise move. You are crawling. With libraries, you WILL end up walking.

I am not getting any error messages 130 price to close or anything.... I wish it were something simple like that..

Last page I had example 1 working, and lots of other similar examples, I just wish I could let it modify once per tick... <--- see below

Instead of trying to get all of my previous SELL orders at once, I wonder if I should just make a new box for each of them... yeah I am really lost how to cycle through all my old SELLS in any of my attempts, and modify them once.<--- see below


Chances are this may offer some assistance, I hope.

Initially, my symbols was 4 digits, all trailing stops, stop/limit orders modify, went just as expected. Then my broker decided to go fractional. I made the necessary changes and saw my pips went like -3.4, -4.5, 0.1, 4.6, 7.3, etc, etc. I thought this was ok, but actually not: Suppose I set my limit=7, and say, new tick 1,tick 2,tick 3 happen sequentially:

1) 7.3>limit, then order is modified... broker fills 1 slippage off...

2) 7.5>limit, then order is modified....broker fills 0 slippage...

3) 7.1>limit, then order is modified...broker fills 2 slippage off...

So all in all, I get my orders modifed 3 times for all 3 ticks. having realise this, I decided to use MathRound() to round those to nearest integer, so 1,2,3 becomes

1) mathround(7.3) =7 >limit, NOT TRUE, not modified

2) mathround(7.5)=8 >limit, TRUE, order is modified

3) mathround(7.1)=7>limit, NOT TRUE, not modified

Actually Im still working on how to improve the above...so far they are working ok, but I think there must be better ways, than mine.

Reason: