Download MetaTrader 5

MQL4 Script Help

To add comments, please log in or register
Mitchell Eaton
Mitchell Eaton  

Hello Everyone,

I am new the MQL5 forum and Forex trading (Within the last 3-4 monts), so please bare with me if I seem to ask dumb or ill-informed questions. I do have some limited experience with programming and writing code, as it was a requirement to take coding classes to pass my Mechanical Engineering degree. I am now working on my first script and have seem to run into some trouble. I am not looking for anyone to program my script for me (I won't say no if you insist on it though!), I am just looking to seek the guidance and help of people who have been doing this for much longer than I; Thank you for your help in advance.

I have attached the script for your viewing. The idea for the script goes like this:

  • -Run this script once and a black horizontal line will appear. This line marks an entry point and can be used to quickly open stop and limit orders (by a second script). The Line can be dragged or set to where the user desires.
  • -Once the user has set the desired entry point, run this script a second time and 2 red horizontal lines will appear. In case you do not need a take profit level, you can delete one of the lines. 
  • -Run this script third time to remove all previously created lines.
There are 2 problems that I am having with running the script.

1. I would like the 2 red lines (Take Profit and Stop Loss) to be set from the black line (Entry) price, and not from the current price when the script is executed for the second time. I assume it has something to do with replacing TimeCurrent() with something else in the lines: ObjectCreate("StopLine1", OBJ_HLINE, 0, TimeCurrent(), Close[0]+(1.7*ATR)+Spread); and ObjectCreate("StopLine2", OBJ_HLINE, 0, TimeCurrent(), Close[0]-(1.7*ATR)+Spread); but I am not sure what it would need to make it work?

2. The second problem that I am having is when I try to add or subtract the spread price to the same lines of code as described above. If you take out the addition or subtraction of the spread, the code seems to work, but when they are put back into the code, no Take Profit or Stop Loss Lines appear when the script is executed for the second time. I think an example would best describe how I think this should work.


  • Execute script once on EURUSD Pair. 
  • Move Entry price from initial location of 1.09586 to 1.09558. 
  • Execute script once more. Current ATR = 0.0008 and Current spread = 0.00010 at time of second script execution.
  • After Second execution of script the window would have 3 lines: Entry = 1.09558, Stop Loss or Take Profit = 1.09704, and Stop Loss or Take Profit = 1.09412.
  • The Stop Loss or Take Profit will be decided by the direction of the trade in a second script that I am writing.

int start()
  double ATR = iATR(NULL, 0, 14, 0);
  double Spread = MarketInfo(Symbol(), MODE_SPREAD);
  if (ObjectFind("EntryLine1") < 0) {
      ObjectCreate("EntryLine1", OBJ_HLINE, 0, TimeCurrent(), Close[0]);
      ObjectSet("EntryLine1", OBJPROP_COLOR, Black);     
    } else {
    if ((ObjectFind("StopLine1") < 0) && (ObjectFind("StopLine2") < 0)) {
    ObjectCreate("StopLine1", OBJ_HLINE, 0, TimeCurrent(), Close[0]+(1.7*ATR)+Spread);
    ObjectSet("StopLine1", OBJPROP_COLOR, Red);
    ObjectCreate("StopLine2", OBJ_HLINE, 0, TimeCurrent(), Close[0]-(1.7*ATR)-Spread);
    ObjectSet("StopLine2", OBJPROP_COLOR, Red);
  } else {
      if (ObjectFind("StopLine1") >= 0) ObjectDelete("StopLine1");
      if (ObjectFind("StopLine2") >= 0) ObjectDelete("StopLine2");
      if (ObjectFind("EntryLine1") >= 0) ObjectDelete("EntryLine1");

 Thank you once again for your help  and expertise! 


Mitchell Eaton 

To add comments, please log in or register