Catch Close at certain time if IOG true

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Catch Close at certain time if IOG true

Postby raven » 30 Dec 2010

How to catch and hold a price at a certain time if IOG is true? The following code holds Exit_Price = 0 if no tick closes exactly at 200000. If a tick occurs – by accident – at 200000 Exit_Price holds the close of that tick until a new tick occurs. Exit_Price is then reset to 0.

Code: Select all

[IntrabarOrderGeneration = True];

var: Exit_Price(0) ;

if currenttime_s = 200000
then Exit_Price = Close ;
But what I want is: Catch the closing price of 200000 (exactly 8 p.m.) and hold it in the variable Exit_Price for further calculation. How to manage this? (IOG has to be on as MC otherwise brings up the message "Open next bar strategies is not permitted on multiple data streams" and I need multiple datastreams and open next bar code).
Any help is appreciated very much.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Catch Close at certain time if IOG true

Postby TJ » 02 Jan 2011

How to catch and hold a price at a certain time if IOG is true? The following code holds Exit_Price = 0 if no tick closes exactly at 200000. If a tick occurs – by accident – at 200000 Exit_Price holds the close of that tick until a new tick occurs. Exit_Price is then reset to 0.
not sure what you mean by: "until a new tick occurs"


try this:

Code: Select all

var:
time.check(0),
Exit_Price(0) ;

if date > date[1] then time.check = 0;

While time.check = 0
Begin
Exit_Price = Close;

if currenttime_s > 195959 then time.check = 1;
End;

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 03 Jan 2011

Thanks for your reply but unfortunately your suggested code led to crashes of MC (maybe forced by a loop within the code) - saw this never before.

Nevertheless this code would keep my problem of changing the value of Exit_Price on every new tick. This is what I meant with "until a new tick occurs". Up to now I know how to let Exit_Price get and hold the Close but only as long as no new tick (tick not bar!) shows up. This is how IOG works as I experience it.

So my question still is: How to catch the Close at a certain time AND hold it unchanged in a variable even if IOG is true and tick after tick (after that certain point in time) comes in?

Many thanks in advance.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Catch Close at certain time if IOG true

Postby TJ » 03 Jan 2011

Thanks for your reply but ...

... This is what I meant with "until a new tick occurs"...

Many thanks in advance.
still don't understand what you want.

you need to make some illustrations,
maybe with a series of charts to show time/tick changes,
maybe with a Time and Sales table...

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 04 Jan 2011

Thank you for your effort to understand. Hope the following makes it clear:

Want to exit at the last traded price that is noticed at exactly 200000 (8 p.m.). This price is the closing price of a tick (IOG = true) that closed at or some time before 200000: If there is no tick closing at 200000 the wanted closing price is that of the last tick (=trade) before 200000.

Example of last traded prices (=closing prices of ticks) at certain times:

195935: 100.10
195942: 100.11
195958: 100.09
200005: 100.08

What I want to store is the price 100.09. This is that one that is noticed as the last traded price at exactly 200000 (is does not matter that it is traded a few seconds before – what matters is that it is the last traded price at 200000). This price should be available for further calculations from 200000 on so I could use 100.09 at any time later than 195959. It must not be overwritten by closing prices that occur later than 200000. But this is what I experience with IOG = true. From 200000 to 200004 (in the given example) 100.09 is stored in the variable Exit_Price. But at 200005 it is overwritten by 100.08 instead of keeping 100.09. And a later tick will overwrite 100.08 and so on.

The code I use:

Code: Select all

[IntrabarOrderGeneration = True];

vars: time.check(0) ,
Exit_Price(0) ;

if time.check = 0 then Exit_Price = Close ;

if currenttime_s >= 200000 then time.check = 1 ;
With IOG = true at every new tick time.check seems to be reset to initial 0 and therefore a new close is stored even after 200000. The second if-condition seems not to matter because it shows no results in meanings of avoiding overwriting of 100.09.

How to store 100.09?

Many thanks again.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Catch Close at certain time if IOG true

Postby TJ » 04 Jan 2011

Thank you for your effort to understand. Hope the following makes it clear:
...

... (is does not matter that it is traded a few seconds before – what matters is that it is the last traded price at 200000)....

Many thanks again.
WOW WOW WOW... hold the water... that's a whole new different explanation....

you cannot have "exactly 8 p.m.", and does not matter that it is traded a few seconds before.
they are not the same thing.

I am glad you have the time & sales example to illustrate your requirement.
Anyway my code would have caught the Exit_price you needed,
but NOT your variation in the previous post.
The code is subtle, but the result is very different.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Catch Close at certain time if IOG true

Postby TJ » 04 Jan 2011

Thanks for your reply but unfortunately your suggested code led to crashes of MC (maybe forced by a loop within the code) - saw this never before.

Nevertheless this code would keep my problem of changing the value of Exit_Price on every new tick. This is what I meant with "until a new tick occurs". Up to now I know how to let Exit_Price get and hold the Close but only as long as no new tick (tick not bar!) shows up. This is how IOG works as I experience it.

So my question still is: How to catch the Close at a certain time AND hold it unchanged in a variable even if IOG is true and tick after tick (after that certain point in time) comes in?

Many thanks in advance.
I think there is a bug in the WHILE keyword.


try this variation:

Code: Select all

var:
time.check(0),
Exit_Price(0) ;

if date > date[1] then time.check = 0;

if time.check = 0
Begin
Exit_Price = Close;

if currenttime_s > 195959 then time.check = 1;
End;

ps: pls use the code tag to wrap your codes.

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 10 Jan 2011

TJ, thank you again.

Unfortunately - after serious examiniation - your last suggestion also did not work. The reason for this is the same I mentioned before: with IOG = true all vars are reset to their initial value tick by tick. So you may assign the latest close price to the variable Exit_Price but with the next occurring tick Exit_Price is first reset to 0 and - depending on the code - every new tick assigns its close price to Exit_Price. You could check it yourself and tell me if I am wrong. Thank you.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 10 Jan 2011

Raven,

I did not even check it but may you try this :

var: hold_close(0);

if date<>date[1] and time_s<200000 then begin
hold_close=0;
end;

if time_s[1]<200000 and time_s>=200000 then hold_close=close[1];

Hope this helps. I don't like the while function.

Regards.

Mike

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 12 Jan 2011

piranhaxp, thank you.

I checked your suggestion but unfortunately hold_close(0) stays 0 and never changes (logfile). Even if it would be change to a value different from 0 it would be reset to initial 0 by the next tick as IOG = true.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Catch Close at certain time if IOG true

Postby TJ » 12 Jan 2011

TJ, thank you again.

Unfortunately - after serious examiniation - your last suggestion also did not work. The reason for this is the same I mentioned before: with IOG = true all vars are reset to their initial value tick by tick. So you may assign the latest close price to the variable Exit_Price but with the next occurring tick Exit_Price is first reset to 0 and - depending on the code - every new tick assigns its close price to Exit_Price. You could check it yourself and tell me if I am wrong. Thank you.
are you using this on a daily chart !?

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 12 Jan 2011

Raven ...

this is crazy. If I implement the code to a plot statement and then to a chart, then I get the close at 200000. May there are more issues in your code. Would be better you would share it. May TJ is with me. It's a better way to understand where the issue is. MAy you use it on the daily chart, or you're using wrong exchange times. Honestly, without an overall overview it will be difficult to share. May your definition from the logfile is wrong etc etc etc.

Regards Mike
Attachments
hold_close.png
(48.15 KiB) Downloaded 1963 times

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 13 Jan 2011

Code: Select all

//data1 and data2 in 24H format; both charts use same local time from 08:00 to 22:00

[IntrabarOrderGeneration = True];

Inputs: Exit_Time_s(200000) ;

var: hold_close(0) ;

//piranhaxp' suggestion
if date <> date[1] and time_s < Exit_Time_s then begin
hold_close = 0 ;
end ;

if time_s[1] < Exit_Time_s and time_s >= Exit_Time_s then hold_close = close[1];

//Entry Long
if date = currentdate
and OpenD(0) data2 < HighD(1) data2
then buy 1 contracts next bar OpenD(0) limit ;

//Exit at Exit_Time_s Close LMT
if marketposition > 0
and currenttime_s >= Exit_Time_s
then begin
sell all contracts next bar hold_close points limit ;
buytocover all contracts next bar hold_close points limit ;
end ;


print(File("C:\TEST1.txt"),hold_close," ",currenttime_s);

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 13 Jan 2011

Raven ...

here we go :

Code: Select all

//data1 and data2 in 24H format; both charts use same local time from 08:00 to 22:00

[IntrabarOrderGeneration = True];

Inputs: Exit_Time_s(200000) ;

var: hold_close(0) ;
var: todayOpen(0);
var: prevHigh(0);
var: actlimit(0);
var: todayPos(0);

prevHigh=HighD(1) data2;
todayOpen=OpenD(0);
actlimit = hold_close;

//piranhaxp' suggestion
if date <> date[1] and time_s < Exit_Time_s then begin
hold_close = 0 ;
todayPos = 0;
end ;

//Entry Long
if marketposition=0 and todayPos < 1 then begin
if todayOpen < prevHigh then begin
buy 1 contracts next bar todayOpen limit ;
end;
end;

//Exit at Exit_Time_s Close LMT
if marketposition <> 0 then begin
if time_s >= Exit_Time_s
then begin
hold_close = close[1];
todayPos=1;
sell all contracts next bar at actlimit stop ;
buytocover all contracts next bar at actlimit stop ;
end ;
end ;
SetExitOnClose;

print(File("d:\TEST1.txt"),hold_close," ",currenttime_s);
I think this should work now. If not we have to check again.

Regards Mike
Attachments
Raven_Logic.png
(78.06 KiB) Downloaded 1958 times

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 13 Jan 2011

Mike,
thank you for your quick answer.

Did you papertrade this code? I did and after opening a position the code tries to exit the position immediately with a stop order and a stop limit of 0.00 again and again. Any further ideas?

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 13 Jan 2011

As you can see in the attached chart, with the exact code like mine it's not buying/selling all the time. Please check my attached screenshot. Let me check after US trading close please.

Mike

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 13 Jan 2011

Raven,

I modified the code a little bit, but just to make sure that after the "ENTRY" no further buying is happening. Further I changed te print comment, to have a better overview in the exported text_file. But honestly I don't get it why you shoud have a new issue with buying/selling all the time. Because there's no rule for this in the code. May you implemented something for the "SHORT"-rule and that's why it's happening.

Here the small update :

Code: Select all

[IntrabarOrderGeneration = True];

Inputs: Exit_Time_s(200000);

var: hold_close(0) ;
var: todayOpen(0);
var: prevHigh(0);
var: actlimit(0);
var: todayPos(0);

prevHigh=HighD(1) data2;
todayOpen=OpenD(0);
actlimit = hold_close;

//piranhaxp' suggestion
if date <> date[1] and time_s < Exit_Time_s then begin
hold_close = 0 ;
todayPos = 0;
end ;

//Entry Long
if marketposition=0 and todayPos < 1 then begin
if todayOpen < prevHigh then begin
buy 1 contracts next bar todayOpen limit ;
todayPos=1;
end;
end;



if marketposition <> 0 then begin
if time_s >= Exit_Time_s
then begin
hold_close = close[1];
todayPos=1;
sell all contracts next bar at actlimit stop ;
buytocover all contracts next bar at actlimit stop ;
end ;
end ;

SetExitOnClose;

print(File("d:\TEST1.txt"), date, " , ", time_s, " , ", hold_close);
Again, I attached a screenshot of the system which is running with my provided code on a one tick chart. Further please explain to me why you are using 'data2' is you are using 'OpenD' & 'HighD' ?

Additionally I attached my exported test1.txt-file. You can see that the variable 'hold-close' is changing it's value from 0 to 1 at 200000.

Regards.

Mike
Attachments
TEST1.rar
(150.77 KiB) Downloaded 286 times
test_raven.png
(39.51 KiB) Downloaded 1968 times

raven
Posts: 31
Joined: 17 Oct 2009
Been thanked: 1 time

Re: Catch Close at certain time if IOG true

Postby raven » 14 Jan 2011

Mike,
thank you again for your detailed response.

As you wrote you run your code on a one tick chart. To compare the same things please run it on a 24H chart as I do (please find my notice above). Tick chart until now was not suitable for me as my code did not work with it. Why are you using tick charts and IOG=true simutaneously? Don’t they do same the both – checking code every tick?

Meanwhile I think I found out the reason for the revolving selling orders in your code: Instead of using currenttime_s as I did you use time_s, what is OK if trading tick charts but not if you trade on 24H charts (as I do I told you above). In such case your condition time_s >= Exit_Time_s is immediately true and leads to selling orders („again and again“). BTW I did implement nothing for the „SHORT“-rule. Did just copy and paste your code.

'data2' using 'OpenD' & 'HighD' : comparison of this 2 values (both of data2! Your code is „wrong“ in this point, please compare to mine above) provide entry rule for data1.

If your code – and your attachements show this – works well with tick charts I will try those to realize my idea of the code. Many thanks for your help!

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Catch Close at certain time if IOG true

Postby piranhaxp » 14 Jan 2011

Raven,

regarding to our private conversation I changed the code a little bit. You are right, there were some OpenD(0) data2 issues. Here ist the revised code :

Code: Select all

[IntrabarOrderGeneration = True];

Inputs: Exit_Time_s(200000);

var: hold_close(0) ;
var: todayOpen(0);
var: prevHigh(0);
var: actentrylimit(0);
var: actexitlimit(0);
var: todayPos(0);

prevHigh=HighD(1) data2;
todayOpen=OpenD(0) data2;
actentrylimit=OpenD(0);
actexitlimit = hold_close;

//piranhaxp' suggestion
if date <> date[1] and time_s < Exit_Time_s then begin
hold_close = 0 ;
todayPos = 0;
end ;

//Entry Long
if marketposition=0 and todayPos < 1 then begin
if todayOpen < prevHigh then begin
buy 1 contracts next bar actentrylimit limit ;
todayPos=1;
end;
end;



if marketposition <> 0 then begin
if time_s >= Exit_Time_s
then begin
hold_close = close[1];
todayPos=1;
sell all contracts next bar at actexitlimit stop ;
buytocover all contracts next bar at actexitlimit stop ;
end ;
end ;

SetExitOnClose;

print(File("d:\TEST1.txt"), date, " , ", time_s, " , ", hold_close);
But please let me come back to your previous post. You said you are using FDAX/BUND combination. Both contracts have session start at 8am and session end at 1005pm (both CET). Why you are using 24h sessions ? Have you set your contracts session different to the regular times ? For me it sounds like, because it doesn't matter to use 24/7 sessions if the contracts are set right. Then you get the right signals. All other settings are not correct or please explain to me why you are using 24h sessions in your symbol properties in the 'QuoteManager'.

I have no more solutions and suggestions. May I don't get it what you want. Sorry.

Regards.

Mike
Attachments
fdax-fgbl-chart.png
(80.73 KiB) Downloaded 1964 times


Return to “User Contributed Studies and Indicator Library”