You open the pantry at home, scratch your head and close it.
You go fishing and you need a fishing rod.
You go home to the storeroom to get a fishing rod.
Went fishing, put in your fishing rod and caught a fish, and you need a net
Went home to the storeroom for the net
Went fishing, picked up the fish you had caught and floated on the hook with the net, and you needed a container for storing the fish you had caught
Go home for ..... Shall I continue?
Or you can take everything you need from the pantry (OnInit) straight away, fish without running around and, when you get home, put everything in the pantry and refrigerator (OnDeinit).
You may have known that here on the forum. You just need to listen and hear what you are told sometimes.
Otherwise you get a question, get an answer, say "bullshit" and do as you think.
You must first think over the question, ask questions, and then start editing.
Are you aware that the simplest thing in programming is printing code? And the lion's share of development is thinking over the logic.
That's a ridiculous analogy. In that case let's make all variables, class objects etc. global and not create them on demand or maybe a graphical object is something else (certainly not a fishing rod or a net :)) ? So my approach is quite rational and justified, it's just that mql or metatrader screws up.
In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do would be to do this:
voidOnTick()
{
datetime cTime;
staticdatetime time = 0;
int nOrders;
cTime = iTime(NULL, PERIOD_CURRENT, 0);
nOrders = CountOrders();
if (time != cTime && nOrders == 0)
time = cTime;
elsereturn;
// Получим значение индикатора
dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.// Если нет открытых ордеров, то входим в условиеif(nOrders == 0) // теперь это условие можно убрать
{
// Если появился сигнал на покупку, то откроем ордер на покупкуif(bSignalBuy() == true)
vOrderOpenBuy();
// Если появился сигнал на продажу, то откроем ордер на продажуif(bSignalSell() == true)
vOrderOpenSell();
}
}
In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do is this:
Artem is right, it should be like this
//+-----------------------------------------------------------------------------------------------+voidOnTick()
{
// Получим значение индикатора
dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.// Если нет открытых ордеров и появился сигнал на покупку, то откроем ордер на покупкуif(CountOrders()==0&&bSignalBuy())
{
vOrderOpenBuy();
}
// Если нет открытых ордеров и появился сигнал на продажу, то откроем ордер на продажуif(CountOrders()==0&&bSignalSell())
{
vOrderOpenSell();
}
}
The result will be the same here. Only the code is more complicated. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...
An open position has an opening time. The open time can be compared to the time of a candle - to find out if the time of opening the position is inside the candle (and this is the current candle).
And why do you always find out the number of orders in all the examples? And if there are zero orders, then you open them. This greatly limits the possibilities.
Generally, we should only focus on the number of orders/positions for a very narrow strategy direction.
The result here will be the same. Only the code is more confusing. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...
Your code
if (time != cTime && nOrders == 0)
time = cTime;
(A bar has opened and no order has been placed)
checked for a signal - no signal
... we wait for the next bar.
You repeated the same mistake as last time
P.S.
this is
time = cTime;
must be carried out after the order has been opened
In your code, if one position is open, another one will not open by the opposite signal. I.e., first of all, when the signal comes in, you need to check whether there is an opposite position and close it.
But again, this all greatly limits the possibilities to improve the strategies.
We should avoid binding to the presence/absence of orders (especially in four - pending orders (which is correct) and positions (which is wrong)).
No, it is correct and necessary to have the number of orders and positions by their types at hand. But it is wrong to rely on the absence of any orders only for the signals.
In general, the correct way is to have functions to count orders and positions (one function that fills the structure when the number of orders and positions changes), functions to open/close positions, functions to set orders and alarm functions. Functions of various trawls and getting data from indicators. Plus - ability to compare the data of a position (open or closed) with some values.
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.
Dobro. Let me put it in simple Russian terms.
You're going fishing.
Or you can take everything you need from the pantry (OnInit) straight away, fish without running around and, when you get home, put everything in the pantry and refrigerator (OnDeinit).
You may have known that here on the forum. You just need to listen and hear what you are told sometimes.
Otherwise you get a question, get an answer, say "bullshit" and do as you think.
You must first think over the question, ask questions, and then start editing.
Are you aware that the simplest thing in programming is printing code? And the lion's share of development is thinking over the logic.
That's a ridiculous analogy. In that case let's make all variables, class objects etc. global and not create them on demand or maybe a graphical object is something else (certainly not a fishing rod or a net :)) ? So my approach is quite rational and justified, it's just that mql or metatrader screws up.
This way the signal will be missed
Either I'm being obtuse, or there is no condition if there are open orders AND there is a signal and the bar is already new.
If the signals are long, more than a bar multiple, it will also work on the first tick of the bar.
The only question is when to open an order, when the signal appears or at the first tick of the next bar.Either I'm being obtuse, or there is no condition if there are open orders AND there is a signal and the bar is already new.
If the signals are long, more than a bar multiple, it will also work on the first tick of the bar.
The only question is when the order should be opened, at the moment of the signal or at the first tick of the next bar.The code now looks like this:
Before opening, check if there is an open position on this candle. If not, open it.
In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do would be to do this:
In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do is this:
Artem is right, it should be like this
You're right, Artem, it should be like this.
The result will be the same here. Only the code is more complicated. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...
There's a lot going on here.
An open position has an opening time. The open time can be compared to the time of a candle - to find out if the time of opening the position is inside the candle (and this is the current candle).
And why do you always find out the number of orders in all the examples? And if there are zero orders, then you open them. This greatly limits the possibilities.
Generally, we should only focus on the number of orders/positions for a very narrow strategy direction.
The result here will be the same. Only the code is more confusing. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...
Your code
(A bar has opened and no order has been placed)
checked for a signal - no signal
... we wait for the next bar.
You repeated the same mistake as last time
P.S.
this is
must be carried out after the order has been openedYou're right, Artem, it should be like this
In your code, if one position is open, another one will not open by the opposite signal. I.e., first of all, when the signal comes in, you need to check whether there is an opposite position and close it.
But again, this all greatly limits the possibilities to improve the strategies.
We should avoid binding to the presence/absence of orders (especially in four - pending orders (which is correct) and positions (which is wrong)).
No, it is correct and necessary to have the number of orders and positions by their types at hand. But it is wrong to rely on the absence of any orders only for the signals.
In general, the correct way is to have functions to count orders and positions (one function that fills the structure when the number of orders and positions changes), functions to open/close positions, functions to set orders and alarm functions. Functions of various trawls and getting data from indicators. Plus - ability to compare the data of a position (open or closed) with some values.
Any strategy can be built out of this set.