Jump to content

Welcome to the new Traders Laboratory! Please bear with us as we finish the migration over the next few days. If you find any issues, want to leave feedback, get in touch with us, or offer suggestions please post to the Support forum here.

  • Welcome Guests

    Welcome. You are currently viewing the forum as a guest which does not give you access to all the great features at Traders Laboratory such as interacting with members, access to all forums, downloading attachments, and eligibility to win free giveaways. Registration is fast, simple and absolutely free. Create a FREE Traders Laboratory account here.

TraderFoo

EL: Get High of a Bar from a Specific Time Yesterday

Recommended Posts

Hello, ... first post here. I just started using Tradestation and EL today. I'm working on a very simple strategy for the sake of understanding the basics.

 

I'm trying to figure out 2 things:

 

1. How can I get the high of a bar at a specific time

2. How can I run a piece of logic at a specific time?

 

For example:

 

At 10:30am, I'd like to get the high of the 3:30 bar yesterday to compare, and place a buy order based on the results.

 

 

Makes no sense, but just trying to understand how to work with bar times.

Share this post


Link to post
Share on other sites

i actually got this figured out over the weekend.

 

variables yesterdaysHigh(0);

// if time is 10am
if Time = 1530 then
 yesterdaysHigh = High;

if Time = 1000 then
 // do something with yesterdaysHigh

Share this post


Link to post
Share on other sites

This is indicator version -- I plotted it as a 'point' on a chart

 

//create variables

//store the 'bar count' in the variable using 'currentbar' keyword

 

vars: startbar(0), endbar(0), length(0);

 

if time=1000 then startbar=currentbar;

 

if time<=1100 then endbar=currentbar;

 

length=endbar-startbar;

 

if time>1000 and time<=1100 then begin

value1=highest(high, length);

 

plot1(value1,"HH");

end;

Share this post


Link to post
Share on other sites

Frank, thank you for your reply!

I've changed the code according to your advice. Here is what happened:

 

vars: startbar(0), endbar(0), length(0);

 

if time=1030 then startbar=currentbar;

if time<=1039 then endbar=currentbar;

 

length=endbar-startbar;

 

if time>1039 and time<=1100 then Buy next bar at highest(h,length) stop;

if marketposition=1 and time=1200 then sell next bar at market;

 

 

If I followed your words correctly, it doesn't work as intended. There are trades between 1040 and 1100 but they are executed not at the high of the price range between 1030 and 1039. (I couldn't even figure out how 'get in' price is calculated).

Could you, please, check my code again?

Share this post


Link to post
Share on other sites

hi Grandeur,

 

I can't be sure what it is you want to do but I can give you some idea how EL reads bars. study this and see if you can convert it to what you want. If not, give it an honest attempt and I will try again.

 

you have to think about the word 'length' here. length was moving target in your last code because endbar was equal to current bar. so I changed it to 'lock' the variable 'length' to a specific time. see if you can see the adjustment I made --- by using the variable 'value1' -- I am able to lock-in the highest price that was set during the specific time period you chose.

 

 

vars: startbar(0), endbar(0), length(0);

 

if time=1030 then startbar=currentbar;

if time<=1100 then endbar=currentbar;

 

length=endbar-startbar;

 

if time>1039 and time<=1100 then value1= highest(h,length);

 

if time>1039 and time<1200 then Buy next bar at value1 stop;

if marketposition=1 and time=1200 then sell next bar at market;

Share this post


Link to post
Share on other sites

Yes, Frank, you're absolutely right! I want to lock the variable 'length'. (At first, I tested with 'value1' but overlooked and came to the thought it is the same as without it).

The code you wrote works fine but how can I avoid repeated trades when I add stoplosses and they are executed?

Share this post


Link to post
Share on other sites

try this for n entries (3):

 

and entriestoday(date)<3

 

vars: startbar(0), endbar(0), length(0);

 

if time=1030 then startbar=currentbar;

if time<=1100 then endbar=currentbar;

 

length=endbar-startbar;

 

if time>1039 and time<=1100 then value1= highest(h,length);

 

if time>1039 and time<1200 and entriestoday(date)<3 then Buy next bar at value1 stop;

if marketposition=1 and time=1200 then sell next bar at market;

Share this post


Link to post
Share on other sites

That is it! The function 'entriestoday(date)' is what I was not aware of.

Now it works fine. Thank you, Frank!

Afterwards I tried to apply this function to my other daily strategies to test if I can optimize my stoplosses. I load 5-min chart and work with it as with daily bars using OpenD, CloseD, HighD, LowD. Also I need to calculate average range of daily bars but it seems it doesn't work OK.

I calculate average range of two daily bars as follows:

average(HighD(1)-LowD(1),2)

but the calculated average is incorrect. It equals only to the range of the last bar.

Could you recommend me anything, please?

Share this post


Link to post
Share on other sites

HighD(1) and LowD(1) will not give you different value's at each bar within an intraday chart as they are daily values stored

in an array.

if you try to average those data's as you are trying to do, you will average two times the same value as HighD(1) will give you the high of yesterday on this bar but also on the next bar untill there is a change of date.

 

what you could do is something like this

If D <> D[1] Then value1 = ((HighD(1)+HighD(2))/2) - ((LowD(1)+LowD(2))/2);

if you need more value's then only 2 days i would recommend using a loop but for 2 days

this should do the trick.

 

 

here is another way you could do it,;

as your calculation of daily high minus daily low represents the same as the function "Range" there is a way you could

use the calculation you have made yourself but in a slight different way.

open another subchart of the same symbol, this time change the timeframe to be daily.

you now have the same symbol ntraday and daily in the same chart.

Value1 = Average( Range of Data2, 2);

if you place this on your intradaychart it uses the data of the second chart which exists from daily bars and therefore should give the

exact same result as first example, only this time you are able to average by a N number without using any loop

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

excellent answer by dutchmen --- let me just elaborate in one way as this HighD(x) LowD(x) syntax is perfect example of how arrays work. An array is just a variable that holds multiple values --- its like a 'hidden spreadsheet' -- you can't see the values but they are there.

 

when you say HighD(1) -- the '1' is referring to a number that exists within a longer list of values --- as each value is assigned an index number, and referred to an 'array element'. HighD(0) refers to the first 'element' within that 'hidden' list of array values. Highd(1) looks into the array (the array is called HighD) and chooses element #2 (arrays start with 0 so (1) refers to the 2nd element within the array). etc...

Share this post


Link to post
Share on other sites

Thank you Frank and Fflyingdutchmen! I really appreciate it!

There is much that has become clearer for me. Now I'm going to ceck it all out for myself.

It's great that there are such people as you guys, who disinterestly devote their time to solving problems of others.

Good luck in your trading and not only in trading!

Share this post


Link to post
Share on other sites

when you get good at EL, share the wealth by helping someone else. I learned tons about various apps that way --- peer to peer help for no seeming reason. its good karma.

Share this post


Link to post
Share on other sites

I'm still working on testing Day-strategies on intraday data and encounted a problem. In order to buy at tomorrow's Open and get out on the following day's Open I wrote the following:

 

if entriestoday(date)<1 then buy next bar at OpenD(0) stop;

setstopposition;

setstoploss(1700);

if marketposition>0 and time=2359 then sell next bar at market;

 

The code looks OK for me but buy-trades are not executed at the open of tomorrow. Why is that?

Share this post


Link to post
Share on other sites

opend(0) is the current day open. EL processes bars at the close by default so you can't back up.

 

by definition, there won't be any trades at open of tomorrow so you can just say

 

buy next bar at market to buy the open price;

 

or say you want to buy above the open+ 1pt or -1pt:

 

buy next bar at open of tomorrow+1 stop;

