How to notify a number increase & decrease in MQL4 code? - mql4

can anyone help me, how to write MQL4 code to know if the result is increase OR decrease..
Example:
if a result was first 0.0543 and then it is increased to 0.1342 and later decreased to 0.10345, I want to implement it to my code below:
int start()
{
double val = iCustom( NULL, 0, "SS2009_B", 0, 0, 0 );
ObjectSet( "TimeLable11", OBJPROP_CORNER, obCorner );
ObjectSetText( "TimeLable11", "Result : " + val,
fsize,
"Microsoft Sans Serif",
Yellow
);
return( 0 );
}
I want the result to have an increasing OR a decreasing notification.
Example :
Result : 0.1849 Increasing
Result : 0.01324 Decreasing

To have a SIMPLE example:
//------------------------------------------------------------------
#property copyright "Copyright 2015"
//------------------------------------------------------------------
// Standard Variables (define a variable to store previous value)
//------------------------------------------------------------------
double viIndicatorValuePrev = 0.0000;
int start() {
double viIndicatorValueNow = iCustom(NULL,0,"SS2009_B",0,0,0);
//------------------------------------------------
//Decide on direction (increasing or decreasing)
//------------------------------------------------
string vsDirection = "Same";
if( viIndicatorValueNow>viIndicatorValuePrev)
vsDirection = "Increasing";
if( viIndicatorValueNow<viIndicatorValuePrev)
vsDirection = "Decreasing";
//------------------------------------------------
//------------------------------------------------
// Do your thing here (ie, display stuff)
//------------------------------------------------
ObjectSet( "TimeLable11", OBJPROP_CORNER, obCorner);
ObjectSetText(
"TimeLable11"
, "Result : " + viIndicatorValueNow + " (" + vsDirection + ")"
, fsize, "Microsoft Sans Serif", Yellow
);
//------------------------------------------------
// Store current value for future comparison
//------------------------------------------------
viIndicatorValuePrev = viIndicatorValueNow;
//------------------------------------------------
return(0);
}
Hope this example helps.

Solution to monitor & show delta per each aMarketEVENT call of start() ( upon aMarketQUOTE aka Tick arrival )
First you need a method to store and retain a value, that becomes "old", in the meantime, before the start() ( well actually an OnTick() in the new-MQL century ) is called next time.
A static double is a smart way to go:
static double previousVALUE = EMPTY; // initialised to EMPTY
Next, initialise it in accord with a forward-stepping logic of the Custom Indicator "SS2009_B"
if ( previousVALUE == EMPTY ) {
previousVALUE = iCustom( NULL, // a Symbol
0, // a TimeFRAME
"SS2009_B",
0,
0, // a Buffer# to get value from
1 // step-back one Bar
);
}
For syntax details, review MQL4 documentation
double iCustom(
string symbol, // symbol
int timeframe, // timeframe
string name, // path/name of the custom indicator compiled program
... // custom indicator input parameterA (if
... // custom indicator input parameterB (if
... // custom indicator input parameterC (if necessary)
int mode, // line index
int shift // shift
);
Finally calculate delta & show it on UI & shuffle to get ready for a next call
double currentVALUE = iCustom( NULL, 0, "SS2009_B", 0, 0, 0);
double deltaVALUE = previousVALUE - currentVALUE;
previousVALUE = currentVALUE; // shuffle [1]<-[0] before RET/next call
if ( deltaVALUE > 0 ){
ObjectSetText( "TimeLabel11",
"RESULT: " + NormalizeDouble( deltaVALUE, Digits ) + "|^|",
fsize,
"Courier New",
Yellow // can be deltaVALUE specific too :o)
);
}
if ( deltaVALUE < 0 ){
ObjectSetText( "TimeLabel11",
"RESULT: " + NormalizeDouble( deltaVALUE, Digits ) + "|v|",
fsize,
"Courier New",
Cyan // can be deltaVALUE specific too :o)
);
}
A few remarks on precision of float numbers
You may notice a use of NormalizeDouble()
This is rather important step, to avoid problems with comparison of floats.
If the precision of standard floats ( MQL4 double ) is not sufficient, you may opt-in to use extended precision numbers in MQL4. Check documentation for that.
However, always be carefull on float comparisons.
Best practice, to be on a safer side, is to use both NormalizeDouble() ( as a prevention )
and
comparing to some thresholdDELTA like if ( MathAbs( floatA - floatB ) < anEqualityTresholdDELTA ){ ... } rather than if ( floatA == floatB ) {...}

