Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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
Can you help answer these questions from other members on NexusFi?
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.