double N = (Account.Get(AccountItem.CashValue) * .01)
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
Default position size
Collapse
X
-
Default position size
In strategy builder when picking the enter long or short the size says default quantity. What is it and how do I change it? It's not under the default properties section of the strategy builder. If I can't change it, can this code be used to calculate a position size based on account size and wanting 1 percent risk:
Code:Tags: None
-
Hello jjaffa,
Thank you for your post.
There's a known issue with the Strategy Builder and changing the size from the default quantity that we expect to be rectified in the next release of NinjaTrader. I don't have a date when that would be released, however.
Even so, the Strategy Builder isn't complex enough to get account values, so you'd definitely have to unlock your code and replace the Default Value with your calculated value regardless. It's worth noting here that EnterLong requires an int for the quantity so you'd also need to round that value to a whole number which can be done by casting the calculated value to an int. Also, Account.Get() also requires you to specify the currency the account is funded in, for example:
Account.Get(AccountItem.CashValue, Currency.UsDollar)
So something like
int N = (int)(Account.Get(AccountItem.CashValue, Currency.UsDollar) * .01);
would get a rounded version of 1 percent of the account value, but you'd then need to keep in mind how many contracts you can buy with that amount of your account money, so you'd want to divide that by the margin necessary for each contract, which would vary depending on the instrument. Your broker can tell you the necessary amount of margin for each instrument.
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
- Likes 1
-
So far it's not entering any positions with that. I'm trying to test this on forex (eur/usd). Would that mean I'd have to change the formula to account for leverage?
Also, is there a close all positions? i had another thread about using color the plot and I want something like if plot = 0 then close all positions. Otherwise i'm afraid it if I choose for it to close open long position and open short position at the same time, it would open an extra position.
Comment
-
Hello jjaffa,
Thank you for your reply.
Yes, you'd have to figure out how many lots you can purchase with that amount of your account. Your broker can likely assist with how much each lot would cost and you'd then need to divide the percentage of the account value by that to get the number of lots. This will also depend on the lot size you're using.
Can you provide a snippet of your code that doesn't appear to be working?
If you're using the managed approach and, for example try to use both an Enter and Exit method on the same bar, yes, that would result in an extra position. However, Enter methods such as EnterLong() will automatically submit an order to close the position in the opposite direction when called, if a position exists at that time.
Closing all positions every time would be unnecessary then as you'd just simply call the Entry method in the direction you wish.
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
Comment
-
For a plot point of zero I just want it to close whatever position I'm in at the time, without entering a new one. I'm not getting any errors when running the strategy, it's just not entering any positions.
Code:int N = (int)(Account.Get(AccountItem.CashValue, Currency.UsDollar) * .01); if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if (ColorThePlot1.CTPplot[0] == 0) { ExitLong(Convert.ToInt32(Position.Quantity), "", @""); } // Set 2 if (ColorThePlot1.CTPplot[0] == 0) { ExitShort(Convert.ToInt32(Position.Quantity), "", @""); } // Set 3 if (ColorThePlot1.CTPplot[0] == 1) { EnterLong(Convert.ToInt32(DefaultQuantity), "N"); } // Set 4 if (ColorThePlot1.CTPplot[0] == -1) { EnterShort(Convert.ToInt32(DefaultQuantity), "N");
Comment
-
Hello jjaffa,
Thank you for your reply.
What you have there looks correct. ExitLong() and ExitShort() will simply be ignored if no corresponding position in that direction exists.
The first thing I would recommend would be to turn on the Order Trace function:
if (State == State.SetDefaults)
{
TraceOrders = true;
}
Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.
Here is a link to our help guide that goes into more detail on tracing orders:
https://ninjatrader.com/support/help...aceorders2.htm
Trace orders alone may not give you the full picture of whether or not a trade should have been entered on a given bar, so adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar, can be helpful.
This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.
https://ninjatrader.com/support/foru...ns-not-working
If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently. With the printout information you can assess what is different between the two.
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
Comment
-
I'm not getting anything at all in the output. Not even cancelled orders. Can you tell me if it has anything to do with the presets from the strategy builder?
EDIT: I've also tried changing the position size back to defaultquantity instead of the new variable, still nothing. Maybe something with the indicator itself?
Code:Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = true; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true;
Last edited by jjaffa; 04-26-2021, 05:42 PM.
Comment
-
Hello jjaffa,
Thanks for your reply.
I see you are using ColorThePlot indicator. Please note that ColorThePlot1.CTPplot[0] would provide the value of the plot on the chart which likely would not be 0 or 1 or -1. Instead, you need to select the "Signal" plot. ColorThePlot1.Signal[0]. You select the plot to use in the "Value plot". I've shown that below:
Paul H.NinjaTrader Customer Service
Comment
-
Wow that solves that thank you. It's trading now. I'm back to the position size problem though. I've tried setting it back to defaultquantity and the backtest/chart always sets the value to 1. I found the link below and added the line to set the defaultquantity in the set defaults section of the code. It's throwing me an error that I can't set it to the equation above. "cannot implicitly convert double to int. an explicit conversion exists (are you missing a cast?)" So basically this at the bottom of the defaults section.
link: https://ninjatrader.com/support/help...ltquantity.htm
Code:[COLOR=#0000ff]protected[/COLOR][COLOR=#0000ff]override[/COLOR][COLOR=#0000ff]void[/COLOR]OnStateChange() { [COLOR=#0000ff]if[/COLOR](State==State.SetDefaults) { DefaultQuantity= (int)(Account.Get(AccountItem.CashValue, Currency.UsDollar) * .01); } }
Last edited by jjaffa; 04-27-2021, 03:38 PM.
Comment
-
Hello jjaffa,
Thanks for your reply.
I may not be understanding what you are trying to do. If all you want is to change the size of the default quantity there are 2 ways to do that but again I am not sure if that is what you want or if you just want to be able to change the order quantity on a per order basis (dynamic position sizing?).
To change the order quantity, in the order method, just replace the Default quantity directly with a value or select a variable that you have specified a quantity in.
OR
The strategy Default quantity means that on every order, this is the amount of contracts or units to use.
To change the strategy default quantity, (you would leave <default quantity> in each order:
On a per order basis, you can specify the number of contracts. To do that, in the entry method change the "Quantity" field from "DefaultQuantity" by moving the mouse in the field and clicking the word "set", From there you can choose a user input if you have one or simply enter the number representing the number of contracts to buy or sell.
Reference: https://ninjatrader.com/support/help...tegyProperties
Alternately, if you want to use the same quantity on every order, when you apply the strategy, in the strategy parameters, locate the field "set order quantity" which will by default show "Strategy", click the drop-down arrow and select "Default quantity" and a new field called "Default Quantity" will appear and you can directly enter the number of contracts to buy or sell each time.
How to set default quantity or create a user input: https://paul-ninjatrader.tinytake.co...Nl8xNTk5OTc3OQPaul H.NinjaTrader Customer Service
Comment
-
My main goal is to have the strategy use 1% of my account size on each order, so it would be dynamic. I’ve tried creating my own variable with an account get equation but it didn’t work. So then i tried messing with default quantity. I’m sure I’ll run into a problem calculating order size when it comes to forex lots to buy
Comment
-
Hello jjaffa,
Thanks for your reply.
You would not be able to do position sizing in a strategy builder strategy. You would have to unlock the script which I suspect you already have. Changing the strategy's default quantity is not what you want to do but instead, create a variable that would hold the size of the order you wish to place and then use that variable in the entry order method for the quantity. I think there is some confusion over default quantity as that is what is displayed in the strategy builder in the quantity field as a default setting and earlier Kate had advised replacing that with your variable but it was meant on a per-entry order basis and not as changing the actual default quantity setting of the strategy. I apologize for any confusion this may have created.
Using the example in Post #2 and a simple entry example:
In OnBarUpdate(), you would have something like:
int N = (int)(Account.Get(AccountItem.CashValue, Currency.UsDollar) * .01);
Print (Time[0]+" N= "+N+" account: "+Account.Get(AccountItem.CashValue, Currency.UsDollar)); // print the value of N and the account value for your debgging reference (send to new>Ninjascript output window)
if (your conditions to enter long)
{
EnterLong(N);
}
Paul H.NinjaTrader Customer Service
Comment
-
Thank you thank you. I figured out where I've been having trouble. The strat builder sets up order entry like : EnterShort(Convert.ToInt32(Position.Quantity), "DefaultQuantity", @""); . I only tried changing what was in the Quotes instead of the entire parenthesis, like EnterShort(Convert.ToInt32(Position.Quantity), "N", @""). I changed it like you have and it worked. Now I have to actually let it run because backtest doesn't take into account new account balances after the traded. Thank you both!
- Likes 1
Comment
-
Code:// Set 1 if (ColorThePlot1.Signal[0] == 0) { ExitLong(Convert.ToInt32(Position.Quantity), "", @""); } // Set 2 if (ColorThePlot1.Signal[0] == 0) { ExitShort(Convert.ToInt32(Position.Quantity), "", @""); } // Set 3 if (ColorThePlot1.Signal[0] == 1) { EnterLong(N); } // Set 4 if (ColorThePlot1.Signal[0] == -1) { EnterShort(N); }
4/29/2021 8:31:41 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:31:41 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=999 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:31:41 PM Strategy 'BESTv1/231203336': Ignored SubmitOrderManaged() method at 4/29/2021 8:31:41 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=999 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
4/29/2021 8:31:41 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:31:41 PM: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=999 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:31:41 PM Strategy '231203336/BESTv1: Cancelled pending exit order, since associated position is closed, orderId='87b0a417f35440408cc61a6a6ddb5d92' account='Sim101' name='Stop loss' orderState=Accepted instrument='EURUSD' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=1.21306 quantity=999 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=22 time='2021-04-29 20:31:36' gtd='2099-12-01' statementDate='2021-04-29'
4/29/2021 8:31:45 PM N= 999 account: 99999.89008
4/29/2021 8:31:55 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:31:55 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:31:55 PM Strategy 'BESTv1/231203336': Ignored SubmitOrderManaged() method at 4/29/2021 8:31:55 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
4/29/2021 8:31:55 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:31:55 PM: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:31:55 PM Strategy 'BESTv1/231203336': Ignored SubmitOrderManaged() method at 4/29/2021 8:31:55 PM: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
4/29/2021 8:31:54 PM N= 999 account: 99999.89008
4/29/2021 8:32:00 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:32:00 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:32:00 PM Strategy 'BESTv1/231203336': Ignored SubmitOrderManaged() method at 4/29/2021 8:32:00 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
4/29/2021 8:32:00 PM Strategy 'BESTv1/231203336': Entered internal SubmitOrderManaged() method at 4/29/2021 8:32:00 PM: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
4/29/2021 8:32:00 PM Strategy 'BESTv1/231203336': Ignored SubmitOrderManaged() method at 4/29/2021 8:32:00 PM: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'
Disabling NinjaScript strategy 'BESTv1/231203336'
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by alifarahani, Today, 09:40 AM
|
6 responses
31 views
0 likes
|
Last Post
by alifarahani
Today, 03:18 PM
|
||
Started by Waxavi, Today, 02:10 AM
|
1 response
17 views
0 likes
|
Last Post Today, 03:03 PM | ||
Started by Kaledus, Today, 01:29 PM
|
5 responses
13 views
0 likes
|
Last Post Today, 02:56 PM | ||
Started by Waxavi, Today, 02:00 AM
|
1 response
12 views
0 likes
|
Last Post Today, 02:53 PM | ||
Is it possible to have a drawing, such as a line, cycle through different colors?
by gentlebenthebear
Started by gentlebenthebear, Today, 01:30 AM
|
3 responses
17 views
0 likes
|
Last Post Today, 02:41 PM |
Comment