how to refer to a certain bar in the past

Questions about MultiCharts and user contributed studies.
fibonaccci
Posts: 44
Joined: 26 Dec 2009
Has thanked: 19 times
Been thanked: 1 time

how to refer to a certain bar in the past

Postby fibonaccci » 15 May 2012

Hi,
I’m trying to refer to a price level as a Short trigger level which is the Low of a bar and which meets a specific condition (MACD cross over). As soon as the price falls through the Low of the bar that meets the MACD condition within the last 20 bars, sell short.
My code shorts at the Low of last bar and not at the trigger level.
Can anyone help, please.
Thank you

Code: Select all


Var: ShortTrig(0);
Var: TrigLow(0);
Var: TrigHigh(0);

Var: ShortCondition(0);
Var: LongCondition(0);



 TrigLow = Low(0);
 TrigHigh = High(0);

inputs:  FastLength( 12 ), SlowLength( 26 ), MACDLength( 9 ) ;
variables:  var0( 0 ), var1( 0 ), var2( 0 ) ;

var0 = MACD( Close, FastLength, SlowLength ) ;
var1 = XAverage( var0, MACDLength ) ;
var2 = var0 - var1 ;

condition1 = CurrentBar > 2 and var2 crosses over 0 ;

if condition1 then begin
TrigLow = Low(0);
end;


if countif(condition1,20)>= 1 then
SellShort next bar at TrigLow stop ;

if barssinceentry > 20 then buytocover next bar at market;

User avatar
TJ
Posts: 6523
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

Re: how to refer to a certain bar in the past

Postby TJ » 15 May 2012

fibonaccci wrote:Hi,
I’m trying to refer to a price level as a Short trigger level which is the Low of a bar and which meets a specific condition (MACD cross over). As soon as the price falls through the Low of the bar that meets the MACD condition within the last 20 bars, sell short.
My code shorts at the Low of last bar and not at the trigger level.
Can anyone help, please.
Thank you

Code: Select all


Var: ShortTrig(0);
Var: TrigLow(0);
Var: TrigHigh(0);

Var: ShortCondition(0);
Var: LongCondition(0);



 TrigLow = Low(0);
 TrigHigh = High(0);

inputs:  FastLength( 12 ), SlowLength( 26 ), MACDLength( 9 ) ;
variables:  var0( 0 ), var1( 0 ), var2( 0 ) ;

var0 = MACD( Close, FastLength, SlowLength ) ;
var1 = XAverage( var0, MACDLength ) ;
var2 = var0 - var1 ;

condition1 = CurrentBar > 2 and var2 crosses over 0 ;

if condition1 then begin
TrigLow = Low(0);
end;


if countif(condition1,20)>= 1 then
SellShort next bar at TrigLow stop ;

if barssinceentry > 20 then buytocover next bar at market;


can you draw a diagram to illustrate your vision?

fibonaccci
Posts: 44
Joined: 26 Dec 2009
Has thanked: 19 times
Been thanked: 1 time

Re: how to refer to a certain bar in the past

Postby fibonaccci » 15 May 2012

TJ,

here is a screenshot for my vision.
The Low of bar with the highest high on this chart is the trigger level.
my condition was the wrong one, sorry.


Code: Select all



condition1 = CurrentBar > 2 and var1[1] > var0[1] and var1 < var0 ;
Attachments
Condition example.jpg
Condition example.jpg (103.56 KiB) Viewed 114 times

User avatar
TJ
Posts: 6523
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

Re: how to refer to a certain bar in the past

Postby TJ » 15 May 2012

fibonaccci wrote:TJ,

here is a screenshot for my vision.
The Low of bar with the highest high on this chart is the trigger level.
my condition was the wrong one, sorry.


Code: Select all



condition1 = CurrentBar > 2 and var1[1] > var0[1] and var1 < var0 ;

Sorry... I don't understand what do you want to achieve.
you can use arrows and lines and notes on the diagram to supplement your description.

Furthermore, to help you see with clarity,
I would suggest you to write out your thoughts in step-by-step logic,
as if the MultiCharts is reading it in real time. Try to use this format:

For each bar, go through these logic evaluation:


IF ___________ is true

(optional) and ____________ is true

THEN do this _____________

ELSE do that ______________


If there are multiple logics, write out the description for each logic.

fibonaccci
Posts: 44
Joined: 26 Dec 2009
Has thanked: 19 times
Been thanked: 1 time

Re: how to refer to a certain bar in the past

Postby fibonaccci » 15 May 2012

TJ,
let’s make it simpler because it is probably too easy for you:
forget about MACD,
let’s say condition = 1% down move
at one single bar …whatever…

I just want to use for further calculations in the future
the price information (e.g. Low of bar 0)
of a specific bar (condition=true = 1% down move)
and this bar occured within the last 20 bars

(OR)

Please see attached screenshot:
Bar#0 is the condition bar
The low of this condition bar (1.32032) is penetrated at bar# 17
Because the price hits my stop level (low of condition bar) within last 20 bars
Go Short!

(OR)

if condition=true (appears) once within last 20 bars then
sellshort on Low of (that) Bar (for that condition= true)


Thank you.
Attachments
Condition example.2.jpg
Condition example.2.jpg (117.18 KiB) Viewed 109 times

User avatar
TJ
Posts: 6523
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

Re: how to refer to a certain bar in the past

Postby TJ » 15 May 2012

fibonaccci wrote:TJ,
let’s make it simpler because it is probably too easy for you:
forget about MACD,
let’s say condition = 1% down move
at one single bar …whatever…

I just want to use for further calculations in the future
the price information (e.g. Low of bar 0)
of a specific bar (condition=true = 1% down move)
and this bar occured within the last 20 bars

(OR)

Please see attached screenshot:
Bar#0 is the condition bar
The low of this condition bar (1.32032) is penetrated at bar# 17
Because the price hits my stop level (low of condition bar) within last 20 bars
Go Short!

(OR)

if condition=true (appears) once within last 20 bars then
sellshort on Low of (that) Bar (for that condition= true)


Thank you.


Fibo:

Good description and good diagram -- that's half the work done !

Here's the 2nd half:

1. you need a counter... so that you can count how many bars back.

Each chart has a built-in counter called CURRENTBAR.
When the 1% happens, you can assign this bar number to a variable.
When the low penetrates the 1% bar's low, you can compare the current bar number to the stored bar number to find out if it is within your 20 bar threshold.

2. You need a variable to store the low price of the 1% bar.


Code: Select all

var:
one.percent.drop(false),
one.percent.bar(0),
one.percent.low(0);

one.percent.drop = close < close[1] * 0.99;

if one.percent.drop then
begin
     one.percent.bar = CURRENTBAR;
     one.percent.low = LOW;
end;

if low < one.percent.low
and CURRENTBAR - one.percent.bar < 20 then
begin

     {----- put your sell order here -----}

end;



Good luck!
These users thanked the author TJ for the post:
fibonaccci

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: how to refer to a certain bar in the past

Postby escamillo » 15 May 2012

I have not tried it with your code, but something like this might work:

Code: Select all

if condition1 then Once TrigLow = Low[0];
These users thanked the author escamillo for the post:
fibonaccci


Return to “MultiCharts”