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

Multi time Frame Strategy (Order of Entry Not Working Properly)

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

    Multi time Frame Strategy (Order of Entry Not Working Properly)

    Now the next step was to use the 15min 5ma along with my enterLong strategy.

    I was able to code this but there seems to be an issue.
    For the 5-minute chart conditioins, i need to have the last 2 candles plus the current 5-minute candle low to be >= the 5-minute sma.

    For the 15-minute chart condition, I need to have at least the current candle low to be >= the 15-minute 5ma.

    However, it will only trigger the long if all the 15-minute candle lows are >= the 15-minute sma within the whole condition of the 5-minute.

    I need it to trigger as long as the current 15-minute low >= the 15-sma regardless if the previous 15-minute low is not greater than the previous 15-minute sma while it is still calculating the 5-minute condition.

    This may sound confusing so I added some images. Hope this helps.

    If anyone can help me restructure the code the right way for it to work, it would be greatly appreciated. I am stuck here already for days.






    public class newplayaround : Strategy
    {
    private SMA SMA5;
    private SMA SMA15;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "newplayaround";
    Calculate = Calculate.OnEachTick;
    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;

    AddPlot(Brushes.Orange, "PLOTA");
    AddPlot(Brushes.Green, "PLOTB");
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 15);
    }
    else if (State == State.DataLoaded)
    {
    SMA15 = SMA(BarsArray[1],5);
    SMA5 = SMA(Close, 5);

    }
    }

    protected override void OnBarUpdate()
    {

    if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
    return;


    Values[0][0] = SMA15[0];
    Values[1][0] = SMA5[0];


    if ((Low[2] >= SMA5[2])
    && (Low[1] >= SMA5[1])
    && (Low[0] >= SMA5[0])
    && (High[0] > High[1])
    && (Lows[1][0] >= SMA(BarsArray[1], 5)[0]))



    {
    EnterLong(100,@"");
    }

    if (GetCurrentBid(0) < SMA5[0])
    {
    ExitLong(100,@"",@"");
    }

    }
    }
    }
    Attached Files

    #2
    Hello dorony,

    Thanks for your post.

    Each data series will call the OnBarUpdate() method. When the 5 minute calls the OnBarUpdate() all your code references like Low[2] will be correct. When the 15-minute bar calls the OnBarUpdate() then Low[2] will then point to the 15 minute Low.

    As a suggestion, after the CurrentBars check, you may want to use if(BarsInProgress !=0) return; This says that the code will only execute when the 5-minute bar calls the OnBarUpdate method.

    Please fully review the helpguide section on multi-time/series as the entire section will clarify important Multi-time/series concepts that will help you to create your script correctly: https://ninjatrader.com/support/help...nstruments.htm

    If you are finding that the code is not executing as expected then we recommend that you debug your strategy following the tips provided here: https://ninjatrader.com/support/foru...ead.php?t=3418
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hey Paul,

      I have a question. When I saw my entries trigger in a live environment, it would add two orders.

      I am thinking this is due to my SetStopLoss an SetProfitTarget was placed in State.Configure

      and now I think it should be in OnBarUpdate.

      I now placed it after

      ExitLong();
      else
      {
      SetProfitTarget(CalculationMode.Currency, 150);
      SetStopLoss(CalculationMode.Currency, 100);
      }

      Is this the correct way of doing it. I still havent yet to test whether this effected the strategy during the live market.

      Comment


        #4
        Hello dorony,

        Thanks for your reply.

        If you use the same stop level and profit level for each trade then it would be better to set them in State.Configure.

        You can certainly use them in OnBarUpdate() if you wish but if they don't change I don't see any effect of the extra coding. If placed in OnBarUpdate(), make sure they are set before the entry order because they take effect upon immediately on a filled entry order.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          So i could have the SetStopLoss and SetProfitTarget in State.Configure and have my indicator exit as well?

          Which ever hits first it will use correct.

          Ex. Either stoploss hits -100 or price is less than 5ma

          Comment


            #6
            Hello Patrick,

            I just had a live execution.

            I don't know why I am getting two orders when it should only be one.

            If I look back at historical, it changes to one order and the correct amount of shares.

            Is it becasue I am doing OnEachTick and not OnPriceChange
            Attached Files

            Comment


              #7
              Hello,

              Thanks for your reply.

              I recommend that you add print statements in each section so that you know if it is the logic conditions creating the extra order or if you have the exit condition at the same level as the setstop. If at the same level then it is possible that both could fire and could create an overfill condition. However when you have an overfill there would be a pop-up error message and the strategy would be disabled. As that didn't occur then that is why I suggest using print statements in your code to see when your enterlong is placed.
              Paul H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by llanqui, Today, 08:32 AM
              1 response
              5 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by lollers, Yesterday, 03:26 AM
              1 response
              51 views
              0 likes
              Last Post lollers
              by lollers
               
              Started by Salahinho99, 05-05-2024, 04:13 AM
              7 responses
              60 views
              0 likes
              Last Post Salahinho99  
              Started by knighty6508, 05-10-2024, 01:20 AM
              4 responses
              29 views
              0 likes
              Last Post knighty6508  
              Started by OllieFeraher, 05-09-2024, 11:14 AM
              6 responses
              19 views
              0 likes
              Last Post OllieFeraher  
              Working...
              X