Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Logical to check the current CLOSE in a minute chart to a 8 EMA

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

    Logical to check the current CLOSE in a minute chart to a 8 EMA

    Can anyone help me to fix my if statement?
    My Code snippets:

    public class MyCustomStrategy : Strategy
    ...
    private EMA myEMA;
    //

    protected override void OnStateChange()
    ...
    if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }
    ...
    if (State == State.SetDefaults)
    ...
    if (State == State.Historical)
    {
    myEMA = EMA(8); // 8 EMA?
    }​

    //
    protected override void OnBarUpdate()
    ...
    if ( Close[0] < myEMA[0] ) // <---- this is my issue. Want to see if the current "Close" closed below the 8 minute EMA.
    ...
    Last edited by thumper57; 05-11-2024, 04:33 PM.

    #2
    Thanks Brandon! Yep, started out with the SB, but I did not include this condition.

    Comment


      #3
      if (Close[0] < myEMA[0]); // EMA[0] does not match the Price of the actual 8 EMA plotted when backtesting. The Plotted 8 minute EMA is way off and therefore the Exit event misses by either a full bar or a couple of points on the bar. Any idea how to verify and change the condition if necessary to capture the 8min EMA data at the Close[0] price?

      The Blue highlights where the condition should have been satisfied. The Yellow is where it satisfied it. Any ideas how to resolve this? Thanks!​
      Click image for larger version

Name:	Screenshot 2024-05-12 154641.png
Views:	73
Size:	19.7 KB
ID:	1303342


      Comment


        #4
        Brandon again, I really appreciate your feedback and guidance. What you say makes sense and I will check out the video and articles you mention.

        Question: Is the only way to accurately exit a position (let's say as in this example of Close[0] <= myEMA[0]) ExitLong(); is to use Calculate.OnPriceChange or Calculate.OnEachTick?

        Comment


          #5
          Hi Brandon,

          Please see the condition of "if (Close[0] < EMA2[0])" which I have printed for examination. This condition should FAIL at this point (see the printed values), yet it executes the next line which EXITS the trade. The values printed says it should fail. Any ideas?

          Order LONG: orderId='NT-00002-2834' account='Backtest' name='LONG' orderState=Working instrument='ES JUN24' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-05-08 09:17:00' gtd='2099-12-01' statementDate='2024-05-12'
          Exit LONG: orderId='NT-00002-2834' account='Backtest' name='LONG' orderState=Filled instrument='ES JUN24' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=5204 onBehalfOf='' id=-1 time='2024-05-08 09:17:00' gtd='2099-12-01' statementDate='2024-05-12' Close[0]: 5203.75< EMA: 5202.7822265993

          Code snippet:


          if (Close[0] < EMA2[0]);
          {
          ExitLong();
          Print("Exit LONG: " + myEntryOrder + " Close[0]: " + Close[0] + "< EMA: " + EMA2[0]);​​
          Attached Files

          Comment


            #6
            Hi Brandon (or if n/a now, anyone),

            Attached is the output BEFOR and AFTER the ExitLong() After the inexplicable condition evaluates as True. Exact same values again.

            Order SHORT: orderId='NT-00000-167' account='Backtest' name='SHORT' orderState=Working instrument='ES JUN24' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-05-08 09:08:00' gtd='2099-12-01' statementDate='2024-05-13'
            Exit SHORT: BEFOREorderId='NT-00000-167' account='Backtest' name='SHORT' orderState=Filled instrument='ES JUN24' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=5202.5 onBehalfOf='' id=-1 time='2024-05-08 09:08:00' gtd='2099-12-01' statementDate='2024-05-13' Close[0]: 5204 < EMA: 5203.56423171032
            Exit SHORT: AFTER orderId='NT-00000-167' account='Backtest' name='SHORT' orderState=Filled instrument='ES JUN24' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=5202.5 onBehalfOf='' id=-1 time='2024-05-08 09:08:00' gtd='2099-12-01' statementDate='2024-05-13' Close[0]: 5204 < EMA: 5203.56423171032​​

            Comment


              #7
              Here it is. Thank you for your help.
              Attached Files

              Comment


                #8
                I just realized I referenced the Exit SHORT and meant to reference the Exit LONG as I did previously and shown in the chart snapshot. I apologize for the confusion. However, the problem remains and is more easily see with the LONG data.

                The LONG trade is between 8:50 and 9:10am on the chart.

                Attached is my strategy *.zip. Please let me know if you need more information.
                Attached Files

                Comment


                  #9
                  Hi Brandon, Here is the Strategy Analyzer setup. The strategy sets all the necessary chart indicators and settings but the dates (5/8 - 5/9) and the ES Jun24 instrument. The chart from the Strategy Analyzer shows the Screen Shot snipet previously included where the Long position is entered and then immediately exited with the erroneous condition if (Close[0] < EMA2[0]) is appears to calculate Close[0] LOWER than EMA2[0] when the output before and after print statements around the ExitLong() shows that the Close[0] value is actually much higher than the EMA2[0].

                  Hope this helps. Is there anything else needed?

                  Dave
                  Attached Files

                  Comment


                    #10
                    Excuse me while I bang my head on the wall. It couldn't have been me, right? I should have gotten more sleep. At least my short condition works. I am soooo sorry for my stupidity! However, thanks again. I will go away for a while. Golly!!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    62 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    134 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    75 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    50 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X