Please help with code

 

Can someone please help. My EA isn't working correctly and I can't seem to find the error. It is suppose to trade the opening hour of a new markets, if a breakout (new high low for the day) occurs. It is trading only long and trades without a breakout. I suspect the MaxofDay is getting set to ask reguardless of "if" conditions. It is also trading at Hours = 1, 2, 7, 18 in stead of 2,3,8,19. I know i can just offset the code by 1. But, I would like to understand why. A Final request. What are some of the ways of troube shooting the code on a back test. For example, is there a way to step through a backtest looking the the varible's values? TIA ---Tom

// At Start of day reset Max and Min Price
if(Hour() == 0 && Seconds() < 5)
{
double MaxofDay = Ask;
double MinofDay = Bid;
}

// Update Max and Min throughout day
if(MaxofDay < Ask) MaxofDay = Ask;
if(MinofDay > Bid) MinofDay = Bid;


// One Trade per bar
if (TimePrev==Time[0]) return(0); // return (0) stops program here. Time[0] is the opening time of current bar.
TimePrev=Time[0];


// European session (2:00am EST), London session (3am EST), New York session (8am EST), Asian session (7:00pm EST)
if(Hour() == 2 || Hour() == 3 || Hour() == 8 || Hour() == 19)
{

// LONG on Breakout to upside
if (Ask == MaxofDay)
{
closeshrts();
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stoplong(Ask,StopLoss),Takelong(Ask,TakeProfit),NULL,0,0,Blue);
}

// SHORT on Breakout to downside
if(Bid == MinofDay)
{
closelongs();
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Stopshrt(Bid,StopLoss),Takeshrt(Bid,TakeProfit),NULL,0,0,Red);
}
}

 

Tom - your BEST bet is to liberally sprinkle your code with Print(...) statements.

Why?

1. is simple to do this

2. you can solve your issues fast

3. you will learn by doing... especially when the penny finally drops and you do the Ah Haaaa exclamation ;-)

Oh yes,

so many forget that others might actually read the code!

which of below do you think readers would find more EZ on the eye [AND brain]?

btw, not picking on you directly... is a common habit of many = I'm having [yet again] another stab at waking people up ;)

1.

// LONG on Breakout to upside
if (Ask == MaxofDay)
{
closeshrts();
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stoplong(Ask,StopLoss),Takelong(Ask,TakeProfit),NULL,0,0,Blue);
}

// SHORT on Breakout to downside
if(Bid == MinofDay)
{
closelongs();
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Stopshrt(Bid,StopLoss),Takeshrt(Bid,TakeProfit),NULL,0,0,Red);
}
}

2.

  // LONG on Breakout to upside
  if (Ask == MaxofDay)
  {
    closeshrts();
    OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Stoplong(Ask,StopLoss),Takelong(Ask,TakeProfit),NULL,0,0,Blue);
  }

  // SHORT on Breakout to downside
  if(Bid == MinofDay)
  {
    closelongs();
    OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Stopshrt(Bid,StopLoss),Takeshrt(Bid,TakeProfit),NULL,0,0,Red);
  }
}
 

I am new at this so please bare with me.

A. From placing Print Statements, I have discovered that during the backtest the MaxofDay and the MinofDay are getting reset to "0" with each run. I stop this issue by making them global varables. I have other problems now. I will post if i can't fix myself.

B. If you will explain how to enter code in color, I will do so in the future.

Thanks again for your help,

Tom

Reason: