Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

simple EMA strategy don't work as expected

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

    simple EMA strategy don't work as expected

    I am writing a simple EMA 20 strategy and try on a sim account. I found it doesn't work well. The execution seems to long/short not at the right timeframe (1 min) and it closes the position prematurely. Thank you in advanced if someone can help.

    Basic logic
    Bearish = EMA(20) across below the current close and previous close > current close ==> Sell
    Bullish = EMA(20) across above the current close and previous close > current close ==> Buy

    I used the strategy builder and the codes look like this

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"EMA";
    Name = "WizEMA";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Loss = 24;
    Profit = 48;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, 20);
    EMA1.Plots[0].Brush = Brushes.Lime;
    AddChartIndicator(EMA1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 2)
    return;

    // Set 1
    if ((CrossBelow(EMA1, Close, 1))
    && (Close[1] > Close[0]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"EMA");
    }

    // Set 2
    if ((CrossAbove(EMA1, Close, 1))
    && (Close[1] < Close[0]))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"EMA");
    }

    }

    #2
    Hello, thanks for writing in. The strategy is likely just activating the conditions that are written out. The best way to debug this is to use Prints so you can see the data the strategy is processing e.g.

    // Set 1
    if ((CrossBelow(EMA1, Close, 1))
    && (Close[1] > Close[0]))
    {
    Print("EnterLong 1 " + Time[0]);
    EnterLong(Convert.ToInt32(DefaultQuantity), @"EMA");
    }

    // Set 2
    if ((CrossAbove(EMA1, Close, 1))
    && (Close[1] < Close[0]))
    {
    Print("EnterShort 1 " + Time[0]);
    EnterShort(Convert.ToInt32(DefaultQuantity), @"EMA");
    }

    Also note that if your strategy calls EnterLong, then it hits the EnterShort signal, the long position will be automatically closed and the position will be reversed. ​

    Comment


      #3
      Somehow, I tried to use your sample codes "Print("EnterLong 1 " + Time[0]);" but the strategy prints the whole original string and not the current timestamp. I also tried string.format() and still not working. I am using the strategy builder or do I need to upload as a file to make it work.

      Comment


        #4
        Hi, in the strategy builder, you need to click Print>Add a string>Set>then choose the Time folder. This is how you print out variables. If you type in the code it will print a string literal.

        Comment


          #5
          Hi, now I manage it to use the custom strategy not the builder. However, this logic still not working. This is my first strategy and still have lots to learn the Ninjatrader API. If you can help me figure this one out, thank you in advanced.


          here is what I try to do
          If it crosses the 20 EMA and its in and uptrend it will LONG
          If it crosses the 20 EMA and its in and downtrend it will SHORT


          Probably the code is not able to find out the uptrend and downtrend so it fails. The attached screenshot, I also indicated what I expected and what the code did - not working.


          protected override void OnBarUpdate()
          {

          double emaValue = EMA(20)[0];
          double closeValue = Close[0];
          double pcloseValue = Close[1];
          string strStatus = string.Empty;

          Print(string.Format("Time:{0} ema20={1:0.00} close0={2:0.00} close1={3:0.00}", Time[0], emaValue, closeValue, pcloseValue));

          if (CrossBelow(ema20, Close, 1))
          strStatus = "EMA 20 below Close - ";
          else
          strStatus = "EMA 20 above Close - ";

          if (Close[1] > Close[0])
          strStatus += "downTrend";

          if (Close[1] < Close[0])
          strStatus += "upTrend";

          Print(string.Format("Time:{0} Current status:{1}", Time[0], strStatus));

          // Set 1
          if ((CrossBelow(ema20, Close, 1))
          && (Close[1] < Close[0]))
          {
          EnterLong(Convert.ToInt32(1));
          Print(string.Format("Time:{0} Enter Long. close[0]={1:0.00} close[1]={2:0.00}", Time[0], Close[0], Close[1]));
          }

          // Set 2
          if ((CrossAbove(ema20, Close, 1))
          && (Close[1] > Close[0]))
          {
          EnterShort(Convert.ToInt32(1));
          Print(string.Format("Time:{0} Enter Short. close[0]={1:0.00} close[1]={2:0.00}", Time[0], Close[0], Close[1]));
          }



          debug outputs:

          Time:1/29/2023 8:47:00 PM ema20=4076.22 close0=4077.00 close1=4077.25
          Time:1/29/2023 8:47:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:48:00 PM ema20=4076.25 close0=4076.50 close1=4077.00
          Time:1/29/2023 8:48:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:49:00 PM ema20=4076.25 close0=4076.25 close1=4076.50
          Time:1/29/2023 8:49:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:50:00 PM ema20=4076.27 close0=4076.50 close1=4076.25
          Time:1/29/2023 8:50:00 PM Current status:EMA 20 above Close - upTrend
          Time:1/29/2023 8:51:00 PM ema20=4076.27 close0=4076.25 close1=4076.50
          Time:1/29/2023 8:51:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:51:00 PM Enter Short. close[0]=4076.25 close[1]=4076.50
          NinjaScript strategy 'MyEMACross/286491400' submitting order
          Time:1/29/2023 8:52:00 PM ema20=4076.27 close0=4076.25 close1=4076.25
          Time:1/29/2023 8:52:00 PM Current status:EMA 20 above Close -
          Time:1/29/2023 8:53:00 PM ema20=4076.24 close0=4076.00 close1=4076.25
          Time:1/29/2023 8:53:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:54:00 PM ema20=4076.27 close0=4076.50 close1=4076.00
          Time:1/29/2023 8:54:00 PM Current status:EMA 20 below Close - upTrend
          Time:1/29/2023 8:54:00 PM Enter Long. close[0]=4076.50 close[1]=4076.00
          Time:1/29/2023 8:55:00 PM ema20=4076.27 close0=4076.25 close1=4076.50
          Time:1/29/2023 8:55:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:55:00 PM Enter Short. close[0]=4076.25 close[1]=4076.50
          Time:1/29/2023 8:56:00 PM ema20=4076.29 close0=4076.50 close1=4076.25
          Time:1/29/2023 8:56:00 PM Current status:EMA 20 below Close - upTrend
          Time:1/29/2023 8:56:00 PM Enter Long. close[0]=4076.50 close[1]=4076.25
          Time:1/29/2023 8:57:00 PM ema20=4076.28 close0=4076.25 close1=4076.50
          Time:1/29/2023 8:57:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 8:57:00 PM Enter Short. close[0]=4076.25 close[1]=4076.50
          Time:1/29/2023 8:58:00 PM ema20=4076.28 close0=4076.25 close1=4076.25
          Time:1/29/2023 8:58:00 PM Current status:EMA 20 above Close -
          Time:1/29/2023 8:59:00 PM ema20=4076.18 close0=4075.25 close1=4076.25
          Time:1/29/2023 8:59:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:00:00 PM ema20=4076.07 close0=4075.00 close1=4075.25
          Time:1/29/2023 9:00:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:01:00 PM ema20=4076.04 close0=4075.75 close1=4075.00
          Time:1/29/2023 9:01:00 PM Current status:EMA 20 above Close - upTrend
          Time:1/29/2023 9:02:00 PM ema20=4075.99 close0=4075.50 close1=4075.75
          Time:1/29/2023 9:02:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:03:00 PM ema20=4075.89 close0=4075.00 close1=4075.50
          Time:1/29/2023 9:03:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:04:00 PM ema20=4075.83 close0=4075.25 close1=4075.00
          Time:1/29/2023 9:04:00 PM Current status:EMA 20 above Close - upTrend
          Time:1/29/2023 9:05:00 PM ema20=4075.78 close0=4075.25 close1=4075.25
          Time:1/29/2023 9:05:00 PM Current status:EMA 20 above Close -
          Time:1/29/2023 9:06:00 PM ema20=4075.70 close0=4075.00 close1=4075.25
          Time:1/29/2023 9:06:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:07:00 PM ema20=4075.68 close0=4075.50 close1=4075.00
          Time:1/29/2023 9:07:00 PM Current status:EMA 20 above Close - upTrend
          Time:1/29/2023 9:08:00 PM ema20=4075.62 close0=4075.00 close1=4075.50
          Time:1/29/2023 9:08:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:09:00 PM ema20=4075.58 close0=4075.25 close1=4075.00
          Time:1/29/2023 9:09:00 PM Current status:EMA 20 above Close - upTrend
          Time:1/29/2023 9:10:00 PM ema20=4075.60 close0=4075.75 close1=4075.25
          Time:1/29/2023 9:10:00 PM Current status:EMA 20 below Close - upTrend
          Time:1/29/2023 9:10:00 PM Enter Long. close[0]=4075.75 close[1]=4075.25
          Time:1/29/2023 9:11:00 PM ema20=4075.59 close0=4075.50 close1=4075.75
          Time:1/29/2023 9:11:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:11:00 PM Enter Short. close[0]=4075.50 close[1]=4075.75
          Time:1/29/2023 9:12:00 PM ema20=4075.61 close0=4075.75 close1=4075.50
          Time:1/29/2023 9:12:00 PM Current status:EMA 20 below Close - upTrend
          Time:1/29/2023 9:12:00 PM Enter Long. close[0]=4075.75 close[1]=4075.50
          Time:1/29/2023 9:13:00 PM ema20=4075.62 close0=4075.75 close1=4075.75
          Time:1/29/2023 9:13:00 PM Current status:EMA 20 above Close -
          Time:1/29/2023 9:14:00 PM ema20=4075.58 close0=4075.25 close1=4075.75
          Time:1/29/2023 9:14:00 PM Current status:EMA 20 above Close - downTrend
          Time:1/29/2023 9:14:00 PM Enter Short. close[0]=4075.25 close[1]=4075.75

          Attached Files

          Comment


            #6
            Hello, thanks for the follow up. I apologize but I am not going to be able to spend any time debugging your code. There are existing examples in the platform that work and can server as a good example, see the SampleMaCrossover strategy for a MA cross example strategy.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            51 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            127 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X