Issue using Data(2) with Signals  [SOLVED]

Questions about MultiCharts and user contributed studies.
flomtl
Posts: 6
Joined: 23 Jan 2014
Location: Montreal
Has thanked: 3 times

Issue using Data(2) with Signals

Postby flomtl » 11 Mar 2014

I have been trying to use multiple data series in signals and indicators.

I have currently loaded Data(1): 1min AUDCAD, Data(2) 20min AUDCAD

Plotting Studies is not an issue. I add the study to the format object screen and select the base data number and everything works as i want it to.

When it comes to Signals I cannot seem to get it to work.

Currently i am testing this with a 2line Moving average cross:

Code: Select all

inputs: Price(Close Data(2)), FastLength( 4 ), SlowLength( 75 );
variables: var0( 0 ), var1( 0 ) ;

var0 = AverageFC( price, FastLength ) ;
var1 = AverageFC( price, SlowLength ) ;

condition1 = CurrentBar > 1 and var0 crosses over var1 ;
if condition1 then
Buy ( "MA2CrossLE") next bar at market ;
Alert(Text("Averages crossed! BUY", var0, var1));

condition2 = CurrentBar > 1 and var0 crosses under var1 ;
if condition2 then
Sell Short ( "MA2CrossSE" ) next bar at market ;
I am assuming I am not placing the "Data(2)" in the right place.... I have also tried the following modification:

Code: Select all

var0 = AverageFC( Close Data(2), FastLength ) ;
var1 = AverageFC( Close Data(2), SlowLength ) ;
(in this case i had no input called price)

as i expected, both of these codes yielded the same results.

If someone could point me in the right direction that would be great.


Some additional related questions:
- When multiple data series are loaded on same instrument with different timescales does the trading (ie: order placement) need to be done on data1?
- Can I have a moving average cross on data2 trigger an order a different timescale?

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

Re: Issue using Data(2) with Signals  [SOLVED]

Postby TJ » 11 Mar 2014

I have been trying to use multiple data series in signals and indicators.
I have currently loaded Data(1): 1min AUDCAD, Data(2) 20min AUDCAD
Plotting Studies is not an issue. I add the study to the format object screen and select the base data number and everything works as i want it to.
When it comes to Signals I cannot seem to get it to work.
Currently i am testing this with a 2line Moving average cross:

Code: Select all

inputs: Price(Close Data(2)), FastLength( 4 ), SlowLength( 75 );
variables: var0( 0 ), var1( 0 ) ;
var0 = AverageFC( price, FastLength ) ;
var1 = AverageFC( price, SlowLength ) ;
condition1 = CurrentBar > 1 and var0 crosses over var1 ;
if condition1 then
Buy ( "MA2CrossLE") next bar at market ;
Alert(Text("Averages crossed! BUY", var0, var1));
condition2 = CurrentBar > 1 and var0 crosses under var1 ;
if condition2 then
Sell Short ( "MA2CrossSE" ) next bar at market ;
I am assuming I am not placing the "Data(2)" in the right place.... I have also tried the following modification:

Code: Select all

var0 = AverageFC( Close Data(2), FastLength ) ;
var1 = AverageFC( Close Data(2), SlowLength ) ;
(in this case i had no input called price)
as i expected, both of these codes yielded the same results.
If someone could point me in the right direction that would be great.
Some additional related questions:
- When multiple data series are loaded on same instrument with different timescales does the trading (ie: order placement) need to be done on data1?
- Can I have a moving average cross on data2 trigger an order a different timescale?
see post #5
viewtopic.php?f=16&t=6929


ps. try not to use generic variable names. ie var0, var1, etc. Giving each variable a meaningful name will save you time (and headache) in future debugging.

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

Re: Issue using Data(2) with Signals

Postby TJ » 11 Mar 2014

also try:

Code: Select all

if BARSTATUS(2)=2 then...

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

Re: Issue using Data(2) with Signals

Postby JoshM » 12 Mar 2014

I have been trying to use multiple data series in signals and indicators.

I have currently loaded Data(1): 1min AUDCAD, Data(2) 20min AUDCAD

Plotting Studies is not an issue. I add the study to the format object screen and select the base data number and everything works as i want it to.

When it comes to Signals I cannot seem to get it to work.
Good that you added the code to your question (makes helping easier), but what do you mean with 'issue' and 'cannot seem to get it work'? What results are you getting that you would not expect?

Btw, if you are comparing your indicators with your signal, have you made sure both have the same MaxBarsBack setting? (see for example here).
I am assuming I am not placing the "Data(2)" in the right place.... I have also tried the following modification:

Code: Select all

var0 = AverageFC( Close Data(2), FastLength ) ;
var1 = AverageFC( Close Data(2), SlowLength ) ;
(in this case i had no input called price)

as i expected, both of these codes yielded the same results.
If both yield the same results, how do you know it does not work? Perhaps both ways work. :)

The only apparent error that I can spot in your code is that you probably forgot a begin...end code block based on your indentation:

Code: Select all

if condition1 then begin
Buy ( "MA2CrossLE") next bar at market ;
Alert(Text("Averages crossed! BUY", var0, var1));
end;
Some additional related questions:
- When multiple data series are loaded on same instrument with different timescales does the trading (ie: order placement) need to be done on data1?
- Can I have a moving average cross on data2 trigger an order a different timescale?
- Yes, in regular MultiCharts trading can only be done on the first data series.
- Yes, then you'll need to reference data 2 in your script that is calculated on data 1.

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

Re: Issue using Data(2) with Signals

Postby JoshM » 12 Mar 2014

I have been trying to use multiple data series in signals and indicators.

I have currently loaded Data(1): 1min AUDCAD, Data(2) 20min AUDCAD

Plotting Studies is not an issue. I add the study to the format object screen and select the base data number and everything works as i want it to.

When it comes to Signals I cannot seem to get it to work.
Your problem is very likely caused by something else than `Data2` or `Data(2)` usage.

For example, if I run this test script on MultiCharts64 Version 8.8 Release (Build 8593):

Code: Select all

Variables:
MAFirstData(0),
MASecondData(0), x(0), y(0),
numDecimals(7);

once cleardebug;

if (BarStatus(1) = 2) and (LastBarOnChart_s = true) then begin

MAFirstData = AverageFC(Close Data1, 5);
MASecondData = AverageFC(Close Data2, 5);

// Checking the calculation for data 1
Print("Calculated MA Data1: ", NumToStr(MAFirstData, numDecimals));

for x = 0 to 4 begin

Print("Data1: ", NumToStr(Close[x] Data1, 4));
y = Close[x] Data1 + y;
end;

Print("Average for data 1: ", NumToStr(y/5, numDecimals));

Print(NewLine);

// Verifying the data for data 2
Print("Calculated MA Data2: ", NumToStr(MASecondData, numDecimals));

y = 0;
for x = 0 to 4 begin

Print("Data2: ", NumToStr(Close[x] Data2, 4));
y = Close[x] Data2 + y;
end;

Print("Average for data2: ", NumToStr(y/5, numDecimals));


// Or, alternatively, with Data(1) and Data(2)
Print(NewLine);
Print("Calculated MA Data(1): ",
NumToStr(AverageFC(Close Data(1), 5), numDecimals));
Print("Calculated MA Data(2): ",
NumToStr(AverageFC(Close Data(2), 5), numDecimals));

Print(NewLine);

// Or with DataN behind the MA
MAFirstData = AverageFC(Close, 5) Data1;
MASecondData = AverageFC(Close, 5) Data2;

Print("Calculated MA Data1: ", NumToStr(MAFirstData, numDecimals));
Print("Calculated MA Data2: ", NumToStr(MASecondData, numDecimals));

end; //: LastBarOnChart
Gives the following output:

Code: Select all

Calculated MA Data1: 1.3907200
Data1: 1.3905
Data1: 1.3902
Data1: 1.3909
Data1: 1.3910
Data1: 1.3910
Average for data 1: 1.3907200


Calculated MA Data2: 0.8369000
Data2: 0.8369
Data2: 0.8370
Data2: 0.8368
Data2: 0.8369
Data2: 0.8369
Average for data2: 0.8369000


Calculated MA Data(1): 1.3907200
Calculated MA Data(2): 0.8369000


Calculated MA Data1: 1.3907200
Calculated MA Data2: 0.8369000
Image


Morale of this example:
It does not matter how you reference a second data series. Both `Data2`, `Data(2)` and `Average(Close, 5) Data2` give the same results.
Attachments
scr.12-03-2014 19.16.23.png
(60.41 KiB) Downloaded 1055 times

flomtl
Posts: 6
Joined: 23 Jan 2014
Location: Montreal
Has thanked: 3 times

Re: Issue using Data(2) with Signals

Postby flomtl » 13 Mar 2014

Thank you all for your contributions and help on this topic!

The fixes you recommended worked and i have attached the code below as a reference in case someone faces similar issues.

This works:

Code: Select all

MAfast = AverageFC( Close, FastLength ) data2 ;
MAslow = AverageFC( Close, SlowLength ) data2 ;
this does not

Code: Select all

var0 = AverageFC( Close Data(2), FastLength ) ;
var1 = AverageFC( Close Data(2), SlowLength ) ;
full working code:

Code: Select all

inputs: Price(Close), FastLength( 20 ), SlowLength( 40 );
variables: MAfast( 0 ), MASlow( 0 ) ;

MAfast = AverageFC( price, FastLength ) data2 ;
MAslow = AverageFC( price, SlowLength ) data2 ;

condition1 = CurrentBar > 1 and MAfast crosses over MAslow ;
if condition1 then
Buy ( "MA2CrossLE") next bar at market ;


condition2 = CurrentBar > 1 and MAfast crosses under MAslow ;
if condition2 then
Sell Short ( "MA2CrossSE" ) next bar at market ;


Return to “MultiCharts”