Help with barstatus and bartype

Questions about MultiCharts and user contributed studies.
Ray
Posts: 5
Joined: 12 Oct 2010

Help with barstatus and bartype

Postby Ray » 03 Nov 2010

Hi, i'm coding a simple stocastic model.
Using v5.5 with IOG on.
1min bar interval.

[intrabarordergeneration=true]
Input: Len(0),
StartTime (2300),
EndTime (2150),
Tradesize (0),
PriceH( High ),
PriceL( Low ),
PriceC( Close ),
StochLength( 5 ),
SmoothingLength1( 3 ),
SmoothingLength2( 3 ),
SmoothingType( 1 );


variables:
oFastK( 0 ),
oFastD( 0 ),
oSlowK( 0 ),
BarsSinceEnt (0),
oSlowD( 0 ),

Value1 = Stochastic( PriceH, PriceL, PriceC, StochLength, SmoothingLength1,
SmoothingLength2, SmoothingType, oFastK, oFastD, oSlowK, oSlowD ) ;


if time > startTime or time < Endtime then begin

if currentbar > 1 and bartype_ex = 2 and marketposition = 0 then begin

if barstatus = 2 and oSlowK > 90 then
sellshort ("Sk") Tradesize Contracts next bar at close[1] + limit

{here we are trying to enter when the stocastic is above 90 calculated on the current bar. If this is true, then we enter a limit order on the start of the next new bar (1min).
As i understand, with IOG on, the next bar is the next tick, hence i include barstatus =2 to indicate the end of the current bar. The trouble is, the limit order is placed fine but after a few seconds into the new bar, it is deleted..i presume because barstatus is no longer =2. How can i code this so that IOG is on (i need this on so to be able to reenter intrabar, next tick update) but be able to sell/buy on next minute bar as well? And with IOG on, is the Stocastic calculated on every tick or on the close of the bar? I presume close as i am not using intrabarpersist. Any advise/help much appreciated.}

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: Help with barstatus and bartype

Postby Dave Masalov » 04 Nov 2010

Dear Ray,

You are correct, you need the construction with an additional flag.

Code: Select all

variables:
short_trigger(true), limit_price(0);

if ( not short_trigger ) then begin
short_trigger = barstatus = 2 and oSlowK > 90; // short_trigger only at close of bar becames true if oSlowK > 90
limit_price = close; // memorize limit price for order
end
else
short_trigger = short_trigger and oSlowK > 90; // this guarantees short_trigger to stay true while oSlowK > 90

if short_trigger then
sellshort ("Sk") Tradesize Contracts next bar at limit_price limit;

Ray
Posts: 5
Joined: 12 Oct 2010

Re: Help with barstatus and bartype

Postby Ray » 05 Nov 2010

Dave,
thanks for reply. adding the addition flag seems to be working.
Another issue that has cropped up is in the execution of limit order.
In the code below, we are buying/selling next bar on open, with the specified limit.
My question is this, when does the limit order gets placed into the market, at the end of current bar (lets say in this case, at the end of the min) or start of new bar (tick or time?). What we see is the limit order is placed as soon as the first tick of the next bar is traded, but this new bar first tick may happen some seconds into the minute. Is there a way to specify the limit order is placed on the 0059 of current bar or 0001 of new bar?
Am i correct in understanding that buy next bar is same as buy on open next bar?
thanks again for your help and time with this.

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: Help with barstatus and bartype

Postby Dave Masalov » 05 Nov 2010

Dear Ray,
Am i correct in understanding that buy next bar is same as buy on open next bar?
Yes.
Is there a way to specify the limit order is placed on the 0059 of current bar or 0001 of new bar?
No, there is no way to do this. It is tick-by calculation. Thus, if the tick comes at 00:59, and the next comes at 01:03, than the order will be generated at 01:03, because in 00:59 calculation barstatus <> 2 (we cannot know if the ticks come in this minute or next minute).

Ray
Posts: 5
Joined: 12 Oct 2010

Re: Help with barstatus and bartype

Postby Ray » 09 Nov 2010

Dave, thanks for response.
I have another q i'm hoping you may be able to help with.
It's in the same strategy as below. So, IOG on and in some cases buying/selling on next tick, other times, buying/selling on next bar (using bar status =2).
The q is this:
I have IOG on and "allow unlimited entries/exits per bar" enabled as there are senarios where the strategy, after a stop, can enter again in this bar and be stopped out; but there is to be only one entry in any one bar.
I have gone about this by setting a flag thus:

Entrybar_trade_closed (true);
Entrybar_trade_closed = (currenttime - entrytime)<= barinterval;

if time > startTime or time < Endtime then begin

if not Entrybar_trade_closed then begin
Entrybar_trade_closed = (currenttime - entrytime)<= barinterval;
end
else
Entrybar_trade_closed = false;

But i still see multiple entries in the same bar: does IOG with "allow unlimited entries/exits per bar" enabled override any entry restrictions?
If not, can you suggest a way to accomplish only one entry allowed per bar but one or two exits (from a previous entry on a bar before) allowd in the same bar?
many thanks again for your help and advice with this.
ray

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: Help with barstatus and bartype

Postby Dave Masalov » 10 Nov 2010

Dear Ray,

I have answered your private message regarding this subject.


Return to “MultiCharts”