Please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.
EDIT: And please make sure to clean up your code and remove all those extra blank and empty lines.
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
Messages Editor -
OrderSend(Symbol(), OP_BUY, LotSize, MarketPrice, 3, StopLoss, TakeProfit, "Expert Advisor", 0, 0, clrGreen);
Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon. - Bastien1717:ligne 156 : ' } ' unexpected end of program
ligne 29 : ' { ' unbalanced parantheses
How do you expect us to know what line is 29 or 156? You didn't use the code button, you didn't mark the line.
-
OrderSend(Symbol(), OP_SELL, LotSize, MarketPrice, 3, StopLoss, TakeProfit, "Expert Advisor", 0, 0, clrRed); OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + TrailingStop, OrderTakeProfit(), 0, clrRed);
You can not use any Trade Functions until you first select an order. Why are you modifying the SL when you already set it while opening the order?
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
(your broker will be similar).
Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)
-
-
TrailingStop = TrailingStop * Point;
Do you really want to repeatedly multiply your globable variable by point? After the first time you have effectively zero.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello MQL5 Community,
I have 2 errors in my code, I need your help please !
ligne 156 : ' } ' unexpected end of program
ligne 29 : ' { ' unbalanced parantheses
Thank you so much for your help !
// Expert Advisor for MetaTrader
//#include <MT4/MT4.mqh>
// define input parameters
input int Bollinger_Periods = 20;
input double Bollinger_Deviation = 2.5;
input int SMA_Periods = 200;
input int RSI_Periods = 14;
input int EMA_Periods = 9;
// define strategy parameters
input int Trade_Timeframe = 1; // 1H timeframe
input int Wait_Timeframe = 5; // M5 timeframe
input double SL_Multiplier = 2.0; // TP must be at least twice superior of SL
input double Trailing_Stop = 10.0; // trailing stop with 10%
// define money management parameters
input double Max_Trade_Loss = 2.0; // maximum loss should not be more than 2% of the total trading account
input double Max_Daily_Loss = 9.0; // stop trading for the day if it loses more than 9% of the total trading account
// global variables
double StopLoss;
double TakeProfit;
// main function
int OnTick()
{
// check if trade is allowed
if (!IsTradeAllowed())
return(0);
// get market prices
double MarketPrice = Ask;
// calculate Bollinger bands
double Bollinger_Middle = iBands(NULL, 0, Bollinger_Periods, Bollinger_Deviation, 0, PRICE_CLOSE, MODE_SMA, 0);
double Bollinger_Lower = Bollinger_Middle - Bollinger_Deviation * iStdDev(NULL, 0, Bollinger_Periods, 0, MODE_SMA, PRICE_CLOSE, 0);
double Bollinger_Upper = Bollinger_Middle + Bollinger_Deviation * iStdDev(NULL, 0, Bollinger_Periods, 0, MODE_SMA, PRICE_CLOSE, 0);
// calculate simple moving average
double SMA = iMA(NULL, 0, SMA_Periods, 0, MODE_SMA, PRICE_CLOSE, 0);
// calculate relative strength index
double RSI = iRSI(NULL, 0, RSI_Periods, PRICE_CLOSE, 0);
// calculate exponential moving average
double EMA = iMA(NULL, 0, EMA_Periods, 0, MODE_EMA, PRICE_CLOSE, 0);
// get last high and low prices
double LastHigh = iHigh(NULL, 0, Trade_Timeframe, 1);
double LastLow = iLow(NULL, 0, Trade_Timeframe, 1);
// get last price on Wait_Timeframe
double LastPrice = iClose(NULL, 0, Wait_Timeframe, 0);
// check if market prices are above SMA 200
if (MarketPrice > SMA)
{
// check if last 1H candle touch lower Bollinger bands
if (LastLow <= Bollinger_Lower)
{
// wait on M5 timeframe until EMA9 cross upward middle Bollinger bands
if (LastPrice < Bollinger_Middle && EMA >= Bollinger_Middle)
{
//
// set stop loss and take profit
StopLoss = LastLow;
TakeProfit = Bollinger_Middle;
// calculate lot size
double LotSize = CalcLotSize(StopLoss);
// place buy order
OrderSend(Symbol(), OP_BUY, LotSize, MarketPrice, 3, StopLoss, TakeProfit, "Expert Advisor", 0, 0, clrGreen);
// set trailing stop
TrailingStop = TrailingStop * Point;
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - TrailingStop, OrderTakeProfit(), 0, clrGreen);
}
}
else
{
// check if market prices are under SMA 200
if (MarketPrice < SMA)
{
// check if last 1H candle touch upper Bollinger bands
if (LastHigh >= Bollinger_Upper)
{
// wait on M5 timeframe until EMA9 cross down middle Bollinger bands
if (LastPrice > Bollinger_Middle && EMA <= Bollinger_Middle)
{
// set stop loss and take profit
StopLoss = LastHigh;
TakeProfit = Bollinger_Middle;
// calculate lot size
double LotSize = CalcLotSize(StopLoss);
// place sell order
OrderSend(Symbol(), OP_SELL, LotSize, MarketPrice, 3, StopLoss, TakeProfit, "Expert Advisor", 0, 0, clrRed);
// set trailing stop
TrailingStop = TrailingStop * Point;
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + TrailingStop, OrderTakeProfit(), 0, clrRed);
}
}
}
}
return(0);
}
// check if trade is allowed
bool IsTradeAllowed()
{
// get account balance
double Balance = AccountBalance();
// calculate maximum trade loss
double MaxTradeLoss = Balance * Max_Trade_Loss / 100.0;
// get total loss for the day
double DailyLoss = AccountInfoDouble(ACCOUNT_DAILY_LOSS);
// calculate maximum daily loss
double MaxDailyLoss = Balance * Max_Daily_Loss / 100.0;
// check if trade is allowed
if (MaxTradeLoss > 0.0 && OrderProfit() < -MaxTradeLoss)
return(false);
else if (MaxDailyLoss > 0.0 && DailyLoss > MaxDailyLoss)
return(false);
else
return(true);
}
// calculate lot size based on stop loss
double CalcLotSize(double StopLoss)
{
// get account balance
double Balance = AccountBalance();
// calculate maximum trade loss
double MaxTradeLoss = Balance * Max_Trade_Loss / 100.0;
// calculate lot size
double LotSize = MaxTradeLoss / (StopLoss * SL_Multiplier);
// return lot size
return(LotSize);
}