Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
how to get account balance
Collapse
X
-
Hello,
Thank you for the question.
From an Indicator, you would need to first get a reference to an account as indicators are not associated with an account at all. After that, you could use the Account methods such as Get to access AccountItems.
The general syntax for Get would be the following:
Code:Account a = Account.All.First(t => t.Name == "Sim101"); double value = a.Get(AccountItem.CashValue, Currency.UsDollar); Print(value);
Please let me know if I may be of further assistance.JesseNinjaTrader Customer Service
- Likes 1
-
Code:protected override void OnBarUpdate() { Account a = Account.All.First(t => t.Name == "Sim101"); double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar); if (Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) >= Balance*0.015) { ExitLong(0,10000,"EURUSD",""); ExitShort(1,10000,"GBPUSD",""); } }
Unrealized Profit >= 1.5% of Account Balance
For some reason, the comparison only works the first time.
After it closes less than 1.5% of Account Balance?Last edited by johnnybegoode; 12-13-2018, 07:20 PM.
Comment
-
Hello johnnybegoode,
From what you provided I don't see anything specific however you mentioned multi-instrument so you may need to instead use the Positions object instead of Position so you can access the multiple instruments positions. You may also need to use more conditions here to determine which BarsInProgress is running the code. We have some samples of using BarsInProgress in the help guide along with using the Positions object. I will link them below.
I would also likely suggest using a Print to gain a better understanding of the problem.
Code:protected override void OnBarUpdate() { Account a = Account.All.First(t => t.Name == "Sim101"); double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar); double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]); Print(BarsInProgress + " " + unrealized + " >= " + Balance*0.015); if (unrealized >= Balance*0.015) { ExitLong(0,10000,"EURUSD",""); ExitShort(1,10000,"GBPUSD",""); } }
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
-
I think
Code:[LIST][*][B][B](PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])[/B][/B][/LIST]
It does not look at overall instrument PnL,
but only at either one of the instrument.
How do I get PnL of all open positions (multiple-instruments)?Last edited by johnnybegoode; 12-14-2018, 08:02 PM.
Comment
-
Code:if (BarsInProgress == 0) { if ( ( (PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) +PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[1])) >= ProfitTarget ) ) {
Am I right that
Close[0] is looking at EURUSD
Close[1] is looking at GBPUSD
and that is no Bars Ago.Last edited by johnnybegoode; 12-16-2018, 02:55 AM.
Comment
-
Hello johnnybegoode,
You are correct, PositionAccount does not represent multiple instruments you would need to instead use PositionsAccount. This is noted in the help guide page for PositionAccount, it notes you would need to use PositionsAccount (plural PositionS) for multi instruments. This holds true for many objects in NinjaScript such as Close Open High Low Position, there are plural versions of each of these items which instead apply toward multi-series scripts: Closes Opens Highs Lows Positions AccountPositions etc..
Regarding your next post, this also comes back to the plural usage of the series. You have used Close (not plural) and supplied a BarsAgo [0] and [1]. If you are referring to a specific series, you instead would use the Plural series which also has an index for the BarsInProgress: Closes[0][0] and Closes[1][0]
Closes[0][0] EURUSD
Closes[1][0] GBPUSD
You can find more of the specifics like this in the following page which outlines some of the common multi-series syntaxes you will use:
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
-
Q1: PositionsAccount does not have the method "GetUnrealizedProfitLoss()"
So I tried
Code:if (BarsInProgress == 0) { if ( ( (PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[0][0]) +PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[1][0])) >= ProfitTarget ) ) {
Q2: Indicators
Code:else if (State == State.DataLoaded) { RSI1 = RSI(Closes[0][0], 14, 3); // for EURUSD RSI2 = RSI(Closes[1][0], 14, 3); // for GBPUSD }
Code:else if (State == State.DataLoaded) { RSI1 = RSI(BarsArray[0], 14, 3); // for EURUSD RSI2 = RSI(BarsArray[1], 14, 3); // for GBPUSD }
Does that mean I could not specific Close, Open, High, Low etc?
....
Code:protected override void OnBarUpdate() { if (State != State.Realtime) return; // Set 1 if ((RSI1.Avg[0] > 70) // for EURUSD && (RSI2.Avg[0] < 30) // for GBPUSD && (BarsInProgress == 0))
Last edited by johnnybegoode; 12-18-2018, 01:09 AM.
Comment
-
Hello johnnybegoode,
Q1: PositionsAccount does not have the method "GetUnrealizedProfitLoss()"
Code:PositionsAccount[1].GetUnrealizedProfitLoss()
Q2: Indicators
else if (State == State.DataLoaded)
{
RSI1 = RSI(Closes[0][0], 14, 3); // for EURUSD
RSI2 = RSI(Closes[1][0], 14, 3); // for GBPUSD
}
Code:RSI1 = RSI(Closes[0], 14, 3); // for EURUSD RSI2 = RSI(Closes[1], 14, 3); // for GBPUSD
The syntax you provided in general looks correct, but this would be a question that you should determine the answer to yourself by confirming the results. You can use Prints here to Print the values used in the condition to confirm it works as you are expecting.
Code:Print(RSI1.Avg[0] + " > 70 " + RSI2.Avg[0] + " < 30 BarsInProgress: " + BarsInProgress); if ((RSI1.Avg[0] > 70) // for EURUSD && (RSI2.Avg[0] < 30) // for GBPUSD && (BarsInProgress == 0))
JesseNinjaTrader Customer Service
Comment
-
I went with
Code:if ( ( (PositionsAccount[0].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[0][0]) +PositionsAccount[1].GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[1][0])) >= ProfitTarget ) ) { PositionsAccount[1].Close(); PositionsAccount[0].Close(); }
It stop placing orders after closing.
*If I execute PositionsAccount[0] first, it stops before closing PositionsAccount[1].
Code:[B][B]ExitLong(0,10000,"EURUSD","");[/B][/B] [B][B]ExitShort(1,10000,"GBPUSD","");[/B][/B]
Comment
-
Hello johnnybegoode,
In this case that would be expected if you are using the Position object to close the overall position directly as that will disable the script.
To exit the position, you could use an exit order with the Position.Quantity as one way of doing this. Generally, you would need to check the direction of your position and submit the correct order to exit that position.
Code:if(Position.MarketPosition == MarketPosition.Long) { ExitLong(Position.Quantity); }
JesseNinjaTrader Customer Service
Comment
-
Under what circumstances do somebody needs to use PostionAccount over Position,
the description on the reference is somewhat ambiguous.
Position
Represents position related information that pertains to an instance of a strategy.
PostionAccount
Represents position related information that pertains to real-world account (live or simulation).
Comment
-
Hello johnnybegoode,
This would really be dependent on your logic. These two objects represent two separate positions which are your account position and the position of a single instance of a strategy.
Your account position does not necessarily need to match the strategies position, you could have an Account Position of 2 long, but you have two separate strategies which put it in that position and each strategies Position would be 1 long. This is generally used for advanced use cases where the strategy needs to track the live account position which may differ from its virtual position.
Strategies are not aware of the actual account position by default or what other strategies are doing in their virtual performance. In advanced scenarios how you see fit, this can be used to access the actual account position if your strategy were to require that type of logic. Generally using the Position object is used as it references this strategies performance opposed to the overall account position which is cumulative.
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
-
I'm trying to create a strategy that adjusts its order quantity based on the Balance, but the balance
does not update and remains at its initial value of 1000
as seen on the attached output screenshot.
Code:protected override void OnBarUpdate() { Account a = Account.All.First(t => t.Name == "Sim101"); double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar); double QuantityRisk = 0.1; double InitialDeposit = 1000; double QuantityD = (Balance * (QuantityRisk/100)/InitialDeposit*100); double QuantityE = (Math.Round(QuantityD, 2)); double QuantityF = QuantityE * 100000; // 1 Lot = 100000 int Quantity = Convert.ToInt32(QuantityF); Print("@@@@@@@@ Balance = " + Balance); Print("@@@@@@@@ QuantityD = " + QuantityD); Print("@@@@@@@@ QuantityE = " + QuantityE); Print("@@@@@@@@ QuantityF = " + QuantityF); Print("@@@@@@@@ Quantity = " + Quantity); // .... // i.e. if ((RSI1.Avg[0] > 80) EnterShort(0,Quantity,"EURUSD"); //... //etc
Comment
-
Hello johnnybegoode,
In this case because you are referencing the account by using the Addon methods, you need to be more specific. You are using the Playback connection which does not use Sim101, it uses Playback101. You can see the account name listed in the image you provided in the control center row. You could use the following instead:
Code:Account a = Account.All.First(t => t.Name == "Playback101");
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Brevo, Today, 01:45 AM
|
0 responses
6 views
0 likes
|
Last Post
by Brevo
Today, 01:45 AM
|
||
Started by aussugardefender, Today, 01:07 AM
|
0 responses
3 views
0 likes
|
Last Post Today, 01:07 AM | ||
Started by pvincent, 06-23-2022, 12:53 PM
|
14 responses
242 views
0 likes
|
Last Post
by Nyman
Today, 12:47 AM
|
||
Started by TraderG23, 12-08-2023, 07:56 AM
|
9 responses
384 views
1 like
|
Last Post
by Gavini
Today, 12:29 AM
|
||
Started by oviejo, Today, 12:28 AM
|
0 responses
6 views
0 likes
|
Last Post
by oviejo
Today, 12:28 AM
|
Comment