EA - Adding code to adjust stoploss twice.

 
Hello!

I'm sad to admit it, but I've hit a wall with my EA. I've been studying and pulling apart EA's for days to try and get mine to work but nothing is meshing. I would be so so grateful for any input from a more experienced mind, if you have time of course 🙏 Very happy to learn and I hate asking for a shortcut but I've rewritten my code so many times now and it's starting to drive me crazy.

I'm trying to get my EA to move my stoploss to break even when I'm in profit of risk x 0.61 and then move it again to this first TP level when I'm in profit at risk x 2.63... Two target prices, two stop loss adjustments, per trade.I managed to get it working for a bit for break even, but nothing more. 

Any help is appreciated! ❤
 
Violet Cross: I'm sad to admit it, but I've hit a wall with my EA. I've been studying and pulling apart EA's for days to try and get mine to work but nothing is meshing. I would be so so grateful for any input from a more experienced mind, if you have time of course 🙏 Very happy to learn and I hate asking for a shortcut but I've rewritten my code so many times now and it's starting to drive me crazy. I'm trying to get my EA to move my stoploss to break even when I'm in profit of risk x 0.61 and then move it again to this first TP level when I'm in profit at risk x 2.63... Two target prices, two stop loss adjustments, per trade.I managed to get it working for a bit for break even, but nothing more. 

If you need help, then show your code where you attempt to achieve what you want. Describe in detail exactly the steps you are having difficulty with.

 
Fernando Carreiro #:

If you need help, then show your code where you attempt to achieve what you want. Describe in detail exactly the steps you are having difficulty with.

Sorry, I was definitely running on fumes and frustration last night and it wouldn't allow me to upload more than a certain number of characters so I gave up ^^' Unfortunately I can't find a way to upload it... I will keep attempting to keep pull EA's apart and see if I can find a solution. Thank you for taking the time to reply to my post :)

 
Violet Cross #: Sorry, I was definitely running on fumes and frustration last night and it wouldn't allow me to upload more than a certain number of characters so I gave up ^^' Unfortunately I can't find a way to upload it... I will keep attempting to keep pull EA's apart and see if I can find a solution. Thank you for taking the time to reply to my post :)

You are not making sense. There is no character limit on posts (at least that I have ever reached).

When posting code, use the "</>" icon or Alt-S. You can also attach the file itself instead by clicking on "+ Attach file" at the bottom left of the input field for the post, and then selecting your "*.mq?" file.

 
Violet Cross: I'm trying to get my EA to move my stoploss to break even when I'm in profit of risk x 0.61 and then move it again to this first TP level when I'm in profit at risk x 2.63...
  1. Remember (in persistent storage,) your initial risk and do it. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

 
Fernando Carreiro #:

You are not making sense. There is no character limit on posts (at least that I have ever reached).

When posting code, use the "</>" icon or Alt-S. You can also attach the file itself instead by clicking on "+ Attach file" at the bottom left of the input field for the post, and then selecting your "*.mq?" file.

Thank you, I have updated the post with the file. It's my third rewrite today and I know it's probably horrific, so please be kind :') I've tried many variations, so any guidance would be appreciated. Thank you again!

 
William Roeder #:
  1. Remember (in persistent storage,) your initial risk and do it. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

Yes, thank you, as I mentioned I was having problems uploading my code but someone has helpfully pointed me in the right direction :)

I hadn't thought this far ahead if I'm honest! Haha, but thank you, valid points and something to fine tune once I have the skeleton of my EA up and running :) 

Mmm, yes I've tried single function loops and multiple functions that flow into one another, but I'm clearly not understanding something... I know it's frustrating dealing with novices like me, but I really am trying to better myself and learn so please be kind :)

Thank you so much for your reply!

 
Rewritten again, still no dice :') 
Files:
 
  1. What is this BS?
    Simplify
       switch(TradeFilter())   {
          case 1: OpenTrade(1);
          break;
          case 2: OpenTrade(2);
          break;
          case 3: OpenTrade(3);
          break;
          case 4: OpenTrade(4);
          break; 
          case 5: OpenTrade(5);
          break;
          ⋮
          case 29: OpenTrade(29);
          break;
          case 30: OpenTrade(30);
          break;
          case 31: OpenTrade(31);
          break;
          case 32: OpenTrade(32);
          break;
          
          default: 
          break;
          }
    OpenTrade(TradeFilter());
    

  2. if (OrdersTotal()==0)                                                                                 //If total number of orders open is 0, return 0 (do nothing)
       {
          return;
       }
    

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  3. } else if (number == 2) {
          
          orderType = ORDER_TYPE_BUY;
    

    There is no such type in MT4.

  4. bool                 FilterOrders         = true;
    ⋮
    if (FilterOrders == true)     FilterOrders(GuestTicket, GuestFib61BUY, GuestFib61SELL, GuestFib261BUY, GuestFib261SELL, GuestOrderOpenPrice);

    Do not name variables and functions the same.

  5.       if(OrderModify(ResForTicket,ResForOrderOpenPrice,ResForNewSL, OrderTakeProfit(),0,Plum))
    

    You can not modify the open price once the order opens. No where do you check.

  6. Violet Cross #: Rewritten again, still no dice :') 

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:
  1. What is this BS?
    Simplify

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  3. There is no such type in MT4.

  4. Do not name variables and functions the same.

  5. You can not modify the open price once the order opens. No where do you check.

  6. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Ouch. Thank you?
Reason: