This script is designed to help traders identify a "break-even" price level for their open trades on a specific currency pair within the MetaTrader platform. Here's a breakdown of what it does, avoiding technical jargon:
-
Initial Setup and Contact Information:
- The script starts by displaying the author's contact information (email address) in the corner of the chart. This is achieved by constructing the text string character by character using their ASCII number.
- It deletes any previous labels and horizontal lines created by this script for a clean display.
-
Calculating the Break-Even Price:
- The script analyzes all currently open trades for the specific currency pair the chart is displaying.
- It separates the trades into two groups: "buy" orders and "sell" orders.
- For "buy" orders, it adds up the total number of lots (trade size) and the total value of these trades (lots multiplied by their opening price).
- For "sell" orders, it subtracts the total number of lots and the total value of these trades. This is because sell orders profit when the price goes down, so they offset the buy orders.
- It then calculates the average opening price based on the remaining lots. This average price is considered the "break-even" level.
-
Displaying the Break-Even Level:
- If a break-even price can be calculated (meaning there are open trades), the script displays it in two ways:
- Horizontal Line: A horizontal line is drawn on the chart at the calculated break-even price. This provides a visual representation of the level.
- Alert: A pop-up message is displayed, showing the break-even price for the current currency pair.
- If a break-even price can be calculated (meaning there are open trades), the script displays it in two ways:
-
Functions Used:
GetDell
: Delete chart objects with a specific name.SetHLine
: Draws a horizontal line on the chart at a given price level.Label
: Adds a text label to the chart, typically used to display information.
In summary, this script automates the calculation and display of a break-even price level for a trader's open positions, helping them quickly visualize where they need the price to move to avoid losses on their trades.
//+------------------------------------------------------------------+
//| ytg_Lewel without the loss.mq4 |
//| YURIY TOKMAN |
//| yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "YURIY TOKMAN"
#property link "yuriytokman@gmail.com"
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----+
GetDell("label");
string char[256]; int i;
for (i = 0; i < 256; i++) char[i] = CharToStr(i);
string txt = char[70]+char[97]+char[99]+char[116]+char[111]+char[114]+char[121]+char[32]
+char[111]+char[102]+char[32]+char[116]+char[104]+char[101]+char[32]+char[97]
+char[100]+char[118]+char[105]+char[115]+char[101]+char[114]+char[115]+char[58]
+char[32]+char[121]+char[117]+char[114]+char[105]+char[121]+char[116]+char[111]
+char[107]+char[109]+char[97]+char[110]+char[64]+char[103]+char[109]+char[97]
+char[105]+char[108]+char[46]+char[99]+char[111]+char[109];Label("label",txt,2,3,15);
//----+
GetDell("ytg_s");
double lots=0;
double sum=0;
for (i=0; i<OrdersTotal(); i++)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY)
{
lots=lots+OrderLots();
sum=sum+OrderLots()*OrderOpenPrice();
}
if (OrderType()==OP_SELL)
{
lots=lots-OrderLots();
sum=sum-OrderLots()*OrderOpenPrice();
}
}
double price=0;
if (lots!=0) price=sum/lots;
string lossfree=" Does not exist";
if (price>0) lossfree=" = "+DoubleToStr(price,Digits);
string msg="Level without loss for "+Symbol()+lossfree+" ";
string line_name = "ytg_s " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
SetHLine(Aqua,line_name,price);
Alert(msg);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ ñîçäà¸ò ãîðèçîíòàëüíóþ ëèíèþ |
//| àâòîð: Þðèé Òîêìàíü |
//| e-mail: yuriytokman@gmail.com |
//| ICQ# 481-971-287 |
//| Skype: yuriy.g.t |
//+------------------------------------------------------------------+
void SetHLine(color cl, string nm="", double p1=0, int st=0, int wd=1)
{
if (ObjectFind(nm)<0) ObjectCreate(nm, OBJ_HLINE, 0, 0,0);
ObjectSet(nm, OBJPROP_PRICE1, p1);
ObjectSet(nm, OBJPROP_COLOR , cl);
ObjectSet(nm, OBJPROP_STYLE , st);
ObjectSet(nm, OBJPROP_WIDTH , wd);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ óäàëÿåò îáúåêòû ñ óêàçàííûì èìåíåì |
//| àâòîð: Þðèé Òîêìàíü |
//| e-mail: yuriytokman@gmail.com |
//| ICQ# 481-971-287 |
//| Skype: yuriy.g.t |
//+------------------------------------------------------------------+
void GetDell( string name)
{
string vName;
for(int i=ObjectsTotal()-1; i>=0;i--)
{
vName = ObjectName(i);
if (StringFind(vName,name) !=-1) ObjectDelete(vName);
}
}
//+----------------------------------------------------------------------+
//| Îïèñàíèå: Ñîçäàíèå òåêñòîâîé ìåòêè |
//| Àâòîð: Þðèé Òîêìàíü |
//| e-mail: yuriytokman@gmail.com |
//+----------------------------------------------------------------------+
void Label(string name_label, //Èìÿ îáúåêòà.
string text_label, //Òåêñò îáüåêòà.
int corner = 2, //Hîìåð óãëà ïðèâÿçêè
int x = 3, //Pàññòîÿíèå X-êîîðäèíàòû â ïèêñåëÿõ
int y = 15, //Pàññòîÿíèå Y-êîîðäèíàòû â ïèêñåëÿõ
int font_size = 10, //Ðàçìåð øðèôòà â ïóíêòàõ.
string font_name = "Arial", //Íàèìåíîâàíèå øðèôòà.
color text_color = LimeGreen //Öâåò òåêñòà.
)
{
if (ObjectFind(name_label)!=-1) ObjectDelete(name_label);
ObjectCreate(name_label,OBJ_LABEL,0,0,0,0,0);
ObjectSet(name_label,OBJPROP_CORNER,corner);
ObjectSet(name_label,OBJPROP_XDISTANCE,x);
ObjectSet(name_label,OBJPROP_YDISTANCE,y);
ObjectSetText(name_label,text_label,font_size,font_name,text_color);
}
Comments