Download MetaTrader 5

Draw Horizontal Line on Specific Chart

To add comments, please log in or register
sschearer
13
sschearer  

I have an EA that I am currently using and works great. I am running one EA that looks at all of my open trades (lots of different pairs) in a loop and deals with each individually, not just the pair that my EA is attached to. The EA gets into trades but does not put a Stop Loss until it meets certain criteria, then modifies the order and puts in a SL.

What I'd like to be able to do is that when my EA sees that a SL has been added or modified, to place a red horizontal line on the corresponding chart for that pair. I have read lots of code to add a horizontal line, that's easy. But I want to be able to tell MT4 which chart to add it to. To make things more complicated, it needs to be independent of the time frame.

Example:

On first loop my software detects that a trade for the USD/JPY has met certain criteria and adds a SL at 109.50. I want the EA to be able to add a red horizontal line to the USDJPY chart. But the chart that is active might be a M1, M5, H1 time frame.....I want it to be able to add the line to the USDJPY chart no matter what time frame I have the chart on at that particular time.

Then on the next loop it adds a SL for the GBP/NZD at 1.7800. I want the EA to be able to add a red horizontal line at the GBPNZD chart, no matter what time frame I currently have the chart on.

Hope that isn't too complicated. Like I said, I can add a horizontal line to a chart through code for the chart that the EA is attached to, that's easy. I just can't figure out how to add a line to a different chart, and one that might be on any given time frame.

nicholishen
943
nicholishen  
sschearer:

I have an EA that I am currently using and works great. I am running one EA that looks at all of my open trades (lots of different pairs) in a loop and deals with each individually, not just the pair that my EA is attached to. The EA gets into trades but does not put a Stop Loss until it meets certain criteria, then modifies the order and puts in a SL.

What I'd like to be able to do is that when my EA sees that a SL has been added or modified, to place a red horizontal line on the corresponding chart for that pair. I have read lots of code to add a horizontal line, that's easy. But I want to be able to tell MT4 which chart to add it to. To make things more complicated, it needs to be independent of the time frame.

Example:

On first loop my software detects that a trade for the USD/JPY has met certain criteria and adds a SL at 109.50. I want the EA to be able to add a red horizontal line to the USDJPY chart. But the chart that is active might be a M1, M5, H1 time frame.....I want it to be able to add the line to the USDJPY chart no matter what time frame I have the chart on at that particular time.

Then on the next loop it adds a SL for the GBP/NZD at 1.7800. I want the EA to be able to add a red horizontal line at the GBPNZD chart, no matter what time frame I currently have the chart on.

Hope that isn't too complicated. Like I said, I can add a horizontal line to a chart through code for the chart that the EA is attached to, that's easy. I just can't figure out how to add a line to a different chart, and one that might be on any given time frame.


#property strict
#include <Arrays\List.mqh>
#include <ChartObjects\ChartObjectsLines.mqh>

CList list;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   AddLinesToCharts(Symbol(),Bid);
}
//+------------------------------------------------------------------+

void AddLinesToCharts(string symbol, double price)
{
   for(long ch=ChartFirst();ch>0;ch=ChartNext(ch))
   {
      if(ChartSymbol(ch)==symbol)
      {
         ObjectsDeleteAll(ch,"__LINE__");
         CChartObjectHLine *line = new CChartObjectHLine();
         line.Create(ch,"__LINE__"+string(price)+string(ch),0,price);
         line.Width(3);
         line.Color(clrPink);
         line.Description("MyLine");
         list.Add(line);
         line.Detach(); // optional: for script demonstration so obj stays on chart after script finishes
      }
      ChartRedraw(ch);
   }
}
sschearer
13
sschearer  

Awesome! With some tweaking I got it to work.

Now, any advice to get it removed if I were to remove the StopLoss line we just created? So.....once it adds a line, that line is there until the EA is removed. However, once that position either closes, or I removed the SL, I want that line to go away.

I thought about maybe something like line.Clear at the beginning of each ChartObject loop to clear all lines, then it only adds the ones back that are still there. I also thought maybe you could tell just a specific chart to clear. However, I was unsuccessful in getting either of those to work.

Help would be appreciated.

whroeder1
16086
whroeder1  
sschearer: The EA gets into trades but does not put a Stop Loss until it meets certain criteria,
That means you can not define your risk
Risk depends on your initial stop loss, lot size, and the value of the pair.
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.
sschearer
13
sschearer  

My EA bases the SL on volitility and a few other things. So, it gets into the trade, then analyzes a few things and modifies the trade to add a S/L. The S/L is added very close to the open, so it is not live for very long without a S/L. I've been trading this method for many years manually, so I know how to assess risk. Now I'm just trying to automate it, that's all.

To add comments, please log in or register