buy next bar at open of tomorrow-1 limit;

Share this post


Link to post
Share on other sites

When I apply 'buy next bar at open of tomorrow+1 stop;' to daily chart, everything works OK.

But when I apply the same line to 5-min chart, the buying happens not at the open+1 of tomorrow but at the open+1 of the second 5-min bar tomorrow.

Obviously, 'buy next bar at market' won't work right here.

Share this post


Link to post
Share on other sites

"of tomorrow" will be ignored making your order behave like a normal "next bar"-order;

you will need to place an order on the first intraday bar on the next trading session which gives you an entry on the second bar of the bar (buy next bar open), or you check on the very last bar of the current

session if your conditions are still valid and if so place an order on the open of next bar that last bar(buy at open of next bar).

Share this post


Link to post
Share on other sites

An entry on the open of the second bar of the session is not what I'm looking for. I need the open price of the first bar.

When I tried to buy on the 'next bar open stop' on the very last bar of the previous session it also wouldn't work as I'd like to. It is traded not every day but every other day.

Guys, please, help me to code the following simple condition:

the timeframe is less than daily. I want to buy at the open of next session + 500 point. If marketposition>0 then setstoploss(1000). Exit on the open of next day. Every day there should be no more than one entry-trade and one exit-trade on stoploss.

 

The best I wrote myself was:

{2349 - the time of last bar of the session}

if entriestoday(date)<1 and time=2349 then buy next bar at open+500 stop;

setstopposition;

setstoploss(1000);

if marketposition>0 and time=2349 then sell next bar at market;

Share this post


Link to post
Share on other sites
An entry on the open of the second bar of the session is not what I'm looking for. I need the open price of the first bar.

When I tried to buy on the 'next bar open stop' on the very last bar of the previous session it also wouldn't work as I'd like to. It is traded not every day but every other day.

Guys, please, help me to code the following simple condition:

the timeframe is less than daily. I want to buy at the open of next session + 500 point. If marketposition>0 then setstoploss(1000). Exit on the open of next day. Every day there should be no more than one entry-trade and one exit-trade on stoploss.

 

The best I wrote myself was:

{2349 - the time of last bar of the session}

if entriestoday(date)<1 and time=2349 then buy next bar at open+500 stop;

setstopposition;

setstoploss(1000);

if marketposition>0 and time=2349 then sell next bar at market;

 

i allready gave you the solution, one must understand there are differents between

 

1)Buy Next Bar at Open + xxx Stop;

and

2)Buy at Open of Next Bar + xxx Stop;

 

1) will place an stop order at the next bar using the open of this bar, this one you could

use like this..

(on the new session)

If High < Open+xxx Then Buy Next Bar at Open+xxx Stop;

 

 

2) will place an stop order at the next bar using the open of next bar, this one you use

on the last bar of current session like this

 

(on the last bar of old session)

If Condition still Valid Then Buy at Open Of Next Bar+xxx Stop;

 

there is absolutely no difference between them beside the fact if you are using example #1

you will need to make sure that your entrypoint has been been reached yet

 

either way you will recieve the open of the first bar of the new session as starting point, the number of bars

do not matter as you dont trade time but price; as long as price did not reach your entry you still have the possebility

to place an order at that price

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

what you could do is following

Variables: EP(0);

If D <> D[1] Then EP = Open+(500*( MinMove/PriceScale ));

Condition1 = EntriesToday(Date) = 0 and ExitsToday(Date) = 0 {and any other condition you willing to use to make the purchase};

If Condition1 and High < EP Then Buy Next Bar EP Stop;

SetStopLoss(1000);

If Time = 2000 Then Sell;{here the time of the last bar of the session to exit at the open of the first bar of the next session}

 

note: if your stoploss will get triggered on the same day as the day of entry you could face another entry the next session,

if you do not want to make that trade on the next session then we could build a simple counter to avoid this, to me this litle socalled

