[Coding Issues] Storing previous' day High and Low for rangebreaking

 

Hi all,

 After a lot of reading, I think I'm ready to post my first message. Which contains a probably easy question but I can't seem to be able to solve it, and after several hours of searching I decided to shoot it out here :)

 Anyhow, I'm trying to make an EA that stores yesterdays range , goes long when the price breaks through YesterdayHigh, goes short when price goes through YesterdayLow. This on small timeframe with SL & TP. But I can't seem to determine the PreviousDay's info correct. I'd thought this would work.

//Related Input Parameters & Stuff
input int TakeProfitPips=10,StopLossPips=5;
input ENUM_TIMEFRAMES MYPeriod=PERIOD_M1;
double iLow(),iHigh();
static int Signal=0;
MqlDateTime timee
//+------------------------------------------------------------------+
//|  YesterdayHigh and YesterdayLow Function                         |
//+------------------------------------------------------------------+
      
double iLow(string strSymbol,ENUM_TIMEFRAMES tf,int nShift)
{
   double timeseries[1];
   if(CopyLow(strSymbol,tf,nShift,1,timeseries)==1) return(timeseries[0]);
   else return(-1.0);
}
double iHigh(string strSymbol,ENUM_TIMEFRAMES tf,int nShift)
{
   double timeseries[1];
   if(CopyLow(strSymbol,tf,nShift,1,timeseries)==1) return(timeseries[0]);
   else return(-1.0);
}
//+------------------------------------------------------------------+
//|   TradeSignal Function: 0 is neutral                             |
//|                         1 is buy,                                |
//|                         2 is close buy                           |
//|                        -1 is sell,                               |
//|                        -2 is close sell                          |
//+------------------------------------------------------------------+

int TradeSignal()
  { 
//Neutralize Signal   
   Signal=0;
   double CloseNew[1];

   double YesterdayLow=iLow(_Symbol,PERIOD_D1,1);
   double YesterdayHigh=iHigh(_Symbol,PERIOD_D1,1);

  CopyClose(_Symbol,MYPeriod,0,1,CloseNew);
      
//Is there any position open?
  bool BuyPositionOpen = false;
  bool SellPositionOpen = false; 
  if(PositionSelect(_Symbol)==true && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     BuyPositionOpen=true;    
  if(PositionSelect(_Symbol)==true && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
     SellPositionOpen=true;

//Determine Close points
  double TakeProfitBuy= YesterdayHigh + (TakeProfitPips/10000);
  double StopLossBuy = YesterdayHigh - (StopLossPips /10000);
  double TakeProfitSell= YesterdayLow - (TakeProfitPips/10000);
  double StopLossSell = YesterdayLow + (StopLossPips /10000);

if(BuyPositionOpen==false)
   {
   if(CloseNew[0] >= YesterdayHigh)
      Signal=1;
   } 

if(BuyPositionOpen==true)
   {
   if(CloseNew[0] >= TakeProfitBuy || CloseNew[0] <= StopLossBuy)
       Signal=2;
   }   
              
if(SellPositionOpen==false)
   {
   if(CloseNew[0] <= YesterdayLow)
      Signal=-1;
   } 

if(SellPositionOpen==true)
   {
   if(CloseNew[0] <= TakeProfitSell || CloseNew[0] >= StopLossSell)
        Signal=-2;
   }   
   
   return(Signal);                  
   }      

 But the problem I get this way, is that the orders only get executed at 00.00(hh:mm), but I would like them to open and given ticks on the 1M Chart. Here's my Newbar Function:

//+------------------------------------------------------------------+
//| IsNewBar Function                                                |
//+------------------------------------------------------------------+
  bool IsNewBar()
  {
   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

   int copied=CopyTime(_Symbol,MYPeriod,0,1,New_Time);
   
   if(copied>0)    
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         Old_Time=New_Time[0];            // saving bar time
         return(true);   // if it isn't a first call, the new bar has appeared
        }
     }
     return(false);
    }

 So, what's going wrong? And does anyone has an alternative solution? I'd be really thankful! Hope you folks be easy on me!

 
roadtoplo:

