Download MetaTrader 5

Bar Crossing Indicator-Line

To add comments, please log in or register
IXO30I 2016.07.16 19:04 

Hallo lovely people


I have the following problem: I want to code a EA that executes an order if the current bar crosses an indicator line! Actually it's simple I could just code (if Close >= MAline than sell etc.) but I plan to close the position when a take profit level is reached. If this happens while the current price is still lower than the MA the EA will execute the next order. At this point I'm stuck. It should just open the next position when the bar crosses the indicator line the next time. I already tried something like that: (If iOpen >= MAline && iClose <= MAline than sell). On my opinion it supposed to work but it doesn't! Does anyone have an advice for me or an idea how to fix this problem?

I appreciate every answer. Thx. for your help.

whroeder1 2016.07.16 21:05  
  1. "I could just code (if Close >= MAline than sell etc.) If this happens while the current price is still lower than the MA the EA will execute the next order." If it opens when close >= MA why would it execute the next order? Your code is broken.
  2. "On my opinion it supposed to work but it doesn't!" "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  3. "how to fix this problem" Show us your broken code - there are no mind readers here.
IXO30I 2016.07.17 00:49  

Ok, thx. WHRoeder

But my code isn’t broke. It’s just logical. When the price is under the MA the EA will execute the first order. But if I gain for example 100 Pips than the position will be closed (stoploss). Than one second later my EA will receive the next tick, so the code will run again. The EA realize that the price is still lower than the MA (it’s not always like this, but possible) So it opens a new position.


You see my problem? I can’t really show you the code because I didn’t write it so far. I have other EA’s with similar code, but Now I’m stuck I can’t even start coding because of this missing knowledge. I’m not searching a mind reader… just someone who already did something like this.

But ok, the possibility that you can help me is higher if you see some code:

//|                                           BBZ Range Breakout.mq4 |
//|                                                   Stefan Siegler |
//|                                  |

#property copyright     "Stefan Siegler"
#property link          ""
#property version       "1.00"
#property description   "This EA is a range trading system based on the BBZ and the MA crossing strategy"
#property strict

//| Includes and object initialization                               |

#include <Mql4Book\Trade.mqh>
CTrade Trade;
CCount Count;

#include <Mql4Book\Timer.mqh>
CTimer Timer;
CNewBar NewBar;

#include <Mql4Book\TrailingStop.mqh>
#include <Mql4Book\MoneyManagement.mqh>
#include <Mql4Book\Indicators.mqh>

//| Input variables                                                  |

sinput string TradeSettings;// Trade Settings
input int MagicNumber = 101;
input int Slippage = 10;
input bool TradeOnBarOpen = true;

sinput string MoneySettings;// Money Management
input bool UseMoneyManagement = false;
input double RiskPercent = 2;
input double FixedLotSize = 0.1;

sinput string StopSettings;// Stop Loss & Take Profit
input int StopLoss = 0;
input int TakeProfit = 0;

sinput string TrailingStopSettings;// Trailing Stop
input bool UseTrailingStop = true;
input int TrailingStop = 0;
input int MinProfit = 0;
input int Step = 0;

sinput string BreakEvenSettings;// Break Even Stop
input bool UseBreakEvenStop = false;
input int MinimumProfit = 0;
input int LockProfit = 0;

sinput string TimerSettings;// Timer
input bool UseTimer = false;
input int StartHour = 0;
input int StartMinute = 0;
input int EndHour = 0;
input int EndMinute = 0;
input bool UseLocalTime = false;

sinput string BbzSettings;//Bollinger Bands Z-Score
input int BbzPeriod = 10;
input ENUM_MA_METHOD BbzMethod = MODE_SMA;
input double BbzDeviation = 1;
input double BbzShift = 0;

//| Global variable and indicators                                   |

int gBuyTicket, gSellTicket;
double gOpen,gClose;

CiBBZ Bbz(_Symbol,_Period,BbzPeriod,BbzDeviation,BbzShift,BbzPrice);

//| Expert initialization function                                   |

int OnInit()
   // Set magic number

//| Expert tick function                                             |

void OnTick()

   // Check timer
   bool tradeEnabled = true;
   if(UseTimer == true)
      tradeEnabled = Timer.DailyTimer(StartHour,StartMinute,EndHour,EndMinute,UseLocalTime);
   // Check for bar open
   bool newBar = true;
   int barShift = 0;
   if(TradeOnBarOpen == true)
      newBar = NewBar.CheckNewBar(_Symbol,_Period);
      barShift = 1;

   double open = iOpen(NULL,0,0);
   double close = iClose(NULL,0,0);

   if(newBar == true && tradeEnabled == true)
           // Money management
      double lotSize = FixedLotSize;
      if(UseMoneyManagement == true)
         lotSize = MoneyManagement(_Symbol,FixedLotSize,RiskPercent,StopLoss); 
         // Open buy 
         if(open <= Bbz.Upper() && close >= Bbz.Upper() && Count.Buy() == 0 )
            int ticket = Trade.OpenBuyOrder(_Symbol,lotSize);
         // Open sell 
         else if(open > Bbz.Lower() && close < Bbz.Lower()  && Count.Sell() == 0 )
            int ticket = Trade.OpenSellOrder(_Symbol,lotSize);

   // Break even stop
   if(UseBreakEvenStop == true)
   // Trailing stop
   if(UseTrailingStop == true)
IXO30I 2016.07.18 16:24  
Someone have an idea? Would be great!!!
To add comments, please log in or register