Related

Can I make a while-loop that uses RSI value as the condition?

When I try to run this while-loop in MQL4, it can not seem to update the rsi from within the loop. It always returns the same value of the rsi and thus stays in an eternal loop. I have tried it on an EA as well as a script.
I have also tried making rsi a globally defined variable too but still did not work.
void OnTick()
{
double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0); // defining rsi
while( rsi < 50 )
{
double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0); // update rsi
Sleep(10000); // slow down loop
Alert("this is from inside while loop rsi is "+rsi);
}
Alert("While loop exited, condition met"); // now code goes
Alert("opening an order now " ); // to place an order
}
Can I make a while-loop that uses RSI value as the condition?
Oh sure you can.
Let's move out the colliding elements.
Given the code as-is, there are several brutal misconceptions to repair:
if a code spends some time "inside" such a loop, there will never be heard a new incoming QUOTE-message from market, so your Close[0]-value will remain "blind" to any such update from Market.
if a second double rsi = ... declaration takes place "inside" a scope of a while-loop-constructor, the newer versions ( Builds ) of the MQL4 language will actually "mask" the older ( "outer" ) variable, that is yet still used in the loop-constructor control variables / conditions. Result? You assign "new" values to an "inner"-variable, that bears just by coincidence a same name as the "outer"-variable ( both are named rsi, yet the "inner" keeps-masking the "outer", so the "outer" never gets any update(s), if any such were stored inside the loop into the "inner" )
Solution
avoid masking by not declaring any same-name named variables (always, it is a sign of good engineering practice)
avoid receiving market-QUOTE-updates by deaf-loop-locking - prefer the non-blocking use of if(){...} instead of while(){...}
MQL4 is a responsive-ecosystem, where OnTick() is automatically called whenever a new QUOTE-message arrives from FX-Market, so design your algorithms so as these never block.
For your learning-directed inspiration, you may try your next few steps using this template :
string MASK = "[%s]_INF:: " // DEF. & INIT...
+ "This is a QUOTE# %5d "
+ "Having arrived at [%s]_FX-MarketTIME\n"
+ "|Ask %16.8f\n"
+ "|Bid %16.8f\n"
+ "|Vol %16d\n"
+ "|RSI %16.8f";
double rsi = EMPTY; // DEF. & INIT...
int nTicks = 0; // DEF. & INIT...
void OnDeinit( const int aDeinitReason )
{ EventKillTimer();
}
int OnInit()
{ EventSetTimer( 1 );
Comment( "------------------------------------------------------\n ",
"WAITING for a first QUOTE-arrival from FX-Market\n",
"------------------------------------------------------"
);
return( 0 );
}
void OnTick()
{ nTicks++; // update COUNTER
rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 ); // update rsi
Comment( SetupComment() ); // update GUI
// -------------------------------- FIRST 50 FX-Market Ticks delivered
if ( nTicks < 50 ) return;
// -------------------------------- AFTER 50 FX-Market Ticks delivered
// -------------------------------- BE WARNED, THIS IS AN AWFUL ANTI-PATTERN
while( True )
{
rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 ); // update rsi
Comment( SetupComment() ); // update GUI
Sleep( 10000 );
}
// -------------------------------- NEVER GETS HERE
Alert( "?" );
}
string SetupComment()
{ return( StringFormat( MASK,
TimeToStr( TimeLocal(),
TIME_DATE|TIME_MINUTES|TIME_SECONDS
),
nTicks,
TimeToStr( TimeCurrent(),
TIME_DATE|TIME_MINUTES|TIME_SECONDS
),
NormalizeDouble( Ask, 8 ),
NormalizeDouble( Bid, 8 ),
Volume[0],
NormalizeDouble( rsi, 8 )
)
);
}
void OnTimer()
{ Comment( ChartGetString( 0, CHART_COMMENT ),
"\n",
TimeToStr( TimeLocal(),
TIME_DATE|TIME_MINUTES|TIME_SECONDS
)
);
Print( ChartGetString( 0, CHART_COMMENT ) );
}
Declare rsi only once, outside the loop, as you already have it. To make it work as you intend REMOVE the type double from in front of variable rsi inside the loop.

