Entering/Exiting before session end

Questions about MultiCharts and user contributed studies.
FutureTrader
Posts: 79
Joined: 28 Mar 2013
Has thanked: 3 times
Been thanked: 4 times
Contact:

Entering/Exiting before session end

Postby FutureTrader » 29 Jun 2015

I'd like to enter/exit my positions before session close. Timeframe is daily. Is the following code working correctly or is the condition only checked when the daily bar is finished.
Thanks for your help

Code: Select all

[IntrabarOrderGeneration = true]


timeClosePosition = 215930.00;
useTimeEntryClosePosition = true;


//this condition would be applied on daily timeframe.
//is the condition only checked when the daily bar is closed, or is it checked in realtime?
//if it is checked in realtime the following code should work?
if Close > Averge(Close, 200) then begin
//force a early exit before exchange close; it's just for realtime execution
if useTimeEntryClosePosition and (currenttime_s >= timeClosePosition) then begin
RecalcLastBarAfter(1);
Buy("LE " + NumToStr(timeClosePosition, 0)) next bar at market;
end else begin
Buy("LE") this bar at close;
end;
end;

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

Re: Entering/Exiting before session end

Postby JoshM » 29 Jun 2015

is the condition only checked when the daily bar is closed, or is it checked in realtime?
That condition is checked in real-time due to the Intra-bar order generation attribute being set to true.

A few comments:

* You're using `RecalcLastBarAfter()` only when the current time is greater than or equal to the `timeClosePosition`. I'd also use `RecalcLastBarAfter()` before that, so that the script is also calculated once every second before the 21:59:30 time occurs.

* You're not closing a position at the close, but instead opening it with `Buy`. You probably mean BuyToCover or Sell.

* The second order uses `this bar at close`, which only works during backtesting. (If that's what you intended, then it's fine of course).

FutureTrader
Posts: 79
Joined: 28 Mar 2013
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Entering/Exiting before session end

Postby FutureTrader » 29 Jun 2015

is the condition only checked when the daily bar is closed, or is it checked in realtime?
That condition is checked in real-time due to the Intra-bar order generation attribute being set to true.
What would happen with IOG enabled with following code:

Code: Select all

if Close > Averge(Close, 200) then begin
Buy("LE") this bar at close;
end;
wouldn't it be also executed as soon as the condition = true without waiting that the daily bar is closed?
A few comments:

* You're using `RecalcLastBarAfter()` only when the current time is greater than or equal to the `timeClosePosition`. I'd also use `RecalcLastBarAfter()` before that, so that the script is also calculated once every second before the 21:59:30 time occurs.
Do I need the RecalcLastBarAfter in the script to get the IOG condition checked in realtime?
* You're not closing a position at the close, but instead opening it with `Buy`. You probably mean BuyToCover or Sell.

* The second order uses `this bar at close`, which only works during backtesting. (If that's what you intended, then it's fine of course).
I'm entering/exiting the trade before session close, so that's correct and yes, the second statement is for backtesting.

thanks for you reply

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

Re: Entering/Exiting before session end

Postby JoshM » 29 Jun 2015

What would happen with IOG enabled with following code:

Code: Select all

if Close > Averge(Close, 200) then begin
Buy("LE") this bar at close;
end;
wouldn't it be also executed as soon as the condition = true without waiting that the daily bar is closed?
That's correct, that code will be executed as soon as the condition is true without waiting for the bar to close.

By the way, if your strategy settings allow it, this code..

Code: Select all

if Close > Averge(Close, 200) then begin
Buy("LE") this bar at close;
end;
.. can buy additional stocks/contracts each time the condition is true. If you only want to enter a position once, you can use the following code:

Code: Select all

// Only open a position when flat
if (MarketPosition = 0) then begin

if Close > Average(Close, 200) then begin
Buy("LE") this bar at close;
end;

end;
Do I need the RecalcLastBarAfter in the script to get the IOG condition checked in realtime?
Yes, that's because IOG does allow intra-bar order generation, but only when the script actually calculates intra-bar. So when there's no new tick coming in, the script will not calculate again with IOG, and so the condition won't be checked.

