NexusFi: Find Your Edge


Home Menu

 





Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?


Discussion in Traders Hideout

Updated
      Top Posters
    1. looks_one ratfink with 9 posts (15 thanks)
    2. looks_two resist with 8 posts (10 thanks)
    3. looks_3 gregid with 5 posts (7 thanks)
    4. looks_4 vincentmarchi with 5 posts (0 thanks)
      Best Posters
    1. looks_one Tasker_182 with 4 thanks per post
    2. looks_two ratfink with 1.7 thanks per post
    3. looks_3 gregid with 1.4 thanks per post
    4. looks_4 resist with 1.3 thanks per post
    1. trending_up 21,310 views
    2. thumb_up 38 thanks given
    3. group 6 followers
    1. forum 32 posts
    2. attach_file 4 attachments




 
Search this Thread

Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?

  #21 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


ratfink View Post
I have no idea why you don't see it working when feed and monitor are in different charts, that's how this was tested and I run loads of my own other similar stuff, both single and multi-threaded, with no problems.

Hi @resist

Unless you were doing historical testing?, it was only created as a live monitor.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Are there any eval firms that allow you to sink to your …
Traders Hideout
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
GFIs1 1 DAX trade per day journal
18 thanks
  #22 (permalink)
 MrTrader 
ITAJAI SC/BRAZIL
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Clear Corretora
Trading: DOLFUT, WINFUT
Posts: 332 since Jun 2014
Thanks Given: 1,314
Thanks Received: 225


ratfink View Post

Communication back to the strategy is easy too, I can add an example of that when I am next on PC (ChromeBook only at moment.)

that would be nice

Reply With Quote
Thanked by:
  #23 (permalink)
 resist 
Berlin Germany /Alicante Spain
 
Experience: Intermediate
Platform: NinjaTrader
Broker: DDT/Rithmic, Global/CQG, IB, Mirus(Ninja)/MarketDelta
Trading: Futures
Posts: 141 since Jan 2010
Thanks Given: 362
Thanks Received: 67



ratfink View Post
Hi @resist


Unless you were doing historical testing?, it was only created as a live monitor.

@ratfink (I learned )
yes live with Topsteptrader account but I will retest it later with Sim101 account - should it work with Replay? I think so.

Reply With Quote
Thanked by:
  #24 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


resist View Post
@ratfink (I learned )
yes live with Topsteptrader account but I will retest it later with Sim101 account - should it work with Replay? I think so.

Yes, should do fine. Cheers.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #25 (permalink)
 resist 
Berlin Germany /Alicante Spain
 
Experience: Intermediate
Platform: NinjaTrader
Broker: DDT/Rithmic, Global/CQG, IB, Mirus(Ninja)/MarketDelta
Trading: Futures
Posts: 141 since Jan 2010
Thanks Given: 362
Thanks Received: 67

@ratfink

if I put in my strategy (in OnBarUpdate() area)

{
rfkCList.set(Instrument.FullName, Close[0]);
}

would that be enough?

Reply With Quote
Thanked by:
  #26 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


resist View Post
@ratfink

if I put in my strategy (in OnBarUpdate() area)

{
rfkCList.set(Instrument.FullName, Close[0]);
}

would that be enough?

Yes, should be fine.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #27 (permalink)
 resist 
Berlin Germany /Alicante Spain
 
Experience: Intermediate
Platform: NinjaTrader
Broker: DDT/Rithmic, Global/CQG, IB, Mirus(Ninja)/MarketDelta
Trading: Futures
Posts: 141 since Jan 2010
Thanks Given: 362
Thanks Received: 67


ratfink View Post
Yes, should be fine.

Thank you VERY MUCH. @ratfink

Short report:
I tried with Replay101 account and I think it works also if feed indicatr is in another chart than position indicator but position indicator has always to be started last.

It also works from strategy directly (see above) but I think itīs running out of sync.
If you restart position indicator and next restart strategy than it seems that it is sync and right values.

I also put in a position counter and average position calculation (total value / counter).

Just now I am thinking about calling position indicator directly in each strategy, but that would mean that position indicator is not started once but because I run multiple strategies (separate for long and short and as I told before separate for each contract) about 22 times.
Now I have doubts if there could occur problems with the global settings when position indicator is called more than one time.

