Beruflich Dokumente
Kultur Dokumente
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.Design;
using System.Windows.Forms;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
[Description("Tools for Vertical Scrolling,Draging and AutoScrolling the NT
Charts")]
public class VerticalScrollTools : Indicator
{
#region Variables
private
System.Windows.Forms.VScrollBar vsbar = null;
private
System.Windows.Forms.Button asbutton = nu
ll;
private
System.Windows.Forms.Button vdbutton = nu
ll;
private int
speed = 2;
private int
extra_margin = 2;
private int
ct;
private int
cb;
private double
pr_min;
private double
pr_max;
private double
old_prmin;
private double
old_prmax;
private double
scale;
private double
margin_size; //used by maximum/minimum p
rice in dataseries and also by auto scroll feature.
private double
un_margin;
// this is due to maximum v
alue of scroll that can't be achieved by user interaction.
// by MSDN , it is equivale
nt to (vsbar.largechange - 1).
private double
space_max;
private double
space_min;
private bool
auto_scroll = true;
private bool
vertical_drag = true;
private double
as_margin = 0; // margin to cover in au
toscroll/verticaldrag mode
private bool
show_as = true;
private bool
show_vs = false;
private bool
show_vd = false;
private AutoScrollButtonPosition asbp = AutoScrollButtonPosi
tion.TOP_RIGHT;
private AutoScrollType ast = AutoScrollType.Extreem;
private int
center_margin_ticks = 2;
// margin in
ticks within which center AutoScroll type will keep current close.
private double
center_margin;
private int
old_mouse_y;
private bool
scale_init;
#endregion
protected override void Initialize()
{
Overlay
= true;
CalculateOnBarClose = false;
AutoScale
= true;
scale_init
= false;
}
#region StartUp/Buttons/Bar
protected override void OnStartUp()
{
base.OnStartUp();
this.ChartControl.YAxisRangeTypeRight = YAxisRangeType.F
ixed;
// Vertical Scroll Bar
vsbar = new VScrollBar();
vsbar.Dock = System.Windows.Forms.DockStyle.Right;
vsbar.Width = 25;
vsbar.Minimum = 0;
vsbar.Maximum = 100;
vsbar.SmallChange = speed;
vsbar.Value = 0;
vsbar.Name = "vsBar";
if(show_vs)
{
vsbar.Show();
}
else vsbar.Hide();
this.ChartControl.Controls.Add(vsbar);
vsbar.Scroll += new System.Windows.Forms.ScrollEventHand
ler(vsbar_Scroll);
this.ChartControl.ChartPanel.MouseUp += new MouseEventHa
ndler(MouseDrag_OnScale);
this.ChartControl.ChartPanel.MouseDown += new MouseEvent
Handler(MouseDown_OnChart);
this.ChartControl.ChartPanel.MouseMove += new MouseEvent
Handler(MouseDrag_OnChart);
//Auto Scroll Button
asbutton = new System.Windows.Forms.Button();
switch (asbp)
{
case AutoScrollButtonPosition.TOP_LEFT:
{
asbutton.Location = new System.D
rawing.Point(this.ChartControl.CanvasLeft + 8,this.ChartControl.CanvasTop + 15);
break;
}
case AutoScrollButtonPosition.TOP_RIGHT:
{
asbutton.Location = new System.D
rawing.Point(this.ChartControl.CanvasRight - 75,this.ChartControl.CanvasTop + 3)
;
break;
}
}
asbutton.Size = new System.Drawing.Size(27,27);
asbutton.BackColor = Color.GreenYellow;
asbutton.Name = "asButton";
asbutton.Text = "AS";
if(show_as)
{
asbutton.Show();
}
else asbutton.Hide();
this.ChartControl.ChartPanel.Controls.Add(asbutton);
asbutton.Click += ToggleAutoScroll;
//Vertical Drag Button
vdbutton = new System.Windows.Forms.Button();
switch (asbp)
{
case AutoScrollButtonPosition.TOP_LEFT:
{
vdbutton.Location = new System.D
rawing.Point(this.ChartControl.CanvasLeft + 38,this.ChartControl.CanvasTop + 15)
;
break;
}
case AutoScrollButtonPosition.TOP_RIGHT:
{
vdbutton.Location = new System.D
rawing.Point(this.ChartControl.CanvasRight - 105, this.ChartControl.CanvasTop +3
);
break;
}
}
vdbutton.Size = new System.Drawing.Size(27,27);
vdbutton.BackColor = Color.GreenYellow;
vdbutton.Name = "vdButton";
vdbutton.Text = "VD";
if(show_vd)
{
vdbutton.Show();
}
else vdbutton.Hide();
this.ChartControl.ChartPanel.Controls.Add(vdbutt
on);
vdbutton.Click += ToggleVerticalDrag;
old_prmin = double.MaxValue;
old_prmax = double.MinValue;
margin_size = (double)(extra_margin*TickSize);
center_margin = (double)(center_margin_ticks*TickSize)/2
;
UpdateScale();
}
#endregion
protected override void OnBarUpdate()
{
if(CurrentBar < 0)return;
auto_scroll = false;
asbutton.BackColor = Color.LightSalmon;
}
as_margin = (double) ( e.Y - old_mouse_y
)*scale;
this.ChartControl.FixedPanelMinRight = t
his.ChartControl.FixedPanelMinRight + as_margin;
this.ChartControl.FixedPanelMaxRight = t
his.ChartControl.FixedPanelMaxRight + as_margin;
as_margin = 0;
SetScrollLimits();
old_mouse_y = e.Y;
}
}
this.ChartControl.ChartPanel.Invalidate();
}
private void vsbar_Scroll(object sender, ScrollEventArgs se)
{
if(auto_scroll)
{
auto_scroll = false;
asbutton.BackColor = Color.LightSalmon;
}
DoScroll(vsbar.Value);
}
private void ToggleAutoScroll(object sender, EventArgs e)
{
if(!auto_scroll)
{
auto_scroll = true;
asbutton.BackColor = Color.GreenYellow;
AutoScroll();
// added to make shift of
chart when clicks.
}
else if (auto_scroll)
{
auto_scroll = false;
asbutton.BackColor = Color.LightSalmon;
}
this.ChartControl.ChartPanel.Invalidate();
}
private void ToggleVerticalDrag(object sender, EventArgs e)
{
if(!vertical_drag)
{
vertical_drag = true;
vdbutton.BackColor = Color.GreenYellow;
}
else if (vertical_drag)
{
vertical_drag = false;
vdbutton.BackColor = Color.LightSalmon;
}
this.ChartControl.ChartPanel.Invalidate();
}
protected bool MouseOnChart(int x, int y)
{
int temp_y;
temp_y = this.ChartControl.GetYByValue(this,this.ChartCo
ntrol.FixedPanelMinRight); // restricted only in price panel
if( x < this.ChartControl.CanvasLeft || x >= this.ChartC
ontrol.CanvasRight || y < this.ChartControl.CanvasTop || y > temp_y) return fals
e;
else return true;
}
#endregion
#region InitScale/UpdateScale/Scrolls
protected void InitScale()
{
int lmb_idx;
// left most bar index
int rmb_idx;
// right most bar index
lmb_idx = CurrentBar - this.FirstBarIndexPainted;
rmb_idx = CurrentBar - this.LastBarIndexPainted;
double screen_high = double.MinValue;
double screen_low = double.MaxValue;
for (int i = rmb_idx ; i <= lmb_idx ; i++)
{
if( High[i] > screen_high ) screen_high
= High[i];
if( Low[i] < screen_low ) screen_low = L
ow[i];
}
/*this.ChartControl.YAxisRangeTypeRight = YAxisRangeType
.Automatic;
this.ChartControl.AutoScaleDateRangeTypeRight = AutoScal
eDateRangeType.ScreenDateRange;
this.ChartControl.AutoScaleMarginTypeRight = AutoScaleMa
rginType.Percent;
this.ChartControl.FixedPanelMinRight = screen_low - thi
s.ChartControl.AutoScaleMarginLowerRight;
this.ChartControl.FixedPanelMaxRight = screen_high + th
is.ChartControl.AutoScaleMarginUpperRight;
this.ChartControl.YAxisRangeTypeRight = YAxisRangeType.F
ixed;*/
this.ChartControl.FixedPanelMinRight = screen_low - 10*
TickSize;
this.ChartControl.FixedPanelMaxRight = screen_high + 10
*TickSize;
this.ChartControl.ChartPanel.Invalidate();
}
if(Low[rmb_index] - this.ChartControl.Fi
xedPanelMinRight < margin_size + un_margin)
{
as_margin = (margin_size + un_m
argin - Low[rmb_index] + this.ChartControl.FixedPanelMinRight);
this.ChartControl.FixedPanelMin
Right = this.ChartControl.FixedPanelMinRight - as_margin;
this.ChartControl.FixedPanelMax
Right = this.ChartControl.FixedPanelMaxRight - as_margin;
}
break;
}
case AutoScrollType.Center:
{
double center_price;
center_price = (this.ChartContro
l.FixedPanelMaxRight + this.ChartControl.FixedPanelMinRight)/2.0;
if( Close[rmb_index] > center_pr
ice + center_margin)
{
as_margin = Close[rmb_in
dex] - center_price - center_margin;
this.ChartControl.FixedP
anelMinRight = this.ChartControl.FixedPanelMinRight + as_margin;
this.ChartControl.FixedP
anelMaxRight = this.ChartControl.FixedPanelMaxRight + as_margin;
}
if( Close[rmb_index] < center_pr
ice - center_margin)
{
as_margin = center_price
- center_margin - Close[rmb_index];
this.ChartControl.FixedP
anelMinRight = this.ChartControl.FixedPanelMinRight - as_margin;
this.ChartControl.FixedP
anelMaxRight = this.ChartControl.FixedPanelMaxRight - as_margin;
}
break;
}
}
as_margin = 0;
SetScrollLimits();
}
}
#endregion
#region Plot/Termination
public override void Plot(Graphics g,Rectangle bounds,double min
,double max)
{
switch (asbp)
{
case AutoScrollButtonPosition.TOP_LEFT:
{
if(asbutton != null)
{
asbutton.Location = new
System.Drawing.Point(this.ChartControl.CanvasLeft + 8,this.ChartControl.CanvasTo
p + 15);
}
if(vdbutton != null)
{
vdbutton.Location = new
System.Drawing.Point(this.ChartControl.CanvasLeft + 38,this.ChartControl.CanvasT
op + 15);
}
break;
}
case AutoScrollButtonPosition.TOP_RIGHT:
{
if(asbutton != null)
{
asbutton.Location = new
System.Drawing.Point(this.ChartControl.CanvasRight - 75,this.ChartControl.Canvas
Top + 3);
}
if(vdbutton != null)
{
vdbutton.Location = new
System.Drawing.Point(this.ChartControl.CanvasRight - 105, this.ChartControl.Canv
asTop +3);
}
break;
}
}
}
protected override void OnTermination()
{
vsbar.Scroll -= vsbar_Scroll;
this.ChartControl.ChartPanel.MouseUp -= MouseDrag_OnScal
e;
this.ChartControl.ChartPanel.MouseDown -= MouseDown_OnCh
art;
this.ChartControl.ChartPanel.MouseMove -= MouseDrag_OnCh
art;
this.ChartControl.Controls.Remove(vsbar);
vsbar.Dispose();
if(asbutton != null)
{
asbutton.Click -= ToggleAutoScroll;
this.ChartControl.ChartPanel.Controls.Remove(asbutto
n);
asbutton.Dispose();
}
if(vdbutton != null)
{
vdbutton.Click -= ToggleVerticalDrag;
this.ChartControl.ChartPanel.Controls.Remove(vdb
utton);
vdbutton.Dispose();
}
this.ChartControl.YAxisRangeTypeRight = YAxisRangeType.A
utomatic;
base.OnTermination();
}
#endregion
#region Properties
[Description("Scroll speed by scroll buttons. Select suitable to
Chart and Instrument.")]
[Category("Parameters")]
[Gui.Design.DisplayName("Scroll Speed")]
public int Speed
{
get { return speed; }
set { speed = Math.Max(1, value); }
}
[Description("Select extra ticks to have on beyond maximum amd m
inimum dataseries prices.")]
[Category("Parameters")]
[Gui.Design.DisplayName("Extra Ticks")]
public int Extra_margin
{
get { return extra_margin; }
set { extra_margin = Math.Max(1, value); }
}
[Description("Select Center Margin Ticks for Center AutoScroll."
)]
[Category("Parameters")]
[Gui.Design.DisplayName("Center AutoScroll Margin")]
public int Center_margin_ticks
{
get { return center_margin_ticks; }
set { center_margin_ticks = Math.Max(2, value); }
}
[Description("Show or Hide the Auto Scroll Button. Auto Scroll w
orks only with show.")]
[Category("Parameters")]
[Gui.Design.DisplayName("Show AutoScroll Button")]
public bool Show_as
{
get { return show_as; }
set { show_as = value; }
}
[Description("Select Location of Auto Scroll Button on Chart")]
[Category("Parameters")]
[Gui.Design.DisplayName("Auto Scroll Button Position")]
public AutoScrollButtonPosition Asbp
{
/// <summary>
/// Tools for Vertical Scrolling,Draging and AutoScrolling the NT Charts
/// </summary>
/// <returns></returns>
public Indicator.VerticalScrollTools VerticalScrollTools(Data.IDataSerie
s input, AutoScrollButtonPosition asbp, AutoScrollType ast, int center_margin_ti
cks, int extra_margin, bool show_as, bool show_vd, bool show_vs, int speed)
{
return _indicator.VerticalScrollTools(input, asbp, ast, center_margi
n_ticks, extra_margin, show_as, show_vd, show_vs, speed);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Tools for Vertical Scrolling,Draging and AutoScrolling the NT Charts
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.VerticalScrollTools VerticalScrollTools(AutoScrollButto
nPosition asbp, AutoScrollType ast, int center_margin_ticks, int extra_margin, b
ool show_as, bool show_vd, bool show_vs, int speed)
{
return _indicator.VerticalScrollTools(Input, asbp, ast, center_margi
n_ticks, extra_margin, show_as, show_vd, show_vs, speed);
}
/// <summary>
/// Tools for Vertical Scrolling,Draging and AutoScrolling the NT Charts
/// </summary>
/// <returns></returns>
public Indicator.VerticalScrollTools VerticalScrollTools(Data.IDataSerie
s input, AutoScrollButtonPosition asbp, AutoScrollType ast, int center_margin_ti
cks, int extra_margin, bool show_as, bool show_vd, bool show_vs, int speed)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator wi
th the default input/bar series from within the 'Initialize()' method");
return _indicator.VerticalScrollTools(input, asbp, ast, center_margi
n_ticks, extra_margin, show_as, show_vd, show_vs, speed);
}
}
}
#endregion