How to code order shift delay?

To add comments, please log in or register
kajironpu
238
kajironpu  

I want to make candle stick delay when place orders.

For example, buy condition matched and place order.


if (buy_condition==1){

Buy_order

}



How can I code (add) to delay order?

For example, if buy_condition is matched and after 5 candle stick later, I want to place order.

William Roeder
18880
William Roeder  
Compute your buy_condition on 5 candles earlier than you have now.
Abir Pathak
164
Abir Pathak  
kajironpu:

I want to make candle stick delay when place orders.

For example, buy condition matched and place order.



How can I code (add) to delay order?

For example, if buy_condition is matched and after 5 candle stick later, I want to place order.

First have a function which checks if new bar has appeared:

datetime lastBarOpenTime;

//+------------------------------------------------------------------+
bool IsNewBar()
//+------------------------------------------------------------------+
{
   datetime thisBarOpenTime = Time[0];
   if(thisBarOpenTime != lastBarOpenTime)
   {
   lastBarOpenTime = thisBarOpenTime;
      return (true);
   }
   else {
      return (false);
   }
}

Then in your original function keep adding a count variable. Once count reaches 5 place order and reset count to 0:

// These 2 should be globals:
bool isBuyOn = false;
uint count_delay = 0;

// Inside your function:
if (buy_condition==1){
   isBuyOn = true;
}

if(IsNewBar() && isBuyOn == true) {
   if(count_delay == 5) {
      // Reset
      count_delay = 0;
      isBuyOn = false;
   
      // Do Buy
      Buy_order
      return;
   }
   else
      count_delay++;
}

This will make your code a bit complicated.

If you are using say an indicator value for your buy signal, you can always use iCustom and it's "shift" argument as 5 to get a 5 bars delayed signal.

But I hope this helps.

kajironpu
238
kajironpu  

Hi, Abir

Thank you for your code sample.

Now I understand how to do this.

I don't use icustom, so I may count new bar after BUY signals is made and if count dalay=5, then open order.

Your code sample really helped me.

Thank you so much.

To add comments, please log in or register