Drawing Tool RiskReward

Hello, I’m currently working on a custom drawing tool that calculates the risk/reward of a position. It’s almost finished, however, there’s one part that has kept me from making progress for several weeks: how to access the ChartTrader “Order Qty” box through code so that the tool can use the number entered in that box to calculate the position size based on the number of contracts. Could you please tell me how, from a drawing tool, I can access that property through code so that my tool can read the value entered in that box?
Thank you very much

In one of mine you access it through:

		ChartControl.Dispatcher.InvokeAsync((Action)(() => {
			NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
			CTQuantity = quantitySelector.Value;
		}));
1 Like

Several, THANK YOU VERY MUCH :smiley:

With the information you gave me, I was able to understand the logic behind the procedure so I could have the correct code to access the value of the Quantity box.

I’m not an expert in programming, nor in the NinjaScript environment, so this is twice as difficult or more. I’d like to ask you a couple more questions now:

  1. For the same drawing tool, I would like to be able to access the “Account” box to obtain the total value (CashValue or NetLiquidation) of the account selected in that box, and thus be able to calculate the percentage of the account at risk in the transaction.

  2. For the same drawing tool, I can’t manage, in the NinjaScript environment, to make some of its features visible only when it’s selected or when the mouse is hovered over it. The problem is that this shouldn’t affect the entire drawing, but only part of it.
    The drawing has two main parts: The background-colored rectangles, which don’t contain any information or text and should be visible at all times. The text rectangles, which do contain position information, are just the text rectangles. I want them to be visible only when the drawing is selected or when the mouse is hovered over it.

Thank you so much for the support you’ve already given me.

I hope you can help me with these two new questions as well.

Hugs.

NinjaTrader.Gui.Tools.AccountSelector accountSelector;

Is the account selector. If you search the old forum you will find some info on it. There’s a sample script somewhere that monitors everything in the chart trader for changes and outputs a print.

1 Like

Why don’t you post it here so others can learn?

Hi Bill, The solutions are already posted here. In both cases, the solution was provided by Several.

1 Like

Thanks. Sorry, I just was curious about this outcome.

1 Like

It’s okay, don’t worry.

Hi, Several. I hope you’re well.
I have a new question for you, and I hope you can help.
Do you happen to know the name of the ChartTrader PnL Text Box?
I want to access that value, but directly in the chart, not in the control box.
As I imagine you already know, the PnL value displayed in the chart depends on the selected account. However, the chart text is separate from the text displayed in the control box. The reason I need to get it from the chart text box is for the “Show realized PnL when flat” feature to work. Depending on whether there is a check mark in the ChartTrader configuration properties, I hope you can help me this time. Best regards, and thank you again.

you don’t need to do that you can access everything you want with these 3 methods

Account_OrderUpdate;
Account.ExecutionUpdate
Account.AccountStatusUpdate

if you search the help pages in Ninja you will find full explanations and some sample code to help you get pnl, cashbalance, last execution, etc.

One word though - do check everything works exactly how you want - positionupdate for instance works in a slightly odd way ( to my way of thinking)

Hi Mindset, thank you very much for your response.

You see, the problem is that the options you mentioned aren’t properties, but rather events, so I don’t see how to access the value I’m looking for through them. My goal is for the ChartTrader features “Show realized PnL when flat” and “PnL display unit” to apply when fetching the value I’m looking to obtain.
Therefore, my idea is to access the text box in ChartTrader that displays the PnL, under the previous options and features, so that the value I obtain also meets those criteria and is displayed in the same way.

I don’t know if I’ve explained myself correctly.
Regards :smiley:

I am not sure what you want so let’s check

AccountItem.UnrealizedProfitLoss.

Is that what you want?

Hey, Mindset

Thanks a lot for responding.

Not really, because the parameter: AccountItem.UnrealizedProfitLoss is obtained from the Account property, and that’s exactly what I wanted to avoid, you see. If you look at the first image, you’ll see the three text boxes in the chartTrader. The “PnL” text box, which is the one I’m interested in, also has the property from which it is obtained assigned, which I imagine must be Account. However, this particular text allows for some formatting changes that are available in the chartTrader UI configuration options, which are visible in the second image.
For this reason, what I’m looking for is to be able to access the text displayed in the “PnL” text box so that it is displayed according to the format selected in the options.

I’m not sure if I explained myself well enough for you to understand.

Hugs.

if(this.ChartControl != null)
this.ChartControl.Dispatcher.InvokeAsync((Action)(() => 
{
	Chart cw = Window.GetWindow(this.ChartControl.Parent) as Chart;
	if(cw == null) { Print("ChartWindow Not Found"); return; }
	ChartTrader ct = cw.FindFirst("ChartWindowChartTraderControl") as ChartTrader;
	if(ct == null) { Print("ChartTrader Not Found"); return; }
	else
	{
		Print("PNL Display Unit =  " + ct.Properties.PnLDisplayUnit.ToString());
		Print("PNL Display Flat =  " + ct.Properties.ShowRealizedPnLWhenFlat.ToString());
	}
	TextBlock cttb = ct.FindFirst("ChartTraderControlPnLText") as TextBlock;
	if(cttb == null) { Print("PNL Display Text Null"); return; }
	else Print("PNL Display Text =  " + cttb.Text);
}));

Be Safe in this Crazy World!
:smiling_face_with_sunglasses:

I am not understanding. Forgive me.

The PNL text box is reporting the account item i mentioned. take a look at the help guide on OnAccountItemUpdate().

If you really want to avoid using that - and you would have to explain why- then you would have to design your own method with entry prices, last prices, etc. If you simply want to mirror the pnl TextBox which is what I think you’re saying you’re betting getting the underlying value and then sorting the formatting out yourself. I hope that explains where I am coming from. To

double pnl = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]); or in an indicator

Account account = Account.All.FirstOrDefault(a => a.Name == "Sim101");
if (account == null)
    return;

Position position = account.Positions.FirstOrDefault(p => p.Instrument == Instrument);
if (position == null)
    return;

double pnl = position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);

Thank you so much for your response and contribution, Mindset.

I’m sorry I didn’t make myself clear.

I’ll investigate what you’ve mentioned here. For now, I’ll continue using the data obtained from the account, i.e.: RealizedProfitLoss, UnrealizedProfitLoss, GrossProfitLoss, etc.

Thank you again, and best regards.

Hey, Edge :smiley:

Thank you very much for your response and the contribution you make.

I don’t quite understand what this code snippet is trying to do.
What I can share with you is that I tested it, and it doesn’t meet any of the conditions I’m looking for.

For now, I’ll continue using the data obtained from the account, i.e.: RealizedProfitLoss, UnrealizedProfitLoss, GrossProfitLoss, etc.

Thank you again, and best regards.