You can structure the code as follows to always have it calculate at least once per second:

Code: Select all

if (Close > Average(Close, 200)) and (MarketPosition = 0) then begin

if useTimeEntryClosePosition and (currenttime_s >= timeClosePosition) then begin

Buy("LE " + NumToStr(timeClosePosition, 0)) next bar at market;

end

else begin

Buy("LE") this bar at close;

end;

end;

RecalcLastBarAfter(1);

FutureTrader
Posts: 79
Joined: 28 Mar 2013
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Entering/Exiting before session end

Postby FutureTrader » 29 Jun 2015

if (Close > Average(Close, 200)) and (MarketPosition = 0) then begin

if useTimeEntryClosePosition and (currenttime_s >= timeClosePosition) then begin

Buy("LE " + NumToStr(timeClosePosition, 0)) next bar at market;

end

else begin

Buy("LE") this bar at close;

end;

end;

RecalcLastBarAfter(1);
wouldn't the else statement immediately executed when the C > MA(C , 200) and IOG enabled?

I think the correct code would be:

Code: Select all

// enabling IOG when useTimeEntryClosePosition = true, other disable IOG
//[IntrabarOrderGeneration = true]


timeClosePosition = 215930.00;
useTimeEntryClosePosition = true;


// executes on time
if useTimeEntryClosePosition and (currenttime_s >= timeClosePosition) then begin
if Close > Averge(Close, 200) then begin
RecalcLastBarAfter(1);
Buy("LE " + NumToStr(timeClosePosition, 0)) next bar at market;
end;
end;


// executes on session end
if (useTimeEntryClosePosition = false or enabelBacktesting = true) and (Close > Averge(Close, 200)) then begin
Buy("LE") this bar at close;
end;

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

Re: Entering/Exiting before session end

Postby TJ » 29 Jun 2015

RecalcLastBarAfter is a declaration for the indicator -- the whole indicator. When the recal is activated, the whole indicator will be recalculated. It is not exclusive to a specific line of logic.

The statement can be placed anywhere in the code; I would put it at the top of the indicator for easy reference.
Having it buried in a condition might cause unexpected behavior. And when the problem occurs, it will be difficult to trace (debug).

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

Re: Entering/Exiting before session end

Postby JoshM » 29 Jun 2015

wouldn't the else statement immediately executed when the C > MA(C , 200) and IOG enabled?
No, because it's an if/else statement:

Code: Select all

if (someCondition = true) then begin

// code to execute

end

else begin

// code that's executed when
// 'someCondition' is false

end;
What you're referring to is possible, but then there have to be a semi-colon after the first `end` and `else` needs to be missing. Like this:

Code: Select all

if (someCondition = true) then begin

// code to execute when
// 'someCondition' is true

end;

begin

// this code block is executed
// unconditionally (that is, every time
// the script calculates)

end;
By the way, how code is executed is not affected by IOG. IOG only affects when the script calculates. So not matter if IOG is on or off, an if/else statement is always processed in the same way.
I think the correct code would be:
Why do you think that? :) Wouldn't the `RecalcLastBarAfter()` keyword still not trigger a script recalculation on every bar?

FutureTrader
Posts: 79
Joined: 28 Mar 2013
Has thanked: 3 times
Been thanked: 4 times
Contact:

Re: Entering/Exiting before session end

Postby FutureTrader » 29 Jun 2015

Code: Select all

if (Close > Average(Close, 200)) and (MarketPosition = 0) then begin

if useTimeEntryClosePosition and (currenttime_s >= timeClosePosition) then begin

Buy("LE " + NumToStr(timeClosePosition, 0)) next bar at market;

end

else begin

Buy("LE") this bar at close;

end;

end;

RecalcLastBarAfter(1);
but the logic here is either it's currenttime_s >= timeClosePosition then do a close on time, or else do a close on this bar, which would be filled immediately if IOG enabled. That was my thinking why I changed the code to the above ...


Return to “MultiCharts”