Signals on several data series

Questions about MultiCharts and user contributed studies.
duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Signals on several data series

Postby duration » 28 May 2010

Hello,

Here is what I am running a simple signal on 2 data series:

Code: Select all

inputs: target(70),stoploss(70);

if marketposition = 0 and (xaverage(close,10) > xaverage(close,20)) and (xaverage(close data2,10) > xaverage(close data2,20)) then buy 1 contracts next bar open;

if marketposition = 0 and (xaverage(close,10) < xaverage(close,20)) and (xaverage(close data2,10) < xaverage(close data2,20)) then sellshort 1 contracts next bar open;

setstopcontract; setstoploss(stoploss); setprofittarget(target);
But it completely ignores the conditions on data 2...

Is there a problem with the above?

Many thanks,

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

Postby TJ » 28 May 2010

when dealing with data2,
use a variable and assign the value to it.

eg.

Code: Select all

var:
close.2(0),
avg10,2(0);

close.2 = close data2;

avg10.2 = xaverage(close.2, 10) ;

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

Hi TJ,

Thank you for your reply.

I am using the following:

Code: Select all

inputs: target(70),stoploss(70);

variables: close2(0),xavg10(0),xavg20(0);

close2 = close data2;

xavg10 = xaverage(close2,10);

xavg20 = xaverage(close2,20);

if marketposition = 0 and (xaverage(close,10) > xaverage(close,20)) and xavg10 > xavg20 then buy 1 contracts next bar open;

if marketposition = 0 and (xaverage(close,10) < xaverage(close,20)) and xavg10 < xavg20 then sellshort 1 contracts next bar open;

setstopcontract; setstoploss(stoploss); setprofittarget(target);
But it does not change anything..

Is there a reason for using a variable?

Many thanks,

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

Postby TJ » 28 May 2010

Hi TJ,
....
Is there a reason for using a variable?

Many thanks,

look at your original code...
it looks crowded;
ie: difficult to follow the logic
and difficult to debug.

in successful coding,
good logic is only half the battle,
good coding style is just as important.

what is good coding style?
code is easy to read,
the logic is easy to follow,
and the bugs are easy to track down.

variables allows you to off load some of the complicated logics
into a container.
so that you can deal with the details one by one.

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

Postby TJ » 28 May 2010

suggestion:

before you jump into coding buy/sell orders,
try the logic with a plot.

when you want to buy, plot a green dot on the chart,
when you want to sell, plot a red dot on the chart...

break down multiple conditions within a logic
into its components,
and see it you are getting a dot on when you want an order triggered.

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

Postby TJ » 28 May 2010

I would format the code this way for easy reading:

Code: Select all

inputs:
target(70),
stoploss(70);

variables:
close2(0),
xavg10(0),
xavg20(0);

{========== end of variables ==========}

close2 = close data2;

xavg10 = xaverage(close2,10);

xavg20 = xaverage(close2,20);

if marketposition = 0
and (xaverage(close,10) > xaverage(close,20))
and xavg10 > xavg20
then buy 1 contracts next bar open;

if marketposition = 0
and (xaverage(close,10) < xaverage(close,20))
and xavg10 < xavg20
then sellshort 1 contracts next bar open;

setstopcontract; setstoploss(stoploss);
setprofittarget(target);

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

Hi TJ,

Sorry for the confusion, you are well better organised than me!

I am plotting to xaverages, this is how I know it does not work..

The code is very simple though!

I don't know what could be wrong...

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

Postby TJ » 28 May 2010

Hi TJ,

Sorry for the confusion, you are well better organised than me!

I am plotting to xaverages, this is how I know it does not work..

The code is very simple though!

I don't know what could be wrong...
can you post a chart showing the plots?

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

Postby TJ » 28 May 2010

once the first logic is triggered,
your marketposition = 1
and your second logic will never be triggered
unless the profittarget or stoploss is hit.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

Hi TJ,

Here is the screenshot. I now use the below, but as you may see there is still short trades triggered although the ema 10 is above the ema 20..

Code: Select all

inputs:
target(70),
stoploss(70);

variables:
close2(0),
xavg10(0),
xavg20(0);

close2 = close data2;

xavg10 = xaverage(close2,10);

xavg20 = xaverage(close2,20);

if (xaverage(close,10) > xaverage(close,20))
and (xavg10 > xavg20) then buy 1 contracts next bar open;

if (xaverage(close,10) < xaverage(close,20))
and (xavg10 < xavg20) then sellshort 1 contracts next bar open;

