NexusFi: Find Your Edge


Home Menu

 





ninjatrader: strategy accessing of accounts info


Discussion in NinjaTrader

Updated
    1. trending_up 1,827 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
pxp333
Parlin NJ/USA
 
Posts: 7 since Mar 2015
Thanks Given: 0
Thanks Received: 0

Hi All,

I am using this snippet of code in my ninja 7 strategy within OnBarUpdate():

if (!Historical)
{rej.Set(0);
foreach (Account acct in Cbi.Globals.Accounts)
{PositionCollection positions=acct.Positions;
foreach (Position pos in positions)
if (pos.MarketPosition.ToString()=="Long")
{rej.Set(1);}
else if (pos.MarketPosition.ToString()=="Short")
{rej.Set(-1);}}}

where rej is an IntSeries

Ninja apparently does not support this type of coding, but it works with ninjascript.

My question is the following:

If I have only 1 account and I am trading only 1 instrument then the above snippet works for me.

But I would like for it to be more directed so that I can direct the code to access only a specific account for example Sim101 and within that account only a specific instrument for example ES futures.

What the code does is to allow the strategy know the current position of my actual trading account upon each bar update i.e. long=1, short=-1 or flat=0.

Since there is no NT documentation regarding accessing Accounts and this above coding I don't know how to modify the above code so as to not use foreach{} but to instead specify the account and instrument to query for the type of position currently being held.

Any assistance regarding this would be greatly appreciated.

Thanks


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Five Days Until the Gap Dies -- CME Goes 24/7 on All Dig …
Traders Hideout
OneChronos Launches First Combinatorial Auction FX Venue …
Currencies
Kharg Island at 6%, Regime Fall at 1.5% -- The Black Swa …
Prediction Markets & Event Contracts
Hormuz Normalization Collapses to 19.5% -- Iran Regime F …
Prediction Markets & Event Contracts
Kalshi Sets $4.13B All-Time Weekly Record as Polymarket …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
8 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #2 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

I haven't tested it but try using Linq (using System.Linq) and do something like:

 
Code
foreach (Position pos in from Account acct in Cbi.Globals.Accounts where acct.Name == "Sim101" select acct.Positions into positions from Position pos in positions select pos)
{
    if (pos.MarketPosition.ToString() == "Long")
    {
       rej.Set(1);
    }
    else if (pos.MarketPosition.ToString() == "Short")
    {
        rej.Set(-1);
    }
}


Reply With Quote
  #3 (permalink)
pxp333
Parlin NJ/USA
 
Posts: 7 since Mar 2015
Thanks Given: 0
Thanks Received: 0


Hi gregid,

I appreciate the reply. I got your code to compile but I didn't test it yet because I also need to discriminate the instrument for example "ES 06-15 Globex".

Would you be able to incorporate this into your code?

Also your code ideas got my brain going again and I was able to modify my code as follows to get it to work.


if (!Historical)
{rej.Set(0);
foreach (Account acct in Cbi.Globals.Accounts)
{PositionCollection positions=acct.Positions;
foreach (Position pos in positions)
if ((pos.Account.Name.ToString()=="Sim101")&&(pos.Instrument.ToString()=="ES 06-15 Globex"))
{if (pos.MarketPosition.ToString()=="Long")
{rej.Set(1);}
else if (pos.MarketPosition.ToString()=="Short")
{rej.Set(-1);}}}}

I forgot that foreach loops so it can parse the PositionCollection generated by the code. So I just added the higher if statement

if ((pos.Account.Name.ToString()=="Sim101")&&(pos.Instrument.ToString()=="ES 06-15 Globex")){}

I have seen on the web that others were trying to do similar accounts info queries from their strategies so us posting these codings here maybe can also help out.

Thanks


Reply With Quote
  #4 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

This is going to be completely dry coding on my side - I don't have any code editor at hand - so don't hold me accountable for this , but I would try:

 
Code
foreach (Position pos in from Account acct in Cbi.Globals.Accounts where acct.Name == "Sim101" select acct.Positions into positions from Position pos in positions where pos.Instrument=="ES 06-15 Globex" select pos)
{
    if (pos.MarketPosition.ToString() == "Long")
    {
       rej.Set(1);
    }
    else if (pos.MarketPosition.ToString() == "Short")
    {
        rej.Set(-1);
    }
}


Reply With Quote
  #5 (permalink)
pxp333
Parlin NJ/USA
 
Posts: 7 since Mar 2015
Thanks Given: 0
Thanks Received: 0

Hi gregid,


I needed to append .ToString() to the pos.Instrument== in your modified statement for it to compile.

I tested it and it works.

Thanks again!


Reply With Quote




Last Updated on April 1, 2015


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts