Download MetaTrader 5

Change Case Break Even or Buy or Sell.

To add comments, please log in or register
johnpratt261
38
johnpratt261 2014.05.26 13:06 

Hi everyone. I'm not good at programming

I have code Break Even but it only show oneline with case all buy sell with every pair.

How to fix it bring two line , one break even for sell , one break for buy. (every pair)


From case when i close All Buy Sell Order (one line break even)

To case i only close only Sell Order, i only closs only Buy Order. (two Line break even , one for buy , one for sell)

if u understand pls let me know, thank dude.


#property copyright "© 2007 RickD"
#property link      "www.e2e-fx.net"

#define major   1
#define minor   0

#property indicator_chart_window
#property indicator_buffers 0

extern bool DrawSymbolChart = true;
extern bool DrawBreakeven = true;
extern int Corner = 0;
extern int FontSize = 8;
extern int dy = 40;

extern color _Header = OrangeRed;
extern color _Text = DodgerBlue;
extern color _Data = Black;
extern color _Separator = MediumPurple;
extern color _Breakeven = MediumBlue;



string prefix = "capital_";
string sepstr = "---------------------------------------------------------------";

void init()
{
  //Comment("");
  clear();
}

void deinit()
{
  //Comment("");
  clear();
}

void clear()
{
  string name;
  int obj_total = ObjectsTotal();
  for (int i=obj_total-1; i>=0; i--)
  {
    name = ObjectName(i);
    if (StringFind(name, prefix) == 0) ObjectDelete(name);
  }
}

void start()
{
  clear();

  string Sym[];
  double Equity[];
  double Lots[];
  ArrayResize(Sym, 0);
  ArrayResize(Equity, 0);
  ArrayResize(Lots, 0);
 
  string eq;
 
  int cnt = OrdersTotal();
  for (int i=0; i<cnt; i++)
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   
    int type = OrderType();
    if (type != OP_BUY && type != OP_SELL) continue;
   
    bool found = false;
   
    int size = ArraySize(Sym);
    for (int k=0; k<size; k++)
    {
      if (Sym[k] == OrderSymbol()) {
        Equity[k] += OrderProfit() + OrderCommission() + OrderSwap();
        if (type == OP_BUY) Lots[k] += OrderLots();
        if (type == OP_SELL) Lots[k] -= OrderLots();
        found = true;
        break;
      }
    }
   
    if (found) continue;
   
    int ind = ArraySize(Sym);
    ArrayResize(Sym, ind+1);
    Sym[ind] = OrderSymbol();
   
    ArrayResize(Equity, ind+1);
    Equity[ind] = OrderProfit() + OrderCommission() + OrderSwap();
   
    ArrayResize(Lots, ind+1);
    if (type == OP_BUY) Lots[k] = OrderLots();
    if (type == OP_SELL) Lots[k] = -OrderLots();
  }
 
  if (DrawSymbolChart==true){
    nb_drawLabel("symbols",  "Symbol",   30+FontSize, dy,   _Header);
    nb_drawLabel("equities",  "Equity",   130+FontSize,dy,   _Header);
    nb_drawLabel("breakeven","Breakeven",220+FontSize,dy,   _Header);
    nb_drawLabel("tmp1",     sepstr,     20+FontSize, dy+FontSize+2,_Separator);
  }
 
  double sum = 0;
  string level0 = "";
 
  size = ArraySize(Sym);
  for (i=0; i<size; i++)
  {
    if (Lots[i] == 0) {
      level0 = "Lock";
    }
    else {
      int dig = MarketInfo(Sym[i], MODE_DIGITS);
      double point = MarketInfo(Sym[i], MODE_POINT);
     
      double COP = Lots[i]*MarketInfo(Sym[i], MODE_TICKVALUE);
      double val = MarketInfo(Sym[i], MODE_BID) - point*Equity[i]/COP;
      level0 = DoubleToStr(val, dig);
    }
   
    if (Equity[i] > 0) eq = "+$"+DoubleToStr(MathAbs(Equity[i]),2);
    else               eq = "-$"+DoubleToStr(MathAbs(Equity[i]),2);
   
    if (DrawSymbolChart==true){    
      nb_drawLabel("symbol"+i,   Sym[i],30+FontSize, (i+1)*(FontSize*2)+2+dy,_Text);
      nb_drawLabel("equity"+i,   eq,    120+FontSize,(i+1)*(FontSize*2)+2+dy,_Data);
      nb_drawLabel("breakeven"+i,level0,230+FontSize,(i+1)*(FontSize*2)+2+dy,_Data);
    }
   
    if (Sym[0]==Symbol())
        if (DrawBreakeven==true){
            ObjectCreate(prefix+"breakevenline",OBJ_HLINE,0,0,StrToDouble(level0));
            ObjectSet(prefix+"breakevenline",OBJPROP_COLOR,_Breakeven);
            ObjectSet(prefix+"breakevenline",OBJPROP_STYLE,STYLE_DASH);
        }
   
    sum += Equity[i];
  }
   
  if (sum > 0) eq = "+$"+DoubleToStr(MathAbs(sum),2);
  else         eq = "-$"+DoubleToStr(MathAbs(sum),2);
 
  if (DrawSymbolChart==true){
    nb_drawLabel("tmp2",  sepstr, 20+FontSize,  (i+0.6)*(FontSize*2)+dy, _Separator);
    nb_drawLabel("total", "Total",30+FontSize,  (i+1.2)*(FontSize*2)+dy, _Text);
    nb_drawLabel("equity", eq,    120+FontSize, (i+1.2)*(FontSize*2)+dy, _Data);
  }
 
}

void nb_drawLabel(string name, string text,
                  int xdistance, int ydistance,
                  color fontcolor)
{
    string n = prefix + name;
    if (ObjectFind(n) == -1)
        ObjectCreate(n,OBJ_LABEL,0,0,0);
    ObjectSet(n,OBJPROP_XDISTANCE,xdistance);
    ObjectSet(n,OBJPROP_YDISTANCE,ydistance);
    ObjectSet(n,OBJPROP_CORNER,Corner);
    ObjectSetText(n,text,FontSize,"Tahoma",fontcolor);
}


Eristocrat
154
Eristocrat 2014.05.26 13:52  
    for (int k=0; k<size; k++)
    {
      if (Sym[k] == OrderSymbol()) {
        Equity[k] += OrderProfit() + OrderCommission() + OrderSwap();
        if (type == OP_BUY) Lots[k] += OrderLots();
        if (type == OP_SELL) Lots[k] -= OrderLots();
        found = true;
        break;
      }
    }
In this loop the Equity is added for BUY orders and substracted for SELL orders. Based on that, the breakeven is calculated in this case
    else {
      int dig = MarketInfo(Sym[i], MODE_DIGITS);
      double point = MarketInfo(Sym[i], MODE_POINT);
     
      double COP = Lots[i]*MarketInfo(Sym[i], MODE_TICKVALUE);
      double val = MarketInfo(Sym[i], MODE_BID) - point*Equity[i]/COP;
      level0 = DoubleToStr(val, dig);
    }
and the corresponding Object is created here
        if (DrawBreakeven==true){
            ObjectCreate(prefix+"breakevenline",OBJ_HLINE,0,0,StrToDouble(level0));
so you have to calculate the equity for BUY and SELL seperatly and Create another Object HLINE.
btw: the code is awkward, no comments and what is that typecasting thingies for? I think its not from you? Its also based on very old mt5 version and is not compatible to actual builds. My Advice, throw it away, read some tutorials on indicators and do it on your own
To add comments, please log in or register