setstopcontract; setstoploss(stoploss); setprofittarget(target);
Attachments
chart.png
(193.55 KiB) Downloaded 1478 times

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

Postby TJ » 28 May 2010

Hi TJ,

Here is the screenshot. I now use the below, but as you may see there is still short trades triggered although the ema 10 is above the ema 20..
...
can you circle the offending trades?

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

Hi TJ,

Well given the conditions. all the short trades are offending.

Since the ema10 is above the ema20, there should be only long trades.

Many thanks,
Attachments
chart.png
(227.19 KiB) Downloaded 1465 times

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

Postby TJ » 28 May 2010

are you using intrabarordergeneration?

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

No I am not. It does not change anything if I use it or not.

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

Postby TJ » 28 May 2010

you are jumping ahead...
you should plot the buy/sell first.
this would verify the integrity of your logic.

see attached for my example.

the chart is made up of 2 data streams (superimposed)
data1 = 1 min ES
data2 = 3 min ES

the blue line is 10MA on data1
the magenta line is 20MA on data1

the blue dotted line is 10MA on data2
the magenta dotted line is 20MA on data2

a blue cross indicates when the buy signal is met,
a red cross indicates when the sell signal is met.
(with the absence of marketposition=0)

you can then scrutinize the trigger bar by bar.
Attachments
duration-test1.png
(106.95 KiB) Downloaded 1477 times

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

I see. Thanks a lot for this suggestion.

I wrote the quick indicator below:

Code: Select all

variables:
close2(0),
xavg10(0),
xavg20(0);

close2 = close data2;

xavg10 = xaverage(close2,10);

xavg20 = xaverage(close2,20);

if (xaverage(close,10) > xaverage(close,20))
and (xavg10 > xavg20) then plot1(low,"",default,default,2) else noplot(1);

if (xaverage(close,10) < xaverage(close,20))
and (xavg10 < xavg20) then plot2(high,"",default,default,2) else noplot(2);
It displays the attached screenshot. I don't understand why there is still red crosses when xavg10 is well high above xavg20 :shock:
Attachments
chart2.png
(188.29 KiB) Downloaded 1468 times

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

Postby TJ » 28 May 2010

try it with average instead of xaverage.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

Changed all xaverage() to average(), and it is the same story... could this be a bug in MC 6 beta 3?

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

Postby TJ » 28 May 2010

I would break down the plot further:

Code: Select all

variables:
close2(0),
xavg10.1(0),
xavg20.1(0),
xavg10.2(0),
xavg20.2(0);

close2 = close data2;

xavg10.1 = xaverage(close,10);
xavg20.1 = xaverage(close,20);

xavg10.2 = xaverage(close2,10);
xavg20.2 = xaverage(close2,20);


if xavg10.1 > xavg20.1
then plot1(low,"B1",default,default,2);

if xavg10.2 > xavg20.2
then plot2(low-1,"B2",default,default,2);

if xavg10.1 < xavg20.1
then plot3(high,"S1",default,default,2);

if xavg10.2 < xavg20.2
then plot4(high+1,"S2",default,default,2);
you can then see exactly when and what is triggering.
Last edited by TJ on 28 May 2010, edited 1 time in total.

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

Postby TJ » 28 May 2010

Changed all xaverage() to average(), and it is the same story... could this be a bug in MC 6 beta 3?
don't ever even doubt for a moment there is a bug.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

I am using what you wrote (added "point" after the +1 & -1), and this is so strange there is candles with plots on both high and lows... and the condition says plot only if xavg10.2 > xavg20.2 ... how come? do you know what could be wrong?

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

Postby TJ » 28 May 2010

the problem lies in the timing of calculation...

in an indicator, the calculation is done in real time.

in a signal, the calculation is done at the end of the bar.


plot the attached and you will see the difference.
Attachments
2datastreams.txt
(396 Bytes) Downloaded 335 times

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 28 May 2010

So it plots differently if it is a variable or an input?

But why does it plot on the lows when the condition is false (xavg10 > xavg20)?

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

Postby TJ » 28 May 2010

So it plots differently if it is a variable or an input?
a variable can be set to evaluate every tick.

But why does it plot on the lows when the condition is false (xavg10 > xavg20)?
don't understand what you mean.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 30 May 2010

Hi TJ,

Here is what I do not understand:

I have written the following, which uses data1 (AVGXD1), and data2 (AVGXD2), and plots and data1:

Name: TEST

Code: Select all

