Hi All,
Would want to ask for a favor please. Ive got this simple EA which should place an order when candle closes above/below given level within given time. It works most of the time but there are times when it doesnt. Cant figure out why. I use it for limit orders and thats when it sometimes fail.
Thanks a lot.
Here it is:
//+------------------------------------------------------------------+
//| cxz
//| cxz
//| cxz
//+------------------------------------------------------------------+
#property copyright "cxz"
#property link "www.cxz.pl/"
extern string startTime="10:00";
extern string endTime="14:00";
extern double above=0;
extern double below=0;
bool fired=false;
extern int orderType=OP_BUYSTOP;
extern double orderLots=0;
extern double orderPrice=0;
extern int orderSl=0;
extern int orderTp=0;
extern bool enableOrderSentAlert=true;
extern bool enableOrderSentSound=true;
extern string orderSentSound="alert.wav";
string comment="codename waliuku";
int magic=384746723;
int init()
{
orderLots=normalizeLots(orderLots);
start();
}
int start()
{
Comment("Start time: ",TimeToStr(StrToTime(startTime),TIME_DATE|TIME_MINUTES),"n",
"End time: ",TimeToStr(StrToTime(endTime),TIME_DATE|TIME_MINUTES),"n",
"Above: ",above,"n",
"Below: ",below,"n",
"Close: ",Close[1],"n",
"Start time condition: ",TimeCurrent()>=StrToTime(startTime),"n",
"End time condition: ",TimeCurrent()<=StrToTime(endTime),"n",
"Above condition: ",above>0 && Close[1]>above,"n",
"Below condition: ",below>0 && Close[1]<below,"n",
"");
if(TimeCurrent()>=StrToTime(startTime) && TimeCurrent()<=StrToTime(endTime))
{
bool fire=false;
if(above>0 && Close[1]>above)
{
fire=true;
}
if(below>0 && Close[1]<below)
{
fire=true;
}
if(fire)
{
if(!fired)
{
if(putOrder(orderType,orderPrice,orderLots)>0)
{
fired=true;
if(enableOrderSentAlert) Alert("Zlecenie zostało złozone");
if(enableOrderSentSound) PlaySound(orderSentSound);
}
}
}
}
if(fired) Comment("Fired");
}
int putOrder(int orderType, double orderPrice, double orderLots)
{
Print("Putting order...");
int minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL);
int result=-1;
if(orderType==OP_BUYSTOP || orderType==OP_BUYLIMIT) {
if(MathAbs(Ask-orderPrice)<=minDistance*Point) {
orderType=OP_BUY;
}
}
if(orderType==OP_SELLSTOP || orderType==OP_SELLLIMIT) {
if(MathAbs(Bid-orderPrice)<=minDistance*Point) {
orderType=OP_BUY;
}
}
if(orderType==OP_BUYLIMIT) {
result=OrderSend(Symbol(),OP_BUYLIMIT,orderLots,orderPrice,0,sltpValue(orderPrice-orderSl*Point,orderSl),sltpValue(orderPrice+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_BUYSTOP) {
result=OrderSend(Symbol(),OP_BUYSTOP,orderLots,orderPrice,0,sltpValue(orderPrice-orderSl*Point,orderSl),sltpValue(orderPrice+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELLLIMIT) {
result=OrderSend(Symbol(),OP_SELLLIMIT,orderLots,orderPrice,0,sltpValue(orderPrice+orderSl*Point,orderSl),sltpValue(orderPrice-orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELLSTOP) {
result=OrderSend(Symbol(),OP_SELLSTOP,orderLots,orderPrice,0,sltpValue(orderPrice+orderSl*Point,orderSl),sltpValue(orderPrice-orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_BUY) {
result=OrderSend(Symbol(),OP_BUY,orderLots,Ask,0,sltpValue(Ask-orderSl*Point,orderSl),sltpValue(Bid+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELL) {
result=OrderSend(Symbol(),OP_SELL,orderLots,Bid,0,sltpValue(Bid+orderSl*Point,orderSl),sltpValue(Ask-orderTp*Point,orderTp),comment,magic);
}
return(result);
}
double sltpValue(double value, int sltp) {
if(sltp>0) {
return(NormalizeDouble(value,Digits));
} else {
return(0);
}
}
double normalizeLots(double value) {
double minLots=MarketInfo(Symbol(),MODE_MINLOT);
double maxLots=MarketInfo(Symbol(),MODE_MAXLOT);
if(value<minLots) {
value=minLots;
}
if(value>maxLots) {
value=maxLots;
}
int digits=1;
if(minLots<0.1) {
digits=2;
}
return(NormalizeDouble(value,digits));
}
Would want to ask for a favor please. Ive got this simple EA which should place an order when candle closes above/below given level within given time. It works most of the time but there are times when it doesnt. Cant figure out why. I use it for limit orders and thats when it sometimes fail.
Thanks a lot.
Here it is:
//+------------------------------------------------------------------+
//| cxz
//| cxz
//| cxz
//+------------------------------------------------------------------+
#property copyright "cxz"
#property link "www.cxz.pl/"
extern string startTime="10:00";
extern string endTime="14:00";
extern double above=0;
extern double below=0;
bool fired=false;
extern int orderType=OP_BUYSTOP;
extern double orderLots=0;
extern double orderPrice=0;
extern int orderSl=0;
extern int orderTp=0;
extern bool enableOrderSentAlert=true;
extern bool enableOrderSentSound=true;
extern string orderSentSound="alert.wav";
string comment="codename waliuku";
int magic=384746723;
int init()
{
orderLots=normalizeLots(orderLots);
start();
}
int start()
{
Comment("Start time: ",TimeToStr(StrToTime(startTime),TIME_DATE|TIME_MINUTES),"n",
"End time: ",TimeToStr(StrToTime(endTime),TIME_DATE|TIME_MINUTES),"n",
"Above: ",above,"n",
"Below: ",below,"n",
"Close: ",Close[1],"n",
"Start time condition: ",TimeCurrent()>=StrToTime(startTime),"n",
"End time condition: ",TimeCurrent()<=StrToTime(endTime),"n",
"Above condition: ",above>0 && Close[1]>above,"n",
"Below condition: ",below>0 && Close[1]<below,"n",
"");
if(TimeCurrent()>=StrToTime(startTime) && TimeCurrent()<=StrToTime(endTime))
{
bool fire=false;
if(above>0 && Close[1]>above)
{
fire=true;
}
if(below>0 && Close[1]<below)
{
fire=true;
}
if(fire)
{
if(!fired)
{
if(putOrder(orderType,orderPrice,orderLots)>0)
{
fired=true;
if(enableOrderSentAlert) Alert("Zlecenie zostało złozone");
if(enableOrderSentSound) PlaySound(orderSentSound);
}
}
}
}
if(fired) Comment("Fired");
}
int putOrder(int orderType, double orderPrice, double orderLots)
{
Print("Putting order...");
int minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL);
int result=-1;
if(orderType==OP_BUYSTOP || orderType==OP_BUYLIMIT) {
if(MathAbs(Ask-orderPrice)<=minDistance*Point) {
orderType=OP_BUY;
}
}
if(orderType==OP_SELLSTOP || orderType==OP_SELLLIMIT) {
if(MathAbs(Bid-orderPrice)<=minDistance*Point) {
orderType=OP_BUY;
}
}
if(orderType==OP_BUYLIMIT) {
result=OrderSend(Symbol(),OP_BUYLIMIT,orderLots,orderPrice,0,sltpValue(orderPrice-orderSl*Point,orderSl),sltpValue(orderPrice+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_BUYSTOP) {
result=OrderSend(Symbol(),OP_BUYSTOP,orderLots,orderPrice,0,sltpValue(orderPrice-orderSl*Point,orderSl),sltpValue(orderPrice+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELLLIMIT) {
result=OrderSend(Symbol(),OP_SELLLIMIT,orderLots,orderPrice,0,sltpValue(orderPrice+orderSl*Point,orderSl),sltpValue(orderPrice-orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELLSTOP) {
result=OrderSend(Symbol(),OP_SELLSTOP,orderLots,orderPrice,0,sltpValue(orderPrice+orderSl*Point,orderSl),sltpValue(orderPrice-orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_BUY) {
result=OrderSend(Symbol(),OP_BUY,orderLots,Ask,0,sltpValue(Ask-orderSl*Point,orderSl),sltpValue(Bid+orderTp*Point,orderTp),comment,magic);
}
if(orderType==OP_SELL) {
result=OrderSend(Symbol(),OP_SELL,orderLots,Bid,0,sltpValue(Bid+orderSl*Point,orderSl),sltpValue(Ask-orderTp*Point,orderTp),comment,magic);
}
return(result);
}
double sltpValue(double value, int sltp) {
if(sltp>0) {
return(NormalizeDouble(value,Digits));
} else {
return(0);
}
}
double normalizeLots(double value) {
double minLots=MarketInfo(Symbol(),MODE_MINLOT);
double maxLots=MarketInfo(Symbol(),MODE_MAXLOT);
if(value<minLots) {
value=minLots;
}
if(value>maxLots) {
value=maxLots;
}
int digits=1;
if(minLots<0.1) {
digits=2;
}
return(NormalizeDouble(value,digits));
}