How to loop through all Symbols and check the current bar and the previous one's High?

The problem is that when the i in the for loop reaches 15, I get an Array out of range error.
What I want to achieve is on every tick to check every Symbol in the MarketWatch and in each Symbol to check the current bar ( PERIOD_M5 ) and the previous one if there is a gap.
void OnTick()
{
int size = ArraySize( Symbols );
for( int i = 0; i < size; i++ )
{
int current_bar_index = iHighest( Symbols[i], PERIOD_M5, MODE_HIGH, 1, 0 );
int previous_bar_index = iHighest( Symbols[i], PERIOD_M5, MODE_HIGH, 1, 1 );
int current_bar_index_low = iLowest( Symbols[i], PERIOD_M5, MODE_LOW, 1, 0 );
int previous_bar_index_low = iLowest( Symbols[i], PERIOD_M5, MODE_LOW, 1, 1 );
double current_high = High[ current_bar_index];
double previous_high = High[previous_bar_index];
double current_low = Low[ current_bar_index_low];
double previous_low = Low[ previous_bar_index_low];
if ( current_low > ( previous_high + 0.00002 )
|| current_high < ( previous_low - 0.00002 )
)
{
Print( "There is a gap" );
}
}
}
Symbols[] is the array containing all the symbols. I loop through it and pass the current Symbol and get the index for the current bar and the previous one and then simply check if there is a gap.
The first problem is that passing every Symbol doesn't work. It gets only one and that's it. How can I achieve that and also, why I get an Array out of range error?
Let's start with the Array out of range error:
Given the MCVE was not present, let's assume, there is some fair declaration of the MQL4 code in the header or something like that.
So to diagnose this, add a trivial debug tool:
string SymbolsARRAY[] = { ..., // be carefull, some Brokers
..., // have quite strange names
...
};
for ( int cellPTR = 0;
cellPTR < ArraySize( SymbolsARRAY );
cellPTR++
)
PrintFormat( "%s[%d] = %s",
"SymbolsARRAY",
cellPTR,
SymbolsARRAY[cellPTR]
);
and post here the output.
Next, the GAP detection:
if you would not mind, checking a pair of M5-Bars for a GAP on each and every price QUOTE message arrival, is quite devastating the MQL4 computing resources. The GAP may principally appear only upon a new Bar starts ( and yet may disappear, during the Bar duration ).
Let's detect the initial GAP-condition ( it is similar to engulfing detection ):
if ( iLow( SymbolsARRAY[cellPTR], PERIOD_M5, 1 ) > iHigh( SymbolsARRAY[cellPTR], PERIOD_M5, 0 )
|| iHigh( SymbolsARRAY[cellPTR], PERIOD_M5, 1 ) < iLow( SymbolsARRAY[cellPTR], PERIOD_M5, 0 )
)
Print( "Initial GAP detected" );
Using this inside a detection of a new bar event may look like this:
void OnTick(){
// -----------------------------------------------------------------
// MINIMALISTIC CONTEXT-AWARE GAP DETECT/REMOVE
// -----------------------------------------------------------------
static bool aCurrentGAP = False;
static datetime aCurrentBAR = Time[0];
if ( aCurrentBAR != Time[0] ){
aCurrentBAR = Time[0];
// testInitGAP .........................present?
aCurrentGAP = testInitialGAP_onNewBarEVENT();
}
else {
if ( aCurrentGAP ){
// testInitGAP .....................disappeared?
aCurrentGAP = testInitialGAP_DISAPPEARED();
}
}
// -----------------------------------------------------------------
// MAIN COMPUTING & TRADING DUTIES OF EA-MQL4-CODE
// -----------------------------------------------------------------
...
}
about the error - when you receive it, it also can see the address of the code that generates it: number of line and symbol number in that line
about Symbol - as you may know, in MQL5 you can select all symbols from the market watch, in MQL4 such option is not available, as far as I remember, so I suspect your array of symbol names is empty or sth like that.
Could you show how you initialized that array? A correct way is the following one:
string Symbol[]={"EURUSD","AUDUSD","GBPUSD"};
also your code doesn't make any sense - when you select iHighest(*,*,*,i,j) - that means that EA-code checks i-elements, starting from j-th element ( 1, 0 ) means a current bar only, ( 1, 1 ) means to select just 1 bar, starting from bar 1, so still only 1 Bar is used.
For that it is much easier to have
double cur_high = iHigh( Symbol[i], 5, 0 ),
prev_high = iHigh( Symbol[i], 5, 1 );