variables:
closedata1(0), closedata2(0),
avg1d1(0), avg2d1(0),
avg1d2(0), avg2d2(0);

closedata1 = close ;

closedata2 = close data2;

avg1d1 = xaverage( closedata1, 10 ) ;

avg2d1 = xaverage( closedata1, 20 ) ;

avg1d2 = xaverage( closedata2, 10 ) ;

avg2d2 = xaverage( closedata2, 20 ) ;

plot1( avg1d1, "AVG1D1", yellow, default, 1 ) ;

plot2( avg2d1, "AVG2D1", darkyellow, default, 2 ) ;

plot3( avg1d2, "AVG1D2", cyan, default, 1 ) ;

plot4( avg2d2, "AVG2D2", darkcyan, default, 2 ) ;
and also the below, which uses data2 but plots on data1:

Name: TEST2

Code: Select all

variables:
avg1d1(0), avg2d1(0);

avg1d1 = xaverage( close, 10 ) ;

avg2d1 = xaverage( close, 20 ) ;

plot1( avg1d1, "AVG1D1", red, default, 1 ) ;

plot2( avg2d1, "AVG2D1", darkred, default, 2 ) ;
If you look on the screenshot, the values of TEST & TEST2 are not the same although they use the same exponential moving average, same period and same data..
Attachments
chart3.png
(149.93 KiB) Downloaded 1475 times

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

Postby TJ » 30 May 2010

you did not say what is your data1 and what is your data2.

instrument
resolution

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 30 May 2010

Data 1 is 6B, 5 Point.

Data 2 is 6B, 20 Point.

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

Postby TJ » 30 May 2010

try this:
make a chart with a single data stream,
and plot your MA on it.
This is your benchmark.

If your MA looks different on a multi-datastream chart,
that's because it is doing intrabar calculation on the faster fractal.


try this experiment:

put the slower fractal as data1
and faster fractal as data2,
you should see the MAs appear the same on both charts.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 31 May 2010

Hi TJ,

I don't understand what you mean by "fractal" in this context. I am only using xaverage().

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

Postby TJ » 31 May 2010

Hi TJ,

I don't understand what you mean by "fractal" in this context. I am only using xaverage().
fractal is another word for resolution,
or bar interval.

sorry for the confusion.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 31 May 2010

Hi TJ,

Thank you for your reply. It seems that the below is what I was looking for (if this can help anybody else).

Code: Select all

variables:
closedata1(0), closedata2(0),
avg1d1(0), avg2d1(0),
avg1d2(0), avg2d2(0);

closedata1 = close ;

closedata2 = close data2;

avg1d1 = xaverage( closedata1, 10 ) ;

avg2d1 = xaverage( closedata1, 20 ) ;

avg1d2 = xaverage( closedata2, 10 ) of data2;

avg2d2 = xaverage( closedata2, 20 ) of data2;

plot1( avg1d1, "AVG1D1", yellow, default, 1 ) ;

plot2( avg2d1, "AVG2D1", darkyellow, default, 2 ) ;

plot3( avg1d2, "AVG1D2", cyan, default, 1 ) ;

plot4( avg2d2, "AVG2D2", darkcyan, default, 2 ) ;

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

Postby Dave Masalov » 02 Jun 2010

Dear duration,

The function type series uses serial variables in its code (which refer to past values).
These variables change in the context of the data series on which the indicator is applied.

xaverage(c of data2,14);

Here data2 is taken for argument. At the same time the variables within xaverage are taken from the data series on which the indicator is applied (data1).

xaverage(c, 14) of data2;

Here data2 is taken for the function xaverage (for the internal variables and arguments)
-
Please change your code the following way:
-
variables:
closedata1(0), closedata2(0),
avg1d1(0), avg2d1(0),
avg1d2(0), avg2d2(0);

closedata1 = close ;

closedata2 = close data2;

avg1d1 = xaverage( closedata1, 10 ) ;

avg2d1 = xaverage( closedata1, 20 ) ;

avg1d2 = xaverage( closedata2, 10 ) of data2;

avg2d2 = xaverage( closedata2, 20 ) of data2;

plot1( avg1d1, "AVG1D1", yellow, default, 1 ) ;

plot2( avg2d1, "AVG2D1", darkyellow, default, 2 ) ;

plot3( avg1d2, "AVG1D2", cyan, default, 1 ) ;

plot4( avg2d2, "AVG2D2", darkcyan, default, 2 ) ;


Return to “MultiCharts”