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

MAE/MFE moving average

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

    #16
    Originally posted by NinjaTrader_ChelseaB View Post
    Trader17,

    It looks like you are declaring double lastMAE in an if statement. It can only be used in that if statements action block and not outside of it.

    Perhaps declare these above the if statement?

    If these are not intended to persist, maybe don't use those variables at all?

    Code:
    if( Close[0] > Open[0])
    {
    EnterLong();
    
    if (SystemPerformance.RealTimeTrades.Count > 0)
    {
    Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];
    
    Draw.Text(this, "MAE"+CurrentBar, "MAE="+ lastTrade.MaeTicks.ToString(), 0, Low[0] - 15 * TickSize);
    Draw.Text(this, "MFE"+CurrentBar, "MFE="+ lastTrade.MfeTicks.ToString(), 0, Low[0] - 20 * TickSize);
    }
    }
    Also, note, the SystemPerformance collection is not updated until the position updates with OnPositionUpdate().
    Chelsea I am attaching a chart for better clarity. As you will see I am getting values of MAE and MFE not from the trade that just closed but the one that closed just before it. Also on one occasion you will see it got the value as usual and from the trade that just closed. You will see 4 values printed below the next trade entry bar. I was testing this on the SampleMACross Strategy to keep it simple. All I am looking for is to see the MAE/MFE of the last closed trade. Also I used Draw Text just above EnterLong() in the same block. Not sure why it would matter as we are updating the values before entry anyway and after the last trade closed.
    Also I tested your code above and it will not print any MAE/MFE Values on the chart.
    Thanks.
    Last edited by Trader17; 02-22-2022, 11:48 AM.

    Comment


      #17
      Hello Trader17,

      To confirm, you have resolved your previous compile error and you have a new inquiry on this same script correct?

      I am not able to see your chart.

      Instead of posting a chart, post a text file with the output from using prints.

      Print the time of the bar, print all values in the conditions that trigger the action.

      Below is a link to a forum post that demonstrates using prints to understand behavior.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Thanks Chelsea. I am seeing it print in the output window. Not sure about a text file of the same. I am seeing it print the values. But that is useless to me as I want to see it print values below a bar after a completed trade. I think I know the issue here. What I went ahead and did with the SampleMACross was use a Stop and Target instead of just reversing as in the original sample. Now there is at least a bar or more after each closed trade. So on the next trade open it does print correctly the MAE and MFE values. So it seems NT8 is not updating a closed position at the same it updates System Performance values. I guess that nano second/s lag was causing that behavior even though not sure why it would matter historically as all values are readily available.
        As you will see from the example the highlighted values are those from the last closed trade several bars back. Click image for larger version  Name:	ChelseaMAEMFE.JPG Views:	0 Size:	238.3 KB ID:	1191133
        Thanks.

        Comment


          #19
          Hello Trader17,

          The Position and SystemPerformance are both updated with OnPositionUpdate().

          Is your code in OnPositionUpdate()?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            No. I am just using OnBarUpdate() for everything. So in a case where you just reverse as in the SampleMACross you recommend using OnPositionUpdate()? I was thinking of maybe printing the values a bar or two after the new entry. Would that work too?

            protected override void OnPositionUpdate(Cbi.Posit ion position, double averagePrice,
            int quantity, Cbi.MarketPosition marketPosition)
            {
            Print("MAE=: " + lastTrade.MaeTicks);
            Print("MFE=: " +lastTrade.MfeTicks);
            }

            Will the above print (In my case I will be using Text) on each bar or I assume only once after a position is closed or twice when a position is opened too? I am basically looking to print the last trade's MAE and MFE right on the exit bar itself or a bar after or even the following entry bar.

            Thanks.
            Last edited by Trader17; 02-22-2022, 03:36 PM.

            Comment


              #21
              Hello Trader17,

              I am letting you know that the position won't be updated until OnPositionUpdate() if you are trying to access the trade MaeTicks.

              Or you could wait until the next time OnBarUpdate() updates to get these values.

              Your entry can remain in OnBarUpdate() if thats where you want the entry to be.

              The lastTrade variable would need to be assigned in OnPositionUpdate() for that suggested code to work. Yes, OnPositionUpdate would update for every position change including when a position is opened, changed, or closed. Your logic could check the position.MarketPosition if you want to print during specific position changes.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Thanks Chelsea. OnPositionUpdate() works too but at the time of reversal I see it further out than just after trade closed. Plus I do not want that text on the chart every time a position is updated as I am simply duplicating them many times and creating more clutter and confusion. What would be the best way to extract these values just once after the trade is closed?
                Thanks.

                Comment


                  #23
                  Hello Trader17,

                  If there is only 1 trade open and it closes, Position.MarketPosition will be MarketPosition.Flat.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello Trader17,

                    If there is only 1 trade open and it closes, Position.MarketPosition will be MarketPosition.Flat.
                    Thanks Chelsea. That again would probably need to be pushed out to bar after entry probably as it will be highly unlikely that we will get those values on the close bar under OnBarUpdate(). It is in reversal scenarios where I have seen it pushed out further.

                    And using MarketPositio.Flat works well a bar out but does not print if a position is reversed on a bar as technically we are not flat.

                    Thanks.
                    Last edited by Trader17; 02-23-2022, 01:16 PM.

                    Comment


                      #25
                      Hello Trader17,

                      OnPositionUpdate would give you the last trade Mae/Mfe immediately when the position closes.

                      If you want to wait in OnBarUpdate() for the next bar update after the position updates that is your choice to do.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Segwin, 05-07-2018, 02:15 PM
                      14 responses
                      1,789 views
                      0 likes
                      Last Post aligator  
                      Started by Jimmyk, 01-26-2018, 05:19 AM
                      6 responses
                      838 views
                      0 likes
                      Last Post emuns
                      by emuns
                       
                      Started by jxs_xrj, 01-12-2020, 09:49 AM
                      6 responses
                      3,294 views
                      1 like
                      Last Post jgualdronc  
                      Started by Touch-Ups, Today, 10:36 AM
                      0 responses
                      13 views
                      0 likes
                      Last Post Touch-Ups  
                      Started by geddyisodin, 04-25-2024, 05:20 AM
                      11 responses
                      63 views
                      0 likes
                      Last Post halgo_boulder  
                      Working...
                      X