Untitled

 avatar
unknown
plain_text
13 days ago
60 kB
9
Indexable
Using elsystem.collections;

using elsystem.io;
using elsystem.DrawingObjects;

Inputs:
	do_output( false ),
	directory( "C:\owncloud\sts_intraday\" ),
	limit_object_number( false ),
	allow_objects_after_x_bars( 1000 ),
	Chart_Prefix( "stch_shrt_2" ),
	manage_trades_with_STS( false ),
	use_wider_dvt_patch( 2 ),
	use_exit_stop_patch( 1 ),
	use_updated_sts_drawing( true ),
	file_prefix( "" ),
	buy_next_bar_market_EOD( 0 ),
	___Limit_Entries___( "" ),
	ues_limit_entries( 0 ),
	limit_entry_percent_padding( 0 ),
	___Cash_Management___( "" ),
	use_cash( true ),
	RoundToNShares( 2 ), 
	PercentCompounding( 0 ),
	StartCapital( 10000 ),
	buy_on_close( 1 ),
	round_to_big_point_for_stops( 0 ),
	___Profit_Targets___( "" ),
	use_percent_targets( 0 ),
	percent_target( 5.5 ),
	use_points_targets( 0 ),
	points_target( 10 ),
	percent_shares_for_target( 100 ),
	use_targets_after_x_bars( 6 ),
	___Stop_Settings___( "" ),
	use_stop_for_exit( 1 ),
	stop_padding_percent( 0.7 ),
	___Reentry_Settings___( "" ),
	allow_up_candle_reentry_on_close( 1 ),
	percent_above_for_reentry( 0.3 ),
	require_up_candle( 1 ),
	allow_trending_reentry( 0 ),
	___Index_Reentry___( "" ),
	do_index_reentry( 0 ),
	num_bars_back_for_high( 100 ),
	percent_decline_for_reentry( 25 ),
	____Wider_DVT___( "" ),
	use_wider_DVT_for_first_2( 0 ),
	____Second_DVT_Settings___( "" ),	
	prevent_second_DVT_under_ATR( false ),
	display_only_second( false ),
	display_both_with_hand_off( false ),
	use_debug( true ),
	____MA_Deluxe_1_Settings___( "" ),
	MA_1_Price( C ), 
	MA_1_Type( 8 ), 
	MA_1_Length( 26 ),
	____MA_Deluxe_2_Settings___( "" ),
	MA_2_Price( C ), 
	MA_2_Type( 8 ), 
	MA_2_Length( 15 ),
	____MA_Deluxe_3_Settings___( "" ),
	MA_3_Price( C ), 
	MA_3_Type( 8 ), 
	MA_3_Length( 14 ),
	____MA_Deluxe_4_Settings___( "" ),
	MA_4_Price( C ), 
	MA_4_Type( 8 ), 
	MA_4_Length( 58 ),
	___DVS_Indicator_Settings___( "" ),
	disable_bear( true ),
	use_override_label( true ),
	disable_starting( false ),
	override_label( "STS" ),
	overide_string_momo( "STS MOMO" ),
	overide_string_oversold( "STS OVERSOLD" ),
	override_label_2( "2vt" ),
	text_color_short( magenta ),
	line_color_short( "magenta" ),
	text_color_long( white ),
	line_color_long( "white" ),
	line_color_default_long( "yellow" ),
	int default_long_weight( 0.5 ),
	first_dvt_color( green ),
	second_dvt_color( cyan ),
	first_dvt_color_line( "green" ),
	second_dvt_color_line( "cyan" ),
	
	attention_color( magenta ),
	attention_line_color( "magenta" ),
	
	display_decimals( 2 ),
	use_auto_adjustment_for_decimals( false ),
	zero_decimal_price( 500 ),
	one_decimal_price( 100 ),
	two_decimal_price( 20 ),
	round_to_nearest_five( false ),
	display_price_on_text( true ),
	use_MACD_for_confirmation( false ),
	FastLength( 12 ), 
	SlowLength( 26 );

Vars: 
	GlobalDictionary GD(null),
	num_long_dvts(0),
	color_val(0),
	color_val_2(0),
	color_val_3(0),
	color_val_4(0),
	
	override_string(""),
	price_string(""),
	ID(-1),
	Offset( 0 ),
	MyMACD(0),
	ATR_inversion_bar(0),
	MACD_inversion_bar(0),
	force_start(false),
	
	DTpoint myDTPoint(null),
	VerticalLine myVLine(null),
	TrendLine myTrendLine1(null),
	
	num_decimals(0),
	last_text_price(0),
	
	Intrabarpersist has_drawn_second(false),
	did_oversold(false),
	app_type(0),
	app_optimizing(0),
	last_tp(0),
	nshares(0),
	stop_drawing(false),
	TickSize(0),
	numberOfTicks(0),
	NearestTick(0),
	NearestValidPrice(0),
	mpos(0),
	id_2(0),
	num_long_dvts_3(0),
	did_reentry(false);

method void AnalysisTechnique_Initialized( elsystem.Object sender, elsystem.InitializedEventArgs args ) 
begin	
	if getappinfo(aiOptimizing) < 1 then begin
		GD = GlobalDictionary.Create(false,"IES");
	end;
end;

Method string get_price_string(double tracked_price)
Begin
	num_decimals = display_decimals;
	If use_auto_adjustment_for_decimals then begin
		If tracked_price <= two_decimal_price then begin
			num_decimals = 2;
		End else if tracked_price <= zero_decimal_price then begin
			num_decimals = 1;
		End else begin
			num_decimals = 0;
		end;
	End;
	If round_to_nearest_five = true then begin
		price_string = numtostr(ceiling(round((tracked_price),display_decimals)/5)*5,num_decimals);
	End else begin 
		price_string = numtostr(round((tracked_price),display_decimals),num_decimals);
	end;
	Return price_string;
end;

method bool can_draw()
Begin
	commentarycl("Can Draw Function");
	commentarycl("mpos: ", mpos);
	commentarycl("num_long_dvts: ", num_long_dvts);
	
	//limit_object_number(false),
	//allow_drawing_objects_last_x_days(750),
	if (mpos = 1 or num_long_dvts = 1 or did_reentry) And
	app_type <> 2 and app_optimizing <> 1 and
	(limit_object_number = false or 
		Currentbar > allow_objects_after_x_bars ) then Begin
		return true;
	end;
	return false;
end;
var:
	temp_id(0),
	last_str(""),
	second_last_str(""),
	last_price(0.0),
	last_bar(0);
method int draw_text(string label, float price_point)
begin 
	commentarycl("label: ", label);
	commentarycl("price_point: ", price_point.ToString());
	//commentarycl("Draw Text function called");
	if (display_only_second = false and (display_both_with_hand_off and has_drawn_second=false))
	  or (display_only_second = false and display_both_with_hand_off = false)  then begin
		//don't run if radar screen, no strat logic should be here
	  	if can_draw() then begin
			temp_id = Text_New(Date[0], Time[0], price_point, label); 
			Text_SetColor(temp_id, attention_color);
			//commentarycl("Drew Text");
			//commentarycl("temp_id: ", temp_id);
		end;
	end else Begin
		temp_id = -1;
	end;
	if(second_last_str <> last_str) then begin
		second_last_str = last_str;
	end;
	last_str = label;
	last_price = price_point;
	last_bar = currentbar;
	return temp_id;
end;

method void draw_vertical_line(string line_color, int line_weight) Begin
	if app_type <> 2 and app_optimizing <> 1 and (mpos = 0 or mpos[1] = 0)  then begin 
		myDTPoint = DTPoint.create(BarDateTime, 0);
		myVLine = VerticalLine.create(myDTPoint);
		myVline.Color =  elsystem.drawing.Color.FromName(line_color);
		myVline.Weight = line_weight;
		DrawingObjects.Add(myVLine);		
	end;
end;

Method void draw_DVT(double dvt_price, double num_dvts, double num_dvts_pre, double last_tp, string line_color)
Begin
	commentarycl("Drawing DVT_Price: ", dvt_price);
	commentarycl("num_dvts: ", num_dvts);
	commentarycl("num_dvts_pre: ", num_dvts_pre);
	commentarycl("last_tp: ", last_tp);
	commentarycl("has_drawn_second: ", has_drawn_second);
	commentarycl("num_long_dvts_3: ", num_long_dvts_3);
	
		if myTrendLine1 <> Null then begin
			commentarycl("Trendline is not null");
			If num_dvts_pre = 1 then begin				
				myTrendLine1.Color = elsystem.drawing.Color.FromName(first_dvt_color_line);
			end;
			If num_dvts_pre = 2 then begin				
				myTrendLine1.Color = elsystem.drawing.Color.FromName(second_dvt_color_line);
			end;
			if num_dvts_pre > 2 then Begin
				myTrendLine1.Color = elsystem.drawing.Color.FromName(line_color);
			end;	
		end;
		//don't run if radar screen, no strat logic should be here
	if can_draw() then begin
		commentarycl("Can Draw Passed");
		If num_dvts_pre <> num_dvts or (mpos = 0) then begin
			commentarycl("Creating new DVT");
			myTrendLine1 = TrendLine.Create(DTPoint.Create(BarDateTime[1], dvt_price), DTPoint.Create(BarDateTime[0], dvt_price));
			myTrendLine1.Persist = True;
			myTrendline1.ExtRight = false;
			myTrendLine1.Style = 2;
			myTrendLine1.Color = elsystem.drawing.Color.FromName(attention_line_color);
			//myTrendLine1.SetStartPoint(DTPoint.Create(BarDateTime[2], dvt_price));
			//myTrendLine1.SetEndPoint(DTPoint.Create(BarDateTime[0], dvt_price));
			DrawingObjects.Add(myTrendLine1);
		end else begin
			if myTrendLine1 <> Null then begin
				If last_tp = dvt_price then begin
					myTrendLine1.SetEndPoint(DTPoint.Create(BarDateTime[0], dvt_price));
				end;
			end;
		end;
	end;
end;

input: 
	___Wider_ATR___( "" ),
	display_other_ATRS( true ),
	limit_wider_using_200( 1 ),
	wider_atr_length( 155 ),
	above_200_for_x_bars( 75 ),
	below_200_for_x_bars( 13 ),
	___ATR_SETTINGS___( "" ),
	use_atr( true ),
	Length( 13 ), 
	Multi( 0.6 ), 
	percent_above_for_trigger( 0 ),
	percent_off_atr( 0.9 ),
	percent_below_atr( 1.9 ),
	percent_above_atr( 0.2 ),
	update_DVT_if_X_percent_above_ATR( true ),
	percent_above_DVT_for_update( 2.3 ),
	use_dynamic_percent_for_update( false ),
	use_true_range_for_calc( true ),
	use_bar_range_for_calc( false ),
	average_length( 100 ),
	dynamic_factor( 1 ),
	update_DVT_if_X_percent_below_ATR( true ),
	percent_below_DVT_for_update( 4.6 ), 
	points_padding( 0 ),
	percent_padding( 0 );
 
var: 
	atrs(0),
	stph(0),
	stpl(0);
	
 Method void CalcATR()
 begin
 	if use_atr then begin
		atrs=ATRtrailingstop2(c, c, length, multi, stph, stpl);
	end;
end;

input: 
	___ATR_SETTINGS____3( "" ),
	use_atr_3( true ),
	show_second_dvt( true ),
	Length_3( 35 ), 
	Multi_3( 0.75 ), 
	percent_above_DVT_for_update_3( 1.2 );
 
var: 
	atrs_3(0),
	stph_3(0),
	stpl_3(0);
 
 Method void CalcATR_3()
 begin
 	if use_atr_3 then begin
		atrs_3=ATRtrailingstop2(c,c,length_3,multi_3,stph_3,stpl_3);
	end;
end;


input: 
	___ATR_SETTINGS____4( "" ),
	use_atr_4( true ),
	Length_4( 34 ), 
	Multi_4( 4.5 ), 
	percent_below_atr_4( 0 );
 
var: 
	atrs_4(0),
	stph_4(0),
	stpl_4(0);
 
 Method void CalcATR_4()
 begin
 	if use_atr_4 then begin
		atrs_4=ATRtrailingstop2(c,c,length_4,multi_4,stph_4,stpl_4);
	end;
end;

vars: 
		last_tracked(0),
	above_200_ma(0),
	num_above_200_ma(0),
	num_below_200_ma(0),
	above_200_ok(true);

method void Plot_ATRS()
Begin
	if limit_wider_using_200 = 1 then Begin
		above_200_ma = average(c, wider_atr_length);
		if c > above_200_ma then Begin
			num_above_200_ma = num_above_200_ma + 1;
			num_below_200_ma = 0;
		end;
		if c < above_200_ma then Begin
			num_below_200_ma = num_below_200_ma + 1;
			num_above_200_ma = 0;
		end;
		if num_above_200_ma > above_200_for_x_bars then Begin
			above_200_ok = true;
		end;
		if num_below_200_ma > below_200_for_x_bars then Begin
			above_200_ok = false;
		end;
	end;
	//commentarycl("above_200_ok: ", above_200_ok);
	//commentarycl("num_above_200_ma: ", num_above_200_ma);
	If BarNumber > Length then begin 
		//if atrs=-1 then plot1(stph,"Short Stop");
	 	//if atrs=1 and (atrs_3 <> 1 or display_other_ATRS = false) then plot2(stpl,"Long Stop");
	end; 
	if display_other_ATRS and above_200_ok then begin
		If BarNumber > Length_3 then begin 
 			//if atrs_3=1 then plot6(stpl_3,"Long Stop 3");
			if stpl_3 <> 0 and atrs_3 = 1 and show_second_dvt and stop_drawing = false then begin
				if last_tracked < stpl_3 * (1-(percent_above_DVT_for_update_3/100)) then begin
					last_tracked = stpl_3;
					if round_to_big_point_for_stops = 1 then Begin
						TickSize = MinMove / PriceScale * BigPointValue;
						NumberOfTicks = last_tracked / TickSize;
						NearestTick = Round(NumberOfTicks, 0);
						last_tracked = NearestTick * TickSize;
					end;
					num_long_dvts_3 = num_long_dvts_3 + 1;
				end;
				override_string = override_label_2 + ": " + get_price_string(last_tracked);
				//commentarycl("OVER RIDE STRING%%%%%%%%: ",override_string); 
				if num_long_dvts_3 <> num_long_dvts_3[1] then begin
					if last_tracked > 0 then begin
						//don't run if radar screen, no strat logic should be here
						if can_draw()  then begin
							//id_2 = Text_New(Date[0], Time[0], last_tracked, override_string);
							last_bar = currentbar; 
						end;
					end;
				end;
			end;
		end;
	end;
	commentarycl("atrs_3:", atrs_3);
	commentarycl("stpl_3:", stpl_3);
	if atrs_3 = -1 then Begin
		last_tracked = 0;
	end;
	if atrs_3 = -1 and num_long_dvts_3 > 0 and use_wider_dvt_patch = 1 then Begin
		last_tracked = last_tracked[1];
		commentarycl("Setting Last Tracked: ", last_tracked);
	end;
end;

vars: 
	current_low_track(0),
	previous_low_track(0),
	current_high_track(0),
	previous_high_track(0);

Method void calc_low_for_trailing()
Begin
	MyMACD = macd(c, fastlength, slowlength);
	If atrs <> atrs[1] then begin
		ATR_inversion_bar = currentbar;
	end;
	If  (mymacd > 0 and mymacd[1] < 0) or
	   (mymacd < 0 and mymacd[1] > 0) then begin
		
		macd_inversion_bar = currentbar;
	end;
	If color_val = -1 and 
		(
			color_val[1] = 1 or
			l < current_low_track
		)
	then begin
		if use_atr and stpl <> 0 then begin
			If percent_off_atr < ((c - stpl) / stpl)*100 and atrs=1 then begin
				current_low_track = stpl * (1-(percent_below_atr/100));
			end else begin
				current_low_track = low;
			end;
		end else begin
			current_low_track = low;
		end;
	end;
	If color_val = -1 and color_val[1] = 1 
	then begin
		previous_high_track = current_high_track[1];
	end;
	If color_val = 1 and 
		(
			color_val[1] = -1 or
			h > current_high_track
		)
	then begin
		if use_atr and stph <> 0 then begin
			If percent_off_atr > ((stph - c) / stph)*100 and atrs=-1 then begin
				current_high_track = stph * (1+(percent_above_atr/100));
			end else begin
				current_high_track = high;
			end;
		end else begin
			current_high_track = low;
		end;
	end;
	If color_val = 1 and color_val[1] = -1 
	then begin
		previous_high_track = current_high_track[1];
	end;
end;


inputs: 
	use_stoch_agreement( 0 ),
	stoch_agreement_length( 34 ),
	stoch_agreement_level( 76 );

vars: 
	has_been_red(false),
	has_been_red_twice(false),
	stop_padding(0),
	last_stop_drawn(0),
	DTPoint myDTPoint1(null), 
	DTPoint myDTPoint2(null),
	text_color(red),
	continuing_to_draw_long(false),
	continuing_to_draw_short(false),
	last_stop_low(0),
	last_stop_high(0),
	drawing_low(false),
	drawing_high(false),
	force_start_price(0),
	first_bear_bar(0),
	num_short_dvts(0),
	first_short_dvts_id(0),
	second_short_dvts_id(0),
	first_long_dvts_id(0),
	second_long_dvts_id(0),
	tracked_range(0);

method void update_colors()
Begin
	//don't run if radar screen, no strat logic should be here
	if app_type <> 2 and app_optimizing <> 1 and id[1] > 0  then begin
		If num_long_dvts[1] = 1 and display_only_second = false and display_both_with_hand_off=false then begin			
			Text_SetColor(ID[1], first_dvt_color);
		end;
		If num_long_dvts[1] = 2 and display_only_second = false and display_both_with_hand_off=false then begin			
			Text_SetColor(ID[1], second_dvt_color);
		end;		
		If num_long_dvts[1] > 2 and display_only_second = false and display_both_with_hand_off=false then begin
			Text_SetColor(ID[1], text_color_long); 
		end;
	end;
end;
	
Method void place_trailing_stops()
Begin
	update_colors();
	
	If points_padding > 0 then begin
		stop_padding = points_padding;
	end;
	If has_been_red and color_val[1] = 1 and color_val = -1 and
		(use_stoch_agreement = 0 or fastk(stoch_agreement_length) < stoch_agreement_level)
	then begin
		has_been_red_twice = true;	
	end;
	If color_val = -1 and (use_stoch_agreement = 0 or fastk(stoch_agreement_length) < stoch_agreement_level) then begin
		has_been_red = true;
	end;
	
	has_been_red = true;
	If continuing_to_draw_short and c < last_tracked then begin
		num_long_dvts = 0;
		if mpos = 0 then begin
			num_long_dvts_3 = 0;
		end;
		has_drawn_second = false;
	end;
	If continuing_to_draw_long then begin
		num_short_dvts = 0;
	end;
	
	If use_true_range_for_calc then begin
		tracked_range = average(TrueRange, average_length);
	end;
	If use_bar_range_for_calc then begin
		tracked_range = average(high-low, average_length);
	end;
	
	If percent_padding > 0 then begin
		stop_padding = current_low_track * points_padding/100;
	end;
	if color_val <> color_val[1] then begin
		continuing_to_draw_short = false;
		continuing_to_draw_long = false;
	end;
	//commentarycl("atrs_4: ", atrs_4);
	//commentarycl("use_atr_4: ", use_atr_4);
	//commentarycl("atrs: ", atrs);
	if atrs_4 = 1 and use_atr_4 and atrs <> 1 then Begin
		//commentarycl("Setting CLT to : ", stpl_4);
		current_low_track = stpl_4 * (1-(percent_below_atr_4/100));
	end;
	If ((color_val = 1 and has_been_red) or force_start or (c > force_start_price and force_start_price > 0)) and stop_drawing = false then begin
		if prevent_second_DVT_under_ATR = false or atrs = 1 or num_long_dvts <> 1 then begin
			if ((disable_starting = false or (current_low_track - stop_padding > last_stop_drawn))
			And (use_atr = false or (atrs = 1 and c > h[currentbar - ATR_inversion_bar] * (1 + percent_above_for_trigger/100)))
			And (use_MACD_for_confirmation = false or (MyMACD > 0 and c > h[currentbar - macd_inversion_bar])))
			or continuing_to_draw_long = true
			Or force_start 
			or (c > force_start_price and force_start_price > 0) then begin
				If update_DVT_if_X_percent_above_ATR then begin
					{
					dynamic_factor(1),}
					If use_dynamic_percent_for_update = false then begin
						If current_low_track - points_padding < stpl * (1-(percent_above_DVT_for_update/100)) then begin
							current_low_track = stpl * (1-(percent_below_atr/100));
							continuing_to_draw_long = false;
							force_start_price = 0;
						end;
					End else begin
						If current_low_track - points_padding < stpl * (1 - ((tracked_range/c)*dynamic_factor)) then begin
							current_low_track = stpl * (1-((tracked_range/c)*dynamic_factor));
							continuing_to_draw_long = false;
							force_start_price = 0;
						end;
					end;
				end;
				NearestValidPrice = current_low_track - stop_padding * (1 - stop_padding_percent/100);

				if round_to_big_point_for_stops = 1 then Begin
					TickSize = MinMove / PriceScale * BigPointValue;
					NumberOfTicks = (current_low_track - stop_padding * (1 - stop_padding_percent/100)) / TickSize;
					NearestTick = Round(NumberOfTicks, 0);
					NearestValidPrice = NearestTick * TickSize;
				end;
				override_string = override_label + ": " + get_price_string(NearestValidPrice* (1 - stop_padding_percent/100));
				If (continuing_to_draw_long = false and override_string <> override_string[1]) then begin			
					If ((disable_starting = false or (NearestValidPrice > last_stop_drawn)) 
					And (use_atr = false or (atrs = 1 and c > h[currentbar - ATR_inversion_bar] * (1 + percent_above_for_trigger/100)))
					And (use_MACD_for_confirmation = false or (MyMACD > 0 and c > h[currentbar - macd_inversion_bar]))) 
					and last_stop_drawn = NearestValidPrice and num_long_dvts = 0 then begin
						draw_vertical_line(line_color_default_long, default_long_weight);
					end;
					if num_long_dvts = 0 and mpos = 0 then Begin
						num_long_dvts_3 = 0;
					end;
					num_long_dvts = num_long_dvts + 1;
					num_short_dvts = 0;
					last_text_price = NearestValidPrice;
					commentarycl("Setting Text");
					commentarycl("num_long_dvts_3: ", num_long_dvts_3);
					commentarycl("NearestValidPrice: ", NearestValidPrice* (1 - stop_padding_percent/100));
					if num_long_dvts_3 = 0 then begin
						commentarycl("Drawing Text");
						if did_oversold then Begin
							//id = draw_text(overide_string_oversold + ": " + price_string, NearestValidPrice* (1 - stop_padding_percent/100));
						end else if color_val <> color_val[1] then begin
							//id = draw_text(overide_string_momo + ": " + price_string, NearestValidPrice* (1 - stop_padding_percent/100));
						end else Begin
							//id = draw_text(override_string, NearestValidPrice* (1 - stop_padding_percent/100));
						end;
					end;
					//commentarycl("Text ID: ", id);
				end;	
				if num_long_dvts = 1 and num_long_dvts[1] = 0 and force_start = false then Begin
							draw_vertical_line(line_color_default_long, default_long_weight);
						end;
				//draw_DVT(NearestValidPrice, num_long_dvts, num_long_dvts[1], last_text_price, line_color_long);		
	
				continuing_to_draw_long = true;
				continuing_to_draw_short = false;		
				drawing_low = false;
				drawing_high = true;
				first_bear_bar = 0;
			end;	
		end;
		last_stop_drawn = NearestValidPrice;
		last_stop_low = NearestValidPrice;
	end;
	If color_val = -1 and has_been_red and (c < force_start_price or force_start_price = 0) then begin
		if ((disable_starting = false or (current_high_track + stop_padding < last_stop_high))
		   And (use_atr = false or (atrs = -1 and c < l[currentbar - ATR_inversion_bar]))
		   And (use_MACD_for_confirmation = false or (MyMACD < 0 and c < l[currentbar - macd_inversion_bar]))
		   And l < last_stop_low)
		   or continuing_to_draw_short = true or (drawing_low and (current_high_track + stop_padding < last_stop_high)) then begin
			If update_DVT_if_X_percent_below_ATR then begin
				If current_high_track + points_padding > stph * (1+(percent_below_DVT_for_update/100)) then begin
					current_high_track = stph * (1+(percent_above_atr/100));
					continuing_to_draw_short = false;
				end;
			end;
			force_start_price = 0;
			//don't run if radar screen, no strat logic should be here
			if app_type<> 2 and app_optimizing <> 1  then begin 
				//myTrendLine1 = TrendLine.Create(DTPoint.Create(BarDateTime[0],current_high_track + stop_padding),DTPoint.Create(BarDateTime[1],current_high_track + stop_padding));
				//myTrendLine1.Persist = False;
				//myTrendline1.ExtRight = False;
				//myTrendLine1.Style = 2;
				//myTrendLine1.Color = elsystem.drawing.Color.FromName(line_color_short);
			end;
			If round_to_nearest_five = true then begin
				price_string = numtostr(ceiling(round((current_high_track + stop_padding),display_decimals)/5)*5,display_decimals);
			End else begin 
				price_string = numtostr(round((current_high_track + stop_padding),display_decimals),display_decimals);
			end;
			override_string = override_label + ": " + price_string;
			If continuing_to_draw_short = false and  disable_bear = false and last_stop_high > current_high_track + stop_padding and override_string <> override_string[1] then begin
				if mpos = 0 then begin
					num_long_dvts_3 = 0;
				end;
				num_long_dvts = 0;
				has_drawn_second = false;
				num_short_dvts = num_short_dvts + 1;
				//id = draw_text(override_string, current_high_track + stop_padding);
				
				If num_short_dvts = 1 then begin				
					first_short_dvts_id = id;
				end;
				If num_short_dvts = 2 then begin
					second_short_dvts_id = id;
				end;
			end;
			//don't run if radar screen, no strat logic should be here
			if disable_bear = false and app_type <> 2 and app_optimizing <> 1  then begin
				//DrawingObjects.Add(myTrendLine1);
			end;
			continuing_to_draw_long = false;
			continuing_to_draw_short = true;	
			drawing_low = true;
			drawing_high = false;
		end;	
		last_stop_high = current_high_track + stop_padding;
	end;
	If (last_tracked > 0 and c < last_tracked and above_200_ok) or ((last_tracked = 0 or above_200_ok = false) and c < last_text_price) then begin
		If first_bear_bar = 0 then begin
			first_bear_bar = currentbar;
		end;
		num_long_dvts = 0;
		if mpos = 0 then begin
			num_long_dvts_3 = 0;
		end;
	end;
	If mpos = 1 and color_val = -1 and (use_stoch_agreement = 0 or fastk(stoch_agreement_length) < stoch_agreement_level) then begin
		If percent_padding > 0 then begin
			stop_padding = previous_low_track * points_padding/100;
		end;
	end;
end;

inputs:
	int Moving_Average_Type( 8 ),
	int MACD_FastLength( 9 ), { the shorter of the two exponential moving average
	 lengths used to calculate the MACD value, in bars }
	int MACD_SlowLength( 26 ), { the longer of the two exponential moving average
	 lengths used to calculate the MACD value, in bars }
	int MACD_MACDLength( 8 ), { the number of bars over which to exponentially average
	 the MACD value }
	MACD_Adap_EFF_Length( 3 ), 
	MACD_Adap_Fast_Length( 3 ), 
	MACD_Adap_Slow_Length( 3 );
		
variables:
	double entryMACD( 0 ),
	double entryMACDAvg( 0 ),
	double entrymacd_val( 0 );

Method void calc_BPT_MACDi()
begin
	entryMACD = MACD( Close, MACD_FastLength, MACD_SlowLength ) ;
	entrymacd_val = entryMACD;

	switch(Moving_Average_Type) begin
	Case 1:
		entryMACDAvg = Average(entrymacd_val, MACD_MACDLength);
	Case 2:
		entryMACDAvg = XAverage(entrymacd_val, MACD_MACDLength);
	Case 3:
		entryMACDAvg = WAverage(entrymacd_val, MACD_MACDLength);
	Case 4:
		entryMACDAvg = TriAverage(entrymacd_val, MACD_MACDLength);
	Case 5:
		entryMACDAvg = AdaptiveMovAvg(entrymacd_val, MACD_Adap_EFF_Length, MACD_Adap_Fast_Length, MACD_Adap_Slow_Length);
	Case 6:
		entryMACDAvg = bptHMA(entrymacd_val, MACD_MACDLength);
	Case 7:
		entryMACDAvg = bptEHMA(entrymacd_val, MACD_MACDLength);
	Case 8:
		entryMACDAvg = bptEOHMA(entrymacd_val, MACD_MACDLength);
	end;
end;

inputs: 
		____Agressive_Oversold_Entry___( "" ),
		use_aggressive_oversold_entry( true ),
		sma_length( 62 ),
		sma_length_2_for_close( 9 ),
		num_bars_below_sma( 32 ),
		percent_candle_body( 15 ),
		size_of_candle( 0.7 ),
		oversold_vertical_color( "white" ),
		int aggressive_vertical_weight( .5 );

vars: 	
		num_days_below(0),
		sma_val(0),
		sma_2_val(0);
		
Method void check_inversion()
Begin
	did_oversold = false;
	sma_val = average(c, sma_length);
	sma_2_val = average(c, sma_length_2_for_close);
	If c < sma_val then begin
		num_days_below = num_days_below + 1;
	End else begin
		num_days_below = 0;
	end;
	If h-l <> 0 and c <> 0 then begin
		If (c > sma_val or c > sma_2_val) and 
	   	num_days_below[1] >= num_bars_below_sma and
	   	(C - O)/(H - L) > percent_candle_body * .01 and
	   	(H - L)/C > size_of_candle * .01 and
	   	color_val_2 = 1 and color_val_2[1] = -1 and
	   	entrymacd_val > entryMACDAvg and (entrymacd_val[1] < entryMACDAvg[1] or entrymacd_val[2] < entryMACDAvg[2])
	   	and use_aggressive_oversold_entry
	   	then begin
			draw_vertical_line(oversold_vertical_color, aggressive_vertical_weight);
			force_start = true;
			force_start_price = c;
			did_oversold = true;
			commentarycl("Did Oversold");
		end;
	end;
end;

inputs: ____ABC_PULLBACK___( "" ),
		use_ABC_pullback( true ),
		long_MA_filter( 310 ),
		percent_above_long_MA( 0 ),
		c_wave_min_length( 3 ),
		ABC_vertical_color( "cyan" ),
		int ABC_weight( 0.5 ),
		num_bars_to_keep_ABC_alive( 36 ),
		allow_new_ABC_without_crossing_ATR( true );
		
vars: 
	counter(0),
	tracked_changes(0),
	long_ma_val(0),
	c_wave_length(0),
	bars_tracked_at_four(0),
	did_abc(false);

Method void how_many_color_changes()
Begin
	long_ma_val = average(c, long_MA_filter);
	if first_bear_bar > 0 then begin
		If color_val_3 <> color_val_3[1] then begin 
			tracked_changes = tracked_changes + 1;
		end;
		If tracked_changes = 3 then begin
			c_wave_length = c_wave_length + 1;
		end;
	End else begin
		tracked_changes = 1;
		c_wave_length = 0;
	end;
	If atrs = 1 then begin
		did_abc = false;
	end;
	If tracked_changes = 4 then begin
		bars_tracked_at_four = bars_tracked_at_four + 1;
	End else begin
		bars_tracked_at_four = 0;
	end;
	If tracked_changes = 4 and use_ABC_pullback and
	   C > long_ma_val * (1 + (percent_above_long_MA *.01))
	   And c_wave_length >= c_wave_min_length
	   And (bars_tracked_at_four <= num_bars_to_keep_ABC_alive)
	   And (allow_new_ABC_without_crossing_ATR or did_abc = false)
       then begin   
	   	draw_vertical_line(ABC_vertical_color, ABC_weight);
		num_long_dvts = 0;
		num_long_dvts_3 = 0;
		has_drawn_second = false;
		force_start = true;
		force_start_price = c;
		did_abc = true;
	end;
end;


inputs: ____ABC_PULLBACK_2___( "" ),
		use_ABC_pullback_2( true ),
		long_MA_filter_2( 350 ),
		percent_above_long_MA_2( 1.9 ),
		c_wave_min_length_2( 13 ),
		ABC_vertical_color_2( "pink" ),
		int ABC_weight_2( 0.5 ),
		num_bars_to_keep_ABC_alive_2( 53 ),
		allow_new_ABC_without_crossing_ATR_2( true );
		
vars: 
	counter_2(0),
	tracked_changes_2(0),
	long_ma_val_2(0),
	c_wave_length_2(0),
	bars_tracked_at_four_2(0),
	did_abc_2(false);

Method void how_many_color_changes_2()
Begin
	long_ma_val_2 = average(c, long_MA_filter_2);
	if first_bear_bar > 0 then begin
		If color_val_4 <> color_val_4[1] then begin 
			tracked_changes_2 = tracked_changes_2 + 1;
		end;
		If tracked_changes = 3 then begin
			c_wave_length_2 = c_wave_length_2 + 1;
		end;
	End else begin
		tracked_changes_2 = 1;
		c_wave_length_2 = 0;
	end;
	If atrs = 1 then begin
		did_abc_2 = false;
	end;
	If tracked_changes_2 = 4 then begin
		bars_tracked_at_four_2 = bars_tracked_at_four_2 + 1;
	End else begin
		bars_tracked_at_four_2 = 0;
	end;
	If tracked_changes_2 = 4 and use_ABC_pullback_2 and
	   C > long_ma_val_2 * (1 + (percent_above_long_MA_2 *.01))
	   And c_wave_length_2 >= c_wave_min_length_2
	   And (bars_tracked_at_four_2 <= num_bars_to_keep_ABC_alive_2)
	   And (allow_new_ABC_without_crossing_ATR_2 or did_abc_2 = false)
       then begin
   		draw_vertical_line(ABC_vertical_color_2, ABC_weight);
		num_long_dvts = 0;
		num_long_dvts_3 = 0;
		has_drawn_second = false;
		force_start = true;
		force_start_price = c;
		did_abc_2 = true;
	end;
end;


method void debug()
Begin
	if true then begin
		{commentarycl("____Second_DVT_Settings___(): ", ____Second_DVT_Settings___);	
		commentarycl("prevent_second_DVT_under_ATR: ", prevent_second_DVT_under_ATR);
		commentarycl("display_only_second: ", display_only_second);
		commentarycl("display_both_with_hand_off: ", display_both_with_hand_off);
		commentarycl("____MA_Deluxe_1_Settings___: ",____MA_Deluxe_1_Settings___);
		commentarycl("MA_1_Price: ", MA_1_Price);
		commentarycl("MA_1_Type: ", MA_1_Type);
		commentarycl("MA_1_Length: ", MA_1_Length);
    	commentarycl("Use_MA_1_Smoothing: ", false);
    	commentarycl("MA_1_Smoothing: ", false);
    	commentarycl("MA_1_Percent_smoothing: ", false);
    	commentarycl("use_whipsaw_filter_MA_1: ", false);
    	commentarycl("use_whipsaw_smoothing_MA_1: ", false);
    	commentarycl("whipsaw_smoothing_MA_1: ", false);
    	commentarycl("whipsaw_smoothing_MA_1_percent: ", false);
    	commentarycl("use_reversal_date_for_smoothing: ", false);
		commentarycl("display_ma1_reversal: ", false);
		commentarycl("use_num_bars_confirmation: ", false);
		commentarycl("num_bars_green: ", false);
		commentarycl("num_bars_red: ", false);
		commentarycl("____MA_Deluxe_2_Settings___: ", ____MA_Deluxe_2_Settings___);
		commentarycl("MA_2_Price: ", MA_2_Price);
		commentarycl("MA_2_Type: ", MA_2_Type);
		commentarycl("MA_2_Length: ", MA_2_Length);
    	commentarycl("MA_2_Disable_Color: ", false);
    	commentarycl("Use_MA_2_Smoothing: ", false);
    	commentarycl("MA_2_Smoothing: ", false);
    	commentarycl("MA_2_Percent_smoothing: ", false);
    	commentarycl("use_whipsaw_filter_MA_2: ", false);
    	commentarycl("use_whipsaw_smoothing_MA_2: ", false);
    	commentarycl("whipsaw_smoothing_MA_2: ", false);
    	commentarycl("whipsaw_smoothing_MA_2_percent: ", false);
    	commentarycl("use_reversal_date_for_smoothing_2: ", false);
		commentarycl("display_ma2_reversal: ", false);
		commentarycl("use_num_bars_confirmation_2: ", false);
		commentarycl("num_bars_green_2: ", false);
		commentarycl("num_bars_red_2: ", false);
		commentarycl("____MA_Deluxe_3_Settings___: ", ____MA_Deluxe_3_Settings___);
		commentarycl("MA_3_Price: ", MA_3_Price);
		commentarycl("MA_3_Type: ", MA_3_Type);
		commentarycl("MA_3_Length: ", MA_3_Length);
    	commentarycl("MA_3_Disable_Color: ", false);
    	commentarycl("Use_MA_3_Smoothing: ", false);
    	commentarycl("MA_3_Smoothing: ", false);
    	commentarycl("MA_3_Percent_smoothing: ", false);
    	commentarycl("use_whipsaw_filter_MA_3: ", false);
    	commentarycl("use_whipsaw_smoothing_MA_3: ", false);
    	commentarycl("whipsaw_smoothing_MA_3: ", false);
    	commentarycl("whipsaw_smoothing_MA_3_percent: ", false);
    	commentarycl("use_reversal_date_for_smoothing_3: ", false);
		commentarycl("display_ma3_reversal: ", false);
		commentarycl("use_num_bars_confirmation_3: ", false);
		commentarycl("num_bars_green_3: ", false);
		commentarycl("num_bars_red_3: ", false);
		commentarycl("____MA_Deluxe_4_Settings___: ", ____MA_Deluxe_4_Settings___);
		commentarycl("MA_4_Price: ", MA_4_Price);
		commentarycl("MA_4_Type: ", MA_4_Type);
		commentarycl("MA_4_Length: ", MA_4_Length);
    	commentarycl("MA_4_Disable_Color: ",false);
    	commentarycl("Use_MA_4_Smoothing: ",false);
    	commentarycl("MA_4_Smoothing: ",false);
    	commentarycl("MA_4_Percent_smoothing: ",false);
    	commentarycl("use_whipsaw_filter_MA_4: ",false);
    	commentarycl("use_whipsaw_smoothing_MA_4: ",false);
    	commentarycl("whipsaw_smoothing_MA_4: ",false);
    	commentarycl("whipsaw_smoothing_MA_4_percent: ",false);
    	commentarycl("use_reversal_date_for_smoothing_4: ",false);
		commentarycl("display_ma4_reversal: ",false);
		commentarycl("use_num_bars_confirmation_4: ",false);
		commentarycl("num_bars_green_4: ",false);
		commentarycl("num_bars_red_4: ",false);
		commentarycl("___DVS_Indicator_Settings___: ",___DVS_Indicator_Settings___);
		commentarycl("disable_bear: ",disable_bear);
		commentarycl("use_override_label: ",use_override_label);
		commentarycl("disable_starting: ",disable_starting);
		commentarycl("override_label: ",override_label);
		commentarycl("override_label_2: ",override_label_2);
		commentarycl("text_color_short: ",text_color_short);
		commentarycl("line_color_short: ",line_color_short);
		commentarycl("text_color_long: ",text_color_long);
		commentarycl("line_color_long: ",line_color_long);
		commentarycl("line_color_default_long: ",line_color_default_long);
		commentarycl("int default_long_weight: ",default_long_weight);
		commentarycl("first_dvt_color: ",first_dvt_color);
		commentarycl("second_dvt_color: ",second_dvt_color);
		commentarycl("first_dvt_color_line: ",first_dvt_color_line);
		commentarycl("second_dvt_color_line: ",second_dvt_color_line);
		commentarycl("attention_color: ", attention_color);
		commentarycl("attention_line_color: ", attention_line_color);
		commentarycl("display_decimals: ", display_decimals);
		commentarycl("use_auto_adjustment_for_decimals: ", use_auto_adjustment_for_decimals);
		commentarycl("zero_decimal_price: ", zero_decimal_price);
		commentarycl("one_decimal_price: ", one_decimal_price);
		commentarycl("two_decimal_price: ", two_decimal_price);
		commentarycl("round_to_nearest_five: ", round_to_nearest_five);
		commentarycl("display_price_on_text: ", display_price_on_text);
		commentarycl("use_MACD_for_confirmation: ", use_MACD_for_confirmation);
		commentarycl("FastLength: ", FastLength);
		commentarycl("SlowLength: ", SlowLength);
		commentarycl("___ATR_SETTINGS___: ", ___ATR_SETTINGS___);
		commentarycl("display_other_ATRS: ", display_other_ATRS);
		commentarycl("use_atr: ", use_atr);
		commentarycl("HighPrice: ", C); 
		commentarycl("LowPrice: ", c); 
		commentarycl("Length: ", Length);
		commentarycl("Multi: ", Multi);
		commentarycl("percent_above_for_trigger: ", percent_above_for_trigger);
		commentarycl("percent_off_atr: ", percent_off_atr);
		commentarycl("percent_below_atr: ", percent_below_atr);
		commentarycl("percent_above_atr: ", percent_above_atr);
		commentarycl("update_DVT_if_X_percent_above_ATR: ", update_DVT_if_X_percent_above_ATR);
		commentarycl("percent_above_DVT_for_update: ", percent_above_DVT_for_update);
		commentarycl("use_dynamic_percent_for_update: ", use_dynamic_percent_for_update);
		commentarycl("use_true_range_for_calc: ", use_true_range_for_calc);
		commentarycl("use_bar_range_for_calc: ", use_bar_range_for_calc);
		commentarycl("average_length: ", average_length);
		commentarycl("dynamic_factor: ", dynamic_factor);
		commentarycl("update_DVT_if_X_percent_below_ATR: ", update_DVT_if_X_percent_below_ATR);
		commentarycl("percent_below_DVT_for_update: ", percent_below_DVT_for_update);
		commentarycl("ChartColor: ", false);
		commentarycl("points_padding: ", points_padding);
		commentarycl("percent_padding: ", percent_padding);
		commentarycl("___ATR_SETTINGS____2: ", false);
		commentarycl("use_atr_2: ", false);
		commentarycl("HighPrice_2: ", C);
		commentarycl("LowPrice_2: ", c);
		commentarycl("Length_2: ", 14);
		commentarycl("Multi_2: ", 2);
		commentarycl("percent_above_for_trigger_2: ", false);
		commentarycl("percent_off_atr_2: ", false);
		commentarycl("percent_below_atr_2: ", false);
		commentarycl("percent_above_atr_2: ", false);
		commentarycl("update_DVT_if_X_percent_above_ATR_2: ", false);
		commentarycl("percent_above_DVT_for_update_2: ", false);
		commentarycl("use_dynamic_percent_for_update_2: ", false);
		commentarycl("use_true_range_for_calc_2: ", false);
		commentarycl("use_bar_range_for_calc_2: ", false);
		commentarycl("average_length_2: ", false);
		commentarycl("dynamic_factor_2: ", false);
		commentarycl("update_DVT_if_X_percent_below_ATR_2: ", false);
		commentarycl("percent_below_DVT_for_update_2: ", false);
		commentarycl("points_padding_2: ", false);
		commentarycl("percent_padding_2: ", false);
		commentarycl("___ATR_SETTINGS____3: ", ___ATR_SETTINGS____3);
		commentarycl("use_atr_3: ", use_atr_3);
		commentarycl("show_second_dvt: ", show_second_dvt);
		commentarycl("HighPrice_3: ", c);
		commentarycl("LowPrice_3: ", c); 
		commentarycl("Length_3: ", Length_3);
		commentarycl("Multi_3: ", Multi_3);
		commentarycl("percent_above_for_trigger_3: ", false);
		commentarycl("percent_off_atr_3: ", false);
		commentarycl("percent_below_atr_3: ", false);
		commentarycl("percent_above_atr_3: ", false);
		commentarycl("update_DVT_if_X_percent_above_ATR_3: ", false);
		commentarycl("percent_above_DVT_for_update_3: ", percent_above_DVT_for_update_3);
		commentarycl("use_dynamic_percent_for_update_3: ", false);
		commentarycl("use_true_range_for_calc_3: ", false);
		commentarycl("use_bar_range_for_calc_3: ", false);
		commentarycl("average_length_3: ", false);
		commentarycl("dynamic_factor_3: ", false);
		commentarycl("update_DVT_if_X_percent_below_ATR_3: ", false);
		commentarycl("percent_below_DVT_for_update_3: ", false);
		commentarycl("points_padding_3: ", false);
		commentarycl("percent_padding_3: ", false);
		commentarycl("use_stoch_agreement: ", use_stoch_agreement);
		commentarycl("stoch_agreement_length: ", stoch_agreement_length);
		commentarycl("stoch_agreement_level: ", stoch_agreement_level);
		commentarycl("int Moving_Average_Type: ", Moving_Average_Type);
		commentarycl("int MACD_FastLength: ", MACD_FastLength);
		commentarycl("int MACD_SlowLength: ", MACD_SlowLength); 
		commentarycl("int MACD_MACDLength: ", MACD_MACDLength); 
		commentarycl("MACD_Adap_EFF_Length: ", MACD_Adap_EFF_Length);
		commentarycl("MACD_Adap_Fast_Length: ", MACD_Adap_Fast_Length);
		commentarycl("MACD_Adap_Slow_Length: ", MACD_Adap_Slow_Length);
		commentarycl("___ATR_SETTINGS____4: ", ___ATR_SETTINGS____4);
		commentarycl("use_atr_4: ", use_atr_4);
		commentarycl("use_atr_4_for_dvt: ",false);
		commentarycl("HighPrice_4: ", c);
		commentarycl("LowPrice_4: ", c); 
		commentarycl("Length_4: ", Length_4);
		commentarycl("Multi_4: ", Multi_4);
		commentarycl("percent_above_for_trigger_4: ", false);
		commentarycl("percent_off_atr_4: ", false);
		commentarycl("percent_below_atr_4: ", percent_below_atr_4);
		commentarycl("percent_above_atr_4: ", false);
		commentarycl("update_DVT_if_X_percent_above_ATR_4: ", false);
		commentarycl("percent_above_DVT_for_update_4: ", false);
		commentarycl("use_dynamic_percent_for_update_4: ", false);
		commentarycl("use_true_range_for_calc_4: ", false);
		commentarycl("use_bar_range_for_calc_4: ", false);
		commentarycl("average_length_4: ", false);
		commentarycl("dynamic_factor_4: ", false);
		commentarycl("update_DVT_if_X_percent_below_ATR_4: ", false);
		commentarycl("percent_below_DVT_for_update_4: ", false);
		commentarycl("points_padding_4: ", false);
		commentarycl("percent_padding_4: ", false);
		commentarycl("____Agressive_Oversold_Entry___: ", ____Agressive_Oversold_Entry___);
		commentarycl("use_aggressive_oversold_entry: ", use_aggressive_oversold_entry);
		commentarycl("sma_length: ", sma_length);
		commentarycl("sma_length_2_for_close: ", sma_length_2_for_close);
		commentarycl("num_bars_below_sma: ", num_bars_below_sma);
		commentarycl("percent_candle_body: ", percent_candle_body);
		commentarycl("size_of_candle: ", size_of_candle);
		commentarycl("oversold_vertical_color: ", oversold_vertical_color);
		commentarycl("int aggressive_vertical_weight: ", aggressive_vertical_weight);
		commentarycl("____ABC_PULLBACK___: ", ____ABC_PULLBACK___);
		commentarycl("use_ABC_pullback: ", use_ABC_pullback);
		commentarycl("long_MA_filter: ", long_MA_filter);
		commentarycl("percent_above_long_MA: ", percent_above_long_MA);
		commentarycl("c_wave_min_length: ", c_wave_min_length);
		commentarycl("ABC_vertical_color: ", ABC_vertical_color);
		commentarycl("int ABC_weight: ", ABC_weight);
		commentarycl("num_bars_to_keep_ABC_alive: ", num_bars_to_keep_ABC_alive);
		commentarycl("use_arrow_instead: ", false);
		commentarycl("allow_new_ABC_without_crossing_ATR: ", allow_new_ABC_without_crossing_ATR);
		commentarycl("____ABC_PULLBACK_2___: ", ____ABC_PULLBACK_2___);
		commentarycl("use_ABC_pullback_2: ", use_ABC_pullback_2);
		commentarycl("long_MA_filter_2: ", long_MA_filter_2);
		commentarycl("percent_above_long_MA_2: ", percent_above_long_MA_2);
		commentarycl("c_wave_min_length_2: ", c_wave_min_length_2);
		commentarycl("ABC_vertical_color_2: ", ABC_vertical_color_2);
		commentarycl("int ABC_weight_2: ", ABC_weight_2);
		commentarycl("num_bars_to_keep_ABC_alive_2: ", num_bars_to_keep_ABC_alive_2);
		commentarycl("use_arrow_instead_2: ", false);
		commentarycl("allow_new_ABC_without_crossing_ATR_2: ", allow_new_ABC_without_crossing_ATR_2);}
	end;
end;

var: StreamWriter myFile(null),
	date_str(""),
	inversion_str(""),
	r_date_str(""),
	status(""),
	num_bull_sequential(0),
	demark_count(0),
	num_bear_sequential(0),
	inversion_bar(0),
	chart_group("file"),
	a_active(0),
	var0(0),
	Ribbon_color_val(0),
	file_name(""),
	BOC(0);
	
	
	vars: exit_stop_price_out(0),
		 exit_stop_price_previous(0),
		 num_sts_out(0),
		 mpos_out(0),
		changed_stops(0);
	
method void write_file()
begin	
	if do_output then begin
		mpos_out = marketposition;
		If BarType <= 1 then Var0 = ABSVALUE( VOLUME  +  OPENINT );
		If BarType > 1 then Var0 = ABSVALUE( VOLUME );
		a_active = 0;
		If HIGHEST(Var0, 20) <> 0 then  begin
			Condition2 = Var0 = NTHMINLIST( 1, Var0, Var0[ 1], Var0[ 2], Var0[ 3], Var0[ 4], Var0[ 5], Var0[ 6], Var0[ 7], Var0[ 8], Var0[ 9], Var0[ 10], Var0[ 11], Var0[ 12], Var0[ 13], Var0[ 14], Var0[ 15], Var0[ 16], Var0[ 17], Var0[ 18], Var0[ 19]);
			Condition3 = Var0 = NTHMINLIST( 2, Var0, Var0[ 1], Var0[ 2], Var0[ 3], Var0[ 4], Var0[ 5], Var0[ 6], Var0[ 7], Var0[ 8], Var0[ 9], Var0[ 10], Var0[ 11], Var0[ 12], Var0[ 13], Var0[ 14], Var0[ 15], Var0[ 16], Var0[ 17], Var0[ 18], Var0[ 19]);
			If Condition2 OR Condition3 then  begin
				If  LOW  = LOWEST( LOW , 20) then  begin
					a_active = -1;
				end;
				If  HIGH  = HIGHEST( HIGH , 20) then  begin
					a_active = 1;
				end;
			end;
		end;
		if mpos_out = 0 then Begin
			num_sts_out = 0;
		end;
		if (num_long_dvts = 1 and num_long_dvts[1] = 0) Or
		did_reentry then Begin
			commentarycl("Setting Num_STS_Out");
			num_sts_out = 1;
		end;
		if mpos_out = 1 and mpos_out[1] = 1 and exit_stop_price_out <> exit_stop_price_out[1] then Begin
			num_sts_out = num_sts_out + 1;
		end;
		commentarycl("num_long_dvts: ", num_long_dvts);
		commentarycl("num_long_dvts[1]: ", num_long_dvts[1]);
		commentarycl("mpos_out: ", mpos_out);
		commentarycl("mpos_out[1]: ", mpos_out[1]);
		commentarycl("Setting num_sts_out: ", num_sts_out);
		If c > c[4] then begin
			num_bull_sequential = num_bull_sequential + 1;
			demark_count = num_bull_sequential;
		end else begin
			num_bull_sequential = 0;
		end;
		
		If c < c[4] then begin
			num_bear_sequential = num_bear_sequential + 1;
			demark_count = -1 * num_bear_sequential;
		end else begin
			num_bear_sequential = 0;
		end;
		if(num_long_dvts = 0 and num_long_dvts[1] > 0) then Begin
			inversion_bar = currentbar-1;
		end;
		if num_long_dvts = 1 and num_long_dvts[1] = 0 then Begin
			inversion_bar = currentbar;
		end;
		inversion_str = Bardatetime[Barssinceentry].ToString();
		date_str = Bardatetime[currentbar - last_bar].ToString();
		r_date_str = Bardatetime.ToString();
		if marketposition = 0 then begin
			status = "Flat";
		end else Begin
			status = "Long";
		end;
		file_name = directory + file_prefix + symbol + "_" + Bartype.ToString() + "_" + Barinterval.ToString() + "_" + chart_group + "_data.csv";
			//commentarycl("Attempting to write file");
			//commentarycl("Writing file");
			
			if currentbar = 1 then begin 
				myFile = StreamWriter.Create(file_name);
			end else Begin
				myFile = StreamWriter.Create(file_name, true);
			end;
			if myFile <> null then begin
				commentarycl("Date Str: ", date_str);
				commentarycl("{'STS':'" + numtostr(exit_stop_price_out, 4) + 
				"','prev_STS':'" + Numtostr(exit_stop_price_previous, 4) +  	
				"','date': '" + date_str + 	
				"','r_date': '" + r_date_str +
				"','i_date': '" + inversion_str +
				"','status':'" + status + 
				"','group':'" + chart_group +
				"','atrs':'" + numtostr(atrs,0) +
				"','demark':'" + numtostr(demark_count,0) + 
				"','num_STS':'" + numtostr(changed_stops, 0) + 
				"','o':'" + numtostr(open,2) + 
				"','h':'" + numtostr(high,2) + 
				"','l':'" + numtostr(low,2) + 
				"','c':'" + numtostr(close,2) + 
				"','a':'" + numtostr(a_active,0) +
				"','r1':'" + numtostr(value11,6) +
				"','r2':'" + numtostr(value12,6) +
				"','r3':'" + numtostr(value13,6) +
				"','r4':'" + numtostr(value14,6) +
				"','rD':'" + numtostr(value9,6) +
				"','rC':'" + numtostr(Ribbon_color_val,0) +
				"','EP':'" + numtostr(entryprice,2) +
				"','TZ':'" + numtostr(Sess1endtime,0) +
				"','BOC':'" + numtostr(BOC,0) +"'}");
				myFile.WriteLine("{'STS':'" + numtostr(exit_stop_price_out, 4) + 
				"','prev_STS':'" + Numtostr(exit_stop_price_previous, 4) +  	
				"','date': '" + date_str + 	
				"','r_date': '" + r_date_str +
				"','i_date': '" + inversion_str +
				"','status':'" + status + 
				"','group':'" + chart_group +
				"','atrs':'" + numtostr(atrs,0) +
				"','demark':'" + numtostr(demark_count,0) + 
				"','num_STS':'" + numtostr(changed_stops, 0) + 
				"','o':'" + numtostr(open,2) + 
				"','h':'" + numtostr(high,2) + 
				"','l':'" + numtostr(low,2) + 
				"','c':'" + numtostr(close,2) + 
				"','a':'" + numtostr(a_active,0) +
				"','r1':'" + numtostr(value11,6) +
				"','r2':'" + numtostr(value12,6) +
				"','r3':'" + numtostr(value13,6) +
				"','r4':'" + numtostr(value14,6) +
				"','rD':'" + numtostr(value9,6) +
				"','rC':'" + numtostr(Ribbon_color_val,0) +
				"','EP':'" + numtostr(entryprice,2) +
				"','TZ':'" + numtostr(Sess1endtime,0) +
				"','BOC':'" + numtostr(BOC,0) +"'}");
			end;
			myFile.Close();
	end;
end;
vars:  
	TrendLine myDTTrendLine(null),
	bars_since_bear(0),
	exit_stop_price(0),
	shares_to_buy(0),
	wider_dvt(0),
	trying_to_enter(false),
	limit_entry_price(0); 

if bpt_valid_license_DVT then begin
//if true then begin
	debug();
	app_type = GetAppInfo(aiApplicationType);
	app_optimizing = GetAppInfo(Aioptimizing);
	NShares = RoundToNShares*(Intportion((StartCapital + PercentCompounding*.01*netprofit)/(C*RoundToNShares)));
	if mpos = 0 then begin
		shares_to_buy = nshares;
		id_2 = -1;
		temp_id = -1;
		myTrendLine1 = null;
		changed_stops = 0;
	end else Begin
		shares_to_buy = nshares / 2;
	end;
	force_start = false;
	mpos = marketposition;
	BOC = 0;
	if mpos = 0 then begin
		did_reentry = false;
	end;
	if stop_drawing[0] and stop_drawing[1] then Begin
		stop_drawing = false;
	end;
	if mpos = 0 and mpos[1] = 1 then Begin
		//stop_drawing = true;
	end;
	if mpos = 0 then begin
		num_long_dvts_3 = 0;
	end;
	//commentarycl("stop_drawing: ", stop_drawing);
	//commentarycl("stop_drawing[30]: ", stop_drawing[30]);
	//commentarycl("stop_drawing[31]: ", stop_drawing[31]);
	//commentarycl("has_been_red: ", has_been_red);
	commentarycl("exit_stop_price[1]: ", exit_stop_price[1]);
	commentarycl("Above for Reentry: ", exit_stop_price[1] * (1 + percent_above_for_reentry/100));
	commentarycl("C: ", c);
	commentarycl("Price check passed: ",  c > exit_stop_price[1] * (1 + percent_above_for_reentry/100));
	if mpos = 0 and mpos[1] = 1 and l < exit_stop_price[1] and c > exit_stop_price[1] * (1 + percent_above_for_reentry/100)
	and allow_up_candle_reentry_on_close = 1 
	and (require_up_candle = 0 or c > o ) then Begin
		if use_cash then begin
			if buy_on_close = 0 then begin
				buy ("stop reentry market") NShares shares next bar market;
				BOC = 2;
			end else Begin
				buy ("stop reentry") NShares shares this bar on close;
				BOC = 1;
			end;
		end else Begin
			if buy_on_close = 0 then begin
				buy ("stop reentry ma nc") next bar market;
				BOC = 2;
			end else Begin
				buy ("stop reentry nc") this bar on close;
				BOC = 1;
			end;
		
		end;
		num_long_dvts = 1;
		force_start = true;
		did_reentry = true;
	end; 
	if did_reentry[1] = true then Begin
		num_long_dvts = 1;
	end;
	Value1 = bptMaDeluxe2(
			MA_1_Price, 
			MA_1_Type, 
			MA_1_Length,
            false, 
            0, 
    		0,
    		false, 
            false, 
            false, 
            0, 
            0.1,
    		false,
			false, 
			false,
			3,
			3,
            color_val);

	Value2 = bptMaDeluxe2(
			MA_2_Price, 
			MA_2_Type, 
			MA_2_Length,
            false, 
            0, 
    		0,
    		false, 
            false, 
            false, 
            0, 
            0.1,
    		false,
			false, 
			false,
			3,
			3,
            color_val_2);

	Value3 = bptMaDeluxe2(
			MA_3_Price, 
			MA_3_Type, 
			MA_3_Length,
           false, 
            0, 
    		0,
    		false, 
            false, 
            false, 
            0, 
            0.1,
    		false,
			false, 
			false,
			3,
			3,
            color_val_3);
            
	Value4 = bptMaDeluxe2(
			MA_4_Price, 
			MA_4_Type, 
			MA_4_Length,
            false, 
            0, 
    		0,
    		false, 
            false, 
            false, 
            0, 
            0.1,
    		false,
			false, 
			false,
			3,
			3,
            color_val_4);
	CalcATR();
	CalcATR_3();
	CalcATR_4();
	
	Plot_ATRS();
	calc_low_for_trailing();
	calc_BPT_MACDi();
	check_inversion();
	how_many_color_changes();	
	how_many_color_changes_2();
	place_trailing_stops();

	//commentarycl("Last Tracked Price: ", last_tracked);
	
	inputs: reentry_vertical_color( "cyan" ),
			reentry_line_weight( .5 ),
			trending_reentry_vertical_color( "cyan" ),
			trending_reentry_line_weight( .5 );
			
	vars: isLastBarOfDay(false);
	
	// Check if the current bar's time is equal to the session end time
	if Time = sessionEndTime(1, Regularsession) then begin
    	isLastBarOfDay = true;
	end else begin
    	isLastBarOfDay = false;		
	end;
	
	if mpos = 0 and do_index_reentry = 1 and use_cash = false then Begin
		if c < highest(h, num_bars_back_for_high) * (1 - (percent_decline_for_reentry/100)) then Begin		
			if buy_on_close = 0 or (buy_next_bar_market_EOD = 1 and isLastBarOfDay) then begin
				if use_cash then begin
					buy ("reentry ma") shares_to_buy shares next bar market;
				end else Begin
					buy ("reentry ma nc") next bar market;
				end;
				BOC = 4;
			end else Begin
				if use_cash then begin
					buy ("reentry") shares_to_buy shares this bar on close;
				end else Begin
					buy ("reentry nc") this bar on close;
				end;
				BOC = 3;
			end;
			num_sts_out = 1;
			draw_vertical_line(reentry_vertical_color, reentry_line_weight);
			did_reentry = true;
		end;
	end;	
	if num_long_dvts = 1 and num_long_dvts[1] = 0 and manage_trades_with_STS = false then Begin
		changed_stops = 0;
		if ues_limit_entries = 1 then Begin
			trying_to_enter = true;
			limit_entry_price = high * (1 + limit_entry_percent_padding/100);
		end else begin
			if use_cash then begin
				if buy_on_close = 0 or (buy_next_bar_market_EOD = 1 and isLastBarOfDay) then begin
					if use_cash then begin
						buy shares_to_buy shares next bar market;
					end else Begin
						buy next bar market;
					end;
					BOC = 2;
				end else begin
					if use_cash then begin
						buy shares_to_buy shares this bar on close;
					end else Begin
						buy this bar on close;
					end;
					BOC = 1;
				end;
			end else Begin
				if buy_on_close = 0 or (buy_next_bar_market_EOD = 1 and isLastBarOfDay) then begin
					if use_cash then begin
						buy  shares_to_buy shares next bar market;
					end else Begin
						buy next bar market;
					end;
					BOC = 2;
				end else begin
					if use_cash then begin
						buy  shares_to_buy shares this bar on close;
					end else Begin
						buy this bar on close;
					end;
					BOC = 1;
				end;
			end;
		end;
	end;
	if trying_to_enter then Begin
		buy ("limit entry") next bar at limit_entry_price stop;
	end;
	if (mpos = 0 and mpos[1] = 1) or mpos = 1 then Begin
		trying_to_enter = false;
	end;
	if use_wider_DVT_for_first_2 = 1 then Begin
		if last_tracked > 0 and last_tracked < last_text_price then begin
			wider_dvt = last_tracked;
		end;
		if last_text_price > 0 and (last_text_price < last_tracked or last_tracked = 0) then Begin
			wider_dvt = last_text_price;
		end;
		if (num_long_dvts = 2 or num_long_dvts = 0) and wider_dvt[1] < wider_dvt then Begin
			wider_dvt = wider_dvt[1];
		end;
		if num_long_dvts = 0 and num_long_dvts[1] = 2 and c > wider_dvt then begin
			num_long_dvts = 2;
		end;
	end;
	//commentarycl("last_tracked: ", last_tracked);
	//commentarycl("last_text_price: ", last_text_price);
	//commentarycl("num_long_dvts: ", num_long_dvts);
	//commentarycl("did_reentry: ", did_reentry);
	//commentarycl("wider_dvt: ", wider_dvt);
	
	
	//commentarycl("mpos: ", mpos);
	//commentarycl("num_long_dvts: ", num_long_dvts);
	//commentarycl("num_long_dvts[1]: ", num_long_dvts[1]);
	//commentarycl("allow_trending_reentry: ", allow_trending_reentry);
	
	
	if mpos = 0 and num_long_dvts > num_long_dvts[1] and allow_trending_reentry = 1 and trying_to_enter = false then Begin
		if buy_on_close = 0 or (buy_next_bar_market_EOD = 1 and isLastBarOfDay) then begin
			if use_cash then begin
				buy ("trend reentry market") shares_to_buy shares next bar market;
			end else Begin
				buy ("trend reentry mrktnc") next bar market;
			end;
			BOC = 2;
			changed_stops = 0;
		end else Begin
			if use_cash then begin
				buy ("trending reentry") shares_to_buy shares this bar on close;
			end else Begin
				buy ("trending reentry nc") this bar on close;
			end;
			BOC = 1;
			changed_stops = 0;
		end;
		draw_vertical_line(trending_reentry_vertical_color, reentry_line_weight);
	end;
	commentarycl("last_tracked: ", last_tracked);
	commentarycl("last_text_price: ", last_text_price);
	commentarycl("wider_dvt: ", wider_dvt);
	If (last_tracked > 0 and above_200_ok) then begin
		commentarycl("Setting above 200");
		exit_stop_price = last_tracked * (1 - stop_padding_percent/100);
	end;
	if (last_tracked = 0 or above_200_ok = false) then begin
		commentarycl("Setting below 200");
		if atrs_3 = 1 and num_long_dvts_3 > 0 and use_wider_dvt_patch > 0 then begin
			exit_stop_price = last_tracked * (1 - stop_padding_percent/100);
		end else begin
			exit_stop_price = last_text_price * (1 - stop_padding_percent/100);
		end;
	end;
	if use_wider_DVT_for_first_2 = 1 then Begin
		exit_stop_price = wider_dvt * (1 - stop_padding_percent/100);
	end;
	if use_stop_for_exit =0 then begin
		if num_long_dvts = 0 and num_long_dvts[1] > 0 then Begin
			sell this bar on close;
		end;
	end else Begin
		commentarycl("Trying to set stop price");
		commentarycl("exit_stop_price: ", exit_stop_price);
		commentarycl("num_long_dvts: ", num_long_dvts);
		commentarycl("num_long_dvts_3: ", num_long_dvts_3);
		
		
		if atrs = -1 and c < exit_stop_price and use_exit_stop_patch = 1 then Begin
			exit_stop_price = exit_stop_price[1];
		end;
		if ((did_reentry = false or num_long_dvts > 0) and (manage_trades_with_STS = false or mpos[1] = 1) and c > exit_stop_price) Or
		(mpos = 1 and use_wider_dvt_patch > 0 and c > exit_stop_price) then begin
			commentarycl("exit_stop_price: ", exit_stop_price);
			if round_to_big_point_for_stops = 1 then Begin
				TickSize = MinMove / PriceScale * BigPointValue;
				NumberOfTicks = exit_stop_price / TickSize;
				NearestTick = Round(NumberOfTicks, 0);
				NearestValidPrice = NearestTick * TickSize;
				sell ("stop exit BP") next bar at NearestValidPrice stop;
				if NearestValidPrice <> NearestValidPrice[1] or changed_stops = 0 then Begin
					changed_stops = changed_stops + 1;
				end;
				draw_DVT(NearestValidPrice, changed_stops, changed_stops[1], NearestValidPrice, "White");
				exit_stop_price_out = NearestValidPrice;
			end else begin
				sell ("stop exit") next bar at exit_stop_price stop;
				if exit_stop_price <> exit_stop_price[1] or changed_stops = 0 then Begin
					changed_stops = changed_stops + 1;
				end;
				draw_DVT(exit_stop_price, changed_stops, changed_stops[1], exit_stop_price[1], "White");
				exit_stop_price_out = exit_stop_price;
				commentarycl("Set Stop Exit at: ", exit_stop_price);
			end;
		end;
	end;
	if mpos = 1 then Begin
		if Barssinceentry >= use_targets_after_x_bars then Begin
	 		if use_percent_targets = 1 then Begin
	 			sell ("percent target") Currentshares * percent_shares_for_target/100 shares next bar at Entryprice * (1 + (percent_target/100)) limit;
	 		end;
	 		if use_points_targets = 1 then Begin
				sell ("points target") Currentshares * percent_shares_for_target/100 shares next bar at entryprice + points_target limit;
	 		end;
		end;
	end;
	
if exit_stop_price_out <> exit_stop_price_out[1] and can_draw() then Begin
		commentarycl("Drawing STS text");
		if num_long_dvts_3 = 0 and exit_stop_price_out <> exit_stop_price_out[1] then begin
			//commentarycl("Setting Exit Stop Label");
			//commentarycl("exit_stop_price_out: ", exit_stop_price_out);
			commentarycl("Drawing STS");
			id = draw_text( "STS: " +numtostr(exit_stop_price_out, 2), exit_stop_price_out);
		end;
		if num_long_dvts_3 > 0 and exit_stop_price_out <> exit_stop_price_out[1] then begin
			//commentarycl("Setting Exit Stop Label");
			//commentarycl("exit_stop_price_out: ", exit_stop_price_out);
			commentarycl("Drawing 2vt");
			id_2 = draw_text( "2vt: " +numtostr(exit_stop_price_out, 2), exit_stop_price_out);
			Text_setcolor(id_2, White);
		end;
end;

	if mpos = 0 and trying_to_enter and c < exit_stop_price_out then Begin
		trying_to_enter = false;
	end;
	if mpos = 0 then Begin
		exit_stop_price_previous = 0;
	end;
	if mpos = 1 and exit_stop_price_previous <> exit_stop_price_out then Begin
		exit_stop_price_previous = exit_stop_price_out;
	end;
	//commentarycl("mpos[1]: ", mpos[1]);
	//commentarycl("manage_trades_with_STS: ", manage_trades_with_STS);
	if getappinfo(aiOptimizing) < 1 then begin
		if manage_trades_with_STS and mpos > 0 and num_long_dvts > 0 then Begin
			GD.Items["sts_active_"+Chart_Prefix+symbol] = true astype bool;
		end else Begin
			GD.Items["sts_active_"+Chart_Prefix+symbol] = false astype bool;
		end;
	end;
end;
//if getappinfo(aiOptimizing) < 1 and (mod(CurrentTime, 10) = 0 or Barstatus(1) = 2) then begin
if getappinfo(aiOptimizing) < 1 then begin
	write_file();
end;
Leave a Comment