Can not get any EA's to work right

 

Been trying to get an EA to work right for 2 weeks now. nothing i try works. if i get the close to work, it wont open, if i get the open to work, it wont close. every code bit that i try to get it to work on a new bar only either does not work or runs multiple times....



This code is no where near what I started with. Everytime it would not work, I removed something to make it simpler to try and find my error. If there is anyone who can throw me a bone and help me get this to work...Thank you

int start()
{
//---------------------------------------------------------------------------------------------------------------------------+



double CCI_ONE = iCCI(NULL,0,3,PRICE_CLOSE,1);// CCI one period ago
double CCI_TWO = iCCI(NULL,0,3,PRICE_CLOSE,2);// CCI two periods ago
int cnt, total;

total = OrdersTotal();


// Stop Loses----------------------------------------------------------------------------------------------------------------+
double LStopLose = iOpen(NULL, 0,0) - (iATR(NULL, 0, 5, 1) * 1.5);
double SStopLose = iOpen(NULL, 0,0) + (iATR(NULL, 0, 5, 1) * 1.5);
NormalizeDouble(LStopLose,4);
NormalizeDouble(SStopLose,4);

// Take Profits--------------------------------------------------------------------------------------------------------------+
double LTakeProfit = iOpen(NULL, 0,0) + (iATR(NULL, 0, 5, 1) * 2);
double STakeProfit = iOpen(NULL, 0,0) - (iATR( NULL, 0, 5, 1) * 2);
NormalizeDouble(LTakeProfit,4);
NormalizeDouble(STakeProfit,4);

//---------------------------------------------------------------------------------------------------------------------------+
if(TimeCurrent() == Time[0])
{
Alert("new bar");

for (cnt=total;cnt>0;cnt--)
{
OrderSelect(cnt,SELECT_BY_POS);
if(OrderType() <= OP_SELL==true || OrderType() <= OP_BUY==true)
{
OrderClose(OrderTicket(),OrderLots(),Ask,5,CLR_NONE); //Close Orders
cnt=0;
return(0);
}
return(0); //end of close orders
}

//---------------------------------------------------------------------------------------------------------------------------+

if ((CCI_ONE > CCI_TWO))
{
OrderSend(Symbol(),OP_SELL,1,Bid,5,SStopLose,STakeProfit,"Short Open 0-3",654321,0,White); //Open Short Order
return(0);//end of sell
}


//---------------------------------------------------------------------------------------------------------------------------+

if ((CCI_ONE < CCI_TWO))
{
OrderSend(Symbol(),OP_BUY,1,Ask,5,LStopLose,LTakeProfit,"Long Open 0-3",123456,0,White); //Open Long Order-------------+
return(0);//end of buy
}

return(0);
}
return(0);//end of EA
}

 

Problem #1:

if(TimeCurrent() == Time[0])
This will be true ONLY if the first tick of the new bar arrives while TimeCurrent() is at 0 seconds.

Improvement:

static int oldBarTime;

if(oldBarTime != Time[0]){

oldBarTime = Time[0];

... continue with your code

.

.

Problem #2

for (cnt=total;cnt>0;cnt--)
If there is one order you will not see it, it will be in position 0 in the pool of orders.

Improvement:

for(cnt = OrdersTotal()-1; cnt >= 0; cnt--)

.

.

Problem #3

if(OrderType() <= OP_SELL==true || OrderType() <= OP_BUY==true)
This syntax looks goofy.

Improvement:

if(OrderType() == OP_SELL || OrderType() == OP_BUY)

.

.

Problem #4

You can't close both buy and sell orders using Ask for the price.

OrderClose(OrderTicket(),OrderLots(),Ask,5,CLR_NONE); //Close Orders
This calls for a change in Problem #3 to seprately address closure of OP_BUY and OP_SELL orders.

.

.

Problem #5

NormalizeDouble(LStopLose,4);
Useage of NormalizeDouble. The normalized value is returned from the call.

Improvment:

lStopLose = NormalizeDouble(LStopLose,4);
repeat for other instances.

.

.

Problem #6

You are using return(0) haphazardly. return(0) inside start() causes your progam to immediately exit (until the next tick restarts the code)

You probably want to remove all of them inside start() except at the very end.

.

.

Problem #7

cnt=0;

Why are you forcibly resetting the loop counter?

.

.

Overall Logic:

If a new bar is detected, close all existing positions.

Open a new Buy position if CCI is rising.

Open a new Sell position if CCI is falling.

.

.

There may be more problems not yet visible.

.

Please use the SRC button here in this editor to format your code

 

Been trying to get an EA to work right for 2 weeks now. nothing i try works. if i get the close to work, it wont open, if i get the open to work, it wont close. every code bit that i try to get it to work on a new bar only either does not work or runs multiple times....

Well, that occurs because you haven't learned how to write code yet.

Write a little piece at a time, and TEST IT to see if it works, and see if it works as you think it should work, then write a little more and TEST IT AGAIN.

Don't try to write the whole program at one time.

Example, you are trying to detect a new bar, and are failing.

Write only that, and get it to work, before you move on. It will save a lot of frustration.

 

Thanks for the help, the alert bit was infinitely useful



It was the returns i think that were killing it and seperating the closing out section. Everything seems to be working except this bit:

Problem #1:

if(TimeCurrent() == Time[0])
This will be true ONLY if the first tick of the new bar arrives while TimeCurrent() is at 0 seconds.

Improvement:

static int oldBarTime;

if(oldBarTime != Time[0]){

oldBarTime = Time[0];

... continue with your code

i put in an alert with it so I know it is running it, but it is running the whole EA every few seconds, but it seems to be random, not lining up with ticks or any pattern that i can see.

 

The start() function of an EA is called on every tick of the chart to which it is attached

static int oldBarTime;

if(oldBarTime != Time[0]){

oldBarTime = Time[0];

... continue with your code here

}

Check the brackets {} in what you wrote.

 

so i've been messing around with the new bar detecter thing, found this...


when i change this:


static int oldBarTime;

if(oldBarTime != Time[0]){

Alert("new bar");

oldBarTime = Time[0];

... continue with your code


to

static int oldBarTime;


oldBarTime = Time[0]; //new bit


if(oldBarTime != Time[0]){

Alert("new bar");

oldBarTime = Time[0];

... continue with your code


the EA will still run. I know it is looking at it as it prints the new bar alert, but it seems to be completely ignoreing the oldBarTime rule.

 

I've got something that has some potential:


int vol;

vol = 1;

if (iVolume(NULL,0,0) == vol )

{

Alert("new bar");

vol = 0;


...continue


this should start the EA on the first tick of a new bar, then stop it due to changin of vol...

 

Ok, have fun experimenting. Have a nice day.

 
evilmonkey42 wrote >>

I've got something that has some potential:

int vol;

vol = 1;

if (iVolume(NULL,0,0) == vol )

{

Alert("new bar");

vol = 0;

...continue

this should start the EA on the first tick of a new bar, then stop it due to changin of vol...

phy already gave you the answer that virtually everyone uses when they only want to run logic at the formation of a new bar. Trust him, it works. If it isn't working for you, then you messed up somewhere.

datetime currTime;
datetime prevTime;

int start() {
   currTime=Time[0];
   if(currTime!=prevTime) {
      prevTime = currTime;
      //  ALL OF YOUR LOGIC GOES HERE!!!!!!!!!!!!
   }
   return(0);
}