Teresa Falcão:
Please edit your code and add it via the Code S or the button </>
Here is the code:
#include <Trade/Trade.mqh> input double Lots = 0.05; input ENUM_TIMEFRAMES Timeframe = PERIOD_H1; input int MAPeriod = 92; input ENUM_MA_METHOD MaMethod = MODE_SMA; input int MaShift = 0; input ENUM_APPLIED_PRICE MAValorAplicado = PRICE_CLOSE; input int StopLossPoints = 4050; input int TrailingStopPoints = 14800; input int TMAPeriodo = 64; input ENUM_MA_METHOD TMaMethod = MODE_SMA; input ENUM_APPLIED_PRICE TMAValorAplicado = PRICE_CLOSE; int maHandle; int TMAHandle; int barsTotal; int maDirection; CTrade trade; //+------------------------------------------------------------------+ //| Função para calcular a TMA | //+------------------------------------------------------------------+ double GetTMA() { double tmaValue[]; int tmaHandle = iMA(_Symbol, Timeframe, TMAPeriodo, 0, TMaMethod, TMAValorAplicado); if (tmaHandle < 0) return 0; if (CopyBuffer(tmaHandle, 0, 0, 1, tmaValue) <= 0) { Print("Erro ao copiar dados da média móvel"); return 0; } return tmaValue[0]; } //+------------------------------------------------------------------+ //| Função para aplicar trailing stop com base na TMA | //+------------------------------------------------------------------+ void UpdateTrailingStop() { double tmaValue = GetTMA(); double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); if (tmaValue == 0) return; for (int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if (PositionSelectByTicket(ticket)) { ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); double stopLoss = PositionGetDouble(POSITION_SL); double PositionPriceOpen = PositionGetDouble(POSITION_PRICE_OPEN); if (type == POSITION_TYPE_BUY){ double TrailingSLoss = Ask - TrailingStopPoints * _Point; double PositionSLoss = PositionPriceOpen - StopLossPoints * _Point; if (stopLoss < tmaValue && TrailingSLoss > tmaValue){ trade.PositionModify(ticket, tmaValue, 0); } else if (stopLoss < tmaValue && TrailingSLoss < tmaValue){ trade.PositionModify(ticket, TrailingSLoss, 0); } } else if (type == POSITION_TYPE_SELL){ double TrailingSLoss = Bid + TrailingStopPoints * _Point; if (stopLoss > tmaValue && TrailingSLoss < tmaValue){ trade.PositionModify(ticket, tmaValue, 0); } else if (stopLoss > tmaValue && TrailingSLoss > tmaValue){ trade.PositionModify(ticket, TrailingSLoss, 0); } } } }} int OnInit(){ maHandle = iMA(_Symbol,Timeframe,MAPeriod,MaShift,MaMethod,MAValorAplicado); maDirection = 0; barsTotal = iBars(_Symbol,Timeframe); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ } void OnTick(){ double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); //---------- int bars = iBars(_Symbol,Timeframe); if(barsTotal < bars){ barsTotal = bars; UpdateTrailingStop(); double ma[]; CopyBuffer(maHandle,0,1,2,ma); if(ma[1] > ma[0] && maDirection <= 0){ maDirection = 1; double SLoss = Ask - StopLossPoints*_Point; trade.PositionClose(_Symbol); trade.Buy(Lots,_Symbol,0,SLoss,0,NULL); } else if(ma[1] < ma[0] && maDirection >= 0){ maDirection = -1; double SLoss = Bid + StopLossPoints*_Point; trade.PositionClose(_Symbol); trade.Sell(Lots,_Symbol,0,SLoss,0,NULL); } } }
I did a little search and I got it.
Thank you

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I made an EA (this is my first Ea) and when testing it, even there is no date and time variables on it, the orders on Friday are not executed and the Diary send a message "market closed".
The startegy is based on the direction of an MA and it has 3 variables for stop loss (stoplosspoints, trailing stop based on other MA and trailing stop points).
Depending on the direction of the MA it make buy or sell orders.
I know this is a very rudimentary EA and is not following all the good rules of a program, but it is to improve with time.
I'm testing it on the Dow Jones cash indice.
Here is the code:
#include <Trade/Trade.mqh>
input double Lots = 0.05;
input ENUM_TIMEFRAMES Timeframe = PERIOD_H1;
input int MAPeriod = 92;
input ENUM_MA_METHOD MaMethod = MODE_SMA;
input int MaShift = 0;
input ENUM_APPLIED_PRICE MAValorAplicado = PRICE_CLOSE;
input int StopLossPoints = 4050;
input int TrailingStopPoints = 14800;
input int TMAPeriodo = 64;
input ENUM_MA_METHOD TMaMethod = MODE_SMA;
input ENUM_APPLIED_PRICE TMAValorAplicado = PRICE_CLOSE;
int maHandle;
int TMAHandle;
int barsTotal;
int maDirection;
CTrade trade;
//+------------------------------------------------------------------+
//| Função para calcular a TMA |
//+------------------------------------------------------------------+
double GetTMA()
{
double tmaValue[];
int tmaHandle = iMA(_Symbol, Timeframe, TMAPeriodo, 0, TMaMethod, TMAValorAplicado);
if (tmaHandle < 0) return 0;
if (CopyBuffer(tmaHandle, 0, 0, 1, tmaValue) <= 0)
{
Print("Erro ao copiar dados da média móvel");
return 0;
}
return tmaValue[0];
}
//+------------------------------------------------------------------+
//| Função para aplicar trailing stop com base na TMA |
//+------------------------------------------------------------------+
void UpdateTrailingStop()
{
double tmaValue = GetTMA();
double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
if (tmaValue == 0) return;
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
double stopLoss = PositionGetDouble(POSITION_SL);
double PositionPriceOpen = PositionGetDouble(POSITION_PRICE_OPEN);
if (type == POSITION_TYPE_BUY){
double TrailingSLoss = Ask - TrailingStopPoints * _Point;
double PositionSLoss = PositionPriceOpen - StopLossPoints * _Point;
if (stopLoss < tmaValue && TrailingSLoss > tmaValue){
trade.PositionModify(ticket, tmaValue, 0);
}
else if (stopLoss < tmaValue && TrailingSLoss < tmaValue){
trade.PositionModify(ticket, TrailingSLoss, 0);
}
}
else if (type == POSITION_TYPE_SELL){
double TrailingSLoss = Bid + TrailingStopPoints * _Point;
if (stopLoss > tmaValue && TrailingSLoss < tmaValue){
trade.PositionModify(ticket, tmaValue, 0);
}
else if (stopLoss > tmaValue && TrailingSLoss > tmaValue){
trade.PositionModify(ticket, TrailingSLoss, 0);
}
}
}
}}
int OnInit(){
maHandle = iMA(_Symbol,Timeframe,MAPeriod,MaShift,MaMethod,MAValorAplicado);
maDirection = 0;
barsTotal = iBars(_Symbol,Timeframe);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//----------
int bars = iBars(_Symbol,Timeframe);
if(barsTotal < bars){
barsTotal = bars;
UpdateTrailingStop();
double ma[];
CopyBuffer(maHandle,0,1,2,ma);
if(ma[1] > ma[0] && maDirection <= 0){
maDirection = 1;
double SLoss = Ask - StopLossPoints*_Point;
trade.PositionClose(_Symbol);
trade.Buy(Lots,_Symbol,0,SLoss,0,NULL);
}
else if(ma[1] < ma[0] && maDirection >= 0){
maDirection = -1;
double SLoss = Bid + StopLossPoints*_Point;
trade.PositionClose(_Symbol);
trade.Sell(Lots,_Symbol,0,SLoss,0,NULL);
}
}
}
Supposedly, there is no reason for the orders don't work on Fridays, since the visual chart of the strategy tester show the market open at Fridays.
Can someone help with this error?
Thank you for your attention.
Teresa