Finding the open price of a specific candle

Back to topics list To post a new topic, please log in or register
avatar
120
DeanDeV 2015.05.15 18:06
 

Hi guys,

 I need some help please. I am looking at finding an open price for a specific candle as reference for a trigger; if the current price is a certain amount from that candle. I cannot use Open[], obviously. I am also aware that if it passes that price and comes back passed it and then passes it again I do not want to send 2 orders, only an order for the initial passing of that mark (not sure what kind of filter to use?) , and it must only send one order after passing that price initially (also another filter I am uncertain about) :( Any ideas? 

Any help is appreciated. :)  

avatar
13004
WHRoeder 2015.05.15 21:02 #
 
DeanDeV: I cannot use Open[], obviously.
  1. The obvious is that to get a candle open you must use Open[]
  2. Remember the open price and compare it to market to open..
  3. If the market passes, remember not to open until you get a new open price to remember.
avatar
120
DeanDeV 2015.05.15 21:21 #
 
WHRoeder:
DeanDeV: I cannot use Open[], obviously.
  1. The obvious is that to get a candle open you must use Open[]
  2. Remember the open price and compare it to market to open..
  3. If the market passes, remember not to open until you get a new open price to remember.

I've gone a different way; this is what I have done (trying to use a previous order as price reference):

 

//global variable
double OpenPrice=OrderOpenPrice();

buyticket=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,PendingPrice,3,Stoploss,TakeProfit,"Buy Stop Order",POMagicNumber,0,clrBlue);
               if(buyticket>0)
                  {           
                  if(OrderSelect(buyticket,SELECT_BY_TICKET,MODE_TRADES)==true)
                  Print("Order Send success, Buy Stop Order placed" , OrderOpenPrice());
                     else
                     Print("Buy Stop Order Send failed, error # ",GetLastError());
                   }

if(Ask-OpenPrice>TriggerPips)
      {
      if((Hour()>=StartTime && Hour()<=FinishTime))  //Preferred trading hours
         {
         if (Bars!=ThisBarTrade) 
            {
            ThisBarTrade=Bars;  
            
            buystop2=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,PendingPrice,3,StopLoss,TakeProfit,"buy stop",MagicNumber,0,clrRed);
               if(buystop2<0) Print("buy stop failed," , GetLastError());
            }
         }
       }

But it's not working. :( Any suggestions? Or should I use a different method? 

avatar
120
DeanDeV 2015.05.15 21:49 #
 
WHRoeder:
DeanDeV: I cannot use Open[], obviously.
  1. The obvious is that to get a candle open you must use Open[]
  2. Remember the open price and compare it to market to open..
  3. If the market passes, remember not to open until you get a new open price to remember.

I have also done this, which seems to be placing, however, it places continuously, what filter can I add to stop this, such that it places once a day once passing that point..?

   if(Ask-iOpen(NULL,0,iBarShift(NULL,0,StrToTime("09:00")))>TriggerPips)
      {
      if(IsNewCandle())
      {
      if (Bars!=ThisBarTrade) 
         {
         ThisBarTrade=Bars;  
         
         buystop=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,OpenPrice,3,StopLoss,TakeProfit,"stop order",MagicNumber,0,clrRed);
            if(buystop<0) Print("buy stop order failed," , GetLastError());
         }
      }
      }

 

avatar
Moderator
3418
GumRai 2015.05.16 05:14 #
 

Be careful with using 

StrToTime("09:00")

 If the local time on your computer is different to the broker's time, this can possibly return the wrong date

 

When the trade is successfully placed, you can set a flag such as

 static bool trade_allowed = true;
 //
 // 
 if(trade_allowed )
   {        
   buystop=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,OpenPrice,3,StopLoss,TakeProfit,"stop order",MagicNumber,0,clrRed);
   if(buystop<0) Print("buy stop order failed," , GetLastError());
   else      trade_allowed  = false; 
   }   

 Of course, you may need to add additional code to reset the flag to true at a certain time and to check for opened trades at start-up

avatar
120
DeanDeV 2015.05.16 06:44 #
 
GumRai:

Be careful with using 

 If the local time on your computer is different to the broker's time, this can possibly return the wrong date

 

When the trade is successfully placed, you can set a flag such as

 Of course, you may need to add additional code to reset the flag to true at a certain time and to check for opened trades at start-up

Thank you for your reply. I used your code from this thread: https://forum.mql4.com/64571

ie.

bool TradePlacedToday()
    {
     datetime now = TimeCurrent();
     datetime bod = now-now%86400; 
     for (int k=OrdersTotal()-1;k>=0;k--)       
      {
         if (OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime opp=OrderOpenTime();
            datetime cur=opp-opp%86400;           
          if (opp<cur)return(0);
         }
           return(false);      
      }
      for (int i=OrdersHistoryTotal()-1;i>=0;i--)       
      {
         if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime oppt=OrderOpenTime();
            datetime curt=oppt-oppt%86400;           
          if (oppt<curt)return(0);
         }
           return(false);      
      }
      return(true);                             
    } 

 Like this:

   if(Ask-iOpen(NULL,0,iBarShift(NULL,0,StrToTime("09:00")))>TriggerPips)
      if(TradePlacedToday() == false)
      {
      if(IsNewCandle())
      {
      if (Bars!=ThisBarTrade) 
         {
         ThisBarTrade=Bars; 
         
         buystop=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,OpenPrice,3,StopLoss,TakeProfit,"stop order",MagicNumber,0,clrRed);
            if(buystop<0) Print("buy stop order failed," , GetLastError());
         }
      }
      }

 However, it is still placing continuously. Not sure what I have done wrong? 

avatar
Moderator
3418
GumRai 2015.05.16 07:25 #
 
DeanDeV:

Thank you for your reply. I used your code from this thread: https://forum.mql4.com/64571

ie.

 Like this:

 However, it is still placing continuously. Not sure what I have done wrong? 

That is not my code, I was making observations on the code attached by the OP. The code doesn't make sense as it checks whether the order was opened earlier than midnight of the day that it was opened.

This is probably how you want the function

bool TradePlacedToday()
  {
   datetime now = TimeCurrent();
   datetime midnight = now-now%86400;
   
   for(int k=OrdersTotal()-1;k>=0;k--)
     {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderOpenTime()>=midnight)
            return(true);
        }
     }
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderOpenTime()>=midnight)
            return(true);
        }
     }
   return(false);
  } 

 

not tested 

 

avatar
120
DeanDeV 2015.05.16 08:42 #
 
GumRai:

That is not my code, I was making observations on the code attached by the OP. The code doesn't make sense as it checks whether the order was opened earlier than midnight of the day that it was opened.

This is probably how you want the function

 

not tested 

 

Yeah man, it is still not working. :(

Will selecting an order and basing it off that work? ie.

if(buyticket>0)
 {      
 for(int p=OrdersTotal()-1; p>=0; p--)
      {
      if(OrderSelect(p,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==POMagicNumber)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(Ask-OrderOpenPrice()>TriggerPips)
                    if(OrderOpenPrice()>OrderStopLoss())
                     if(OrderTakeProfit()>0)
                     buystop=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,OpenPrice,3,StopLoss,TakeProfit,"stop order",MagicNumber,0,clrRed);
                        if(buystop<0) Print("buy stop failed." , GetLastError());
       }
}

 This doesn't work either. Meh. Any suggestions with this? It seems the easier method...?

avatar
Moderator
3418
GumRai 2015.05.16 10:47 #
 

I quickly wrote this EA

#property strict

input int StopLoss=20;
input int TakeProfit=20;

double PipDecimal;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits==3 || Digits==5)
     {
      PipDecimal=Point*10;
     }
   else
     {
      PipDecimal=Point;
     }
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  double entry,sl,tp;
  static int trade=OP_SELL;
  if(Hour()>=9)
    if(!TradePlacedToday() )
       {
       if(trade==OP_BUY)
         {
         entry=Ask;
         sl=Ask-StopLoss*PipDecimal;
         tp=Ask+TakeProfit*PipDecimal;
         }
       else
         {
         entry=Bid;
         sl=Bid+StopLoss*PipDecimal;
         tp=Bid-TakeProfit*PipDecimal;
         }
       int ticket=OrderSend(Symbol(),trade,0.1,entry,50,sl,tp,NULL,0,0,clrNONE);
       if(ticket>0)
          {
          if(trade==OP_BUY)
             trade=OP_SELL;
          else
             trade=OP_BUY;
          }
       else
          Print("Error placing trade - Error code=",GetLastError() );
       }
//---
   
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool TradePlacedToday()
  {
   datetime now = TimeCurrent();
   datetime midnight = now-now%86400;
   
   for(int k=OrdersTotal()-1;k>=0;k--)
     {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderOpenTime()>=midnight)
            return(true);
        }
     }
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderOpenTime()>=midnight)
            return(true);
        }
     }
   return(false);
  } 
//+------------------------------------------------------------------+

 and it only opens 1 trade per day when run in the strategy tester.

So I don't see why yours should be opening trades continuously. 

avatar
120
DeanDeV 2015.05.17 11:33 #
 
GumRai:

I quickly wrote this EA

 and it only opens 1 trade per day when run in the strategy tester.

So I don't see why yours should be opening trades continuously. 

Hey man, I thank you for putting your time into helping me, however, I do no think you understand my problem. When the EA places an order and when it gets to X points in profit I need it to place another order, however, it must only place this order once per day, because price can oscillate around the X profit mark and it can keep sending an order every time it passes X. 

So I thought the easiest route would be to select that initial order using OrderSelect() and then using OrderSend(); however my method seems incorrect. When using OrderSend() it must only send that one order once per day..? I hope this understanding is better?

 This is my idea:

if(buyticket>0 || buytickets>0)
   for(int k=OrdersTotal()-1;k>=0;k--)
     {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==POMagicNumber)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(Ask-OrderOpenPrice()>TriggerPips*Point)
                    if(OrderOpenPrice()>OrderStopLoss())
                     buystop=OrderSend(Symbol(),OP_BUYSTOP,Lotsize,OpenPrice,3,StopLoss,TakeProfit,"buy stop",MagicNumber,0,clrRed);
                        if(buystop<0) Print("buy stop failed." , GetLastError());

      }

 

Back to topics list  

To add comments, please log in or register