Results 1 to 3 of 3
 0 Attachment(s)    

Thread: Close all open trades after x profit/loss and disable ea

  1. #1
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default Close all open trades after x profit/loss and disable ea

    hi all

    does anyone have a code snippet where orders closed and the ea is disabled on a chart, after reaching specific profit or loss? Disabling the ea should only be on the current chart not in general for all charts.

    Example: trading 3 pairs, 1 account, the ea is working mit unique magic number on each pair, one pair hits the loss (negative profit calculation) closing all trades and disable the ea on this pair, like removing it (1st choice) or disabled trading (ea active, 2nd choice), independent from the account balance or equity overall or the other running charts/pairs?
    The profit/loss calculation must be done on the open orders (+- profit) of this chart/pair, not on account balance or equity!

    PlusProfit=200
    NegProfit=300

    thx for any suggestions

    IX

  2. #2

    Default

    double TotalProfit;

    for(int i=0; i < OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS);

    if(OrderMagicNumber() == Magic.Number)
    TotalProfit != OrderProfit();
    }

    if(TotalProfit >= PlusProfit || TotalProfit <= NegProfit)
    {
    //Close all trades
    for(i=0; i < OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS);

    if(OrderSymbol() == Symbol() &&
    OrderMagicNumber() == Magic.Number)
    {
    if(OrderType() == OP_BUY)
    {
    OrderClose(OrderTicket(), OrderLots(), Bid, 3);
    i--;
    }

    if(OrderType() == OP_SELL)
    {
    OrderClose(OrderTicket(), OrderLots(), Ask, 3);
    i--;
    }
    }
    }
    }

    In order to stop the EA from working you need to set a global variable.. it depends on your EA.
    FXCode Inc.
    Expert Advisor Developer

  3. #3
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by FXCode View Post
    double TotalProfit;

    for(int i=0; i < OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS);

    if(OrderMagicNumber() == Magic.Number)
    TotalProfit != OrderProfit();
    }

    if(TotalProfit >= PlusProfit || TotalProfit <= NegProfit)
    {
    //Close all trades
    for(i=0; i < OrdersTotal(); i++)
    {
    OrderSelect(i, SELECT_BY_POS);

    if(OrderSymbol() == Symbol() &&
    OrderMagicNumber() == Magic.Number)
    {
    if(OrderType() == OP_BUY)
    {
    OrderClose(OrderTicket(), OrderLots(), Bid, 3);
    i--;
    }

    if(OrderType() == OP_SELL)
    {
    OrderClose(OrderTicket(), OrderLots(), Ask, 3);
    i--;
    }
    }
    }
    }

    In order to stop the EA from working you need to set a global variable.. it depends on your EA.
    Thanks FXCode!

    will test it and report back

Similar Threads

  1. Replies: 1
    Last Post: 09-03-2011, 21:22
  2. Replies: 2
    Last Post: 04-15-2010, 16:44
  3. Close few trades after profit
    By przem81621 in forum MQL programming
    Replies: 1
    Last Post: 05-29-2009, 09:12
  4. Close all open trades after every x profit
    By begu in forum MQL programming
    Replies: 1
    Last Post: 04-09-2009, 19:03

Tags for this Thread

closing, magic, magic number, pairs, profit, stop, test, trading

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •