How to get values from "Sample Trading Panel" cBot

Created at 29 Jan 2021, 08:05
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!
DI

dinhvuongfx

Joined 17.10.2019

How to get values from "Sample Trading Panel" cBot
29 Jan 2021, 08:05


Hi all,
I want to edit "Sample Trading Panel" cBot. How to use "Stop Loss (pips)" from Panel in OnTick() method?
 

Thanks a lot!


@dinhvuongfx
Replies

winsonet
29 Jan 2021, 08:12

the value will save in a TextBox control, you can get the value with the TextBox Text attribute, for example:

the textbox control name is :  tbSL, then you can get the value as below:

 

tbSL.Text


@winsonet

dinhvuongfx
29 Jan 2021, 08:32 ( Updated at: 29 Jan 2021, 08:33 )

RE:

winsonet said:

the value will save in a TextBox control, you can get the value with the TextBox Text attribute, for example:

the textbox control name is :  tbSL, then you can get the value as below:

 

tbSL.Text

I added 2 functions in class "SampleTradingPanel"

private readonly IDictionary<string, TextBox> _inputMap = new Dictionary<string, TextBox>();

protected override void OnTick()
{
    var stopLossPips = GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);

}

private double GetValueFromInput(string inputKey, double defaultValue)
{
    double value;

    return double.TryParse(_inputMap[inputKey].Text, out value) ? value : defaultValue;
}

I got an error: "Crashed in OnTick with KeyNotFoundException: The given key was not present in the dictionary."


@dinhvuongfx

winsonet
29 Jan 2021, 08:38

RE: RE:

dinhvuongfx said:

winsonet said:

the value will save in a TextBox control, you can get the value with the TextBox Text attribute, for example:

the textbox control name is :  tbSL, then you can get the value as below:

 

tbSL.Text

I added 2 functions in class "SampleTradingPanel"

private readonly IDictionary<string, TextBox> _inputMap = new Dictionary<string, TextBox>();

protected override void OnTick()
{
    var stopLossPips = GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);

}

private double GetValueFromInput(string inputKey, double defaultValue)
{
    double value;

    return double.TryParse(_inputMap[inputKey].Text, out value) ? value : defaultValue;
}

I got an error: "Crashed in OnTick with KeyNotFoundException: The given key was not present in the dictionary."

why you need to use _inputMap for save the textbox? also, if you want to do that, you need to add the textbox object in your dict at first


@winsonet

dinhvuongfx
29 Jan 2021, 08:43

RE: RE: RE:

winsonet said:

dinhvuongfx said:

winsonet said:

the value will save in a TextBox control, you can get the value with the TextBox Text attribute, for example:

the textbox control name is :  tbSL, then you can get the value as below:

 

tbSL.Text

I added 2 functions in class "SampleTradingPanel"

private readonly IDictionary<string, TextBox> _inputMap = new Dictionary<string, TextBox>();

protected override void OnTick()
{
    var stopLossPips = GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);

}

private double GetValueFromInput(string inputKey, double defaultValue)
{
    double value;

    return double.TryParse(_inputMap[inputKey].Text, out value) ? value : defaultValue;
}

I got an error: "Crashed in OnTick with KeyNotFoundException: The given key was not present in the dictionary."

why you need to use _inputMap for save the textbox? also, if you want to do that, you need to add the textbox object in your dict at first

I don't have much C# textbox experience. Could you please check "Sample Trading Panel" cBot and print Stop Loss (pips) from Panel to the cTrader screen whenever tick changes and give me all the code?

Thank you so much!


@dinhvuongfx

winsonet
29 Jan 2021, 08:54 ( Updated at: 15 Jan 2024, 14:51 )

RE: RE: RE: RE:

dinhvuongfx said:

winsonet said:

dinhvuongfx said:

winsonet said:

the value will save in a TextBox control, you can get the value with the TextBox Text attribute, for example:

the textbox control name is :  tbSL, then you can get the value as below:

 

tbSL.Text

I added 2 functions in class "SampleTradingPanel"

private readonly IDictionary<string, TextBox> _inputMap = new Dictionary<string, TextBox>();

protected override void OnTick()
{
    var stopLossPips = GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);

}

private double GetValueFromInput(string inputKey, double defaultValue)
{
    double value;

    return double.TryParse(_inputMap[inputKey].Text, out value) ? value : defaultValue;
}

I got an error: "Crashed in OnTick with KeyNotFoundException: The given key was not present in the dictionary."

why you need to use _inputMap for save the textbox? also, if you want to do that, you need to add the textbox object in your dict at first

I don't have much C# textbox experience. Could you please check "Sample Trading Panel" cBot and print Stop Loss (pips) from Panel to the cTrader screen whenever tick changes and give me all the code?

Thank you so much!

this sample there is a little complicated for beginner, it created a custom control object and encapsulated the textbox control in it, if you just want to create a simple panel and get the value, you can take a look my Information Pad indicator and the description in here:

 

my indicator:

[Information Pad Indicator | Algorithmic Forex Trading | cTrader...]

 


@winsonet

winsonet
29 Jan 2021, 08:57

RE: RE: RE: RE: RE:

But I did't say how to add the TextBox control, but if you know how to add the label into the panel, you should know how to do with the TextBox, it's same :)

 

@winsonet

dinhvuongfx
29 Jan 2021, 09:01

RE: RE: RE: RE: RE: RE:

winsonet said:

But I did't say how to add the TextBox control, but if you know how to add the label into the panel, you should know how to do with the TextBox, it's same :)

 

Thank you for your resources. I do appreciate your help.

 

@dinhvuongfx

winsonet
29 Jan 2021, 09:02

RE: RE: RE: RE: RE: RE:

oh, if you really want to get the value in the sample, there is a method GetValueFromInput for do that:

 

var lots = GetValueFromInput(LotsInputKey, 0);

 

this code from the sample, the LotsInputKey is the TextBox object name


@winsonet

dinhvuongfx
29 Jan 2021, 09:04

RE: RE: RE: RE: RE: RE: RE:

winsonet said:

oh, if you really want to get the value in the sample, there is a method GetValueFromInput for do that:

 

var lots = GetValueFromInput(LotsInputKey, 0);

 

this code from the sample, the LotsInputKey is the TextBox object name

"GetValueFromInput" is in TradingPanel class, not in main Class so I cannot get the value from panel

 

@dinhvuongfx

winsonet
29 Jan 2021, 09:05 ( Updated at: 29 Jan 2021, 09:07 )

RE: RE: RE: RE: RE: RE: RE: RE:

dinhvuongfx said:

winsonet said:

oh, if you really want to get the value in the sample, there is a method GetValueFromInput for do that:

 

var lots = GetValueFromInput(LotsInputKey, 0);

 

this code from the sample, the LotsInputKey is the TextBox object name

"GetValueFromInput" is in TradingPanel class, not in main Class so I cannot get the value from panel

 

easy, you just make it public and shoiuld be ok :)

for example , change below

private double GetValueFromInput(string inputKey, double defaultValue)

to

public double GetValueFromInput(string inputKey, double defaultValue)

and you will can use it as below:

tradingPanel.GetValueFromInput("name");

 

@winsonet

dinhvuongfx
29 Jan 2021, 10:16

RE: RE: RE: RE: RE: RE: RE: RE: RE:
protected override void OnTick()
{
    var tradingPanel = new TradingPanel(this, Symbol, defaultRR, DefaultStopLossPips);
    
    double stopLossPips = tradingPanel.GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);
}

Thank you. But I got only the default SL, I cannot get the SL value whenever I change the SL in Textbox


@dinhvuongfx

winsonet
29 Jan 2021, 10:54

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

dinhvuongfx said:

protected override void OnTick()
{
    var tradingPanel = new TradingPanel(this, Symbol, defaultRR, DefaultStopLossPips);
    
    double stopLossPips = tradingPanel.GetValueFromInput("SLKey", 0);

    Chart.DrawStaticText("SL", stopLossPips.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);
}

Thank you. But I got only the default SL, I cannot get the SL value whenever I change the SL in Textbox

You don't create the new tradingPanel every time on tick, you need to put the trading Panel in a global and use it on tick:

TradingPanel tradingPanel; //define as global variable

protected override void OnStart()
{
    //just need to create once time on start
    tradingPanel = new TradingPanel(this, Symbol, DefaultLots, DefaultStopLossPips, DefaultTakeProfitPips);
}

protected override void OnTick()
{
    //call the get value on tick
    var sl = tradingPanel.GetValueFromInput("SLKey", 0);
    Print("SL: " +sl);
}

 


@winsonet

dinhvuongfx
29 Jan 2021, 11:14

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

@winsonet You are great. Thank you ❤


@dinhvuongfx