possible to close open position at any time on a 5m chart?

Questions about MultiCharts and user contributed studies.
kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

possible to close open position at any time on a 5m chart?

Postby kernel » 10 Oct 2015

Hi,

I'd like to know if it's possible to close an open position at anytime (not a multiple of 5) on a 5m chart?

Code: Select all

if time=1021 then begin
if MP=1 then
sell next bar at market;
if MP=-1 then
buytocover next bar at market;
end;
this won't work, since 21 is not multiple of the time interval, i.e., 5m.

Basically what I'd like to do is to close all my positions 1m before the closing bell on a 5m chart.

thanks,


kernel

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby tony » 10 Oct 2015

One possible solution that comes to mind, is use a data2, which is a 1 minute chart. Then in your script have if time of Data2 = 1021 then begin. I didn't check to see if it works and am working from memory as to how you write it i.e. "time of data2." To test you'll need to add a sub chart on your WS with the same instrument, just change the resolution to 1 min bars. The upper chart is Data1 and lower charts are Data2, DataN, etc.

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 11 Oct 2015

Tony,

Thanks for the input. That solution was also the first alternative that came across my mind, which should work. However, to a trader, screen is a limited resource. That being said, loading a 1m chart just as a timer is not an elegant solution to me. I'm trying to stay within a single 5m chart.

kernel

One possible solution that comes to mind, is use a data2, which is a 1 minute chart. Then in your script have if time of Data2 = 1021 then begin. I didn't check to see if it works and am working from memory as to how you write it i.e. "time of data2." To test you'll need to add a sub chart on your WS with the same instrument, just change the resolution to 1 min bars. The upper chart is Data1 and lower charts are Data2, DataN, etc.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby tony » 11 Oct 2015

You could try the reserve word computerdatetime which may work, but again I haven't tried it in your situation.

SP
Posts: 465
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 286 times

Re: possible to close open position at any time on a 5m char

Postby SP » 11 Oct 2015

You need to use [IntrabarOrderGeneration = true] at your code. Then "next bar" is "next tick" if the condition is true and there is a tick/trade at the 1021 bar.

After inserting the code go to Format->Properties->Check "Enable Intra-Bar Order Generation" and check under Exits "Allow any exits from this strategy to occur multiple times this bar".

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby sptrader » 11 Oct 2015

[quote="kernel"]Tony,

Thanks for the input. That solution was also the first alternative that came across my mind, which should work. However, to a trader, screen is a limited resource. That being said, loading a 1m chart just as a timer is not an elegant solution to me. I'm trying to stay within a single 5m chart.

kernel
************************************************************************
* Don't worry about screen real-estate- just make the Data2 1 min bars- "hidden"...
or use IOG, that should work too. (as SP suggested above)

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 11 Oct 2015

You could try the reserve word computerdatetime which may work, but again I haven't tried it in your situation.
I haven't got a chance to test it yet, but it seems to return my local computer time, right? That'd put a lot of stress on my local time accuracy if you really don't want to hold your positions overnight. That being said, I'd rather rely on the broker's server time rather than my local time.

kernel

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 11 Oct 2015

You need to use [IntrabarOrderGeneration = true] at your code. Then "next bar" is "next tick" if the condition is true and there is a tick/trade at the 1021 bar.

After inserting the code go to Format->Properties->Check "Enable Intra-Bar Order Generation" and check under Exits "Allow any exits from this strategy to occur multiple times this bar".
IOG is true in my code. I checked that box you suggested but it didn't seem to do the trick. Nothing happens @1021.


kernel

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 11 Oct 2015

************************************************************************
* Don't worry about screen real-estate- just make the Data2 1 min bars- "hidden"...
or use IOG, that should work too. (as SP suggested above)
How do you make a chart "hidden" btw?
I'd leave that as the last ditch though.

kernel

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby tony » 11 Oct 2015

You need to use [IntrabarOrderGeneration = true] at your code. Then "next bar" is "next tick" if the condition is true and there is a tick/trade at the 1021 bar.

After inserting the code go to Format->Properties->Check "Enable Intra-Bar Order Generation" and check under Exits "Allow any exits from this strategy to occur multiple times this bar".
That won't work. All that will do is have (a) script calculations every tick and (b) place trades / exit trades intrabar. Your likely options are either to use data2 or reference computerdatetime which will give you the resolution of 1 minute. There are likely other solutions but without researching these are the two that come to mind.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby sptrader » 11 Oct 2015

************************************************************************
* Don't worry about screen real-estate- just make the Data2 1 min bars- "hidden"...
or use IOG, that should work too. (as SP suggested above)
How do you make a chart "hidden" btw?
I'd leave that as the last ditch though.

