
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
I have sorted a solution for the Time Filter Close All
External Variables
extern string timefilter="Time Filter";
extern int gmtshift=1; // gmt offset of the broker
extern bool generalfilter=false; // enable time filter
extern int starthour=7; // start hour to trade after this hour
extern int startminutes=0; // minutes of the start hour
extern int endhour=21; // stop to trade after this hour
extern int endminutes=0; // minutes of the start hour
extern bool tradesunday=true; // trade on sunday
extern bool fridayfilter=false; // enable special time filter on friday
extern int fridayhour=21; // stop to trade after this hour
extern int fridayminutes=0; // minutes of the friday hour
[/CODE]
string istarthour,istartminutes,iendhour,iendminutes,ifridayhour,ifridayminutes;
datetime tstart,tend,tfriday;[/CODE]
Place Code in Start,
[CODE] if(generalfilter){
nstarthour=starthour+(gmtshift);if(nstarthour>23)nstarthour=nstarthour-24;
if(nstarthour<10)istarthour="0"+nstarthour;
if(nstarthour>9)istarthour=nstarthour;
if(startminutes<10)istartminutes="0"+startminutes;
if(startminutes>9)istartminutes=startminutes;
tstart=StrToTime(istarthour+":"+istartminutes);
nendhour=endhour+(gmtshift);if(nendhour>23)nendhour=nendhour-24;
if(endhour<10)iendhour="0"+nendhour;
if(endhour>9)iendhour=nendhour;
if(endminutes<10)iendminutes="0"+endminutes;
if(endminutes>9)iendminutes=endminutes;
tend=StrToTime(iendhour+":"+iendminutes);
}
if(fridayfilter){
nfridayhour=fridayhour+(gmtshift);if(nfridayhour>23)nfridayhour=nfridayhour-24;
if(nfridayhour<10)ifridayhour="0"+nfridayhour;
if(nfridayhour>9)ifridayhour=nfridayhour;
if(fridayminutes<10)ifridayminutes="0"+fridayminutes;
if(fridayminutes>9)ifridayminutes=fridayminutes;
tfriday=StrToTime(ifridayhour+":"+ifridayminutes);
}
if((generalfilter && (nstarthour<nendhour && TimeCurrent()tend) || (nstarthour>nendhour && TimeCurrent()tend))
|| (tradesunday==false && DayOfWeek()==0) || (fridayfilter && DayOfWeek()==5 && TimeCurrent()>tfriday))
{
CloseAll();
Comment("Non-trading Hours All Positions Closed");
return(0);
}and the CloseAll function.
[CODE]
void CloseAll(){
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type){
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Lime); break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, LightGreen );
} if(result == false){
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
thanks for the hints and tips Kalenzo
StopLoss
Gidday
I have been trying an experiment for calculating an SL based on the tickvalue a max dd and percentage per trade.
double lotSize = NormalizeDouble(Kellylot(),2);
double Pip = Point;
if(Digits==3 || Digits==5) Pip = 10*Point;
double Bal = AccountBalance(); // $1000
double TV = MarketInfo(Symbol(), MODE_TICKVALUE); // $1
double Risk = 2; // 2%
double MaxDD = 10; // 10%
double RiskCapital = Bal*MaxDD/100; // Total Exposure 10% of $1000 = $100
double RPT = Bal*Risk/100; // 2% of $1000 = $20
double stopLoss = NormalizeDouble(RPT/TV*lotSize/Pip,Digits); //$20/1*0.01/10 = 0.02000
double Positions = MathAbs(RiskCapital/RPT); // Maximum 5 positions 100 / 20
The idea is that depending on the pair and the pairs individual tickvalue and the lotsize the 2% sl can be set.
I think its right but would like someone to check it over if possible.
Gidday
I have been trying an experiment for calculating an SL based on the tickvalue a max dd and percentage per trade.
double lotSize = NormalizeDouble(Kellylot(),2);
double Pip = Point;
if(Digits==3 || Digits==5) Pip = 10*Point;
double Bal = AccountBalance(); // $1000
double TV = MarketInfo(Symbol(), MODE_TICKVALUE); // $1
double Risk = 2; // 2%
double MaxDD = 10; // 10%
double RiskCapital = Bal*MaxDD/100; // Total Exposure 10% of $1000 = $100
double RPT = Bal*Risk/100; // 2% of $1000 = $20
double stopLoss = NormalizeDouble(RPT/TV*lotSize/Pip,Digits); //$20/1*0.01/10 = 0.02000
double Positions = MathAbs(RiskCapital/RPT); // Maximum 5 positions 100 / 20
The idea is that depending on the pair and the pairs individual tickvalue and the lotsize the 2% sl can be set.
I think its right but would like someone to check it over if possible.Good job Beno,
It's allways better to give fishing rod than fish. I'm very happy that I could help you with some comments.
As for the stop loss, the idea is good, however try also to consider it in reverse way:
first set stop loss
secondly set trade size
In this way you will fit the system entry/exit to the market conditions, and won't risk more than X% from your account, but the exit will depend on system logic instead of risk percent.
Good job Beno,
It's allways better to give fishing rod than fish. I'm very happy that I could help you with some comments.
As for the stop loss, the idea is good, however try also to consider it in reverse way:
first set stop loss
secondly set trade size
In this way you will fit the system entry/exit to the market conditions, and won't risk more than X% from your account, but the exit will depend on system logic instead of risk percent.Gidday Kalenzo
I am getting an Error opening SELL order : invalid stops with the EA is on the demo and live account and no trades are opening. but it works well on the tester and has no error messages.
EURUSD,Daily: modify #1 buy 0.01 EURUSD at 1.43348 sl: 1.43895 tp: 0.00000 ok
The SL 0.00547 points difference
I have checked the
MODE_FREEZELEVEL 0.0000
MODE_STOPLEVEL 0.0000
double Bal = AccountFreeMargin();
double TV = MarketInfo(Symbol(), MODE_TICKVALUE);
double Risk = 0.3;
double MaxDD = 10;
double RiskCapital = Bal*MaxDD/100;
double RPT = Bal*Risk/100;
double stopLoss = NormalizeDouble((RPT/TV)*lotSize/pips2dbl,Digits);
double Positions = MathAbs(RiskCapital/RPT);
double SL;
//---- sell conditions
if(sellsig && ttime!=Time[0]){
double bid = NormalizeDouble(Bid, Digits);
SL = NormalizeDouble(Bid + stopLoss * pips2dbl, Digits);
res=OrderSend(symbol,OP_SELL,lotSize,bid,slippage * pips2dbl,SL,0,"Pivot_Sell",SpecificMagic,0,Red);
if( res<0 )
{
Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
Print("Bid=" + DoubleToStr(bid,Digits) + " : SL=" + DoubleToStr(SL,Digits));
res=0;
}
else //now enter in the SL and TP via OrderModify to make compatible with ECN broker
{
if (OrderSelect(res, SELECT_BY_TICKET))
{
if (!OrderModify(res, Bid, SL, 0, 0, Red))
Print("Error modifying order");
}
}
ttime=Time[0];
return;
}
//---- buy conditions
if(buysig && ttime!=Time[0]) {
double ask = NormalizeDouble(Ask, Digits);
SL = NormalizeDouble(Ask - stopLoss * pips2dbl, Digits);
res=OrderSend(symbol,OP_BUY,lotSize,ask,slippage * pips2dbl,SL,0,"Pivot Buy",SpecificMagic,0,Blue);
if( res<0 )
{
Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
Print("Ask=" + DoubleToStr(ask,Digits) + " : SL=" + DoubleToStr(SL,Digits));
res=0;
}
else //now enter in the SL and TP via OrderModify to make compatible with ECN broker
{
if (OrderSelect(res, SELECT_BY_TICKET))
{
if (!OrderModify(res, Ask, SL, 0, 0, Blue))
Print("Error modifying order");
}
}
ttime=Time[0];
return;
}
}I have never come accross this before.
Cheers
Beno
Latch/Unlatch function
Is there a way to code a latch/unlatch function in mq4. You would latch a bit true based on a condition and it would store this value until unlatched by another condition.
cmfxtrader
Gidday Kalenzo
I am getting an Error opening SELL order : invalid stops with the EA is on the demo and live account and no trades are opening. but it works well on the tester and has no error messages.
EURUSD,Daily: modify #1 buy 0.01 EURUSD at 1.43348 sl: 1.43895 tp: 0.00000 ok
The SL 0.00547 points difference
I have checked the
MODE_FREEZELEVEL 0.0000
MODE_STOPLEVEL 0.0000
double Bal = AccountFreeMargin();
double TV = MarketInfo(Symbol(), MODE_TICKVALUE);
double Risk = 0.3;
double MaxDD = 10;
double RiskCapital = Bal*MaxDD/100;
double RPT = Bal*Risk/100;
double stopLoss = NormalizeDouble((RPT/TV)*lotSize/pips2dbl,Digits);
double Positions = MathAbs(RiskCapital/RPT);
double SL;
//---- sell conditions
if(sellsig && ttime!=Time[0]){
double bid = NormalizeDouble(Bid, Digits);
SL = NormalizeDouble(Bid + stopLoss * pips2dbl, Digits);
res=OrderSend(symbol,OP_SELL,lotSize,bid,slippage * pips2dbl,SL,0,"Pivot_Sell",SpecificMagic,0,Red);
if( res<0 )
{
Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
Print("Bid=" + DoubleToStr(bid,Digits) + " : SL=" + DoubleToStr(SL,Digits));
res=0;
}
else //now enter in the SL and TP via OrderModify to make compatible with ECN broker
{
if (OrderSelect(res, SELECT_BY_TICKET))
{
if (!OrderModify(res, Bid, SL, 0, 0, Red))
Print("Error modifying order");
}
}
ttime=Time[0];
return;
}
//---- buy conditions
if(buysig && ttime!=Time[0]) {
double ask = NormalizeDouble(Ask, Digits);
SL = NormalizeDouble(Ask - stopLoss * pips2dbl, Digits);
res=OrderSend(symbol,OP_BUY,lotSize,ask,slippage * pips2dbl,SL,0,"Pivot Buy",SpecificMagic,0,Blue);
if( res<0 )
{
Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
Print("Ask=" + DoubleToStr(ask,Digits) + " : SL=" + DoubleToStr(SL,Digits));
res=0;
}
else //now enter in the SL and TP via OrderModify to make compatible with ECN broker
{
if (OrderSelect(res, SELECT_BY_TICKET))
{
if (!OrderModify(res, Ask, SL, 0, 0, Blue))
Print("Error modifying order");
}
}
ttime=Time[0];
return;
}
}I have never come accross this before.
Cheers
BenoProbably your stop loss is too close. Try to multiply stop loss value by 10. It's frequent problem when you were testing system on 4 digit broker, and you are trading it on 5 digit broker. You could also test it on 5 digit broker, but never connect to the broker (in offline mode) and then metatrader will have old settings (from 4 dig).
First from this line:
NormalizeDouble(Ask - stopLoss * pips2dbl, Digits);
Print this value => stopLoss*pips2dbl
then you will know real value of the stop loss.
if it will be like 20 or 10 then it means that you need to multiply it by Point value
NormalizeDouble(Ask - (stopLoss * pips2dbl) *Point, Digits);
if it will be like 0.00009 then you need to mulitply it just by 10 because it should be 0.0009 (of course if you wish to set 9 pips stop loss)
Is there a way to code a latch/unlatch function in mq4. You would latch a bit true based on a condition and it would store this value until unlatched by another condition. cmfxtrader
I'm not sure if I understood you right. You wish to turn on/off specific part of ea (function) or you wish to deacvitate function from ea A by the action taken in ea B ? This way or another, both are possible.
Convert money value to price (for profit target calculation)
I would like to program a function that returns a price value for a given profit target of a basket of trades, but I'm having difficulties. The target I would like to reach, is a simple horizontal line to be drawn onscreen which indicates the profit target. However, the profit target is user definable variable in the form of a money value and not a price value. (e.g. target = € 100 instead of target = 1.2000)
The routine I have so far:
(My account is in EUR!)
1) Let's say that I open several buy positions for the USDJPY (at random positions for the sake of creating an example situation)
2) I calculate the basket's average open price (let's say 1.1500, again hypothetically) and display a horizontal line to indicate this
3) I have a variable with a profit target set: let's say target = € 100
4) I can successfully close all open positions when basketprofit >= target
The step inbetween is missing. I need a function: double targetPrice(){} which returns the targetPrice. Drawing the horizontal line is not the issue: calculating the targetPrice is!
The targetPrice = average open price + money-value-target (€100)
So what I basically would like to know: how do I convert a money-value to pips. This way I can add the pips to the average price and voila I have my targetPrice. Remember this also has to take into account the fact that I have a EUR account and I'm trading USDJPY. I guess there are also pitfalls there?
ECN SL not working
Gidday
I am trying to set up an EA that will work on an ECN, I understand the the SL and TP have to be placed / modified and I think the set up is correct, the order opens now but the SL is not placed extern double StopLoss = 100; any help would be great.
if(buysig && ttime!=Time[0]) {
ticket = OrderSend(Symbol(), OP_BUY, lotSize, MarketInfo(Symbol(), MODE_ASK), 2, 0, 0, "", 12345);
if(ticket > -1){
if (!OrderSelect(ticket, SELECT_BY_TICKET)) {
error_code = GetLastError();
Print("Error: " + ErrorDescription(error_code));
return(-1);
}
Print("order "+ticket+" successfully opened");
//now enter in the SL and TP via OrderModify to make compatible with ECN broker
SL = MarketInfo(Symbol(), MODE_ASK) - (StopLoss * pips2points);
Print("Ask = " + DoubleToStr(Ask,Digits) + " : SL = " + DoubleToStr(SL,Digits));
//round to nearest Tickvalue
SL = DoubleRound(SL, MarketInfo(Symbol(), MODE_TICKSIZE));
Print("SL rounded: " + SL);
if(!OrderModify(ticket, entry_price, SL, 0, Blue)) {
error_code = GetLastError();
Print("Error: " + ErrorDescription(error_code));
return(-1);
}
Print("Stoploss successfully set");
ttime=Time[0];
return(0);
}
}
}
//Tickvalue Rounding
double DoubleRound(double number, double step){
double mod = MathMod(number, step);
if(mod < step/2.0)
step = 0;
double rounded = number - mod + step;
return (rounded);
},,,,,,,,,,,,,.,,,,,,,,,