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).
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.