Kindly check my code, i have spent two weeks figuring it out and now i am exhausted so posting this one.
//+------------------------------------------------------------------+ //| Take Trades Function 1 | //+------------------------------------------------------------------+ void trade() { double MyRsiValue = NormalizeDouble(rsiArray[0],2); Comment(MyRsiValue); if (MyRsiValue >= Overbought) { // Implement your overbought trading logic here // For example, you can open a sell trade // OrderSend(Symbol(), OP_SELL, LotSize, Ask, 3, 0, 0, "", 0, clrNONE); double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double Sl = StopLoss==0 ? 0 : Bid + StopLoss * _Point; double Tp = Bid - TakeProfit * _Point; trade.Sell(LotSize,_Symbol,Bid,Sl,Tp); } else if (MyRsiValue <= Oversold) { // Implement your oversold trading logic here // For example, you can open a buy trade // OrderSend(Symbol(), OP_BUY, LotSize, Bid, 3, 0, 0, "", 0, clrNONE); double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double Sl = StopLoss==0 ? 0 : Ask - StopLoss * _Point; double Tp = Ask + TakeProfit * _Point; trade.Buy(LotSize,_Symbol,Ask,Sl,Tp); } if(PositionsTotal() >1) { int totalPositions = PositionsTotal(); // Get the total number of open positions // Iterate through all open positions to get their information for (int i = 0; i < totalPositions; i++) { ulong ticket= PositionGetTicket(i); double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); if (PositionSelectByTicket(ticket) == POSITION_TYPE_BUY && Ask <= def_SetNewPrice) { double Sl = StopLoss==0 ? 0 : Ask - StopLoss * _Point; double Tp = Ask + TakeProfit * _Point; if(trade.Buy(LotSize,_Symbol,Ask,Sl,Tp)) { MyArray[LastRow][def_SetNewPrice] = true; } } double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); if (PositionSelectByTicket(ticket) == POSITION_TYPE_SELL && Bid <= MyArray[LastRow][def_NextDayPrice]) { double Sl = StopLoss==0 ? 0 : Bid + StopLoss * _Point; double Tp = Bid - TakeProfit * _Point; if(trade.Sell(LotSize,_Symbol,Bid,Sl,Tp)) { MyArray[LastRow][def_SetNewPrice] = true; } } if(PositionsTotal() >= 2) { double NewTakeProfit = NormalizeDouble(positionInfo[0][2] + TakeProfit *_Point,_Digits); //double NewTakeProfit = positionInfo[0][2] + TakeProfit; trade.PositionModify(ticket,0,NewTakeProfit); } } } }
My advices for newbies:
- Before learning to code in MQL5 learn to search as there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you! Either with google (forgives typps) "site:mwl5.com WhatIwant" or here:
=> articles: https://www.mql5.com/en/articles
=> codebase: https://www.mql5.com/en/code
=> generally: https://www.mql5.com/en/search - In your case "entry on every 200 points " I would search for grid trading.
- If your program does not do what it should use the debugger to control your variables on every step: https://www.metatrader5.com/en/metaeditor/help/development/debug
- www.metatrader5.com
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
- www.mql5.com
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Thank you fernando :) i hope to get some help regarding my issue i am facing.
Bro I'm noticing a lot of ternary operators and long if statements, have you tried simplifying the code?
Sometimes that works for me, I start again and keep it stupid simple (KISS), dead simple. From there once I have everything working in the simplest way possible I slowly make it more sophisticated.
Also "def_SetNewPirce" and "def_NextDayPirce" aren't defined in the code snippet you've provided, I'm assuming they are functions, the error may lie in them. Instead of calling sperate functions, try setting those values at the top of trade so we can be sure the error lies here in trade and nowhere else. Sometimes it's easier for me to create one monolith function and if everything works, I start breaking it down and separating into separate functions.
double Tp = Ask + TakeProfit * _Point;
I don't understand what this logic is doing, but I think it could be simpler. The beauty of keeping the code simple is that errors become easier to spot just from glancing at the code.
MyArray[LastRow][def_SetNewPrice] = true;
Hope this helps.
Bro I'm noticing a lot of ternary operators and long if statements, have you tried simplifying the code?
Sometimes that works for me, I start again and keep it stupid simple (KISS), dead simple. From there once I have everything working in the simplest way possible I slowly make it more sophisticated.
Also "def_SetNewPirce" and "def_NextDayPirce" aren't defined in the code snippet you've provided, I'm assuming they are functions, the error may lie in them. Instead of calling sperate functions, try setting those values at the top of trade so we can be sure the error lies here in trade and nowhere else. Sometimes it's easier for me to create one monolith function and if everything works, I start breaking it down and separating into separate functions.
I don't understand what this logic is doing, but I think it could be simpler. The beauty of keeping the code simple is that errors become easier to spot just from glancing at the code.
Hope this helps.
thank you brother, i will try to code the simpler version of my ea to see where the issue is.
however i added the complete code file as well.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
example. first buy trade was taken when market was at 1.5000 now the second trade is supposed to be taken when price reaches 1.4980.
i am attaching my ea code file as well.