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

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. ​
    Chris L.NinjaTrader Customer Service

    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.
        Chris L.NinjaTrader Customer Service

        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.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            6 views
            0 likes
            Last Post Christopher_R  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            166 responses
            2,235 views
            0 likes
            Last Post sidlercom80  
            Started by thread, Yesterday, 11:58 PM
            0 responses
            3 views
            0 likes
            Last Post thread
            by thread
             
            Started by jclose, Yesterday, 09:37 PM
            0 responses
            7 views
            0 likes
            Last Post jclose
            by jclose
             
            Started by WeyldFalcon, 08-07-2020, 06:13 AM
            10 responses
            1,415 views
            0 likes
            Last Post Traderontheroad  
            Working...
            X