By depth im hoping you mean lookback period, i usually have 150-200 bars on my screen, so i would like for it to be 200
Yes, you got it right. Next, you need to enter the settings for "similar candles" - this is a percentage spread or a spread in points. The parameter "group of candles" is also needed - a group of neighboring candles among which there is a search ...
Yes, you got it right. Next, you need to enter the settings for "similar candles" - this is a percentage spread or a spread in points. The parameter "group of candles" is also needed - a group of neighboring candles among which there is a search ...
//+------------------------------------------------------------------+//| Close all positions First Swap |//+------------------------------------------------------------------+void CloseAllPositionsFirstSwap(void)
{
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positionsif(m_position.SelectByIndex(i)) // selects the position by index for further access to its propertiesif(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
{
double swap=m_position.Swap();
if(swap<=0.0)
continue;
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbolif(InpPrintLog)
Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
}
if(m_position.PositionType()==POSITION_TYPE_SELL)
{
if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbolif(InpPrintLog)
Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
}
}
}
It is recommended to call this function at the beginning of the day.
The EA can open only one position of the same type
Code: Example One position of one type.mq5
How the Expert Advisor works: the 'CalculateAllPositions' function is called in 'OnTick'. The 'CalculateAllPositions' function calculates 'BUY' and 'SELL' positions.
The number of pending orders (only system functions, no OOP)
'CalculateAllPendingOrdersSystem' function
//+------------------------------------------------------------------+//| Calculate all pending orders for symbol |//+------------------------------------------------------------------+int CalculateAllPendingOrdersSystem(conststring symbol,constulong magic)
{
int total=0;
for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
{
ulong order_ticket=OrderGetTicket(i); // selects the pending order by index for further access to its propertiesif(order_ticket==0)
continue;
string order_symbol=OrderGetString(ORDER_SYMBOL); // get symbollong order_magic=OrderGetInteger(ORDER_MAGIC); // get magic//---if(order_symbol==symbol && order_magic==magic)
total++;
}
//---return(total);
}
There are two pending orders with Magic '200' and one with Magic '100' in the market - only three pending orders (all three pending orders for the 'EURAUD' symbol).
Now we start thinking: we need to add two arrays - for two iMAs. So we change the counter and add these two arrays, and we will declare two variables - they will store two handles
#property indicator_separate_window#property indicator_buffers3#property indicator_plots1//--- plot Diff_Two_iMA#property indicator_label1"Diff_Two_iMA"#property indicator_type1DRAW_LINE#property indicator_color1clrYellowGreen#property indicator_style1STYLE_SOLID#property indicator_width11//--- input parametersinputint Input1=9;
//--- indicator buffersdouble Diff_Two_iMABuffer[];
double FastBuffer[];
double SlowBuffer[];
//---
int handle_iMA_Fast; // variable for storing the handle of the iMA indicator
int handle_iMA_Slow; // variable for storing the handle of the iMA indicator//+------------------------------------------------------------------+//| Custom indicator initialization function |
Add variable 'm_init_error' - this is a flag. If there is an error while creating the indicator, this flag will be set to 'true'.
double SlowBuffer[];
//---bool m_init_error = false; // error on InInit//+------------------------------------------------------------------+//| Custom indicator initialization function |
Adding input parameters for the 'Fast' and 'Slow' indicator
//--- input parametersinput group "Fast MA"inputint Inp_MA_Fast_ma_period = 9; // MA Fast: averaging periodinputint Inp_MA_Fast_ma_shift = 0; // MA Fast: horizontal shiftinputENUM_MA_METHOD Inp_MA_Fast_ma_method = MODE_SMA; // MA Fast: smoothing typeinputENUM_APPLIED_PRICE Inp_MA_Fast_applied_price = PRICE_CLOSE; // MA Fast: type of priceinput group "Slow MA"inputint Inp_MA_Slow_ma_period = 26; // MA Slow: averaging periodinputint Inp_MA_Slow_ma_shift = 0; // MA Slow: horizontal shiftinputENUM_MA_METHOD Inp_MA_Slow_ma_method = MODE_SMA; // MA Slow: smoothing typeinputENUM_APPLIED_PRICE Inp_MA_Slow_applied_price = PRICE_CLOSE; // MA Slow: type of price//--- indicator buffers
Go to 'OnInit': link two arrays and create two handles
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mappingSetIndexBuffer(0,Diff_Two_iMABuffer,INDICATOR_DATA);
SetIndexBuffer(1,FastBuffer,INDICATOR_DATA);
SetIndexBuffer(2,SlowBuffer,INDICATOR_DATA);//--- create handle of the indicator iMA
handle_iMA_Fast=iMA(Symbol(),Period(),Inp_MA_Fast_ma_period,Inp_MA_Fast_ma_shift,
Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price);
//--- if the handle is not createdif(handle_iMA_Fast==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iMA ('Fast') indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
m_init_error=true;
return(INIT_SUCCEEDED);
}
//--- create handle of the indicator iMA
handle_iMA_Slow=iMA(Symbol(),Period(),Inp_MA_Slow_ma_period,Inp_MA_Slow_ma_shift,
Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price);
//--- if the handle is not createdif(handle_iMA_Slow==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iMA ('Slow') indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
m_init_error=true;
return(INIT_SUCCEEDED);
}//---return(INIT_SUCCEEDED);
}
Go to OnCalculate - Here are mainly copy-paste operations (using the iMA help)
Apply copy-paste and do some manual work. From the help ( iMA ) we copy almost the entire function and change it a little for ourselves (change the name of the indicator buffers, change the name of the handle and throw out 'Comment') Here just carefully compare the help ( iMA ) and my code. First of all, add the variable 'bars_calculated' to the program header:
int handle_iMA_Slow; // variable for storing the handle of the iMA indicator//---int bars_calculated = 0; // we will keep the number of values in the Moving Average indicator bool m_init_error = false; // error on InInit//+------------------------------------------------------------------+//| Custom indicator initialization function |
I have highlighted the main thing - this is the calculation of the difference
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
if(m_init_error)
return(0);
//--- number of values copied from the iMA indicatorint values_to_copy;
//--- determine the number of values calculated in the indicatorint calculated_fast=BarsCalculated(handle_iMA_Fast);
if(calculated_fast<=0)
{
PrintFormat("BarsCalculated(handle_iMA_Fast) returned %d, error code %d",calculated_fast,GetLastError());
return(0);
}
int calculated_slow=BarsCalculated(handle_iMA_Slow);
if(calculated_slow<=0)
{
PrintFormat("BarsCalculated(handle_iMA_Slow) returned %d, error code %d",calculated_slow,GetLastError());
return(0);
}
if(calculated_fast!=calculated_slow)
{
PrintFormat("BarsCalculated(handle_iMA_Fast) returned %d, BarsCalculated(handle_iMA_Slow) returned %d",calculated_fast,calculated_slow);
return(0);
}
int calculated=calculated_fast;
//--- if it is the first start of calculation of the indicator or if the number of values in the iMA indicator changed//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- if the iMABuffer array is greater than the number of values in the iMA indicator for symbol/period, then we don't copy everything//--- otherwise, we copy less than the size of indicator buffersif(calculated>rates_total)
values_to_copy=rates_total;
else
values_to_copy=calculated;
}
else
{
//--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()//--- for calculation not more than one bar is added
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- fill the iMABuffer array with values of the Moving Average indicator//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operationif(!FillArrayFromBuffer(FastBuffer,Inp_MA_Fast_ma_shift,handle_iMA_Fast,values_to_copy))
return(0);
if(!FillArrayFromBuffer(SlowBuffer,Inp_MA_Slow_ma_shift,handle_iMA_Slow,values_to_copy))
return(0);
//--- memorize the number of values in the Moving Average indicator
bars_calculated=calculated;
//--- main loopint limit=prev_calculated-1;
if(prev_calculated==0)
limit=0;
for(int i=limit; i<rates_total; i++)
{
Diff_Two_iMABuffer[i]=FastBuffer[i]-SlowBuffer[i];
}//--- return value of prev_calculated for next callreturn(rates_total);
}
//+------------------------------------------------------------------+//| Filling indicator buffers from the MA indicator |//+------------------------------------------------------------------+bool FillArrayFromBuffer(double &values[], // indicator buffer of Moving Average valuesint shift, // shiftint ind_handle, // handle of the iMA indicatorint amount // number of copied values
)
{
//--- reset error codeResetLastError();
//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 indexif(CopyBuffer(ind_handle,0,-shift,amount,values)<0)
{
//--- if the copying fails, tell the error codePrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false);
}
//--- everything is finereturn(true);
}
//+------------------------------------------------------------------+//| Indicator deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
if(handle_iMA_Fast!=INVALID_HANDLE)
IndicatorRelease(handle_iMA_Fast);
if(handle_iMA_Slow!=INVALID_HANDLE)
IndicatorRelease(handle_iMA_Slow);
}
//+------------------------------------------------------------------+
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.
Problem # 1: Determine to what depth you need to scan the market.
By depth im hoping you mean lookback period, i usually have 150-200 bars on my screen, so i would like for it to be 200
By depth im hoping you mean lookback period, i usually have 150-200 bars on my screen, so i would like for it to be 200
Yes, you got it right. Next, you need to enter the settings for "similar candles" - this is a percentage spread or a spread in points. The parameter "group of candles" is also needed - a group of neighboring candles among which there is a search ...
Yes, you got it right. Next, you need to enter the settings for "similar candles" - this is a percentage spread or a spread in points. The parameter "group of candles" is also needed - a group of neighboring candles among which there is a search ...
I did not get this part
I searched for similar candles in the Similar Candle indicator - you can take it as a basis.
Im sorry, but i still don't understand what i should be doing
Close a position if Swap was accrued
Function: 'CloseAllPositionsFirstSwap'
It is recommended to call this function at the beginning of the day.
The EA can open only one position of the same type
Code: Example One position of one type.mq5
How the Expert Advisor works: the 'CalculateAllPositions' function is called in 'OnTick'. The 'CalculateAllPositions' function calculates 'BUY' and 'SELL' positions.
The number of pending orders (only system functions, no OOP)
'CalculateAllPendingOrdersSystem' function
There are two pending orders with Magic '200' and one with Magic '100' in the market - only three pending orders (all three pending orders for the 'EURAUD' symbol).
Run the script:
We get the result:
How to create an indicator - the difference between two iMAs (Moving Average)
Forum on trading, automated trading systems and testing trading strategies
Cannot initialize buffers
Vladimir Karputov, 2021.08.27 05:32
How to create an indicator - the difference between two iMAs (Moving Average).
Step 1. Right click on the folder and select 'New File'
Step 2. Indicator name (string 'Namde'), one input parameter ('Parameters')
Step 3. Selecting the full version of OnCalculate
Step 4. 'Indicator in separate windows'c, add one line' Diff_Two_iMA '
We get the workpiece:
Now we start thinking: we need to add two arrays - for two iMAs. So we change the counter and add these two arrays, and we will declare two variables - they will store two handles
Add variable 'm_init_error' - this is a flag. If there is an error while creating the indicator, this flag will be set to 'true'.
Adding input parameters for the 'Fast' and 'Slow' indicator
Go to 'OnInit': link two arrays and create two handles
Go to OnCalculate - Here are mainly copy-paste operations (using the iMA help)
Continued later ...
Forum on trading, automated trading systems and testing trading strategies
Cannot initialize buffers
Vladimir Karputov, 2021.08.27 12:24
Continuation ...
Apply copy-paste and do some manual work. From the help ( iMA ) we copy almost the entire function and change it a little for ourselves (change the name of the indicator buffers, change the name of the handle and throw out 'Comment') Here just carefully compare the help ( iMA ) and my code. First of all, add the variable 'bars_calculated' to the program header:
I have highlighted the main thing - this is the calculation of the difference
Result: