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!
See more
See less

Partner 728x90

Collapse

Sporadic Draw.TextFixed() Refresh

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sporadic Draw.TextFixed() Refresh

    I have the following code in a strategy to plot PnL and display the current dollar amount centered over the plot:

    Code:
    protected override void OnBarUpdate() {
    
    if (BarsInProgress != 0 || CurrentBar < 1)
        return;
    
    // Plot cumulative strategy PnL
    cumProfit = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
    
    if (IncludeUnrealizedPnL)
        PnL[0] = cumProfit + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency);
    else
        PnL[0] = cumProfit;
    
    if (PnL[0] > 0)
        PlotBrushes[0][0] = Brushes.Green;
    else if (PnL[0] < 0)
        PlotBrushes[0][0] = Brushes.Red;
    else
        PlotBrushes[0][0] = Brushes.Gray;
    
    Draw.TextFixed(this, "PnL", cumProfit.ToString("C"), TextPosition.Center, ChartControl.Properties.ChartText, pnlFont, Brushes.Transparent, Brushes.Transparent, 0);
    
    ...
    The plot works reliably, but the dollar amount only updates sporadically, sometimes on every bar, but often not for hours. Is there a way to force Draw.TextFixed() to refresh?
    Last edited by caveat_lector; 08-26-2021, 11:24 AM.

    #2
    Hello caveat_lector,

    Thanks for your post.

    In the DrawText you are using the variable cumProfit and I think you want to be using the data series PnL[0].


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I initially wrote the code using the data series PnL[0] as shown below and had the sporadic dollar-value display problem. I thought that might have been due someway to converting a data series array value to a string and so changed the code to use cumProfit.ToString("C") as posted above.

      Code:
      Draw.TextFixed(this, "PnL", PnL[0].ToString("C"), TextPosition.Center, ChartControl.Properties.ChartText, pnlFont, Brushes.Transparent, Brushes.Transparent, 0)
      ;

      Both methods have exactly the same sporadic updating end-result.

      Comment


        #4
        Hello caveat_lector,

        Thanks for your reply.

        So the cumProfit variable is only updated when a trade completes. Does that match your observtion?

        If not I would suggest adding a print statement and print out the value of cumProfit on each OnBarUpdate() and compare to what is displayed.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          The cumProfit variable updates as each trade completes unless IncludeUnrealizedPnL is set true, in which case it updates each time OnBarUpdate() runs with new primary data series data. There is nothing wrong with those aspects, because correct values are plotted and the plot updates on each bar as it should. The cumProfit value is correct. The problem is that Draw.TextFixed only updates the value it displays sporadically.

          Comment


            #6
            Hello caveat_lector,

            Thanks for your reply.

            Your code will not update cumProfit with UnrealizedPNL when selected. Only the series PnL[0] is updated when unrealizedPNL added.

            In short the Draw.Text with cumProfit as currently shown will only update as each trade completes.

            To verify, please add a print statement that outputs the bar time, cumprofit and Pnl[0].


            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by StockTrader88, 03-06-2021, 08:58 AM
            45 responses
            3,992 views
            3 likes
            Last Post johntraderuser2  
            Started by TAJTrades, Today, 09:46 AM
            0 responses
            7 views
            0 likes
            Last Post TAJTrades  
            Started by rhyminkevin, Yesterday, 04:58 PM
            5 responses
            62 views
            0 likes
            Last Post dp8282
            by dp8282
             
            Started by realblubb, Today, 09:28 AM
            0 responses
            8 views
            0 likes
            Last Post realblubb  
            Started by AaronKoRn, Yesterday, 09:49 PM
            1 response
            19 views
            0 likes
            Last Post Rikazkhan007  
            Working...
            X