Forum on trading, automated trading systems and testing trading strategies
Hello,
Please EDIT your post and use the SRC button when you post code.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi all,
I would like to run an EA that allows me to close an open trade that is X number of lots once the loss reaches Y amount.
For example, I might have many open positions with various lot sizes (eg, 0.01, 0.02, 0.03, 0.04, 0.05 etc). What I would like the EA to do is only for trades with lot size 0.01, close the trade when losses reach $20, and ignore the rest of the positions in other lot sizes.
Below is an EA which closes any position once it losses reach $20. How do I incorporate the lot size condition into the EA? Any help will really be greatly appreciated!
-----------------------------------------------------------
extern int LT = -20; // Maximum Loss target
int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : if ( OrderProfit() <= LT) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : if ( OrderProfit() <= LT) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
//Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(0);
}
}
return(0);
}