Buy/Sell after first hour of trading

 

Hi All,


Im rather new here and I want to create a simple system where I look at the high and low of the first 1 hour candle at a start time (in this case 9.00). 

If after this first 1 hour the rate goes above the high, than sell. If it goes below the low than sell.

What I did so far is the following:

extern double        StoplossPrice;
extern double        SLPips            = 100;;
extern double        TakeProfitPrice;
extern double        TPPips            =  300;
extern double        StartHour         =  9;
extern double        StartMinute       =  0;
extern double        StartSecond       =  0;
extern double        FinishHour        =  23;
extern double        FinishMinute      =  59;
extern double        FinishSecond      =  0;

extern double        Pips;
extern double        Lots              = 0.1;
extern int           Slippage          = 3;

extern bool          UseRR             = true;
extern double        RiskPercent       = 2;
extern double        RewardPercent     = 6;

extern int           Ticketnumber;
extern int           TicketnumberB;
extern int           TicketnumberS;
extern int           TicketnumberCB;
extern int           TicketnumberCS;

extern int           FirstAsk;
extern int           FirstBid;
extern double        PrevHigh;
extern double        PrevLow;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--FIRST ADJUST FOR A 4 OR 5 DIGIT BROKER-------------------
   double ticketsize = MarketInfo(Symbol(),MODE_TICKSIZE);           
      if(ticketsize == 0.00001|| Point == 0.0001)
      Pips = ticketsize*10;
      else Pips=ticketsize; 
    return(INIT_SUCCEEDED);
   
   //--DONT CONTINUE IF NOT ENOUGH MARGIN--------------
 
    
   }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
   //--MONEY MANAEMGEMENT ENTRY---------------------------
   double Equity=AccountEquity();
   double RiskedAmount = Equity*RiskPercent*0.01;
   double RewardAmount= Equity *RewardPercent*0.01;                           // Calculate your max loss & reward per trade
   
   double SL=Bid - (RiskedAmount / (Lots*10)*Pips);                           //ıf one pip is 10 USD... otherwise change the code
   double TP=Bid + (RewardAmount / (Lots*10)*Pips);
   
   if(UseRR)StoplossPrice=SL;
   else StoplossPrice=Bid-SLPips*Pips;                                                
   
   if(UseRR)TakeProfitPrice=TP;
   else TakeProfitPrice= Bid+TPPips*Pips;
   
   //--CHECK FOR OTHER ISSUES (INCL SIGNALS------------------------
   if(AccountFreeMarginCheck(Symbol(),OP_BUY,Lots)>=0)                            // Check if you have enough money in your account
      if(Hour()== StartHour && Minute()==StartMinute && Seconds()==StartSecond)   // Only act if the hour, minute and second on the chart is equal to set parameter
         if(OrdersTotal()==0)                                                      // Only send an order if there is none outstanding
            TradeSignal();
       if(Hour()== FinishHour && Minute()==FinishMinute && Seconds()==FinishSecond)
            CloseAllTrades();
}

void TradeSignal()

{
      Ask == FirstAsk;
      Bid == FirstBid;
      High[1] == PrevHigh;
      Low[1] == PrevLow;
      
   if(FirstAsk >PrevHigh)
         TicketnumberB = OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, StoplossPrice, TakeProfitPrice,NULL);
   if(FirstBid <PrevLow)
         TicketnumberS = OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, StoplossPrice, TakeProfitPrice,NULL);
}

void CloseAllTrades()
{
if(OrdersTotal()==1)
      if (OrderType() == OP_BUY) TicketnumberCB = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, NULL );
      if (OrderType() == OP_SELL) TicketnumberCS = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, NULL );
    
}



Also Im trying to find a code that closes my trade at the end of each day (at 23.59) no matter what. It seems simple but I tried many things but couldnt find anything.

Thanks for the help!


JB

 
 
Mohamad Zulhairi Baba:

Hour(), Minutes(), & Seconds() are integer based.
You should use datetime based, against TimeCurrent().


Thanks for the reply,

How would something like that work then?

I mean my logic was that I can write: if at time of starthour the price is above the close of (starthour-1) then OrderSend.

How would the datetime based variables fit in here?


Thanks!

 
Jerommeke007:

Thanks for the reply,

How would something like that work then?

I mean my logic was that I can write: if at time of starthour the price is above the close of (starthour-1) then OrderSend.

How would the datetime based variables fit in here?


Thanks!


I changed it into the following:

It gives me the high of te candle at 9.00

What do you think?

      if(Ask > iHigh(NULL,0,iBarShift(NULL,0,(TimeCurrent()-(TimeCurrent()-(60*60*(Hour()-9)))))))
      TicketnumberB = OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, BStoplossPrice, BTakeProfitPrice,NULL);
      if(Bid < iLow(NULL,0,iBarShift(NULL,0,(TimeCurrent()-(TimeCurrent()-(60*60*(Hour()-9)))))))
      TicketnumberS = OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, SStoplossPrice, STakeProfitPrice,NULL);
 
Jerommeke007:

I changed it into the following:

It gives me the high of te candle at 9.00

What do you think?

Please read on documentation for MqlDateTime data structure, and TimeToStruct function.

You need to convert current time into MqlDateTime structure, then compare it to TimeCurrent().


Something like this

datetime HourMin(int pHour = 0, int pMinute = 0)
{
MqlDateTime timeStruct;

TimeToStruct(TimeCurrent(),timeStruct);
timeStruct.hour = pHour;
timeStruct.min = pMinute;

datetime useTime = StructToTime(timeStruct);
return useTime;
}

then call it on void OnTick()

void OnTick()
{
   if(TimeCurrent()==HourMin(9,0))
   {
      // your order function
   }

}

just pseudocode, but you should figure it out.

 
Mohamad Zulhairi Baba: You need to convert current time into MqlDateTime structure, then compare it to TimeCurrent().
Don't even need to be that complicated, just compare the time and see if it is past 9AM
if( TimeCurrent() >= StringToTime("09:00") ) ...
// or
#define HR0900 32400 // 9*3600
if( TimeCurrent() >= date()+HR0900 ) ...
 

Hi 

Anyone can help me to code

Statement : previous candle's close is higher than candle's high at 02.00 server time

iClose(NULL,0,1) > iHigh(?????)

Thank you

 
dompetku:

Do not double post

I have deleted your other post