// Create form CForm form = App.Forms.CreateForm(); CGrid grid = form.AddGrid(); grid.SetColumns("Position", "Entry", "Exit", "Return"); grid.Location = new Point(10, 10); grid.Size = new Size(260, 200); grid.SetNumberOfRows(0); CButton button = form.AddButton("Start"); button.Location = new Point(10, 215); button.MouseClick = Start; CLabel label = form.AddLabel("Info"); label.Location = new Point(90, 220); CLabel labelSpeed = form.AddLabel("Bars/Minute"); labelSpeed.Location = new Point(10, 245); CNumericUpDown speed = form.AddNumericUpDown("Speed"); speed.Location = new Point(80, 242); speed.Size = new Size(60, 30); speed.Value = 60; speed.Minimum = 0; speed.Maximum = 6000; form.Show(); Form parent = (Form)form.GetControl(); parent.FormClosed += new FormClosedEventHandler(parent_FormClosed); App.StoredVariables.SaveVariable("stop", "1"); Simulation(speed, button, grid, label); #functions# public void Simulation(CNumericUpDown speed, CButton button, CGrid grid, CLabel label) { Chart chart = Charts.GetSelectedChart(); string symbol = chart.SymbolName; PaneObject obj = chart.Panes[0].CreatePaneObject("buysell"); // Buy TextObj buy = obj.DrawText("buy", "BUY", 12, 15); buy.MouseMoveCursor = Cursors.Hand; buy.FontSpec.Fill.Color = System.Drawing.Color.FromArgb(100, System.Drawing.Color.Blue); buy.FontSpec.FontColor = System.Drawing.Color.White; buy.UsePixelCoordinate = true; buy.OnMouseClick = ClickBuyCover; // Short TextObj sell = obj.DrawText("sell", "SHORT", 50, 15); sell.MouseMoveCursor = Cursors.Hand; sell.FontSpec.Fill.Color = System.Drawing.Color.FromArgb(100, System.Drawing.Color.Red); sell.FontSpec.FontColor = System.Drawing.Color.White; sell.UsePixelCoordinate = true; sell.IsVisible = true; sell.OnMouseClick = ClickShortSell; // Set tags buy.Tag1 = sell; sell.Tag1 = buy; // Balance TextObj balance = obj.DrawText("balance", "0 %", 110, 15); balance.UsePixelCoordinate = true; obj.Update(); // Start Logic App.StoredVariables.RemoveVariable("isbuy"); App.StoredVariables.RemoveVariable("issell"); double[] close = chart.ChartData.GetTimeSeries("close"); double[] open = chart.ChartData.GetTimeSeries("open"); double[] high = chart.ChartData.GetTimeSeries("high"); double[] low = chart.ChartData.GetTimeSeries("low"); double openprice = double.NaN; int positionindex = 0; chart.ScrollBarNbQuotes = 50; int index = chart.ScrollBarNbQuotes + 10; label.Text = "Symbol: " + symbol; while(true) { int avgSpeed = (int)((60 / speed.Value) * 1000); int spread = (int)((double)avgSpeed / 1.2); Random random = new Random(); int sleep = (int)random.Next(avgSpeed - spread, avgSpeed + spread); App.Main.Sleep(sleep); if(Charts.GetSelectedChart().SymbolName != symbol) { App.StoredVariables.SaveVariable("stop", "1"); // Stop simulation // Restart simulation Simulation(speed, button, grid, label); break; } if(index >= close.Length) { if(!label.Text.StartsWith("No more quotes")) { label.Text = "No more quotes (" + symbol + ")"; button.Text = "Start"; App.StoredVariables.SaveVariable("stop", "1"); // Stop simulation } continue; } if(App.StoredVariables.LoadVariable("stop") == "1") { if(button.Text != "Start") { button.Text = "Start"; } continue; } else { if(button.Text != "Stop") { button.Text = "Stop"; } } int curindex = chart.ScrollBarIndex + chart.ScrollBarNbQuotes; // Check whether a buy/short occured string isBuy = (string)App.StoredVariables.LoadVariable("isbuy"); string isShort = (string)App.StoredVariables.LoadVariable("isshort"); if(isBuy == "1") { positionindex++; openprice = open[index]; App.StoredVariables.RemoveVariable("isbuy"); DrawArrowBottom(obj, chart.ChartData, index, "BUY", low, openprice, positionindex); } else if(isShort == "1") { positionindex++; openprice = open[index]; App.StoredVariables.RemoveVariable("isshort"); DrawArrowTop(obj, chart.ChartData, index, "SHORT", high, openprice, positionindex); } else { // Check whether a sell/cover occured string isSell = (string)App.StoredVariables.LoadVariable("issell"); string isCover = (string)App.StoredVariables.LoadVariable("iscover"); double exitprice = double.NaN; double perf = double.NaN; if(isSell == "1") { App.StoredVariables.RemoveVariable("issell"); exitprice = open[index]; DrawArrowTop(obj, chart.ChartData, index, "SELL", high, exitprice, positionindex); obj.Update(); perf = Math.Round(((exitprice / openprice) - 1) * 100, 2); grid.InsertRow(0); grid.GetCell(0, 0).SetValue("Long"); grid.GetCell(1, 0).SetValue(openprice); grid.GetCell(2, 0).SetValue(exitprice); grid.GetCell(3, 0).SetValue(perf); // Unset Open Price openprice = double.NaN; } else if(isCover == "1") { App.StoredVariables.RemoveVariable("iscover"); exitprice = open[index]; DrawArrowBottom(obj, chart.ChartData, index, "COVER", low, exitprice, positionindex); obj.Update(); perf = Math.Round(((openprice / exitprice) - 1) * 100, 2); grid.InsertRow(0); grid.GetCell(0, 0).SetValue("Short"); grid.GetCell(1, 0).SetValue(openprice); grid.GetCell(2, 0).SetValue(exitprice); grid.GetCell(3, 0).SetValue(perf); // Unset Open Price openprice = double.NaN; } } // There is a position if(!double.IsNaN(openprice)) { // Get last price double lastprice = close[curindex]; double ret = ((lastprice / openprice) - 1) * 100; if(App.StoredVariables.LoadVariable("isshortpos") == "1") { ret = ((openprice / lastprice) - 1) * 100; } balance.Text = Math.Round(ret, 2) + " %"; balance.FontSpec.Alignment = StringAlignment.Far; obj.Update(); } index++; int val = index - chart.ScrollBarNbQuotes; if(chart.ScrollBarIndex == val - 1) { chart.SetScrollBar(val, chart.ScrollBarNbQuotes, index); } else { chart.ScrollBarIndex++; chart.SetScrollBar(chart.ScrollBarIndex, chart.ScrollBarNbQuotes, index); } /* if(val > 0) // { //chart.ScrollBarIndex = val; } else { chart.SetScrollBar(val, chart.ScrollBarNbQuotes, index); MessageBox.Show("pass: " + val + " - " + chart.ScrollBarNbQuotes); //chart.SetScrollBar(0, index); }*/ } } public void ClickBuyCover(PaneObject pane, GraphObj obj) { TextObj buyCover = (TextObj)obj; TextObj shortSell = ((TextObj)buyCover.Tag1); if((string)App.StoredVariables.LoadVariable("isin") == "1") { // Cover position buyCover.IsVisible = true; buyCover.Text = "BUY"; shortSell.IsVisible = true; shortSell.Text = "SHORT"; App.StoredVariables.SaveVariable("isin", "0"); App.StoredVariables.SaveVariable("iscover", "1"); App.StoredVariables.SaveVariable("islongpos", "0"); App.StoredVariables.SaveVariable("isshortpos", "0"); } else { // Buy position buyCover.IsVisible = false; shortSell.Text = "SELL"; shortSell.IsVisible = true; App.StoredVariables.SaveVariable("isin", "1"); App.StoredVariables.SaveVariable("isbuy", "1"); App.StoredVariables.SaveVariable("islongpos", "1"); App.StoredVariables.SaveVariable("isshortpos", "0"); } pane.Update(); } public void ClickShortSell(PaneObject pane, GraphObj obj) { TextObj shortSell = (TextObj)obj; TextObj buyCover = ((TextObj)shortSell.Tag1); if((string)App.StoredVariables.LoadVariable("isin") == "1") { // Sell position shortSell.IsVisible = true; shortSell.Text = "SHORT"; buyCover.IsVisible = true; buyCover.Text = "BUY"; App.StoredVariables.SaveVariable("isin", "0"); App.StoredVariables.SaveVariable("issell", "1"); App.StoredVariables.SaveVariable("islongpos", "0"); App.StoredVariables.SaveVariable("isshortpos", "0"); } else { // Short position shortSell.IsVisible = false; buyCover.Text = "COVER"; buyCover.IsVisible = true; App.StoredVariables.SaveVariable("isin", "1"); App.StoredVariables.SaveVariable("isshort", "1"); App.StoredVariables.SaveVariable("islongpos", "0"); App.StoredVariables.SaveVariable("isshortpos", "1"); } pane.Update(); } public void DrawArrowTop(PaneObject obj, ChartData chartData, int curindex, string label, double[] high, double price, int positionindex) { // Draw arrow PointF barLocation = obj.ScaleToPixel(curindex, high[curindex]); PointF barLocation1 = obj.PixelToScale((int)barLocation.X, (int)barLocation.Y - 15); PointF barLocation2 = obj.PixelToScale((int)barLocation.X, (int)barLocation.Y - 2); ArrowObj arrow = obj.DrawArrow("toparrow-" + positionindex, curindex + 1, barLocation1.Y, curindex + 1, barLocation2.Y); arrow.Line.Width = 2; arrow.Line.Color = System.Drawing.Color.Red; TextObj opentext = obj.DrawText("toptext-" + positionindex, label + Environment.NewLine + price.ToString(), curindex + 1, barLocation1.Y); opentext.FontSpec.Border.IsVisible = false; opentext.FontSpec.FontColor = System.Drawing.Color.Red; opentext.FontSpec.Fill.Color = System.Drawing.Color.Transparent; opentext.FontSpec.Size = 10; opentext.Location.HorizontalAlignment = "center"; opentext.Location.VerticalAlignment = "bottom"; } public void DrawArrowBottom(PaneObject obj, ChartData chartData, int curindex, string label, double[] low, double price, int positionindex) { PointF barLocation = obj.ScaleToPixel(curindex, low[curindex]); PointF barLocation1 = obj.PixelToScale((int)barLocation.X, (int)barLocation.Y + 15); PointF barLocation2 = obj.PixelToScale((int)barLocation.X, (int)barLocation.Y + 2); ArrowObj arrow = obj.DrawArrow("bottomarrow-" + positionindex, curindex + 1, barLocation1.Y, curindex + 1, barLocation2.Y); arrow.Line.Width = 2; arrow.Line.Color = System.Drawing.Color.Blue; TextObj opentext = obj.DrawText("bottomtext-" + positionindex, price.ToString() + Environment.NewLine + label, curindex + 1, barLocation1.Y); opentext.FontSpec.Border.IsVisible = false; opentext.FontSpec.FontColor = System.Drawing.Color.Blue; opentext.FontSpec.Fill.Color = System.Drawing.Color.Transparent; opentext.FontSpec.Size = 10; opentext.Location.HorizontalAlignment = "center"; opentext.Location.VerticalAlignment = "top"; } void Start(object sender, MouseEventArgs e) { if(App.StoredVariables.LoadVariable("stop") == "1") { App.StoredVariables.SaveVariable("stop", "0"); } else { App.StoredVariables.SaveVariable("stop", "1"); } } void parent_FormClosed(object sender, FormClosedEventArgs e) { App.StoredVariables.SaveVariable("stop", "1"); }