Hi guys, I am working on this program:
The program should operate as follows: declared RsiPrec and Rsi which are the values of the Rsi candel "x-1"(a) and the value at the candel "x"(b), respectively, at each new candel.
"if (NuovaCandela ()==true)" and there are no "in machine" orders (if (CountTrades (MagicID)===0) if the value (a) is greater than RsiLevelUp and (b) is lower than RsiLevelUp.
(it means that the value of the Rsi has "crossed down" RsiLevelUP) opens a position in Buy (OpenBuy (lots, MagicID, 3)), conversely if the value (a) is lower than RsiLevelDown and (b) is higher than RsiLevelDown.
(it's to say that the Rsi value has "crossed up" RsiLevelDown) opens a position in Sell (OpenSell (lots, MagicID, 3)); if instead "if (NewCandela ()==true)" and there are "in machine" orders (if (CountTrades (MagicID)>0)
the program calls the function TrailingStop (e_TrailingStep, e_TrailingStep) that should manage the trailingStop to the order that is "in machine";
Problems:
1)When I turn the backtest of the program it opens and closes positions on the same candel, although there is the function (NewCandel () that should start the controls only if there is a new candel);
2)The CountTrades (MagicID) function always returns 0, so that the TrailingStop function "(e_TrailingStep, e_TrailingStep)" does not change the open position and alert "Alert (" Number of Orders "+CountTrades (MagicID)";".
always returns 0 open positions;
- [WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you.
- Can't get this function to run.
- [ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5.
texoro:
1)When I turn the backtest of the program it opens and closes positions on the same candel, although there is the function (NewCandel () that should start the controls only if there is a new candel);
What do you mean by "it opens and closes positions on the same candle"? It opens multiple positions or just opens and closes one position on the same candle?
2)The CountTrades (MagicID) function always returns 0, so that the TrailingStop function "(e_TrailingStep, e_TrailingStep)" does not change the open position and alert "Alert (" Number of Orders "+CountTrades (MagicID)";".
always returns 0 open positions;
int CountTrades(int MagicNumber) { int count = 0; for (int trade = OrdersTotal() - 1; trade >= 0; trade--) { if (!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++; } return (count); }
int start()
Start using the new Event Handling Functions.
Event Handling Functions - Functions - Language Basics - MQL4 Referenceif(NuovaCandela()==true)
You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.int CountTrades(int MagicNumber) { int count = 0; for (int trade = OrdersTotal() - 1; trade >= 0; trade--) { if (OrderSelect(trade, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++; } return (count); }
If you select an order, you continue. When can count ever be non-zero?If is isn't the symbol or the MN you continue. So what is the point of checking those two a second time?
double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits); double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits);
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong- SL/TP (stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 and MetaTrader 4 - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 and MetaTrader 4 - MQL4 programming forum
- Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 and MetaTrader 4 - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 and MetaTrader 4 - MQL4 programming forum
-
Lot size must also be adjusted to a multiple of LotStep and check against
min and max. If that is not a power of 1/10 then
NormalizeDouble is wrong. Do it
right.
(e_trailingstop * Point))
Using Points means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forumif (OrderType() == OP_BUY ...{ if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask - e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
You buy at the Ask and sell at the Bid.- Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
-
Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at
a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3 - 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.)

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