Getting the max and min price of current chart

After performing
ChartNavigate(0, CHART_END, -5142);
I want to get the max and min price of that particular chart.
I tried
ChartGetDouble(0,CHART_PRICE_MAX,0,top)
ChartGetDouble(0,CHART_PRICE_MIN,0,top)
WindowPriceMax
WindowPriceMin
None of them gives me the price after the ChartNavigate.
What is the right way to do this?
New-MQL4.56789 features may surprise, nevertheless:
Step 0: always catch Chart_OkFLAG
bool Chart_OkFLAG;
double Chart_min,
Chart_MAX;
int Chart_ToBeMODIFIED = ChartID();
int Chart_ToBeMODIFIED_SubWinID = 0;
int Chart_nBarsRelativeSHIFT = 0;
Chart_OkFLAG = ChartNavigate( Chart_ToBeMODIFIED,
< CHART_BEGIN |
CHART_END |
CHART_CURRENT_POS >,
Chart_nBarsRelativeSHIFT
);
If OK: you won the first round, the GUI command has been accepted for being syntactically & context-wise correct. That does not mean, it was executed by the GUI processor. It was just let in, to the tail of the GUI-command-QUEUE.
Step 1: Enforce it's execution ( wise to think about deferring such processing to some safe place, after mission-critical elements have been fulfilled )
if ( Chart_OkFLAG ){
WindowRedraw(); // ol'-MQL4
ChartRedraw( Chart_ToBeMODIFIED ); // new-MQL4
Chart_min = ChartPriceMin( Chart_ToBeMODIFIED,
Chart_ToBeMODIFIED_SubWinID
);
Chart_MAX = ChartPriceMAX( Chart_ToBeMODIFIED,
Chart_ToBeMODIFIED_SubWinID
);
}
else{
// ERROR HANDLING onChartNavigate(T)ERROR
}
ALWAYS: do not hesitate to inline error handlers, even where MQL4 language seems straightforward ( you never know, when that effort pays off by avoiding hours of bug tracking )
double ChartPriceMin( const long Chart_ID = 0,
const int Chart_SubWindow_ID = 0
) {
double result = EMPTY_VALUE;
ResetLastError();
if ( !ChartGetDouble( Chart_ID,
CHART_PRICE_MIN,
Chart_SubWindow_ID,
result
)
){
PrintFormat( "ERROR: Code(%d) in [%s]",
GetLastError(),
__FUNCTION__,
);
}
return( result );
}
double ChartPriceMAX( const long Chart_ID = 0,
const int Chart_SubWindow_ID = 0
) {
double result = EMPTY_VALUE;
ResetLastError();
if ( !ChartGetDouble( Chart_ID,
CHART_PRICE_MAX,
Chart_SubWindow_ID,
result
)
){
PrintFormat( "ERROR: Code(%d) in [%s]",
GetLastError(),
__FUNCTION__,
);
}
return( result );
}

Why [ MetaTrader 4 ] can't write/open a file in windows 10 with the FileWrite() function?

I tried implementing the solutions from this post
Cannot open file on Ubuntu
But to no avail. I keep getting a 5002 error. (failed to open file) after FileWrite()
Even when I try out the example script on https://docs.mql4.com/files/filewrite
I get the same error. I tried running MetaTrader Terminal 4 in administrator mode but to no avail either.Does anyone know a solution to this?
#property version "1.00"
#property strict
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for receiving data from the terminal
input string InpSymbolName = "EURUSD"; // Сurrency pair
input ENUM_TIMEFRAMES InpSymbolPeriod = PERIOD_H1; // Time frame
input int InpFastEMAPeriod = 12; // Fast EMA period
input int InpSlowEMAPeriod = 26; // Slow EMA period
input int InpSignalPeriod = 9; // Difference averaging period
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Price type
//--- parameters for writing data to file
input string InpFileName = "MACD.csv"; // File name
input string InpDirectoryName = "Data"; // Folder name
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
bool sign_buff[]; // signal array (true - buy, false - sell)
datetime time_buff[]; // array of signals' appear time
int sign_size=0; // signal array size
double macd_buff[]; // array of indicator values
datetime date_buff[]; // array of indicator dates
int macd_size=0; // size of indicator arrays
//--- set indexing as time series
ArraySetAsSeries( sign_buff, true );
ArraySetAsSeries( time_buff, true );
ArraySetAsSeries( macd_buff, true );
ArraySetAsSeries( date_buff, true );
//--- reset last error code
ResetLastError();
//--- copying the time from last 1000 bars
int copied = CopyTime( NULL, 0, 0, 1000, date_buff );
if ( copied <= 0 )
{ PrintFormat( "Failed to copy time values. Error code = %d", GetLastError() );`
return;
}
//--- prepare macd_buff array
ArrayResize( macd_buff, copied );
//--- copy the values of main line of the iMACD indicator
for ( int i = 0; i< copied; i++ )
{ macd_buff[i] = iMACD( InpSymbolName, InpSymbolPeriod, InpFastEMAPeriod, InpSlowEMAPeriod, InpSignalPeriod, InpAppliedPrice, MODE_MAIN, i );
}
//--- get size
macd_size = ArraySize( macd_buff );
//--- analyze the data and save the indicator signals to the arrays
ArrayResize( sign_buff, macd_size - 1 );
ArrayResize( time_buff, macd_size - 1 );
for ( int i = 1; i < macd_size; i++ )
{
//--- buy signal
if ( macd_buff[i-1] < 0
&& macd_buff[i] >= 0 )
{
sign_buff[sign_size] = true;
time_buff[sign_size] = date_buff[i];
sign_size++;
}
//--- sell signal
if ( macd_buff[i-1] > 0
&& macd_buff[i] <= 0 )
{
sign_buff[sign_size] = false;
time_buff[sign_size] = date_buff[i];
sign_size++;
}
}
//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)
ResetLastError();
int file_handle = FileOpen( InpDirectoryName // aDirNAME
+ "//" // aFileSystemTreeBranchSEPARATOR
+ InpFileName, // aFileNAME
FILE_READ|FILE_WRITE|FILE_CSV // fileIoMODE_FLAGs
);
if ( file_handle != INVALID_HANDLE )
{
PrintFormat( "%s file is available for writing", InpFileName );
PrintFormat( "File path: %s\\Files\\", TerminalInfoString( TERMINAL_DATA_PATH ) );
//--- first, write the number of signals
FileWrite( file_handle, sign_size );
//--- write the time and values of signals to the file
for ( int i = 0; i < sign_size; i++ )
FileWrite( file_handle, time_buff[i], sign_buff[i] );
//--- close the file
FileClose( file_handle );
PrintFormat( "Data is written, %s file is closed", InpFileName );
}
else
PrintFormat( "Failed to open %s file, Error code = %d", InpFileName, GetLastError() );
}
Alt. A)
the algo does not check for a positive presence of an InpDirectoryName node in the filesystem.
Alt. B)
in case the Alt. A) passes, there ought be used a compatible delimiter in aFilePATH + aFileNAME string, i.e. == "\\", ( aFileSystemTreeBranchSEPARATOR ref.: Help for details )
thena fileIO shall take place in \Terminal\Common\Files ( in case a FILE_COMMON bitmask flag was added (used) in the int open_flags parameter),
otherwise,the operations will take place in a (sub)-directory of a MetaTrader Terminal 4 local folder== MQL4\Files ( for a live ExpertAdvisor or Custom Indicator or Script mode of operations )or== MQL4\Tester\Files ( in case of using ExpertAdvisor or Custom Indicatorinside a back-testing framework of MT4 Strategy Tester).

Order Sent Failed with Error #130

I'm trying to insert a takeprofit and stoploss argument in my SendOrder() function, but I'm getting the following error:
Order Sent Failed with Error #130
This is my code:
extern double takeprofit = 30.0;
extern double stoploss = 20.0;
stoploss = NormalizeDouble( stoploss, 5 ); // SET stop loss
Print( "stoploss", stoploss );
takeprofit = NormalizeDouble( takeprofit, 5 ); // SET take profit
ticket = OrderSend( Symbol(),
OP_SELL,
lotsize,
Ask,
100,
stoploss,
takeprofit,
0,
0,
0,
CLR_NONE
);
if ( ticket < 0 ) {
Print( "Order send failed with error #", GetLastError() );
}
else Print( "Order send sucesso!!" );
I already checked documentation for the function NormalizeDouble(), but I'm still getting the error.
What should I do?
A ) Fully comply with the MQL4 OrderSend() syntax requirements
int anOrderTKT; // DECLARE int
double anOrderLotSIZE; // DECLARE double
string anOrderCommentSTRING; // DECLARE string
anOrderTKT = OrderSend( _Symbol, // CPU-glitch, is faster than calling Symbol(),
OP_SELL, // XTO.Type
anOrderLotSIZE, // XTO.Size [LT]s
Bid, // XTO.EntryPRICE { OP_BUY: Ask | OP_SELL: Bid }
100, // XTO.SLIPPAGE [PT]s
0, // XTO.SL_PRICE
0, // XTO.TP_PRICE,
anOrderCommentSTRING, // XTO.Comment
0, // XTO.MagNUM#
0, // XTO.PendingOrderEXPIRE
CLR_NONE // GUI.MarkerCOLOR
); // ==> { EMPTY | aTkt# }
Your code fails at setting a correct SHORT trade Entry-Price, as it shall read rather Bid, not Ask ( this error is hidden as it is effectively masked-out by a rather cosmic distance of 100 points in a tolerable slippage distance from the said price ).
Your code fails at assigning int ( 0 ) in place, where string is expected.
B) Error 130: == "invalid stops"
You shall verify with your Broker a few details:
Does their Terms & Conditions allow to OrderSend() one-stop-instruction, incl, TP & SL, or does the Broker T&C require to first open a trade-position & only after that happens to allow an OrderModify() instruction to setup TP & SL price-levels?
In either case, check your Broker T&C settings for STOPLEVEL & FREEZELEVEL distances, within which Broker rejects any TP & SL setup(s) or modification(s) thereof.
C) A good practice is not to assign into extern iterator-variables
While this is not a root-cause for your trouble, do get accustomed with an industry best practices, one of which is not to assign any value to a declared extern. Rather declare your own variable, that you control scope & assignments thereof, but leave extern(s) un-touched from your code side.

Resources