Closing orders on achieving hedge profit level

 

Can anyone point me in the right direction? I have been successfully hedging currencies for a while now, but because most of the action happens in the middle of the night (I live in Australia) I am not able to close out positions when overall profit reaches a certain level. I tried to find an EA in this forum which closes all open orders once a user defined profit level has been reached , but nothing seems exactly what I need.

I will usually hedge GBP/USD (or EUR/USD) with USD/CHF so I need the EA to be able to calculate what the current profit level is (ie/- swap plus profit due to rate movements) , and if it hits a level that has been preset, it will close all open orders (and pending orders if thats possible) so that profit is locked in. Although it seems quite basic I have tried to work it out myself but not getting anywhere. So if there actually isnt anything like this on this site, I would be most grateful if anyone can help an old dinosaur like me.

Thanks

 
Isn't that what AccountEquity() gives you? The following would do like you say, assuming of course that you don't do other things on the same account.
You'd drop this EA onto your GBPUSD chart and set required profit.

extern double required_profit = 100;

int start()
{
    if ( AccountEquity() < AccountBalance() + required_profit )
        return( 0 );

    int slippage = 1;
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS ) )
            continue;
        switch ( OrderType() ) {
        case OP_BUY:
            OrderClose( OrderTicket(), OrderLots(), Bid, slippage );
            break;
        case OP_SELL:
            OrderClose( OrderTicket(), OrderLots(), Ask, slippage  );
            break;
        default: // All pending orders
            OrderDelete( OrderTicket() );
            break;
        }
    }
    return( 1 / 0 ); // This division by zero might stop the EA from getting invoked again
}
 
richplank, that sounds like the sort of EA I need, but I need it to take into account more than one currency pair ie. combined profit of say GBP/USD and USD/CHF. Typically these currency pairs will move in opposite directions a lot of the time, so not much profit there, mostly loss situations. What I need the EA for is when both these currency pairs move in the same direction, and create a profit situation. When it hits my profit target, all open positions and pending orders are closed out. Any further thoughts?
richplank:
Isn't that what AccountEquity() gives you? The following would do like you say, assuming of course that you don't do other things on the same account.
You'd drop this EA onto your GBPUSD chart and set required profit.

extern double required_profit = 100;

int start()
{
    if ( AccountEquity() < AccountBalance() + required_profit )
        return( 0 );

    int slippage = 1;
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS ) )
            continue;
        switch ( OrderType() ) {
        case OP_BUY:
            OrderClose( OrderTicket(), OrderLots(), Bid, slippage );
            break;
        case OP_SELL:
            OrderClose( OrderTicket(), OrderLots(), Ask, slippage  );
            break;
        default: // All pending orders
            OrderDelete( OrderTicket() );
            break;
        }
    }
    return( 1 / 0 ); // This division by zero might stop the EA from getting invoked again
}


richplank wrote:

Isn't that what AccountEquity() gives you? The following would do like you say, assuming of course that you don't do other things on the same account.
You'd drop this EA onto your GBPUSD chart and set required profit.

extern double required_profit = 100;

int start()
{
    if ( AccountEquity() < AccountBalance() + required_profit )
        return( 0 );

    int slippage = 1;
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS ) )
            continue;
        switch ( OrderType() ) {
        case OP_BUY:
            OrderClose( OrderTicket(), OrderLots(), Bid, slippage );
            break;
        case OP_SELL:
            OrderClose( OrderTicket(), OrderLots(), Ask, slippage  );
            break;
        default: // All pending orders
            OrderDelete( OrderTicket() );
            break;
        }
    }
    return( 1 / 0 ); // This division by zero might stop the EA from getting invoked again
}

 
richplank:
Isn't that what AccountEquity() gives you? The following would do like you say, assuming of course that you don't do other things on the same account.
You'd drop this EA onto your GBPUSD chart and set required profit.

extern double required_profit = 100;

int start()
{
    if ( AccountEquity() < AccountBalance() + required_profit )
        return( 0 );

    int slippage = 1;
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS ) )
            continue;
        switch ( OrderType() ) {
        case OP_BUY:
            OrderClose( OrderTicket(), OrderLots(), Bid, slippage );
            break;
        case OP_SELL:
            OrderClose( OrderTicket(), OrderLots(), Ask, slippage  );
            break;
        default: // All pending orders
            OrderDelete( OrderTicket() );
            break;
        }
    }
    return( 1 / 0 ); // This division by zero might stop the EA from getting invoked again
}


Your code needs the improvement

extern double required_profit = 100;
 
int start()
{
    double closePrice;
    if ( AccountEquity() < AccountBalance() + required_profit )
        return( 0 );
 
    int slippage = 1;
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS ) )
            continue;
        RefreshRates();
        switch ( OrderType() ) {
        case OP_BUY:
            closePrice=MarketInfo(OrderSymbol(),MODE_BID);
            OrderClose( OrderTicket(), OrderLots(), closePrice, slippage );
            break;
        case OP_SELL:
            closePrice=MarketInfo(OrderSymbol(),MODE_ASK);
            OrderClose( OrderTicket(), OrderLots(), closePrice, slippage  );
            break;
        default: // All pending orders
            OrderDelete( OrderTicket() );
            break;
        }
    }
    return( 1 / 0 ); // This division by zero might stop the EA from getting invoked again
}
See MarketInfo and RefreshRates