• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 11:49pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 11:49pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

run, pips, run! 9 replies

help, help, help (templates) 5 replies

Help, Help, Help 3 replies

I need help making my MoneyMaker EA run quicker 1 reply

Broker to run EA's with...I need help. 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 2
Attachments: Help - EA run amok!
Exit Attachments

Help - EA run amok!

  • Post #1
  • Quote
  • First Post: Nov 4, 2021 10:18am Nov 4, 2021 10:18am
  •  pepperb
  • | Joined Mar 2020 | Status: Member | 38 Posts
I coded a portion of my EA to close half my position when I get to TP1 and move my stop to breakeven + a certain amount of pips. And it does...over and over again! It closes half my position, then on the next tick, it closes half, and half, until it gets down to 0.01 lot size. And I'm not sure why. As part of my if hierarchy, i have it stop if the Stop Loss has been moved. It should close half the position, move the stop loss (which it does), and then stop. Here's the code:

Inserted Code
void CloseHalfTrade()
{
   double tp2 = MathAbs(OrderTakeProfit() - OrderOpenPrice());
   double tp1 = NormalizeDouble(tp2 * 0.5, Digits);
   double breakevenPips = PipsToAddToBreakeven / GetPipValue();
  
//----
   for(int b=OrdersTotal()-1; b>=0; b--)
      {
      if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==ChartSymbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice()>=tp1)
                  if(OrderOpenPrice()>OrderStopLoss())
                     {
                      OrderClose(OrderTicket(), OrderLots()/2, Bid, 3, clrNONE);
                      OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + breakevenPips, OrderTakeProfit(), 0,clrNONE);
                      Alert(OrderSymbol() + " TP1 Hit, closing half trade & moving stop to BE+ 2 pips");
                      SendNotification(OrderSymbol() + " TP1 Hit, closing half trade & moving stop to BE+ 2 pips");
                     }
      }
(I didn't include the Sell side, but you see what it would be.) So as it's executing, first it closes half, then it modifies the remaining order to move the SL, then the next time it does the loop, it should find that the OrderPrice is no longer > OrderStopLoss, so it should stop. Yet in reality it seems like it closes all my positions by half, then moves the SL. Though it could just be my human perception of what's going on, being too slow to see what's actually happening in real time.

The only thing that I can think of is that the server is executing the OrderClose faster than the OrderModify, so that it's looping through before my Stop Loss can be moved by the server. Maybe I should put a sleep() statement at the end?

Anyway, can anyone see if I'm missing something simple & obvious?

Thanks,

Pepper...
  • Post #2
  • Quote
  • Nov 4, 2021 10:39am Nov 4, 2021 10:39am
  •  amando77
  • | Joined Nov 2008 | Status: Member | 96 Posts
nothing run amok, fix your code
The biggest drawdown is ahead of you
 
 
  • Post #3
  • Quote
  • Nov 4, 2021 10:45am Nov 4, 2021 10:45am
  •  pepperb
  • | Joined Mar 2020 | Status: Member | 38 Posts
Quoting amando77
Disliked
nothing run amok, fix your code
Ignored
Yes, I think it's obvious that my code needs fixing, thank you. That is why I created the post in the first place. If I knew what the problem was, I wouldn't be asking for help.
 
 
  • Post #4
  • Quote
  • Nov 4, 2021 10:59am Nov 4, 2021 10:59am
  •  eess
  • Joined Feb 2021 | Status: Member | 415 Posts
Quoting pepperb
Disliked
{quote} Yes, I think it's obvious that my code needs fixing, thank you. That is why I created the post in the first place. If I knew what the problem was, I wouldn't be asking for help.
Ignored

When half of the order was closed, the order is still open, so as long as the order is still open and the criteria is met (Bid-OrderOpenPrice()>=tp1), it will keep executing that code, that's why it keeps halving your lots on every tick until it reaches 0.01.
 
 
  • Post #5
  • Quote
  • Nov 4, 2021 11:08am Nov 4, 2021 11:08am
  •  pepperb
  • | Joined Mar 2020 | Status: Member | 38 Posts
Quoting eess
Disliked
{quote} When half of the order was closed, the order is still open, so as long as the order is still open and the criteria is met (Bid-OrderOpenPrice()>=tp1), it will keep executing that code, that's why it keeps halving your lots on every tick until it reaches 0.01.
Ignored
Well that's what I thought at first, so I put in the line about checking if the OrderOpenPrice was greater than the OrderStopLoss. Because after it executes the OrderClose, it does an OrderModify to move the Stop Loss 2 pips above the Open Price. So on the next loop, the OrderOpenPrice is no longer greater than the OrderStopLoss, so that should stop the loop, right?
 
 
  • Post #6
  • Quote
  • Nov 4, 2021 11:14am Nov 4, 2021 11:14am
  •  eess
  • Joined Feb 2021 | Status: Member | 415 Posts
Quoting pepperb
Disliked
{quote} Well that's what I thought at first, so I put in the line about checking if the OrderOpenPrice was greater than the OrderStopLoss. Because after it executes the OrderClose, it does an OrderModify to move the Stop Loss 2 pips above the Open Price. So on the next loop, the OrderOpenPrice is no longer greater than the OrderStopLoss, so that should stop the loop, right?
Ignored

Did you check if the stoploss was moved successfully to breakeven by checking the result of the return value of OrderModify?

What I would do to debug this would be to print the values out like stoploss, open price etc to verify which part of the code is causing the problem, but since you didnt attach the full source code, it's impossible to help to that extent, so I can only give you suggestions. And of course I understand that you can't post the full source for privacy reason also.
 
 
  • Post #7
  • Quote
  • Nov 4, 2021 11:22am Nov 4, 2021 11:22am
  •  pepperb
  • | Joined Mar 2020 | Status: Member | 38 Posts
I reviewed the data from my terminal, and it is running the OrderClose function 3 times before it is executing the OrderModify, all in about 1.5~2 seconds. So I've added a Sleep(5000) function at the end of the loop. Hope that will at least give the OrderClose time to work and serve as a stopgap measure while I figure this out.

it is moving the Stop Loss as it is supposed to...just not fast enough.
 
 
  • Post #8
  • Quote
  • Edited at 11:53am Nov 4, 2021 11:37am | Edited at 11:53am
  •  eess
  • Joined Feb 2021 | Status: Member | 415 Posts
Quoting pepperb
Disliked
I reviewed the data from my terminal, and it is running the OrderClose function 3 times before it is executing the OrderModify, all in about 1.5~2 seconds. So I've added a Sleep(5000) function at the end of the loop. Hope that will at least give the OrderClose time to work and serve as a stopgap measure while I figure this out. it is moving the Stop Loss as it is supposed to...just not fast enough.
Ignored

Putting 5s delay there would delay the execution of the rest of the code.You can also try to move stoploss first before doing the OrderClose to see if it can solve better. The other way to do it without having to put a delay would be to set a global variable to store the initial Lot Size when you open the order, then in your function above, you can just verify if the OrderLots is equal to the initial lotsize, then close half of the order, this is assuming that you will only have one order open per symbol, if multiple orders can be opened for same symbol with different lot sizes, then need to store LotSize with the Order ticket number. And the 3rd way to try, perhaps the easiest, would be to open 2 orders with half the lot sizes, one with TP1, the other with TP2, when TP1 is reached, Order1 would be closed automatically, leaving Order 2 behind.
 
 
  • Post #9
  • Quote
  • Nov 4, 2021 4:10pm Nov 4, 2021 4:10pm
  •  pepperb
  • | Joined Mar 2020 | Status: Member | 38 Posts
Well I found the problem, and it was something simple, though as of yet I don't know how to solve it. When it does the OrderClose line, and closes half the order, what's left open no longer has the same OrderTicket number as it originally had. Turns out it wasn't moving my stop loss, or at least during my test today it wasn't moving it. I finally found a line that said "invalid Ticket Number" when it tried to execute OrderModify. Now I just have to figure out how to get the new OrderTicket number inside the same loop.
 
 
  • Post #10
  • Quote
  • Edited at 11:13pm Nov 4, 2021 10:23pm | Edited at 11:13pm
  •  eess
  • Joined Feb 2021 | Status: Member | 415 Posts
Move the stop loss first before closing the trade:

Inserted Code
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + breakevenPips, OrderTakeProfit(), 0,clrNONE);
OrderClose(OrderTicket(), OrderLots()/2, Bid, 3, clrNONE);

BEFORE:


AFTER:
Attached Images (click to enlarge)
Click to Enlarge

Name: 38c09ac8d2269d5a4e2f7c1c32e10606.png
Size: 30 KB Click to Enlarge

Name: 4fd0d5a4a58330baf319e3ba66f9c114.png
Size: 34 KB
 
 
  • Post #11
  • Quote
  • Last Post: Nov 5, 2021 7:00am Nov 5, 2021 7:00am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,843 Posts
Quoting pepperb
Disliked
I coded a portion of my EA to close half my position when I get to TP1 and move my stop to breakeven + a certain amount of pips. And it does...over and over again! It closes half my position, then on the next tick, it closes half, and half, until it gets down to 0.01 lot size. And I'm not sure why. As part of my if hierarchy, i have it stop if the Stop Loss has been moved. It should close half the position, move the stop loss (which it does), and then stop. Here's the code: void CloseHalfTrade() { double tp2 = MathAbs(OrderTakeProfit() - OrderOpenPrice());...
Ignored
The simple way to avoid a partial close to run multiple times is to check if the order was already partially closed. You can notice in the 'comment' column in the terminal it is now labeled with "from ...".

Put the below in your for loop after the OrderSelect():

Inserted Code
if(StringFind(OrderComment(), "from") == -1)

So only the orders not yet partially closed will go through the partial closure process.

Of course it works only if you have only 1 TP step for the partial close.
MT4/MT5 EAs/Indicators/Alerts coder
 
2
  • Platform Tech
  • /
  • Help - EA run amok!
  • Reply to Thread
0 traders viewing now
Top of Page
Forex Factory Blog Updated: Alerting All Members
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022