Ahmad861 : Hey, since i use loops to iterate through symbols and timeframes how can i place orders, modify and close them ?
Your question is not clear. I don’t understand your goal - you open a POSITION (not an order !!!) for a symbol. The timeframe does not matter here - the POSITION will be opened by the symbol. It's another matter when several strategies want to trade on a hedge account at once - then for each strategy you need to use its own 'Magic number'.
Your question is not clear. I don’t understand your goal - you open a POSITION (not an order !!!) for a symbol. The timeframe does not matter here - the POSITION will be opened by the symbol. It's another matter when several strategies want to trade on a hedge account at once - then for each strategy you need to use its own 'Magic number'.
Sorry for not making it clear, it's a scalping strategy, the EA will find a setup on M3 chart as well as M5 chart and open a position, i want to set take profit based on an indicator, in this case M3 chart, how would i go about this ?
Sorry for not making it clear, it's a scalping strategy, the EA will find a setup on M3 chart as well as M5 chart and open a position, i want to set take profit based on an indicator, in this case M3 chart, how would i go about this ?
If Stop Loss is set using the indicator from the M3 timeframe, then you must create an indicator handle and specify the timeframe when creating it.
The Expert Advisor code demonstrates how to get values from two iMA indicators (a dash of the 'Fast' value and three values for the 'Slow' are displayed).
Also, if an intersection is detected, a message is displayed on the screen.
//+------------------------------------------------------------------+//| Example Intersection of two iMAs.mq5 |//| Copyright 2021, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2021, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.000"//--- input parametersinput group "Moving Average 'Fast'"inputint Inp_MA_Fast_ma_period = 3; // 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 "Moving Average 'Slow'"inputint Inp_MA_Slow_ma_period = 12; // MA Slow: averaging periodinputint Inp_MA_Slow_ma_shift = 1; // 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 priceinput group "Additional features"inputbool InpPrintLog = false; // Print log//---bool m_init_error = false; // error on InInitint handle_iMA_Fast; // variable for storing the handle of the iMA indicatorint handle_iMA_Slow; // variable for storing the handle of the iMA indicator//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- 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 indicator (\"Fast\") for the symbol %s/%s, error code %d",
Symbol(),Period(),GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}
//--- 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 indicator (\"Slow\") for the symbol %s/%s, error code %d",
Symbol(),Period(),GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//---double ma_fast[],ma_slow[];
ArraySetAsSeries(ma_fast,true);
ArraySetAsSeries(ma_slow,true);
int start_pos=0,count=3;
if(!iGetArray(handle_iMA_Fast,0,start_pos,count,ma_fast) || !iGetArray(handle_iMA_Slow,0,start_pos,count,ma_slow))
return;
string ima_values="";
for(int i=0; i<count; i++)
ima_values=ima_values+IntegerToString(i)+": Fast "+DoubleToString(ma_fast[i],Digits()+1)+", Slow "+DoubleToString(ma_slow[i],Digits()+1)+"\n";
string ima_intersections="no crossing";
if(ma_fast[1]<ma_slow[1] && ma_fast[0]>ma_slow[0])
ima_intersections="crossing up";
elseif(ma_fast[1]>ma_slow[1] && ma_fast[0]<ma_slow[0])
ima_intersections="crossing down";
Comment(ima_values+"\n"+ima_intersections);
}
//+------------------------------------------------------------------+//| Get value of buffers |//+------------------------------------------------------------------+bool iGetArray(constint handle,constint buffer,constint start_pos,
constint count,double &arr_buffer[])
{
bool result=true;
if(!ArrayIsDynamic(arr_buffer))
{
if(InpPrintLog)
PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
return(false);
}
ArrayFree(arr_buffer);
//--- reset error codeResetLastError();
//--- fill a part of the iBands array with values from the indicator bufferint copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
if(copied!=count)
{
//--- if the copying fails, tell the error codeif(InpPrintLog)
PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
__FILE__,__FUNCTION__,count,copied,GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false);
}
return(result);
}
//+------------------------------------------------------------------+
How to use the CSymbolInfo trade class
The code:
Hey, since i use loops to iterate through symbols and timeframes how can i place orders, modify and close them ?
Your question is not clear. I don’t understand your goal - you open a POSITION (not an order !!!) for a symbol. The timeframe does not matter here - the POSITION will be opened by the symbol. It's another matter when several strategies want to trade on a hedge account at once - then for each strategy you need to use its own 'Magic number'.
The simplest example is - Simple example One position
Your question is not clear. I don’t understand your goal - you open a POSITION (not an order !!!) for a symbol. The timeframe does not matter here - the POSITION will be opened by the symbol. It's another matter when several strategies want to trade on a hedge account at once - then for each strategy you need to use its own 'Magic number'.
The simplest example is - Simple example One position
Sorry for not making it clear, it's a scalping strategy, the EA will find a setup on M3 chart as well as M5 chart and open a position, i want to set take profit based on an indicator, in this case M3 chart, how would i go about this ?
Sorry for not making it clear, it's a scalping strategy, the EA will find a setup on M3 chart as well as M5 chart and open a position, i want to set take profit based on an indicator, in this case M3 chart, how would i go about this ?
If Stop Loss is set using the indicator from the M3 timeframe, then you must create an indicator handle and specify the timeframe when creating it.
For example like this:
can you help me understand how to code the part that opens and closes positions, checks for existing postions etc.
I cant find an article that clearly explains how to go about it, any suggestions from you would be grateful
can you help me understand how to code the part that opens and closes positions, checks for existing postions etc.
I cant find an article that clearly explains how to go about it, any suggestions from you would be grateful
Simple Advisor with Stop Loss and Take Profit
Intersection of two iMAs
Code: ' Example Intersection of two iMAs.mq5'
Task: determine the moment of intersection of two iMAs.
Remember the main rule: in MQL5 the indicator handle needs to be received once (it is optimal to do this in the OnInit function).
The Expert Advisor code demonstrates how to get values from two iMA indicators (a dash of the 'Fast' value and three values for the 'Slow' are displayed).
Also, if an intersection is detected, a message is displayed on the screen.
Result:
We are waiting for the size of the current bar to reach the specified size
Code: Example Current candle size.mq5