chronological conditions

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

chronological conditions

Postby fibonaccci » 03 Sep 2012

can anyone help me please!
I want to generate a trade signal as soon as 3 conditions chronologically become true.
e.g.
as soon as cond1=true (at least) once within the last 10 bars and
as soon as cond1=true AND cond2 become true (at least) once within the last 10 bars and
as soon as cond1=true AND cond2 become true (at least) once within the last 10 bars
then buy ..

in other words: first cond1 and then cond2 and then cond3 becomes true within the last 10 bars!

Below is my try which compiles but does not do what I need.

Thanks.

Code: Select all


cond1 = (countif( cond.a or cond.b or cond.c ,10)>= 1);
cond2 = (countif( cond.d or cond.e or cond.f ,10)>= 1);
cond3 = (countif( cond.g or cond.h or cond.i ,10)>= 1);

if MRO( cond1=true, 10,1) >0 and MRO( cond1=true, 10,1) <=10 then
if MRO( cond2=true, 10,1) >0 and MRO( cond2=true, 10,1) <=10 then
if MRO( cond3=true, 10,1) =1 and MRO( cond3=true, 10,1) <=10 then

if MRO( cond1=true, 10,1)> MRO( cond2=true, 10,1) then
if MRO( cond2=true, 10,1)> MRO( cond3=true, 10,1) then

buy next bar at market;

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

Re: chronological conditions

Postby TJ » 03 Sep 2012

a side note:

Always use a meaningful variable name; avoid generic names like value1, value2, condition1, etc.,

A good coding practice will save you lots of headache in debugging.

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

Re: chronological conditions

Postby TJ » 03 Sep 2012

suggestion:

draw a flow chart

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: chronological conditions

Postby Henry MultiСharts » 05 Sep 2012

Fibonaccci, you need to add bar counter that will be reset to zero when the conditions are met, then add control for the amount of bars. Here is an example:

Code: Select all

var: OpenLong(false),OpenShort(false),BarCounterLong(0),BarCounterShort(0);


if condition1 then begin //Long
OpenLong = true;
BarCounterLong = 0;
end;

if condition1 then begin //Long
OpenShort = true;
BarCounterShort = 0;
end;

if marketposition = 0 then begin

if OpenLong and BarCounterLong = 10 then begin
buy("LONG") 1 Contract Next Bar Market;
OpenLong = false;
end;

if OpenShort and BarCounterShort = 10 then begin
SellShort("SHORT")1 Contract Next Bar Market;
OpenShort = false;
end;

end;


BarCounterLong = BarCounterLong +1;
BarCounterShort = BarCounterShort +1;

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

Re: chronological conditions

Postby piranhaxp » 05 Sep 2012

Henry ....

like TJ said :
avoid generic names like value1, value2, condition1, etc.
Therefore I like to put everything in var's with declaration for 0 (false) or 1 (true) without using FALSE & TRUE. For a checkup you can plot every "condition" with 0 and 1, but not with false and true. Or in other words it would need more declaration.

Example :

Code: Select all

var: x(0); // if "condition" = false then value of x is 0 ... if true value of x = 1
var: openlong(0);
var: barcounterlong(0);

if h > h[1] then x = 1 else x = 0;
or

Code: Select all

if h > h[1] then x = 1 else if h <= h[1] then x = 0;
I don't know why but I prefer the second declaration. Further you can go with :

Code: Select all

if x = 0 then
begin
OpenLong = 0;
BarCounterLong = 0;
end;

if x = 1 then
begin
OpenLong = 1;
BarCounterLong = BarCounterLong +1;
If OpenLong = 1 and BarCounterLong > 0 then
begin
.... and further "conditions" can be declared .... take it as a rat tail ;)
end;
end;
To check everything you can use plots :

Code: Select all

If x = 1 and x[1] <> 1 then
begin
plot1(x, "Check for X");
if openlong = 1 and openlong[1] <> 1 then
begin
plot2(openlong,"Check for OL");
etc etc .........
end;
end;

Mike

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

Re: chronological conditions

Postby TJ » 05 Sep 2012

Henry ....
like TJ said :
avoid generic names like value1, value2, condition1, etc.
...
Example :

Code: Select all

var: x(0); // if "condition" = false then value of x is 0 ... if true value of x = 1
var: openlong(0);
var: barcounterlong(0);
if h > h[1] then x = 1 else x = 0;
or

Code: Select all

if h > h[1] then x = 1 else if h <= h[1] then x = 0;
...
Mike
I would do this:

Code: Select all


var:
higher.high(false),
lower.low(false);

higher.high = H > H[1];
lower.low = L < L[1];

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

Re: chronological conditions

Postby piranhaxp » 05 Sep 2012

TJ ....

LOL from my side. Yes of course that is another style. Like I wrote I don't like TRUE & FALSE. As long as I love to check my code (rules & conditions) with plots, I don't know how to plot TRUE & FALSE without any extra code or declaration for it. With my style I need 1 variable. With TRUE & FALSE you will have to declare 2 variables or an extra "if ... then" for checking your code via plots.

But yes, you are correct. It depends on how do you prefer to work on the problem.

Mike

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

Re: chronological conditions

Postby TJ » 05 Sep 2012

TJ ....

LOL from my side. Yes of course that is another style. Like I wrote I don't like TRUE & FALSE. As long as I love to check my code (rules & conditions) with plots, I don't know how to plot TRUE & FALSE without any extra code or declaration for it. With my style I need 1 variable. With TRUE & FALSE you will have to declare 2 variables or an extra "if ... then" for checking your code via plots.

But yes, you are correct. It depends on how do you prefer to work on the problem.

Mike
The key is, you should be able to read your code 3 months from now without scratching your head.

What is "x = 1" ?
Is it a meaningful representation of the underlying logic?
This is not a debate of preference or style,
you are merely converting the problem to another cryptic riddle, which provides no assistance in your debugging effort.

Whether you like or dislike TRUE & FALSE, or whatever, has no bearing to the issue at hand.
If you are the only one who knows what "x = 1" is, then it is NOT a solution.
If every time you encounter "x = 1", you have to go back to look up the meaning, then it is NOT a solution. What if your strategy has 150 conditions? Do you memorize all of them?

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

Re: chronological conditions

Postby TJ » 05 Sep 2012

ps. if you do not like TRUE or FALSE, you can try this:

Code: Select all


var:
higher.high(0),
lower.low(0);

if H > H[1] then higher.high = 1 else higher.high = 0;
if L < L[1] then lower.low = 1 else lower.low = 0;

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

Re: chronological conditions

Postby piranhaxp » 05 Sep 2012

ps. if you do not like TRUE or FALSE, you can try this:


Code:

var:
higher.high(0),
lower.low(0);

if H > H[1] then higher.high = 1 else higher.high = 0;
if L < L[1] then lower.low = 1 else lower.low = 0;
And where is the difference to x ? ... LOL ... I just like to make sure the rule for x = 0 ... Have some experience with

Code: Select all

if h > h[1] then x = 1 else x = 0;


only ... ;O)

Mike

fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

Re: chronological conditions

Postby fibonaccci » 06 Sep 2012

TJ, Henry and Mike, thank you very much for your help.
I’m still having problems with my code.
Just to make clear my code requirement once again as I do not know how a flow chart might look like:
All 3 conditions (cond.L1, cond.L2 and cond.L3) have to appear over the last 10 bars at least once
and cond.L1 appears first then cond.L2 appears and at the last bar cond.L3 appears.
The problem with my version and your version is that the more bars (e.g. 20 bars instead of 10 bars ) I choose for the “lookback period” in which the 3 conditions should occur the less signals I get which seems not logic without knowing/understanding the code. It should be the other way around, higher lookback period e.g. 50 bars leads to much more signals.
Here is my version of the code:

Code: Select all

{-------------------- 1. version --------------------------- }

vars: cond.a(0), cond.b(0), cond.c(0),cond.d(0), cond.e(0), cond.f(0), cond.g(0), cond.h(0), cond.i(0);
vars: cond.L1(false), cond.L2(false), cond.L3(false);
vars: cond.S1(false), cond.S2(false), cond.S3(false);

{ Hi Condition }
cond.L1 = (countif( cond.a or cond.b or cond.c ,10)>= 1);
cond.S3 = (countif( cond.a or cond.b or cond.c ,10)>= 1);

{ Hi or Lo Condition }
cond.L2 = (countif( cond.d or cond.e or cond.f,10)>= 1);
cond.S2 = (countif( cond.d or cond.e or cond.f,10)>= 1);

{ Lo Condition }
cond.L3 = (countif( cond.g or cond.h or cond.i ,10)>= 1);
cond.S1 = (countif( cond.g or cond.h or cond.i ,10)>= 1);

{------- -----------------}
if cond.L1 then value1 = CurrentBar;
if CurrentBar > value1 then
value2 = CurrentBar - value1;

if cond.L2 then value3 = CurrentBar;
if CurrentBar > value3 then
value4 = CurrentBar - value3;

if cond.L3 then value5 = CurrentBar;
if CurrentBar > value5 then
value6 = CurrentBar - value5;
{------- -----------------}
if cond.S1 then value7 = CurrentBar;
if CurrentBar > value7 then
value8 = CurrentBar - value7;

if cond.S2 then value9 = CurrentBar;
if CurrentBar > value9 then
value10 = CurrentBar - value9;

if cond.S3 then value11 = CurrentBar;
if CurrentBar > value11 then
value12 = CurrentBar - value11;


{------- Long -----------------}

{ 3 conditions within last 10 bars }
if cond.L1 and cond.L2 and cond.L3 then

{ Chronologic order for 3 conditions }
if value2 > value4 and value4 > value6 then

Buy("Long") next bar at market;

{------- Short -----------------}

{ 3 conditions within last 10 bars }
if cond.S1 and cond.S2 and cond.S3 then

{ Chronologic order for 3 conditions }
if value8 > value10 and value10 > value12 then

SellShort("Short") next bar at market;
I tried to alter your example code but I’m not quite sure what it does. I have the impression that it looks back up to 30 bars and checks whether cond.L3 occurred within the last 10 bars and cond.L2 occurred 10 bars before cond.L3 occure and cond.L1 occurred 10 bars before cond.L2 occurred:

Code: Select all

{-------------------- 2. version --------------------------- }

var: cond.L1(false), cond.L2(false), cond.L3(false);
var: cond.S1(false), cond.S2(false), cond.S3(false);
var: OpenLong1(false),OpenLong2(false),OpenLong3(false);
var: OpenShort1(false),OpenShort2(false),OpenShort3(false);
var: BarCounterLong.1(0),BarCounterLong.2(0),BarCounterLong.3(0);
var: BarCounterShort.1(0),BarCounterShort.2(0),BarCounterShort.3(0);


if cond.L1 then begin
OpenLong1 = true;
BarCounterLong.1 = 0;
end;
if cond.L2 then begin
OpenLong2 = true;
BarCounterLong.2 = 0;
end;
if cond.L3 then begin
OpenLong3 = true;
BarCounterLong.3 = 0;
end;

//Short
if cond.S1 then begin
OpenShort1 = true;
BarCounterShort.1 = 0;
end;
if cond.S2 then begin
OpenShort2 = true;
BarCounterShort.2 = 0;
end;
if cond.S3 then begin
OpenShort3 = true;
BarCounterShort.3 = 0;
end;

if marketposition = 0 then begin

if OpenLong1 and OpenLong2 and OpenLong3 and BarCounterLong.1 = 1 and BarCounterLong.2 = 1 and BarCounterLong.3 = 1 then begin
buy("LONG") Next Bar Market;
OpenLong1 = false;
OpenLong2 = false;
OpenLong3 = false;
end;

if OpenShort1 and OpenShort2 and OpenShort3 and BarCounterShort.1 = 1 and BarCounterShort.2 = 1 and BarCounterShort.3 = 1 then begin
SellShort("SHORT") Next Bar Market;
OpenShort1 = false;
OpenShort2 = false;
OpenShort3 = false;
end;

end;


BarCounterLong.1 = BarCounterLong.1 +1;
BarCounterLong.2 = BarCounterLong.2 +1;
BarCounterLong.3 = BarCounterLong.3 +1;
BarCounterLong.1 = BarCounterLong.1 +1;
BarCounterLong.2 = BarCounterLong.2 +1;
BarCounterLong.3 = BarCounterLong.3 +1;


@Mike: thanks for your 1/0 instead of true/false procedure. I will check my code by plotting as soon as I got the right thing.

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

Re: chronological conditions

Postby piranhaxp » 06 Sep 2012

From my standpoint with reading your code :

(1) You are declaring your conditions from cond.a to cond.i without any "rule" .... How cond.L1 can know that one of the 3 conditions cond.a-c occured or the sum of the conditions cond.a-c is at least >= 1 ? I'm missing the counter for the cond.a-c !! Same with the other conditions.

Maybe give this a try or I'm missing the point here . May it is written way tooooo complicated or doesn't fit to your needs. Did not test anything, just tried to give you an example ... Please put your own condition in this example :

Code: Select all

var: cond.a(0), count.a(0); // Variable for declaring the condition for "a" with counter for "a"
var: cond.b(0), count.b(0); // Variable for declaring the condition for "b" with counter for "b"
var: cond.c(0), count.c(0); // Variable for declaring the condition for "c" with counter for "c"
var: sum.abc(0), sum.count.abc(0); // Summation for cond.a-c to be sure that at least 1 occured in the last 10 bars
var: cond.L1(0), count.L1(0); // Variable for declaring the condition for "L1" with counter for "L1"

