![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Thanks mladen. You've been really helpful. I have finally managed to get my head around the code. I actually read those lessons a few weeks back but forgot about the functions. I went through the code manually and used dummy figures which helped. But when I did so one line of code seemed to be miscalculating lots:
if (MarketInfo(Symbol(), MODE_DIGITS) == 3 || MarketInfo(Symbol(), MODE_DIGITS) == 5) SLDistanceLong *= 10.0;
So with my example:
EURUSD with my broker is to 5 decimal places. So MODE_DIGITS == 5. Hence SLDistanceLong gets * 10.
SLDistanceLong is 50 pips. So 50 pips * 10 = 500 pips. MODE_TICKVALUE is 0.06 (I think) so 500 pips * 0.06 = 30.
5% Risk of 20k (as an example) = 1000. 1000 / 30 = 33.33333 recurring, rounded to 33.3 because LOTSTEP = 0.1 say. So 33.3 lots, but it should be 333.3 - the *= 10 seems to be throwing out the calc. And I also dont understand why the currency MODE_DIGITS is needed for the calculation?
Another question I had was if I put the GetLots function calc at the very end of my code, would it still work? Because the OrderSend() function would occur before the GetLots function, and so the OrderSend() wouldnt have the lots as the GetLots function hasnt calculated lots yet.
Oh it's finally clicked, the tick value is diluted on 5 digit brokers so you * 10 to bring tick value back. Is it done for 3 digits because of JPY pairs?
...
Yes
All that is done in order to make it work on 4 digit as well as 5 digit brokers
As of this question :
"Another question I had was if I put the GetLots function calc at the very end of my code, would it still work? Because the OrderSend() function would occur before the GetLots function, and so the OrderSend() wouldnt have the lots as the GetLots function hasnt calculated lots yet."
It does not matter where it is in within the code, functions works "per call" sort of speaking, so when you place a call to a function, program executes the function code and then resumes its regular execution
Oh it's finally clicked, the tick value is diluted on 5 digit brokers so you * 10 to bring tick value back. Is it done for 3 digits because of JPY pairs?
Hello all,
I have sifted through my compiled error log, but one error remains and it relates to my functions. For instance one reads:
Function 'GetLotsLong' is not referenced and will be removed from exp-file'
This function calculates lots when going long, and is called under my OrderSend(Symbol(), OP_BUY, GetLotsLong, ...
Any idea where I have gone wrong?
...
Do it like this (in the example yo showed) :
where risk is your defined risk in % and stop loss is a stop loss in pips
Hello all,
I have sifted through my compiled error log, but one error remains and it relates to my functions. For instance one reads:
Function 'GetLotsLong' is not referenced and will be removed from exp-file'
This function calculates lots when going long, and is called under my OrderSend(Symbol(), OP_BUY, GetLotsLong, ...
Any idea where I have gone wrong?Ah I see - I read the mql4 book on functions but didnt realise it required the brackets and variables included. It now works error free (sort of anyway!) Just working my way through the errors in the journal, but weirdly had an error 134 yesterday but now I don't, even though I didnt change the code.
And the EA starts with order #218. . .there are no orders from #1 up to #217 visible in the journal.
Then EA then stopped because of Stop Out.
Then says Tester: order #218 is closed, and prints this all the way to #1 all in the space of a few seconds. Is there a typical code issue that gives rise to this?
Also I've been reading this:
MyForexResults - Limiting the number of trades placed by your Expert Advisor using MQL4
And was wondering whether I would need to limit trades to a maximum of 1 every four hours as I base entries on MACD H4 chart using the code below:
MacdCurrent = iCustom(Symbol(), PERIOD_H4, "MACD TRUE", FastMACDPeriod,SlowMACDPeriod,SignalPeriod, 0, 1);
MacdPrevious = iCustom(Symbol(), PERIOD_H4, "MACD TRUE", FastMACDPeriod,SlowMACDPeriod,SignalPeriod, 0, 2);
SignalCurrent = iCustom(Symbol(), PERIOD_H4, "MACD TRUE", FastMACDPeriod,SlowMACDPeriod,SignalPeriod, 1, 1);
SignalPrevious = iCustom(Symbol(), PERIOD_H4, "MACD TRUE", FastMACDPeriod,SlowMACDPeriod,SignalPeriod, 1, 2);
Thanks in advance.
Oh by the way, in addition to the above ^^^ my lots dont calculate correctly as it enters 0.01 each time. Will look at this tonight.
...
Order size depends onOh by the way, in addition to the above ^^^ my lots dont calculate correctly as it enters 0.01 each time. Will look at this tonight.
Order Send Error 130 - invalid stop. Why?
I have this code using an icustom indicator trigger to send and order short or long. In the journal I can see the trigger firing but then the orders fail with the above error.
The code is very simple, all i can accomplish at the moment but im learning slowly.
//+------------------------------------------------------------------+
//| Tester.mq4 |
//| |
//+------------------------------------------------------------------+
#include
extern int dist2=6;
extern int SignalBar=2;
input double lots=3;
input double TakeProfit=360; //? is this to be points or pips?
input double StopLoss=80; //? is this to be points or pips?
int OpenOrders=0, cnt=0;
int start()
{
bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",1,1)!=EMPTY_VALUE;
bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,1)!=EMPTY_VALUE;
Comment(uptrend," ",downtrend);
if(downtrend)
{
bool ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,2,StopLoss,TakeProfit,NULL,0,0,clrRed);
}
else if(uptrend)
{
ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,2,StopLoss,TakeProfit,NULL,0,0,clrGreen);
}
return(0);
}
Now why for the life of me can I not get it to open an order?
MLaden did try to help me with this and other issues in another post, thank you sir, however I am still at a loss with this and need some help, if someone could give an example of how to fix this?
Thanks
I have this code using an icustom indicator trigger to send and order short or long. In the journal I can see the trigger firing but then the orders fail with the above error.
The code is very simple, all i can accomplish at the moment but im learning slowly.
//+------------------------------------------------------------------+
//| Tester.mq4 |
//| |
//+------------------------------------------------------------------+
#include
extern int dist2=6;
extern int SignalBar=2;
input double lots=3;
input double TakeProfit=360; //? is this to be points or pips?
input double StopLoss=80; //? is this to be points or pips?
int OpenOrders=0, cnt=0;
int start()
{
bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",1,1)!=EMPTY_VALUE;
bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,1)!=EMPTY_VALUE;
Comment(uptrend," ",downtrend);
if(downtrend)
{
bool ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,2,StopLoss,TakeProfit,NULL,0,0,clrRed);
}
else if(uptrend)
{
ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,2,StopLoss,TakeProfit,NULL,0,0,clrGreen);
}
return(0);
}
Now why for the life of me can I not get it to open an order?
MLaden did try to help me with this and other issues in another post, thank you sir, however I am still at a loss with this and need some help, if someone could give an example of how to fix this?
ThanksTry opening the orders without stop loss and take profit
If you do not get an error then, than it means that your broker is ECN/STP type of broker and that you have to open an order without sl and tp, and then modify an already opened order to desired sl and tp