Thank you! I didn't know MQL had a function for that. I was about to create my own. But before that I decided toask on the forum.
This is a consequence of the wrong approach to language learning. If you had worked with the documentation, not with the g.code from......... reading the resource help, you would have read about it. And you wouldn't have to ask.
I have done this so that when I am trailing a contract, the Take Profitequals 500 and Stop Loss equals 150.
Up to five additional contracts are bought.
Can I prohibit the opening of new positions when two are already open? Limit the volume of open positions.
//--------------------------------------------------------------------void OPENORDER(string ord)
{
double priceL=m_symbol.Ask();
if(ord=="Sell")
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,my_lot,priceL)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
elseif(!m_trade.Sell(my_lot,Symbol(),m_symbol.Bid(),my_SL,my_TP,""))
Print("BUY_STOP -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of Retcode: ",m_trade.ResultRetcodeDescription(),
", ticket of order: ",m_trade.ResultOrder()); // Если sell, то не открываемсяdouble priceS=m_symbol.Bid();
if(ord=="Buy")
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,my_lot,priceS)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
elseif(!m_trade.Buy(my_lot,Symbol(),m_symbol.Ask(),my_SL,my_TP,""))
Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription(),
", ticket of deal: ",m_trade.ResultDeal());
return;
}
double iMAGet(constint handle,constint index)
{
double MA[];
ArraySetAsSeries(MA,true);
//--- reset error codeResetLastError();
//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 indexif(CopyBuffer(handle,0,0,index+1,MA)<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(0.0);
}
return(MA[index]);
}
//+------------------------------------------------------------------+//| Refreshes the symbol quotes data |//+------------------------------------------------------------------+bool RefreshRates()
{
//--- refresh ratesif(!m_symbol.RefreshRates())
return(false);
//--- protection against the return value of "zero"if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
return(false);
//---return(true);
}
//+------------------------------------------------------------------+//| Get Time for specified bar index |//+------------------------------------------------------------------+datetimeiTime(constint index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
{
if(symbol==NULL)
symbol=Symbol();
if(timeframe==0)
timeframe=Period();
datetime Time[];
datetime time=0;
ArraySetAsSeries(Time,true);
int copied=CopyTime(symbol,timeframe,index,1,Time);
if(copied>0)
time=Time[0];
return(time);
}
//+------------------------------------------------------------------+//| 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))
{
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 codePrintFormat("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);
}
void TrailingOrder()
{
if(InpTrailingOrderLimit==0)
return;
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positionsif(m_position.SelectByIndex(i))
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
{
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingOrderLimit+ExtTrailingOrderStep)
if(m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingOrderLimit+ExtTrailingOrderStep))
{
if(!m_trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTrailingOrderLimit),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket());
OPENORDER("Buy");
}
}
else
{
if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingOrderLimit+ExtTrailingOrderStep)
if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingOrderLimit+ExtTrailingOrderStep))) ||
(m_position.StopLoss()==0))
{
if(!m_trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingOrderLimit),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket());
OPENORDER("Sell");
}
}
}
}
void Trailing()
{
if(InpTStop==0)
return;
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
{
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTStop+ExtTStep)
if(m_position.StopLoss()<m_position.PriceCurrent()-(ExtTStop+ExtTStep))
{
if(!m_trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTStepShift),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket(),
" Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
else
{
if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTStop+ExtTStep)
if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTStop+ExtTStep))) ||
(m_position.StopLoss()==0))
{
if(!m_trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTStepShift),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket(),
" Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
}
}
Alexey Viktorov: This is a consequence of the wrong approach to the study of the language. If you worked with documentation, rather than with coding from ......... reading the help on resources, you would read about it. And you wouldn't have to ask.
I knew my answer would be followed by a lecture... You know, like you do... I thought I said: "Thank you," but no... You have to write something... If you know the function from the documentation, good for you! But if you don't know the code properly, you may study MQL documentation up and down, but it will be of very little use in practice...!
Hello.
Can you tell me if it's possible to add a column with comments or explanations in the EA parameters... ???
Thank you.
Yes, but you need the source.
Thank you! I didn't know MQL had a function for that. I was about to create my own. But before that I decided toask on the forum.
Hello.
Can you tell me if it's possible to add a column with comments or explanations in the EA parameters... ???
Thank you.
When will you learn to read the documentation?
This is a consequence of the wrong approach to the study of the language. If you worked with documentation, rather than with coding from ......... reading the help on resources, you would read about it. And you wouldn't have to ask.
I knew my answer would be followed by a lecture... You know, like you do... I thought I said: "Thank you," but no... You have to write something... If you know the function from the documentation, good for you! But if you don't know the code properly, you may study MQL documentation up and down, but it will be of very little use in practice...!
I accidentally came across this bug
Create two identical buttons with slightly different coordinates
it looks like this
But as a result, only the first button reacts to OnChartEvent, the second doesn't.
I did it so that when trailing a contract, the Take Profitequals 500 and Stop Loss equals 150.
I don't know MQL5, but I think
I accidentally came across this bug
Create two identical buttons with slightly different coordinates
it looks like this
but as a result, only the first button reacts to OnChartEvent, the second doesn't.
Can you see the code in the handler?
And why is there -73 in the parameters, it's not very clear...?
Can I see the code in the handler?
And why is there -73 in the parameters, not very clear...?
It doesn't give out any errors.
With these coordinates the button is upside down, because the length of the button is calculated +/- from point X