kernel
******************************************************************
To hide data2-
right click on the chart,"format instruments",select data2, then "format", look for "subchart" and make it "hidden" (drop down list), then data2 stream will be invisible but you can still access it in power language.....

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 12 Oct 2015

Your likely options are either to use data2 or reference computerdatetime which will give you the resolution of 1 minute. There are likely other solutions but without researching these are the two that come to mind.
I agree with you. Both are likely to be working. However, one lacks elegance, the other raises security issue. I'm looking for the 3rd one out there... :-)

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: possible to close open position at any time on a 5m char

Postby sptrader » 12 Oct 2015

Your likely options are either to use data2 or reference computerdatetime which will give you the resolution of 1 minute. There are likely other solutions but without researching these are the two that come to mind.
I agree with you. Both are likely to be working. However, one lacks elegance, the other raises security issue. I'm looking for the 3rd one out there... :-)
**********************************************************************
* How about this option, use a single 1 minute chart and make a simulated 5 minute chart out of it for your other calcs-
A "simulated" 5 minute chart would be:
open = open[5], high = highest(h,5) Low = lowest(L,5), and C[0].... just a thought.

kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

Re: possible to close open position at any time on a 5m char

Postby kernel » 12 Oct 2015

* How about this option, use a single 1 minute chart and make a simulated 5 minute chart out of it for your other calcs-
A "simulated" 5 minute chart would be:
open = open[5], high = highest(h,5) Low = lowest(L,5), and C[0].... just a thought.
I trade 5m chart, all computations are based on 5m charts. You may replace a 5m chart with higher resolution 1m chart, that way, you get the high-frequency details, but lose the low-frequency envelope or sketch, although the lost info may be recovered from high resolution chart, but I don't think the mess created worth the benefit you gain, wrong RRR. Nice try, thank you though:-)

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

Re: possible to close open position at any time on a 5m char

Postby JoshM » 15 Oct 2015

You need to use [IntrabarOrderGeneration = true] at your code. Then "next bar" is "next tick" if the condition is true and there is a tick/trade at the 1021 bar.
That's true, there's no need to add a second data series to a chart for this. With IOG it works fine:

Code: Select all

[IntrabarOrderGeneration = true];

if (LastBarOnChart_s) then begin

Print("Time of last trade: ", Q_Time_s);

end;
Prints:

Code: Select all

Time of last trade: 150901.00
Time of last trade: 150902.00
Time of last trade: 150903.00
Time of last trade: 150903.00
Time of last trade: 150906.00
Time of last trade: 150906.00
Time of last trade: 150907.00
Time of last trade: 150908.00
Time of last trade: 150912.00
Time of last trade: 150912.00
Time of last trade: 150915.00
Time of last trade: 150917.00
Time of last trade: 150921.00
This is on a 30-minute chart by the way, but here we only need to match the time of the last trade with whichever time you want to use in your strategy.
I haven't got a chance to test it yet, but it seems to return my local computer time, right? That'd put a lot of stress on my local time accuracy if you really don't want to hold your positions overnight. That being said, I'd rather rely on the broker's server time rather than my local time.
That may be true, but how much would your computer time differ with your broker time -- perhaps 3 seconds at most? But let's say it's 10 seconds; using `ComputerDateTime` is still the lesser of two evils.

That's because `ComputerDateTime` is updated every second, while the time of the last trade (see code example above) or the closing time of a data series with a lower granularity (as suggested in this thread) updates considerably less often. With CFDs and (midcap and smallcap) stocks it's not uncommon that there's a time gap of 5 minutes of more between trades. So using the current computer time is then a more secure approach than using the last trade (or the time of a bar), I think.

Also: using the time of the last trade has the disadvantage that, during 'slow' market periods, there can be a considerable lag between the current (computer) time and the time of the last trade.

What you could do in your case:
- Use `ComputerDateTime` in your script for the current time
- Optionally: calculate the seconds between the broker time and your computer time to correct for any time difference.
- And use RecalcLastBarAfter to make sure that the script recalculates every x seconds (to prevent lagging scripts due to slow market conditions).

Code: Select all

if time=1021 then begin
if MP=1 then
sell next bar at market;
if MP=-1 then
buytocover next bar at market;
end;
I think your script will be more robust if you use a time range instead of an exact time (`time=1021`). Because, if there are for whatever reason no ticks between 10:20 and 10:22, then the 10:21 time never happens.

I think this is more robust:

Code: Select all

// Execute in the last 10 minutes prior to 10:30
if (Time >= 1020 and Time < 1030) then begin

// ..

end;


Return to “MultiCharts”