Hi all,

 After a lot of reading, I think I'm ready to post my first message. Which contains a probably easy question but I can't seem to be able to solve it, and after several hours of searching I decided to shoot it out here :)

 Anyhow, I'm trying to make an EA that stores yesterdays range , goes long when the price breaks through YesterdayHigh, goes short when price goes through YesterdayLow. This on small timeframe with SL & TP. But I can't seem to determine the PreviousDay's info correct. I'd thought this would work.

 But the problem I get this way, is that the orders only get executed at 00.00(hh:mm), but I would like them to open and given ticks on the 1M Chart. Here's my Newbar Function:

 So, what's going wrong? And does anyone has an alternative solution? I'd be really thankful! Hope you folks be easy on me!

Use Print() function to check your variables values.

This code is not necessary:

double iLow(),iHigh();

In place of this :

double TakeProfitBuy= YesterdayHigh + (TakeProfitPips/10000);

Use :

double Point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double TakeProfitBuy= YesterdayHigh + (TakeProfitPips * Point);

You have also to take into account Ask & Bid, Buy is open at Ask price and Sell is open at Bid price.

Please give us complete code, that compile, otherwise it's difficult to do any test. You can attach a file to your post.

 

Thanks for your quick reply, appreciate it!

Ha you're right guess I was a bit sleepy ^^

I added the rest of the code! Do you note some points you think are very inefficient? Or other things you don't like? Ps. I don't understand how to work with classes at the moment, you think that's crucial?

 Edit 12;36GMT+1: Updated version 

Files:
 
roadtoplo:

Thanks for your quick reply, appreciate it!

Ha you're right guess I was a bit sleepy ^^

I added the rest of the code! Do you note some points you think are very inefficient? Or other things you don't like? Ps. I don't understand how to work with classes at the moment, you think that's crucial?

 Edit 12;36GMT+1: Updated version 

It's not necessary to use class, it's an option.

If you want me to read your code, you have to attach a mql5 file and not a compiled one (ex5). I am not a robot ;-)

 
Woops sorry thanks for your patience
Files:
 
roadtoplo:
Woops sorry thanks for your patience

Ok, your EA open a trade on new bar and close it on next bar, then open on new bar then close, etc...

So it seems that it's you exit strategy who is bad. What are you trying to do ?

EDIT: Oh, I see an error finally :

double iHigh(string strSymbol,ENUM_TIMEFRAMES tf,int nShift)
{
   double timeseries[1];
   if(CopyLow(strSymbol,tf,nShift,1,timeseries)==1) return(timeseries[0]);
   else return(-1.0);
}

must use CopyHigh. I am not very awake either :-D

 

I'd like to open a Long Position as soon as the previous days high is broken. If the price breaks through the top I'd like to have a 10Pips TP and 5Pips SL as exit.

I'd like to open a Short Position as soon as the previous days low is broken. If the price breaks through the bottom I'd like to have a 10Pips TP and 5Pips SL as exit.

Spread still excluded by both of them btw, want to add it later as soon as it functions as I like it to.

 

Edit: Ah Cool thank you, lol  must be careful when being lazy copying things next time! You see other things you don't like? Since I just started programming recently and don't really know if I'm writing some stuff that could be written way better?

Edit2: There are still some things not working out well, like trades that stay open for 3 months. You can spot anything else?

Edit3:It's probably better to send the SL an TP with the initial order since that's compared per tick and if I let the EA look at the SL and TP per bar it's much more inaccurate since a new bar has to appear before the EA takes SL and TP, right?

 
roadtoplo:

I'd like to open a Long Position as soon as the previous days high is broken. If the price breaks through the top I'd like to have a 10Pips TP and 5Pips SL as exit.

I'd like to open a Short Position as soon as the previous days low is broken. If the price breaks through the bottom I'd like to have a 10Pips TP and 5Pips SL as exit.

Spread still excluded by both of them btw, want to add it later as soon as it functions as I like it to.

 

Edit: Ah Cool thank you, lol  must be careful when being lazy copying things next time! You see other things you don't like? Since I just started programming recently and don't really know if I'm writing some stuff that could be written way better?

Edit2: There are still some things not working out well, like trades that stay open for 3 months. You can spot anything else? 

For a beginner your code is very well, don't worry. Modify your code to take account of spread. And as already said use Print() to monitor your variables and debug your code.
Reason: