TS code using “date of next bar” command with two data sources

Hi Jock,

My only concern is that if I need to calculate a very long daily ATR value, for example ATR(200) then the array may take some time to run.
It should not take long. You can try it out.


Meantime I have actually come up with a way of being able to refer to data2 without using the "date of next bar" command …

If (marketposition = 0 and entrydate(1) < date) then buy at OpenD(0) + 0.3*AvgTrueRange(1) of data2 stop;

The only problem with this simple code is that it won’t take a trade on the very first bar of the day.
If (marketposition = 0 and entrydate(1) < date) then...

This is a combination of two boolean. Only when both are True then the combined condition is True. Can EntryDate(1) < Date be False?

I actaully don't understand what you want to do with:
If (marketposition = 0 and entrydate(1) < date) then buy at OpenD(0) + 0.3*AvgTrueRange(1) of data2 stop;

Edited to add:
When is this order issued?
 
Dear Jock,

You will need to take care of the initial values of DATR when the number of days is less than 20. Something like this:


Var: NumOfDaysGoneBy(0);
.
.
.

{Daily TrueRange}
DTR[19] = DTH - DTL;
NumOfDaysGoneBy = NumOfDaysGoneBy[1] + 1;
.
.
.

{Daily ATR}
if NumOfDaysGoneBy >= 20 then
DATR = Sum/20;
Else
DATR = Sum/NumOfDaysGoneBy;


Change to suit accordingly.
 
Quote from ChoSingKum:

I actaully don't understand what you want to do with:
If (marketposition = 0 and entrydate(1) < date) then buy at OpenD(0) + 0.3*AvgTrueRange(1) of data2 stop;

I wish to make no more than one long entry each day therefore…

marketposition = 0 is to ensure there is no existing open position on the current bar.

entrydate(1) < date is to ensure that the date of the next entry is not the same date as the previous entry.

Both conditions need to be true to ensure there is no more than one long entry each day.
 
Hi Jock,

Okay, I was confused by your earlier objective of issuing an order on the last 5-min bar of the day to be good for the first bar of the next day.

Since in this case, the order is not for that purpose, yes, the earliest it can issue that order is on the close of the first 5-min bar of the day. So no order will be done out of this for the first 5-min bar. The earliest is the second bar of the day.
 
Quote from ChoSingKum:

Use Array.

I think this may do the trick for you. You don't need Data2 for the daily data.

ChoSingKum, after a break I’m back on to this same problem and I can’t get your suggestion of the array to work. Remember that we have 5-minute data as Data1 and daily data as Data2 and that we wish to calculate the daily ATR using Data1. So using the following …

Code:
Var: Counter(0), Sum(0), DTH(0), DTL(0), DATR(0);
Array: DTR[2](0);
 
{Shift DTR backwards to make room for latest}
For Counter = 0 to 1 Begin
DTR[Counter] = DTR[Counter + 1];
End;
 
{Daily TrueHigh}
DTH = MaxList(CloseD(1),HighD(0));
 
{Daily TrueLow}
DTL = MinList(CloseD(1),LowD(0));
 
{Daily TrueRange}
DTR[2] = DTH - DTL;
 
{Initialise Sum to zero}
Sum = 0;

{Summ all the DTR}
For Counter = 0 to 2 Begin
Sum = Sum + DTR[Counter];
End;

{Daily ATR}
DATR = Sum/3;


This code doesn’t generate the Daily Average True Range for the last 3 days. It calculates the Daily True Range in DTR[2] but it averages this true range (DTR) for the last 3 (5-minute) bars instead of last 3 days.
 
Hi Jock,

You miss out this line I wrote:

"At the last 5-min bar of the day, do the following:

Use a loop to move the ... "


Note: I no longer come to this forum regularly.
 
Here's part of a system someone shared a long time ago which addresses your *1 long trade per day only* issue.

Code:
Inputs: StartTime(935), EndTime(1555);
Vars: MPos(0),TToday(0);

If Date>Date[1] Then TToday=0;

If MPos<>MarketPosition Then Begin
	MPos=MarketPosition;
	If MPos<>0 Then TToday=TToday+1;
End;	

If TToday=0 AND Time<EndTime AND Time>StartTime Then 
Begin
        Buy next bar at ......................your logic here......................;
End;



Quote from Jock:

I wish to make no more than one long entry each day therefore…

marketposition = 0 is to ensure there is no existing open position on the current bar.

entrydate(1) < date is to ensure that the date of the next entry is not the same date as the previous entry.

Both conditions need to be true to ensure there is no more than one long entry each day.
 
Back
Top