Hi, I have the following code below - it seems to work fine running live (don't worry, it's a non-profitable strategy) where the stop loss and profit targets are hit, but when I run it in Tester, it does not trigger then and instead just closes the position(s) on the very last data set. Do I need to program the EA to close the order if either my stop loss or profit target gets hit? I thought just passing them into OrderSend, that Tester would simulate it for me - or am I wrong on this? here's my code:
externdouble BuyLots = 1;
externint BuySlippage = 0;
externint BuyStopLoss = 10;
externint BuyTakeProfit = 5;
bool newBar = false;
double prevMa;
double openLots = 0;
int start() {
// only run on the closing/formation of a bar
newBar();
if (newBar == false) return;
double ma = iMA(NULL, 0, 9, 8, MODE_SMMA, PRICE_CLOSE, 1);
if (prevMa != NULL) {
openLots = 0;
for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderType() == OP_BUY) {
openLots = openLots + OrderLots();
}
}
if (openLots < 5) {
if (ma < 0) {
double slippage = NULL;
if (BuySlippage > 0) slippage = BuySlippage;
double stopLoss = NULL;
if (BuyStopLoss > 40) {
stopLoss = (Ask - (BuyStopLoss * Point));
} else {
stopLoss = (Ask - (40 * Point));
}
double takeProfit = NULL;
if (BuyTakeProfit > 40) {
takeProfit = (Ask + (BuyTakeProfit * Point));
} else {
takeProfit = (Ask + (40 * Point));
}
int ticket = OrderSend(Symbol(), OP_BUY, BuyLots, Ask, BuySlippage, stopLoss, takeProfit, "BUY triggered", 1234, 0, Green);
if (ticket > 0) {
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
}
}
}
}
}
prevMa = ma;
return(0);
}
void newBar() {
staticdatetime newTime = 0;
newBar = false;
if (newTime != Time[0]) {
newTime = Time[0];
newBar = true;
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi, I have the following code below - it seems to work fine running live (don't worry, it's a non-profitable strategy) where the stop loss and profit targets are hit, but when I run it in Tester, it does not trigger then and instead just closes the position(s) on the very last data set. Do I need to program the EA to close the order if either my stop loss or profit target gets hit? I thought just passing them into OrderSend, that Tester would simulate it for me - or am I wrong on this? here's my code: