My EA doesn't Open positions

Hello Guys,
I developed my first EA named (CCI Stoch.)
I solved all problems concerning programming except the code of opening positions.
So, the EA is running on MT4 but with no positions opened and generating 2 errors:
1-unmatched data error (volume limit 2178 at 2016.01.18 22:00 exceeded)
2-CCI Stoch. EA AUDUSD,H1: OrderSend error 130

I want EA to open long position when cci crosses 0 upward and stochastic crosses 50 upward
and open short position when cci crosses 0 downward and stochastic crosses 50 downward
as indicated in the attached photo
This is the EA code :

//±-----------------------------------------------------------------+
//| test1.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//|
//±-----------------------------------------------------------------+
#property copyright “Copyright 2016, MetaQuotes Software Corp.”
#property link
#property version “1.00”
#property strict
//±-----------------------------------------------------------------+
//| Expert Variables |
//±-----------------------------------------------------------------+
extern double TP=100,SL=100,LotSize=0.1;
double Stoch=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRICE_CLOSE,0);
double cci=iCCI(Symbol(),0,14,PRICE_CLOSE,1);
int MagicNum=987876;
//±-----------------------------------------------------------------+
//| Expert initialization function |
//±-----------------------------------------------------------------+
int OnInit()
{
//—

//—
return(INIT_SUCCEEDED);
}
//±-----------------------------------------------------------------+
//| Expert deinitialization function |
//±-----------------------------------------------------------------+
void OnDeinit(const int reason)
{
//—

}

int orderstotal()
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
int x = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber())
{
(cnt++);
}
}
return(cnt);
}
//±-----------------------------------------------------------------+
//| Expert tick function |
//±-----------------------------------------------------------------+

void OnTick()
{
double stopless=Ask-SLPoint;
double takeprofit=Ask+TP
Point;

if(orderstotal()<1)
{
if(Stoch>50 && cci>0)
{
int y = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,stopless,takeprofit,NULL,MagicNum,0,clrGreen);

        }

}
if(Stoch<50 && cci<0)
{
int z = OrderSend(Symbol(),OP_SELL,LotSize,Ask,3,stopless,takeprofit,NULL,MagicNum,0,clrRed);

        }

}
//±-----------------------------------------------------------------+


hany,

You can ignore the unmatched data error. That is simply the tester telling you that your history data is crap.

The error 130 is far more serious. It means invalid stops which means your price calculations are wrong.

When it come to EA’s you need to know the basics and rule 1 is that you Buy at ASK and you Sell at BID.

So first off, your order to sell is incorrect as it is trying to sell at the Ask price. Change it to Bid.

Next look at your stoploss in the buy order. Remember that when you close a buy order, you are SELLING…and we sell at BID. There foreyour stoploss is calculated relative to Bid for a sell. You may get away with using the wrong price, especially if the spread is small but it is still incorrect. Same goes for the takeprofit.

BTW, the following lines :-

double Stoch=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRI CE_CLOSE,0);
double cci=iCCI(Symbol(),0,14,PRICE_CLOSE,1);

should fall within your On Tick function, otherwise they will never be updated.

Hope this helps.

Thnx for ur help kenny.
EA started to open postions.
But it opens (buy) order as long as stochastic>50 and cci >0
i need it to open only when they cross 50 and 0 respectively upward and the opposite for the (sell) order as indicated in the attachment.

hany,

This EA falls far short of that. In order to find a cross, you need to compare 2 stoch’s(or CCI). So set up something like this :-

double Stoch_0=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRI CE_CLOSE,0);
double Stoch_1=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRI CE_CLOSE,1);

Now you have a stoch for the last bar(1) and the current(0). So, if the stoch on the last bar was <50 then you are waiting for it now to be >50…that gives you the cross…eg

if(Stoch_1<50 && Stoch_0>50) we have a buy

Good. It works now and opening only 1 order.
But,it opens only (Buy) orders and doesn’t open (sell) orders even if sell conditions are met.
I attach for you the void OnTick() function


void OnTick()
{
double Stoch0=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRICE_CLOSE,0);
double Stoch1=iStochastic(Symbol(),0,14,3,3,MODE_SMA,1,PRICE_CLOSE,1);
double cci=iCCI(Symbol(),0,14,PRICE_CLOSE,0);
double stoploss_BUY=Bid-SLPoint;
double stoploss_SELL=Ask-SL
Point;
double takeprofit_BUY=Ask+TPPoint;
double takeprofit_SELL=Bid+TP
Point;

if(orderstotal()<1)
{
if(Stoch1<50 && Stoch0>50 && cci>0)
{
OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,stoploss_BUY,takeprofit_BUY,NULL,MagicNum,0,clrGreen);
}
}
if(Stoch1>50 && Stoch0<50 && cci<0)
{ OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,stoploss_SELL,takeprofit_SELL,NULL,MagicNum,0,clrRed);
}
}
//±-----------------------------------------------------------------+

Try this:
double stoploss_BUY=Bid-SLPoint;
double stoploss_SELL=Ask[B]+[/B]SL
Point;
double takeprofit_BUY=Ask+TPPoint;
double takeprofit_SELL=Bid[B]-[/B]TP
Point;

Thnx Holland. That was the missing part of the puzzle. :smiley:
Now I need to make alittle modification.
I don’t need TP or SL
I want to close order (eg. Buy) when stoch crosses down 50 and same for sell order
How can I do that ?
Thnx for help <3

hany,

I believe you have the tools now at your disposal to get this done. I would offer to help but I believe that you will find immense benefit in discovering for yourself. Once you can code, it will open up a whole new world for you.

In order to close, you would simply apply the principles that are discussed here ie look for the cross and instead of having it open an order, simply tell it to close the order…

Thnx for your support kenny.
Igot ur idea and yea, It’d be better for me to find it myself.
Thnx all for help <3