system does not make any sence as it has zero sensetivity to changes in volatility. remember you will not recieve entries each second day as there

will be days that your entryprice will not get reached.

 

if your software does not support the function ExitsToday let me know.

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

Flyingdutchmen, thank you for your help!

Unfortunately, I faced some difficulties implementing your pieces of advice.

i allready gave you the solution, one must understand there are differents between

1)Buy Next Bar at Open + xxx Stop;

and

2)Buy at Open of Next Bar + xxx Stop;

About the second solution. When I just compile 'Buy at Open Of Next Bar+500 Stop;', an error comes up - 'syntax error, expecting 'stop', 'limit', 'contracts', or 'shares''(after the word 'open' as far as I see). I use MultiCharts. Then I verified the same line in TradeStation - it's the same. That's why I skipped on this solution before. I thought I missed something but you're still writing it. Does my software not understand the function your software does?

 

About an Average range of daily ranges.

what you could do is something like this

If D <> D[1] Then value1 = ((HighD(1)+HighD(2))/2) - ((LowD(1)+LowD(2))/2);

...

here is another way you could do it,;

 

Value1 = Average( Range of Data2, 2);

I tested those two variants but the calculations aren't equal. It seems, that in the second variant an average equals just the previous day's range. I don't know why.

 

The last code you posted (slightly edited)

Variables: EP(0);
If D <> D[1] Then EP = Open+500;
Condition1 = EntriesToday(Date) = 0;
If Condition1 and High < EP Then Buy Next Bar EP Stop;
SetStopLoss(1000);
If Time = 2359 Then Sell next bar at market;

works somehow strange. Sometimes it buys on the first bar that satisfy the condition, sometimes not. Here is the image (buy on the 28-th). vOgZ0-96963f39e7fa4413b8783dcae98d594b.gif

 

I found out that the code that works is

If time=1030 {first bar of the session} then value1=o;
If entriestoday(date)<1 then 
buy next bar at value1+500 stop;
SetStopLoss(1000);
If Time = 2359 {the last bar of the session} Then Sell next bar at market;

But it's a bit complicated.

Share this post


Link to post
Share on other sites
Flyingdutchmen, thank you for your help!

Unfortunately, I faced some difficulties implementing your pieces of advice.

 

About the second solution. When I just compile 'Buy at Open Of Next Bar+500 Stop;', an error comes up - 'syntax error, expecting 'stop', 'limit', 'contracts', or 'shares''(after the word 'open' as far as I see). I use MultiCharts. Then I verified the same line in TradeStation - it's the same. That's why I skipped on this solution before. I thought I missed something but you're still writing it. Does my software not understand the function your software does?

use the Buy Next Bar at xxx Stop; it is all you need as you have detemined your entry

at the beginning of the session and it will compile in each EL compatible programm

About an Average range of daily ranges.

 

I tested those two variants but the calculations aren't equal. It seems, that in the second variant an average equals just the previous day's range. I don't know why.

 

correct, i have forgot to place a displacement, simply write an " [1] "behind the word "range"

and it will refer to the data of 1 bar back, my mistake. i simply make up these things here without testing them.

Value1 = Average( Range[1] of Data2, 2);

 

The last code you posted (slightly edited)

Variables: EP(0);
If D <> D[1] Then EP = Open+500;
Condition1 = EntriesToday(Date) = 0;
If Condition1 and High < EP Then Buy Next Bar EP Stop;
SetStopLoss(1000);
If Time = 2359 Then Sell next bar at market;

works somehow strange. Sometimes it buys on the first bar that satisfy the condition, sometimes not. Here is the image (buy on the 28-th). vOgZ0-96963f39e7fa4413b8783dcae98d594b.gif

 

i do not see any differences in the scripts, where you are using 1030 as startingpoint i

have used the start of a new date, where you are asking if entriestoday are lower then 1

i am asking if entriestoday is equal to zero, it can never be below 0.

where you are determing the open only at start of a session day an later give an order to buy

at that open + 500 i determine the open at the start of a new session and add 500 to it;

the codes are equal.

it can only be an time-issue, you can verify this by printing the times.

If D<>D[1] then print( Time ); if this will give 1030 as outcome it is ok

 

 

I found out that the code that works is

If time=1030 {first bar of the session} then value1=o;
If entriestoday(date)<1 then 
buy next bar at value1+500 stop;
SetStopLoss(1000);
If Time = 2359 {the last bar of the session} Then Sell next bar at market;

But it's a bit complicated.

 

which part do you find complicated?

 

if you run your code like this on here, it will give you an entry each session.

this is not what you wanted to achieve in your initial post.you are not making sure there

has not been any exit at the same session

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

I am sorry for delays in my posts. I'm not always able to test what you write rightaway.

First of all, I'd like to thank you so much for 'Value1 = Average( Range[1] of Data2, 2);'! With loops you mentioned I'm not aquainted yet and once it was easier for me to write '((HighD(1)+HighD(2))/2) - ((LowD(1)+LowD(2))' for 30-time period:)

 

i do not see any differences in the scripts...
I don't see either. Moreover, I like your script more because it doesn't depend on the timeframe. In my script I have to change the beginning and the end of the session each time I change the timeframe (5-min, 15-min, etc.). That's what I called 'complicated'.

However, your script sometimes misses the first bars in the session and doesn't trade on them and mine does. I don't know why.

 

But I faced here another problem. Let the script be the same. Usually it works fine but some trades are weird to me. At first, they were absolutely weird but then became just weird :)

Sometimes buy trades happen not on next day's open+500 but happen on the next day's first bar but at the price of previous day's open+500 (on the previous day there was no trade because the condition was not fulfilled for that day). How can I handle this?

Share this post


Link to post
Share on other sites
I am sorry for delays in my posts. I'm not always able to test what you write rightaway.

First of all, I'd like to thank you so much for 'Value1 = Average( Range[1] of Data2, 2);'! With loops you mentioned I'm not aquainted yet and once it was easier for me to write '((HighD(1)+HighD(2))/2) - ((LowD(1)+LowD(2))' for 30-time period:)

 

I don't see either. Moreover, I like your script more because it doesn't depend on the timeframe. In my script I have to change the beginning and the end of the session each time I change the timeframe (5-min, 15-min, etc.). That's what I called 'complicated'.

However, your script sometimes misses the first bars in the session and doesn't trade on them and mine does. I don't know why.

 

But I faced here another problem. Let the script be the same. Usually it works fine but some trades are weird to me. At first, they were absolutely weird but then became just weird :)

Sometimes buy trades happen not on next day's open+500 but happen on the next day's first bar but at the price of previous day's open+500 (on the previous day there was no trade because the condition was not fulfilled for that day). How can I handle this?

 

there are various ways to make your script fully undependent of the barinterval in which

your script is running, by example you could use the reserved word Sess1StartTime

which would return 0930 wenn aplied to a stockchart tradet on the nyse.

 

you can check the first bar of the day on an intraday chart with:

if time = calctime(sess1startTime, barinterval)

or the last bar of the day

if time = calctime(sess1endTime, barinterval)

 

the reason you are getting this entry on the next session is because you did not have

any position on the prior day which makes it entriestoday(date)=0 then if your instrument

gaps up you still have that order lying in the market from the last bar of the former session,

this giving you the entry you do not wish.

 

you could avoid this by NOT placing an entry on the last bar of the session,

you may keep the order troughout the whole session but must remove it on the last bar,

change your script to

Variables: EP(0);
If D <> D[1] Then EP = Open+500;
If EntriesToday(Date) = 0 and and High < EP and Time < CalcTime(Sess1endTime,BarInterval) Then Buy Next Bar EP Stop;
SetStopLoss(1000);
If Time = Calctime(Sess1endTime,Barinterval) Then Sell next Bar at Market;

 

and give it another try

also try make a search in the help-files for the reserved words Sess1StartTime, Sess1EndTime, CalcTime it will clarify a bit more

Edited by flyingdutchmen

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Topics

  • Posts

    • ADMA Adma Biologics stock, watch for a range breakout, target 26 area at https://stockconsultant.com/?ADMA
    • URI United Rentals stock, nice rally off 829 support area, watch for top of range breakout at https://stockconsultant.com/?URI
    • Date: 27th November 2024. S&P500 at its 52nd new peak for 2024; USD Firmer, Kiwi & Yen Up. Asia & European Sessions: Wall Street rallied into the close with the S&P500 and Dow registering more record highs with the S&P500 climbing 0.57% to 6045, its 52nd new peak for 2024. The Dow rose 0.28% to 44,860.3 for its 46th record of the year. The NASDAQ advanced 0.63%. Trump named Jamieson Greer as the US Trade Representative and Kevin Hassett to direct the National Economic Council. Greer was intimately involved in Trump’s first-term trade policy decisions. President Biden announced Israel and Hezbollah have reached a cease fire. Over the next 60 days the Lebanese army and state security will take control of their own territory and Israel will gradually withdraw its forces. FOMC minutes: Minutes from the Fed’s latest policy meeting revealed officials leaning toward a cautious approach to future rate cuts. All agreed to cut the rate by -25 bps and nearly all thought risks between achieving employment and inflation goals were “roughly in balance.” Upside risks to the inflation outlook were little changed, and while inflation had eased, it remained elevated. The implied December rate continues to hover around a 50-50 bet as we await the PCE price data Wednesday and the crucial jobs report on December 6. The January 2025 rate is priced for a total of 20 bps in cuts, with -75 bps by January 2026. RBNZ cut its cash rate by 50 bps, yet the Kiwi gained as traders analyzed the central bank’s rate outlook and the governor’s remarks. Chinese government approved a 500 billion yuan ($69 billion) bond quota, enabling two state-owned asset managers to issue bonds for funding projects aimed at spurring economic growth. Today: US inflation and economic growth may provide clues to the Federal Reserve’s next policy move. Financial Markets Performance: The USDIndex has dropped to currently 106.459. The Yen climbed with USDJPY pulling back to 151.82, while NZDUSD jumped to 0.5900 despite the RBNZ’s 50 bps rate cut. Oil prices stabilized at $68.84, with optimism over delayed OPEC+ output increases balancing the reduced geopolitical risk stemming from the ceasefire. Gold rebounds to 2653.54, with next Resistance at 2660-2664. Always trade with strict risk management. Your capital is the single most important aspect of your trading business. Please note that times displayed based on local time zone and are from time of writing this report. Click HERE to access the full HFM Economic calendar. Want to learn to trade and analyse the markets? Join our webinars and get analysis and trading ideas combined with better understanding of how markets work. Click HERE to register for FREE! Click HERE to READ more Market news. Andria Pichidi HFMarkets Disclaimer: This material is provided as a general marketing communication for information purposes only and does not constitute an independent investment research. Nothing in this communication contains, or should be considered as containing, an investment advice or an investment recommendation or a solicitation for the purpose of buying or selling of any financial instrument. All information provided is gathered from reputable sources and any information containing an indication of past performance is not a guarantee or reliable indicator of future performance. Users acknowledge that any investment in FX and CFDs products is characterized by a certain degree of uncertainty and that any investment of this nature involves a high level of risk for which the users are solely responsible and liable. We assume no liability for any loss arising from any investment made based on the information provided in this communication. This communication must not be reproduced or further distributed without our prior written permission.
    • RBLX Roblox stock, pull back to 49.2 gap support area at https://stockconsultant.com/?RBLX
    • UHS Universal Health Services stock, nice rally off the 197 support area, from Stocks to Watch at https://stockconsultant.com/?UHS
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.