Help with Windows Forms

Created at 14 Dec 2020, 19:53
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
LI

lisabeaney

Joined 08.10.2019

Help with Windows Forms
14 Dec 2020, 19:53


Hi,

I've been working on integrating Windows Forms in to my BOTs and just have what I'm sure is a simple query..... How do I get the Windows Form code to interact with the positions I have open ?

The error I'm getting is 

'Positions' is a type, which is not valid in the given context

I'll post the simple code I'm working with:

This is the Form1.cs file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    public partial class Form1 : Form
    {
  
        public Form1()
        {
            
            InitializeComponent();
        }
        
        public void UpdateLabel(string NewLabel)
        {
            lbBotName.Text = NewLabel;
        }

        public void UpdateAccount(double NewBalance)
        {
            lblAcctBalance.Text = "Account Balance £" + Math.Round(NewBalance,2);
        }

        public void btnCloseSell_Click(object sender, EventArgs e)
        {
           
                foreach (var position in Positions)
                {
                        ClosePosition(position);
                }
            
        }
    }
}

 

This is the BOT code

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Collections.Generic;
using cAlgo.API.Internals;
using System.Net;
using System.Threading;
using System.Windows.Forms;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class FormTestBOT : Robot
    {
        [Parameter("Test", DefaultValue = true)]
        public bool pTest { get; set; }
        public string ThiscBotLabel;

        // Form section

        private Form1 _f1;
        private Thread _thread;
        protected override void OnStart()
        {
            // Set position label to cBot name
            ThiscBotLabel = this.GetType().Name + "-" + SymbolName + "-" + RunningMode;

            // Display Windows Form
            _f1 = new Form1();
            _thread = new Thread(() => _f1.ShowDialog());
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();

            _thread = new Thread(() => _f1.UpdateLabel(ThiscBotLabel));
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }

        protected override void OnTick()
        {
            // Put your core logic here
            _thread = new Thread(() => _f1.UpdateAccount(Math.Round(Account.Balance,2)));
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
            _thread = new Thread(() => { _f1.Close(); });
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
    }
}

And finally this is the code for the form

namespace cAlgo
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lbBotName = new System.Windows.Forms.Label();
            this.lblAcctBalance = new System.Windows.Forms.Label();
            this.btnCloseSell = new System.Windows.Forms.Button();
            this.btnCloseBuy = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lbBotName
            // 
            this.lbBotName.AutoSize = true;
            this.lbBotName.Font = new System.Drawing.Font("Microsoft Sans Serif", 13.875F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbBotName.Location = new System.Drawing.Point(12, 20);
            this.lbBotName.Name = "lbBotName";
            this.lbBotName.Size = new System.Drawing.Size(118, 42);
            this.lbBotName.TabIndex = 0;
            this.lbBotName.Text = "label1";
           
            // 
            // lblAcctBalance
            // 
            this.lblAcctBalance.AutoSize = true;
            this.lblAcctBalance.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblAcctBalance.Location = new System.Drawing.Point(19, 92);
            this.lblAcctBalance.Name = "lblAcctBalance";
            this.lblAcctBalance.Size = new System.Drawing.Size(227, 37);
            this.lblAcctBalance.TabIndex = 1;
            this.lblAcctBalance.Text = "lblAcctBalance";
            // 
            // btnCloseSell
            // 
            this.btnCloseSell.BackColor = System.Drawing.Color.Red;
            this.btnCloseSell.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnCloseSell.ForeColor = System.Drawing.Color.White;
            this.btnCloseSell.Location = new System.Drawing.Point(26, 170);
            this.btnCloseSell.Name = "btnCloseSell";
            this.btnCloseSell.Size = new System.Drawing.Size(347, 65);
            this.btnCloseSell.TabIndex = 2;
            this.btnCloseSell.Text = "Close SELL Trades";
            this.btnCloseSell.UseVisualStyleBackColor = false;
            this.btnCloseSell.Click += new System.EventHandler(this.btnCloseSell_Click);
            // 
            // btnCloseBuy
            // 
            this.btnCloseBuy.BackColor = System.Drawing.Color.ForestGreen;
            this.btnCloseBuy.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnCloseBuy.ForeColor = System.Drawing.Color.White;
            this.btnCloseBuy.Location = new System.Drawing.Point(393, 170);
            this.btnCloseBuy.Name = "btnCloseBuy";
            this.btnCloseBuy.Size = new System.Drawing.Size(347, 65);
            this.btnCloseBuy.TabIndex = 3;
            this.btnCloseBuy.Text = "Close BUY Trades";
            this.btnCloseBuy.UseVisualStyleBackColor = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.btnCloseBuy);
            this.Controls.Add(this.btnCloseSell);
            this.Controls.Add(this.lblAcctBalance);
            this.Controls.Add(this.lbBotName);
            this.Name = "Form1";
            this.Text = "Form1";
            this.TopMost = true;
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lbBotName;
        private System.Windows.Forms.Label lblAcctBalance;
        private System.Windows.Forms.Button btnCloseSell;
        private System.Windows.Forms.Button btnCloseBuy;
    }
}

 


@lisabeaney
Replies

PanagiotisCharalampous
15 Dec 2020, 08:13

Hi Lisa,

Positions is a property of the Robot class. In order to access it, you need to pass the Positions or the Robot object somehow to the form.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous