First things first! You are processing data on every tick when necessary. Why copy the MACD data on every tick when you only need to check it on a new bar?
Also, don't use the Timer for delays. Detect the new bar the proper way. Read the following blog post: Detecting the start of a new bar or candle, in an expert advisor.
After you fix that up first, then we can proceed to the other stuff.
- 2022.03.15
- www.mql5.com
First things first! You are processing data on every tick when necessary. Why copy the MACD data on every tick when you only need to check it on a new bar?
Also, don't use the Timer for delays. Detect the new bar the proper way. Read the following blog post: Detecting the start of a new bar or candle, in an expert advisor.
After you fix that up first, then we can proceed to the other stuff.
Hi Fernando.
I need to calculate the MACD at each tick, this is part of my strategy, due to the market I am in, the market movements can be very abrupt and depending on those tick movements, I make decisions
That may be your strategy, but its not what your code shows. You are only "Copying" from position 1 onwards (meaning previous bar, not current bar). Now given the previous bar has already closed and will not change, you only need to read it once at the start of the new candle.
Your own topic's header is "Calculate the stop loss at the close of the candle" and that is how we are responding to your request.
If not, then please explain yourself more clearly and in more detail.
That may be your strategy, but its not what your code shows. You are only "Copying" from position 1 onwards (meaning previous bar, not current bar). Now given the previous bar has already closed and will not change, you only need to read it once at the start of the new candle.
Your own topic's header is "Calculate the stop loss at the close of the candle" and that is how we are responding to your request.
If not, then please explain yourself more clearly and in more detail.
So let's change the code, I just need to know how to take in the calculations at the close of the candle if the stoploss is activated, see the following code how could I do it? you know ?
#include <Trade/Trade.mqh> CTrade trade; ulong trade_ticket = 0; bool time_passed = true; int contador = 0; void OnTick() { static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE ); bool bNewBarEvent = ( dtBarCurrent != dtBarPrevious ); if(bNewBarEvent) { if (PositionSelectByTicket(trade_ticket) == false) { trade_ticket = 0; } double O1=iOpen(Symbol(),_Period,1); double C1=iClose(Symbol(),_Period,1); double O2=iOpen(Symbol(),_Period,2); double C2=iClose(Symbol(),_Period,2); double O3=iOpen(Symbol(),_Period,3); double C3=iClose(Symbol(),_Period,3); if ( O1 > C1 && O2 > C2 && O3 > C3 && trade_ticket <= 0 && time_passed == true ) { double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits); trade.Sell(10, _Symbol, Bid, Bid+90000*_Point, Bid-100000*_Point, NULL); trade_ticket = trade.ResultOrder(); Alert("OP"); contador = contador + 1; Print("#############################################################################: ", contador); time_passed = false; time_passed = false; EventSetTimer(PeriodSeconds(PERIOD_CURRENT)*3); } } } void OnTimer() { time_passed = true; }
As I have stated, if you want to do something at the start of a new bar (the close of the previous bar), then please incorporate the detection of the new bar so that we can proceed. Nothing else can be accomplished until you have done that part first.
Also, please don't normalise the OHLC prices. They are already normalised, and even if they were not, then that is not the correct way to normalise a price which needs to be normalised according to the tick size, not the number of digits. However, that is a different story.
So for now, please incorporate the new bar detection as explained.
As I have stated, if you want to do something at the start of a new bar (the close of the previous bar), then please incorporate the detection of the new bar so that we can proceed. Nothing else can be accomplished until you have done that part first.
Also, please don't normalise the OHLC prices. They are already normalised, and even if they were not, then that is not the correct way to normalise a price which needs to be normalised according to the tick size, not the number of digits. However, that is a different story.
So for now, please incorporate the new bar detection as explained.
I already updated my previous code, is there something wrong ?
#include <Trade/Trade.mqh> CTrade trade; ulong trade_ticket = 0; int contador = 0; void OnTick() { static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE ); bool bNewBarEvent = ( dtBarCurrent != dtBarPrevious ); if(bNewBarEvent) { if (PositionSelectByTicket(trade_ticket) == false) { trade_ticket = 0; } double O1=iOpen(Symbol(),_Period,1); double C1=iClose(Symbol(),_Period,1); double O2=iOpen(Symbol(),_Period,2); double C2=iClose(Symbol(),_Period,2); double O3=iOpen(Symbol(),_Period,3); double C3=iClose(Symbol(),_Period,3); if ( O1 > C1 && O2 > C2 && O3 > C3 && trade_ticket <= 0 && time_passed == true ) { double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); trade.Sell(10, _Symbol, Bid, Bid+90000*_Point, Bid-100000*_Point, NULL); trade_ticket = trade.ResultOrder(); Alert("OP"); contador = contador + 1; Print("#############################################################################: ", contador); } } }
- Now that the new bar is detected, what do you still need the timer for?
- You removed normalisation of OHLC data, but you are still normalising the bid price. Why, if It is already normalised?
- You said that you wanted to calculate the Stop-loss based on the close price of the candle, so why are you using the bid price for that?
At least that is what I understood from your explanation in broken English, so correct me if I am wrong.
Please stop altering the code of previous posts, otherwise our conversation does not follow a logical path and we can no longer reference previous code to explain things.
Place a new post with the altered code please. This is also important for other readers to follow.
- Now that the new bar is detected, what do you still need the timer for?
- You removed normalisation of OHLC data, but you are still normalising the bid price. Why, if It is already normalised?
- You said that you wanted to calculate the Stop-loss based on the close price of the candle, so why are you using the bid price for that?
At least that is what I understood from your explanation in broken English, so correct me if I am wrong.
You're absolutely right, I've updated my code again, but I don't understand how I should take the close of the candlestick as a stoploss once it reaches or exceeds (Bid+90000*_Point)
#include <Trade/Trade.mqh> CTrade trade; ulong trade_ticket = 0; int contador = 0; void OnTick() { static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE ); bool bNewBarEvent = ( dtBarCurrent != dtBarPrevious ); if(bNewBarEvent) { if (PositionSelectByTicket(trade_ticket) == false) { trade_ticket = 0; } double O1=iOpen(Symbol(),_Period,1); double C1=iClose(Symbol(),_Period,1); double O2=iOpen(Symbol(),_Period,2); double C2=iClose(Symbol(),_Period,2); double O3=iOpen(Symbol(),_Period,3); double C3=iClose(Symbol(),_Period,3); if ( O1 > C1 && O2 > C2 && O3 > C3 && trade_ticket <= 0 ) { double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); trade.Sell(10, _Symbol, Bid, Bid+90000*_Point, Bid-100000*_Point, NULL); trade_ticket = trade.ResultOrder(); Alert("OP"); contador = contador + 1; Print("#############################################################################: ", contador); } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I try to use the strategy tester to know if the strategy works or not, but I would like to calculate the stoploss at the close of the candle in which the stoploss was activated and not the stoploss of the operation that was created, does anyone have an idea of how do it ?