//+------------------------------------------------------------------+ //| Test_AvgBreakEvenPosn.mq4 | //| John Taylor | //| http://www.linkedin.com/in/johntaylorhk | //+------------------------------------------------------------------+ #property copyright "John Taylor" #property link "http://www.linkedin.com/in/johntaylorhk" #include #define DEBUG 1 int start() { string msg=""; double avgLong=GetAvgPrice(OP_BUY); if (avgLong >0) msg=StringConcatenate("Average Long price is ",DoubleToStr(avgLong,Digits)); else msg=("There is no long position"); double avgShort=GetAvgPrice(OP_SELL); if (avgShort >0) msg=StringConcatenate(msg,"\nAverage Short price is ",DoubleToStr(avgShort,Digits)); else msg=StringConcatenate(msg,"\nThere is no short position"); MessageBox(msg); return(0); } double GetAvgPrice(int orderType) { double result=0.0; int nOrders=0; for (int pos=OrdersTotal()-1;pos>=0;pos--) { if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) { Alert("GetAvgPosn: OrderSelect failed - ",ErrorDescription(GetLastError())); } else { if (OrderType()==orderType) { if(DEBUG==1)Print("GetAvgPosn: Adding (",nOrders,") to total ",DoubleToStr(result,Digits), " order #",OrderTicket()," with open price ",DoubleToStr(OrderOpenPrice(),Digits)); result=result+OrderOpenPrice(); nOrders+=1; } } } if(nOrders>0) result=result/nOrders; if (DEBUG==1)Print("GetAvgPrice: returning result ",DoubleToStr(result,Digits)); return(result); }