Lets say I have a few Buy orders and Sell orders
What I want do is :
- Exit all the trades " Basket" at 5 pips more than the breakeven price of the open trades
What I am trying to do in the first for loop is find the value of open trades + swap and convert it to pips , and this is the part where I am getting stuck
I have tried few ideas but I have reached a dead end, I know the problem is in the first for statement but I can't solve it, any help is highly appropriated
PHP Code:
SymbolPL = 0;
OrdLots = 0;
Equity = 0;
MinPro = 5;
for(i = 0; i < OrdersTotal(); i++)
{OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{if(OrderType() == OP_BUY) OrdLots += OrderLots();
if(OrderType() == OP_SELL) OrdLots -= OrderLots();
Equity += OrderProfit() + OrderSwap();
}
}
MinPro= MathRound (MathAbs(Equity /OrdLots)+MinPro);
//--- Count the open trades
int i;
int count=0;
for(i=0;i<OrdersTotal();i++)
{ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()==MagicNumber && OrderType()<2)
{ count++;
}
}
//--- find the profit in pips of the open trades
RefreshRates();
double profits=0,openPrice=0,points=0;
string sym="";
int i;
int totalOrders=OrdersTotal();
for(i=0;i<totalOrders;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()==MagicNumber)
{
sym=OrderSymbol();
openPrice=OrderOpenPrice();
if(OrderType()==0)
{
profits+=(MarketInfo(sym,MODE_BID)-openPrice)/MarketInfo(sym,MODE_POINT);
}
if(OrderType()==1)
{
profits+=(OrderOpenPrice()-MarketInfo(sym,MODE_ASK))/MarketInfo(sym,MODE_POINT);
}
}
}
//--- Close when the open trades are 5 pips more than the breakeven price
if (count>1 && profits>MinPro )
{
for(i = OrdersTotal()-1; i >=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Plum);
}
}
}