Arrays/Lookback  [SOLVED]

Questions about MultiCharts and user contributed studies.
Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Arrays/Lookback

Postby Jonny473 » 20 Dec 2016

I have the following problem: In Easy Language sometimes something has to happen at the same bar for a buy/sell to trigger. In the example below a cross of 3MA's has to happen at the same bar to be valid. Of course I could use „>“ and „<“ instead of „crosses above“ and „crosses below“, but I want to point out the following: If I am not mistaken, you are able to „store“ the last MA crosses by using an array. The array defines the „lookback“ period?!
See the example code here for the MA crosses:

Code: Select all

Inputs:AllowSameDirectionEntries (true),
MA1_F_1(5),
MA1_F_2(20),
MA1_S_1(50),
MA1_S_2(100);


Variables:
ma1_fast_1(0,data2),ma1_slow_2(0),ma1_fast_2(0),ma1_slow_1(0);

array: Myarray1[100]( 0 ),Myarray2[100]( 0 ),Myarray3[100]( 0 ),Myarray4[100]( 0 );

Myarray1[100] = ma1_fast_1;
Myarray2[100] = ma1_fast_2;
Myarray3[100] = ma1_slow_1;
Myarray4[100] = ma1_slow_2;


ma1_fast_1=avg(Close,MA1_F_1);
ma1_fast_2=avg(Close,MA1_F_2);
ma1_slow_1=avg(Close,MA1_S_1);
ma1_slow_2=avg(Close,MA1_S_2);


if Marketposition=0 then begin



if ma1_fast_1 crosses above ma1_fast_2 and ma1_fast_1 crosses above ma1_slow_1 and ma1_fast_1 crosses above ma1_slow_2 then buy 1000 contract next bar market;


if ma1_fast_1 crosses below ma1_fast_2 and ma1_fast_1 crosses below ma1_slow_1 and ma1_fast_1 crosses below ma1_slow_2 then sellshort 1000 contract next bar market;
end;



if marketposition=1 then begin

if ma1_fast_1 crosses below ma1_fast_2 or ma1_fast_1 crosses below ma1_slow_1 or ma1_fast_1 crosses below ma1_slow_2 then sell 1000 contract next bar market;
end;

if marketposition=-1 then begin

if ma1_fast_1 crosses above ma1_fast_2 or ma1_fast_1 crosses above ma1_slow_1 or ma1_fast_1 crosses above ma1_slow_2 then buytocover 1000 contract next bar market;
end;



So what I ma trying to do: Whenever those crosses happened within the last [arraylength] bars a buy/ sell is triggered. So the 5 MA (red line) crosses above the white line 10 MA first and probably the yellow 50 MA last. This happens within a certain range of bars [arraylength] and the buy in this case would be triggered with the cross of the last MA (yellow line).

Image

My question is how can I define this in my example code?The MA crosses might not be the best example in that case, but if you combine different indicators and certain conditions have to be met first within the last [arraylength] bars before something is triggered, this indeed makes sense. Thanks for any help.

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

Re: Arrays/Lookback

Postby JoshM » 23 Dec 2016

So what I ma trying to do: Whenever those crosses happened within the last [arraylength] bars a buy/ sell is triggered. So the 5 MA (red line) crosses above the white line 10 MA first and probably the yellow 50 MA last. This happens within a certain range of bars [arraylength] and the buy in this case would be triggered with the cross of the last MA (yellow line).
I think it would be easier to not use an array, but just store the bar number of when the crossover happened. Then you can calculate how many bars ago that bar number happened, and that tells whether the crossover happened in the last 100 bars.

This has a couple of benefits I think:
- Easier to work with than managing an 100-element array.
- Probably quicker and more memory efficient, although it might be a tiny tiny effect.
- And easier to optimise on the number of bars in which a crossover should (or shouldn't) happen.

An example of what I mean is:

Code: Select all

Variables:
signalBarCondition1(0),
ma1_fast_1(0), ma1_fast_2(0);

ma1_fast_1=avg(Close,MA1_F_1);
ma1_fast_2=avg(Close,MA1_F_2);

if (ma1_fast_1 crosses over ma1_fast_2) then
signalBarCondition = CurrentBar;

if (CurrentBar - signalBarCondition < 100) then begin

// The crossover happened in the last 100 bars

end;

Jonny473
Posts: 68
Joined: 04 Apr 2016
Has thanked: 5 times
Been thanked: 1 time

Re: Arrays/Lookback  [SOLVED]

Postby Jonny473 » 23 Dec 2016

Hi Josh,

thanks for the answer. In the meantime I came up with an answer with loops. If somebody is interested let me know.


Return to “MultiCharts”