I would call the indicator that way:
private rfkPositionMonitor ratfink; // Variables section
ratfink=rfkPositionMonitor ("Replay101"); // initialize section - account name I would make a get set later

What do you think about multiple times calling position indicator via strategy?

Reply With Quote
Thanked by:
  #28 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


resist View Post
Thank you VERY MUCH. @ratfink

Short report:
I tried with Replay101 account and I think it works also if feed indicatr is in another chart than position indicator but position indicator has always to be started last.

It also works from strategy directly (see above) but I think itīs running out of sync.
If you restart position indicator and next restart strategy than it seems that it is sync and right values.

I also put in a position counter and average position calculation (total value / counter).

Just now I am thinking about calling position indicator directly in each strategy, but that would mean that position indicator is not started once but because I run multiple strategies (separate for long and short and as I told before separate for each contract) about 22 times.
Now I have doubts if there could occur problems with the global settings when position indicator is called more than one time.

I would call the indicator that way:
private rfkPositionMonitor ratfink; // Variables section
ratfink=rfkPositionMonitor ("Replay101"); // initialize section - account name I would make a get set later

What do you think about multiple times calling position indicator via strategy?

The idea is that the position indicator only exists once, it should not be called from multiple places.

The feed indicator (or code) can be called any number of times, but is only required to be called once per instrument.

A synchronisation issue should only show up if some things are running COBC=false and some are running COBC=true, even then it will be a transient currency amount that your strategy positions would be experiencing anyway. Unless of course there are bugs to fix.

You can simply access the ongoing total value directly in your strategy e.g.:

 
Code
Print("Pos value " + rfkMonitor.totalValue);

Happy to work on developing this as we go, but it shouldn't need to be complicated.

Cheers

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #29 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425

Just checked in my setup, I have the Feeds running in COBC=false (this is the code default), otherwise price update can be delayed due to Ninja random chart/strat/indy processing order.

To get a test/illustration clear in your mind, do the following:

1) Make no changes to your existing strategies.

2) Create a separate Ninja Workspace, will be running in background.

3) Add two charts to this new workspace.

3a) To the first chart add one series for each of your instruments, e.g. 1min, or your t/f choice.
3b) To each instrument series in this chart add one feed indicator, making sure to select the correct series source each time. They will be invisible as Overlay is true and no plots are made.

4) Make the other chart of whatever series has the widest session span e.g. ES, again 1min or your t/f choice.
4a) Just add the positionMonitor to this other chart. If COBC is true (the code default) it will be a delayed position update at that bar, or if switched to false it will be a live rolling update.

You should see that the position total plots fine all the while regardless of when you start and stop strategies, or use ChartTrader.

If only one instrument is active you should see the the monitor value matches ChartTrader's value (can be in a third chart) so long as the monitor is switched to COBC=false. I left the default set at true to match strategies which are usually run in COBC=true.

Let me know if this helps.

Thought: If it is easier/better maybe use the Market Analyser route that @Tasker_182 suggested?

Edit: just realised you maybe can't get back the info you want that way, I don't know the MA hardly at all.

Cheers

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #30 (permalink)
 resist 
Berlin Germany /Alicante Spain
 
Experience: Intermediate
Platform: NinjaTrader
Broker: DDT/Rithmic, Global/CQG, IB, Mirus(Ninja)/MarketDelta
Trading: Futures
Posts: 141 since Jan 2010
Thanks Given: 362
Thanks Received: 67


I am working a lot backtesting my strategies using your indicator and I can confirm that itīs working fine if you start strategy (of course) after starting indicator. I have still position and feed in one multichart together, strategies are taking the global variable of totalvalue.

Working with MA makes no sense to me because I need the total value or average value in my strategy (I also added a calculation for average value as global variable, itīs working, too but has not improved strategy).

As I made some interesting observations my question to you:
Could you add a second account - I would like to test opening "dummy" positions on SIM parallel with real account. I think you got my question, if not, again: I want to open e.g. YM, CL, ES trades on SIM and CL on trading account but I need the total of all 4 positions.

Thank you again. @ratfink

Reply With Quote
Thanked by:




Last Updated on March 21, 2020


© 2024 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 - Privacy Policy - Downloads - Top
no new posts