Add a delay ?

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
lowfrequencytrader
Posts: 5
Joined: 28 Jan 2015
Has thanked: 2 times

Add a delay ?

Postby lowfrequencytrader » 28 Jan 2015

Hello and thanks for any help.
I am looking to add a delay to change the type of order from a limit to a market order.

How would I add a 90 second delay (after an exit condition has been triggered) to condition2 like the pseudo code below.
Condtion1 would be the exit criterion.

if (condition1 and condition2) is true then sell X contracts at market.

thanks in advance,

Steve

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Add a delay ?

Postby JoshM » 28 Jan 2015

I am looking to add a delay to change the type of order from a limit to a market order.

How would I add a 90 second delay (after an exit condition has been triggered) to condition2 like the pseudo code below.
Condtion1 would be the exit criterion.
Do you mean with "after an exit condition has been triggered" that the position has been closed due to this triggering, or that the code intends to close the position? In practice, there won't be a big difference in time between both, but if you scale out there can be.

PosTradeExitDateTime can be used to retrieve the DateTime value of the previous position (so the time when the position closed, not the time the script intended to do so).

With PostTradeCount you can retrieve how many trades there were in the previous position. When `PosTradeCount` is combined with `PosTradeExitDateTime`, the DateTime value of the last trade from the last position can be retrieved.

But then 90 seconds (or 1,5 minutes) still needs to be converted to DateTime (instead of HHmm time). That can be done with ELTimeToDateTime_s.

When I understood you correctly, that would something similar to this:

Code: Select all

Variables:
dtExitTime(0),
dt90Seconds(ELTimeToDateTime_s(130));

dtExitTime = PosTradeExitDateTime(1, PosTradeCount(1) - 1)
+ dt90Seconds;

// Submit market order when the current time (ComputerDateTime)
// is less than the exit time of the last position + 90 seconds
if (ComputerDateTime <= dtExitTime) then
Sell ("XL MKT") 1 contract next bar at market

// If we've passed that 90 seconds time window, switch to
// submitting limit orders:
else
Sell ("XL LMT") 1 contract next bar at Close[1] limit;

lowfrequencytrader
Posts: 5
Joined: 28 Jan 2015
Has thanked: 2 times

Re: Add a delay ?

Postby lowfrequencytrader » 28 Jan 2015

Thanks JoshM for the detailed answer.

As I was thinking about this delay question I am beginning to think adding a "delay" at trade entry would also be helpful. As a simple example say I am looking to delay trade entry after a moving average cross over as in pseudo code below.

Condtion1 would be the entry criterion (moving average crossed over).
Condition2 would be to wait 90 seconds after condition1 triggered.

if (condition1 and condition2) is true then buy X contracts at market.

Thanks thanks-

lowfrequencytrader
Posts: 5
Joined: 28 Jan 2015
Has thanked: 2 times

Add a delay (part 2) ?

Postby lowfrequencytrader » 28 Jan 2015

Thanks again crew for any and all help

I am looking to add a "delay" after a trigger before trade entry . As a simple example say I am looking to delay trade entry after a moving average cross AND 90 seconds elapse.

Simple pseudo code below:
Condtion1 would be the entry criterion (moving average crossed over).
Condition2 would be to wait 90 seconds after condition1 triggered.

if (condition1 and condition2) is true then buy X contracts at market.

How could I create condition 2 ??

Thanks thanks again-

lowfrequencytrader
Posts: 5
Joined: 28 Jan 2015
Has thanked: 2 times

Add a delay II

Postby lowfrequencytrader » 28 Jan 2015

Hello and thanks for any help.
I am looking to add a delay to an entry after a trigger has been hit. Example might be if a cross over happens then wait 90 seconds and enter a trade. How could I add the 90 second delay ?

Pseudo code below:
Condtion1 would be the entry criterion (moving average crossed over).
Condition2 would be to wait 90 seconds after condition1 triggered.

if (condition1 and condition2) is true then buy X contracts at market.

Thanks thanks-

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Add a delay (part 2) ?

Postby JoshM » 29 Jan 2015

I am looking to add a "delay" after a trigger before trade entry . As a simple example say I am looking to delay trade entry after a moving average cross AND 90 seconds elapse.

Simple pseudo code below:
Condtion1 would be the entry criterion (moving average crossed over).
Condition2 would be to wait 90 seconds after condition1 triggered.

if (condition1 and condition2) is true then buy X contracts at market.

How could I create condition 2 ??
I think one way to do this would be by storing the time when the entry condition happened, add 90 seconds to it, and then submit the order as soon as the current time has elapsed this 'entry condition time + 90 seconds'.

For that, we can use `ComputerDateTime` and `ELTimeToDateTime_s()` (just like above) and then we'd end up with something like:

Code: Select all

Variables:
shortMA(0),
longMA(0),
maCross(False),
timeToEnter(0);

if (BarStatus(1) = 2) then begin

shortMA = XAverage(Close, 10);
longMA = XAverage(Close, 25);

maCross = (shortMA > longMA) and (shortMA[1] <= longMA[1]);

// Determine the time to enter when the
// MA cross has happened:
if (maCross) then
timeToEnter = ComputerDateTime + ELTimeToDateTime_s(130)
else
timeToEnter = 0; // reset variable

end;

// Open position
if (MarketPosition(0) = 0) then begin

// Go long after the pause
if (timeToEnter > 0) and (timeToEnter >= ComputerDateTime) then
Buy ("EL") 1 contract next bar at market;

end;

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Add a delay ?

Postby MAtricks » 29 Jan 2015

Josh looks to have nailed it. To give another example, I've done it like this in the past:

Code: Select all

Inputs:
TimeDelayS( 90 ) ;

Variables:
BuyFlag( false ),
SellFlag( false ),
BeginTime( 0 ),
TimeNow( 0 ) ;


//Time Delay filter
TimeNow=DateTime2ELTime_s( DateTime ) ;

//LONG
if {Your Stategy's Entry criteria here} then begin
BuyFlag=True ;
BeginTime=TimeNow ;
end ;

//SHORT
if {Your Stategy's Entry criteria here} then begin
SellFlag=True ;
BeginTime=TimeNow ;
end ;

If BuyFlag and ( TimeNow-BeginTime )>TimeDelayS then buy this bar ;
If SellFlag and ( TimeNow-BeginTime )>TimeDelayS then sellshort this bar ;

if MP>0 then begin
BuyFlag=False ;
BeginTime=0 ;
end ;
if MP<0 then begin
SellFlag=False ;
BeginTime=0 ;
end ;

lowfrequencytrader
Posts: 5
Joined: 28 Jan 2015
Has thanked: 2 times

Re: Add a delay ?

Postby lowfrequencytrader » 01 Feb 2015

Big THANKS JoshM and MAtricks- works like a charm !!!

You guys are a great resource.
Jeff


Return to “User Contributed Studies and Indicator Library”