In MQL4 the variable doesn't change
In MQL4 the variable doesn't change
Loading saved threads...
Rafał · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Rafał
Original post: https://stackoverflow.com/questions/76162750
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am starting to learn MQL4 and wanted to write a simple program for practice. When I open a long position, the positions variable is supposed to take the value 1, and when I take a short position, the positions variable is supposed to take the value -1. Unfortunately, after the loop is executed, the variable is still 0. What am I doing wrong? PS. Close_Trade is my internal function and works well.
void OnTick()
{
{
double stoch= iStochastic(NULL,0,14,3,3,0,1,0,0);
int number_of_order= OrdersTotal();
static int positions=0;
if(number_of_order==0)
{
if(stoch<=20)
{
int purchase_order=OrderSend(_Symbol,OP_BUY,0.01,Ask,0,0,0,NULL,0,0,clrNONE);
positions++;
}
else
if(stoch>=80)
{
int sales_order=OrderSend(_Symbol,OP_SELL,0.01,Bid,0,0,0,NULL,0,0,clrNONE);
positions--;
}
}
Alert(positions);
if(stoch>70 && positions==1)
{
Close_Trade();
positions--;
}
if(stoch<30&&positions==-1)
{
Close_Trade();
positions++;
}
}
}
//+------------------------------------------------------------------+
I added a static to the position variable, but that didn't help.
Quote
Report
Jalal Nouri · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Jalal Nouri
Original post: https://stackoverflow.com/a/76177697
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Short answer
: delete
static int positions=0;
from
OnTick
function and write out and before
OnTick
function. No need to write
static
.
Long answer
:
Please put the line numbers in the next questions
However, the problem is in the line
static int positions=0;
Behold
The process of your program is that your code is executed once for each market tick
In
double stoch= iStochastic(NULL,0,14,3,3,0,1,0,0);
the value of the
stoch
variable is set, for example, we assume its value is equal to 15.
In
int number_of_order= OrdersTotal();
,
number_of_order
takes a value equal to
OrdersTotal()
, we assume that you do not have an open order and the value of this variable is equal to 0.
In
static int positions=0;
the position variable takes the value 0.
Since you don't have an open order, the program enters the
if(number_of_order==0)
.
Because the
stock
value is below 20, so program enters
if(stoch<=20)
and opens an order, and
position
increases by one unit and becomes equal to 1.
In
Alert(positions);
it gives a warning and outputs the value of the
position
, which is 1 (it is useless to warn the value of the position variable in every market tick, write the program in such a way that it alert if the value of the
position
changes. I think this is better).
Because the value of the
stoch
is 15 and the value of the
position
is 1, the program does not enter the remaining two
if
statements and the value of the
position
is still equal to 1.
The program ends and waits for the next market tick (which is very short). At the beginning of the next tick, the
stoch
variable is reset with
ouble stoch= iStochastic(NULL,0,14,3,3,0,1,0,0);
. In
int number_of_order= OrdersTotal();
,
number_of_order
becomes 1. The value of the
position
, which is equal to 1, will be equal to 0 with
static int positions=0;
.
This is the problem with your program You have to delete
static int positions=0;
from
OnTick
function and write out and before it. No need to write
static
.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Jalal Nouri Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/76177697 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Short answer : delete static int positions=0; from OnTick function and write out and before OnTick function. No need to write static . Long answer : Please put the line numbers in the next questions However, the problem is in the line static int positions=0; Behold The process of your program is that your code is executed once for each market tick In double stoch= iStochastic(NULL,0,14,3,3,0,1,0,0); the value of the stoch variable is set, for example, we assume its value is equal to 15. In int number_of_order= OrdersTotal(); , number_of_order takes a value equal to OrdersTotal() , we assume that you do not have an open order and the value of this variable is equal to 0. In static int positions=0; the position variable takes the value 0. Since you don't have an open order, the program enters the if(number_of_order==0) . Because the stock value is below 20, so program enters if(stoch<=20) and opens an order, and position increases by one unit and becomes equal to 1. In Alert(positions); it gives a warning and outputs the value of the position , which is 1 (it is useless to warn the value of the position variable in every market tick, write the program in such a way that it alert if the value of the position changes. I think this is better). Because the value of the stoch is 15 and the value of the position is 1, the program does not enter the remaining two if statements and the value of the position is still equal to 1. The program ends and waits for the next market tick (which is very short). At the beginning of the next tick, the stoch variable is reset with ouble stoch= iStochastic(NULL,0,14,3,3,0,1,0,0); . In int number_of_order= OrdersTotal(); , number_of_order becomes 1. The value of the position , which is equal to 1, will be equal to 0 with static int positions=0; . This is the problem with your program You have to delete static int positions=0; from OnTick function and write out and before it. No need to write static .
Checking account access…