

inputs: wkdy(1), wkdy2(5), adx_len(14), adx_threshold(30), ddff_on(1), num_contracts(4),tgt1(1), tgt2(2), tgt3(3), stop1(2.5), stop2(2), stop3(.5), num_t1(1),
 num_t2(1), num_t3(1), start_time(830), close_time(1445), 
delay(30), pct(.65), Price( Close ), FastLength( 9 ), SlowLength( 18 ), FastLength_s( 9 ), SlowLength_s( 18 ) ;
variables: FastAvg( 0 ), SlowAvg( 0 ), FastAvg_s( 0 ), SlowAvg_s( 0 ), ddff(0), ok2trade(false), mp(0), lt1_hit(false),
	lt2_hit(false),lt3_hit(false),st1_hit(false), st2_hit(false),st3_hit(false), adx_on_off(false);

mp = marketposition;
FastAvg = AverageFC( Price, FastLength ) ;
SlowAvg = AverageFC( Price, SlowLength ) ;

FastAvg_s = AverageFC( Price, FastLength_s ) ;
SlowAvg_s = AverageFC( Price, SlowLength_s ) ;


//remove the two forward slashes below to activate the function call
//ddff = ddf_pct_tk(start_time, delay, pct);

If time >= calctime(start_time,delay) then ok2trade = true;
If t >= close_time then ok2trade = false;

If adx(adx_len) > adx_threshold then adx_on_off = true;
If adx(adx_len) < adx_threshold then adx_on_off = false;

If volume <1000000 and t  > 1100 then ok2trade = false; // stops all trading if volume is less that 1000000 after 1100 am

If (dayofweek(d) = wkdy  or dayofweek(d) = wkdy2) then begin // 2 = tuesday  3 = wednesday 4 = thursday 5 = friday      6 saturday amd 7 sunday
	If ok2trade {and ddff_on = 1}  and adx_on_off = true then begin
		if CurrentBar > 1 and FastAvg crosses over SlowAvg {and ddff = 1} then
		{ CB > 1 check used to avoid spurious cross confirmation at CB = 1 }
			Buy ( "MA2CrossLE" ) num_contracts contracts next bar at market ;
		if CurrentBar > 1 and FastAvg_s crosses under SlowAvg_s  {and ddff = -1} then
		{ CB > 1 check used to avoid spurious cross confirmation at CB = 1 }
			Sell Short ( "MA2CrossSE" ) num_contracts contracts   next bar at market ;
	end;

	If ok2trade and ddff_on <> 1 and adx_on_off = true then begin
		if CurrentBar > 1 and FastAvg crosses over SlowAvg {and ddff = 1} then
		{ CB > 1 check used to avoid spurious cross confirmation at CB = 1 }
			Buy ( "MA2CrossLE " ) num_contracts contracts next bar at market ;
		if CurrentBar > 1 and FastAvg_s crosses under SlowAvg_s  {and ddff = -1} then
		{ CB > 1 check used to avoid spurious cross confirmation at CB = 1 }
			Sell Short ( "MA2CrossSE " ) num_contracts contracts   next bar at market ;
	end;
end; // end for dayof week bracket
// scale out targets
// long side
If mp = 1 then begin // long position
	sell("t1") next bar num_t1 contracts entryprice + tgt1 limit;
	sell("t2") next bar num_t2 contracts entryprice + tgt2 limit;
	sell("t3") next bar num_t3 contracts entryprice + tgt3 limit;
end;

//short side
If mp = -1 then begin // short position
	buy to cover("t1 ") next bar num_t1 contracts entryprice - tgt1 limit;
	buy to cover("t2 ") next bar num_t2 contracts entryprice - tgt2 limit;
	buy to cover("t3 ") next bar num_t3 contracts entryprice - tgt3 limit;
end;

//scale out stops or move stops after hit targets
// long side
If mp < 1 then begin // resets if mp = 0 or mp = -1
	lt1_hit = false;
	lt2_hit = false;
	lt3_hit = false;
end;

If mp = 1 then begin
	If high >= (entryprice+tgt1) then lt1_hit = true;
	If high >= (entryprice+tgt2) then lt2_hit = true;
	If high >= (entryprice+tgt3) then lt3_hit = true;	

	If lt1_hit then  sell("T1 stop") next bar entryprice - stop1 stop;
	If lt2_hit then sell("T2 stop") next bar entryprice - stop2 stop;
	If lt3_hit then sell("T3 stop") next bar entryprice - stop3 stop;
end;

// short side
If mp > -1 then begin // resets
	st1_hit = false;
	st2_hit = false;
	st3_hit = false;
end;


If mp = -1 then begin
	If low <= (entryprice-tgt1) then st1_hit = true;
	If low <= (entryprice-tgt2) then st2_hit = true;
	If low <= (entryprice-tgt3) then st3_hit = true;	

	If st1_hit then  buy to cover("T1 stop ") next bar entryprice + stop1 stop;
	If st2_hit then  buy to cover("T2 stop ") next bar entryprice + stop2 stop;
	If st3_hit then  buy to cover("T3 stop ") next bar entryprice + stop3 stop;
end;

//breakeven stop after hit target 1

//long side
input: toggle_be_tgt(1), be_plus(1);
vars: lng_tgt_1_hit(false),sht_tgt_1_hit(false);

If toggle_be_tgt = 1 then begin	
	If mp < 1 then lng_tgt_1_hit = false;  // reset
	If h >= entryprice + tgt1 then lng_tgt_1_hit = true;
	If lng_tgt_1_hit then sell( "Tgt1 BE ")next bar entryprice + be_plus stop;
	
	If mp > -1 then sht_tgt_1_hit = false;  // reset
	If l <= entryprice - tgt1 then sht_tgt_1_hit = true;
	If sht_tgt_1_hit then buy to cover( "Tgt1 BE") next bar entryprice - be_plus  stop;
end;


If ok2trade = false then begin	
	buy to cover("close time") this bar;
	sell ("close time ")this bar;
end;
setstopcontract;
//setstopposition;
setstoploss(150);

commentary(
"FastAvg = ",FastAvg,newline+
"SlowAvg = ",SlowAvg,newline+
"ok2trade = ",ok2trade,newline+
"entryprice = ",entryprice,newline+
"tgt1 = ",tgt1,newline+
"entryprice + tgt1 = ",entryprice + tgt1,newline+
"entryprice + tgt2 = ", entryprice + tgt2,newline
);