// -------------------------------------------------------------------------------------------------------
// Rule for condition a - c

if h > h[1] and count.a = 0 then
begin
cond.a = 1;
count.a = count.a + 1;
end;

// Rule for condition "a"

if h > h[2] and count.b = 0 then
begin
cond.b = 1;
count.b = count.b + 1;
end;

// Rule for condition "c"

if h > h[3] and count.c = 0 then
begin
cond.c = 1;
count.c = count.c + 1;
end;

// -------------------------------------------------------------------------------------------------------
// Reset counter for count.a - c

if count.a > 10 then
begin
cond.a = 0;
count.a = 0;
end;

if count.b > 10 then
begin
cond.b = 0;
count.b = 0;
end;

if count.c > 10 then
begin
cond.c = 0;
count.c = 0;
end;

// -------------------------------------------------------------------------------------------------------
// Summation for cond.a-c to be sure that at least 1 occured in the last 10 bars ....
// If sum.count.abc is > 30 not one of cond.a - c occured in the last 10 bars .... CORRECT ???

sum.abc = sum(cond.a, cond.b, cond.c);

if sum.count..abc <= 30 then
begin
sum(count.a, count.b, count.c);
end
else
sum.count.abc = 0;

// -------------------------------------------------------------------------------------------------------
// Declaring condition L1

If sum.abc > 0 and sum.count.abc <= 30 then
begin
cond.L1 = 1;
count.L1 = count.L1 + 1;
end
else
cond.L1 = 0;
count.L1 = 0;

:
:

then go on with your entry conditions and counter
But as I said, I may lost the point here.

Mike

fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

Re: chronological conditions

Postby fibonaccci » 06 Sep 2012

Mike, thanks for the reply.

Sorry for any confusion, you are right I have not provided any sub-conditions ( cond.a …) as they are hopefully not a part of my problem 
the below code returns whether cond.a or cond.b or cond.c occurred at least once within the last 10 bars, so it should be the same procedure as your summing up procedure:

Code: Select all

cond.L1 = (countif( cond.a or cond.b or cond.c ,10)>= 1);
If - cond.a or cond.b or cond.c - occurred at least once within the last 10 bars then cond.L1 = TRUE etc.

Does your statement “ If sum.count.abc is > 30 “ mean over the last 30 bars? Then it is NOT correct, because cond.L1 and cond.L2 and cond.L3 should be TRUE when they “TOGETHER” appear over the last 10 bars,
e.g. if - cond.a and cond.d and cond.g - appear over the last 10 bars then BUY etc..
or e.g. if - cond.b and cond.d and cond.h - appear over the last 10 bars then BUY etc..

or

if cond.a appeared at CurrentBar(9) then cond.L1 = True
if cond.d appeared at CurrentBar(6) then cond.L2 = True
if cond.g appeared at CurrentBar(1) then cond.L3 = True
1. therefore - cond.L1 and cond.L2 and cond.L3 - appeared within the last 10 bars = True

Code: Select all

{ 3 conditions within last 10 bars }
if cond.L1 and cond.L2 and cond.L3 then
2. therefore - cond.L1 appeared before cond.L2 and cond.L2 - appeared before cond.L3 = True

Code: Select all

{ Chronologic order for 3 conditions }
if value2 > value4 and value4 > value6 then
3. BUY next bar…

Hopefully I could make myself a little bit clearer, thanks

fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

Re: chronological conditions

Postby fibonaccci » 06 Sep 2012

here are the condition rules I have extended from your idea:

Code: Select all

{-------------------- Rule for conditions a - i --------------------------- }

// Rule for condition "a"
count.a = h > h[1] ;

// Rule for condition "b"
count.b = h > h[2];

// Rule for condition "c"
count.c = h > h[3];

// Rule for condition "d"
count.d = c > c[1] ;

// Rule for condition "e"
count.e = c > c[2];

// Rule for condition "f"
count.f = c > c[3];

// Rule for condition "g"
count.g = l > l[1] ;

// Rule for condition "h"
count.h = l > l[2];

// Rule for condition "i"
count.i = l > l[3];

fibonaccci
Posts: 49
Joined: 26 Dec 2009
Has thanked: 34 times
Been thanked: 1 time

Re: chronological conditions

Postby fibonaccci » 06 Sep 2012

Ok, a very simplistic approach would be:

If
condition-x, condition-y and condition-z
occur (all) within the last 10 bars
and condition-x occurs first (e.g. currentbar(9) )
and condition –y occurs second (e.g. currentbar(4) )
and condition-z occurs third (e.g. currentbar(1) )

then buy at market…


Return to “MultiCharts”