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

challenge - onprice change startegy base on signal coming from on bar close indicator

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

    challenge - onprice change startegy base on signal coming from on bar close indicator

    Hello,

    I have a challenge, I am trying to use the trailingstop that is updating onpricechange and not onbarclose.

    Basically ( see picture below)

    1- if the price goes below the green line entry long with trailing stop
    2- if the price goes above the red line entry short with trailing stop

    the problem if I call that indicator into the strategy , the indicator is going to calculate the line on each price change.

    The green and red line in image below are based on an custom indicator that is calculate the line onbarclose.

    is it possible to use the red / green line values calculated onbarclose or as seen on the screen into a strategy that calculate onpricechange ?

    any example?

    the bar are a custom bar line break chart with wicks

    thank you

    Click image for larger version

Name:	challenge.JPG
Views:	301
Size:	88.0 KB
ID:	1091902


    #2
    Hello madams212121,

    Thanks for your post.

    An indicator added to a strategy will calculate based on the Calculate mode of the strategy. If you would like to force the indicator to calculate following OnBarClose operations, you can use IsFirstTickOfBar in the indicator and use BarsAgo 1 references instead of BarsAgo 0 references. The Help Guide provides an example for how that logic would be set up.

    IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

    I have also attached a modification of the SMA indicator so it is forced to calculate on bar closes. The indicator is also updating i's current plot value with the plot value that was assigned on bar close. The current plot value is not being re-calculated but using the the last value calculated.

    We look forward to assisting.
    Attached Files
    Last edited by NinjaTrader_Jim; 03-27-2020, 03:53 PM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello, thank you for you answer but here few issue.

      If you use one bar ago then you are going to be delayed by 1 bar -not really good a strategy

      I used your example provided and put in into a strategy and reading the result nothing show up.

      here the code I used in the strategy .


      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
       public class Eachticktesting : Strategy
       {  private  SMAForceOnBarClose SMA1;
        protected override void OnStateChange()
        {
         if (State == State.SetDefaults)
         {
          Description         = @"Enter the description for your new custom Strategy here.";
          Name          = "Eachticktesting";
          Calculate         = Calculate.OnPriceChange;
          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.IgnoreAllErrors;
          StopTargetHandling       = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade       = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          Periods     = 14;
         }
         else if (State == State.Configure)
         {
          SMA1 = SMAForceOnBarClose (Periods);
         }
        }
      
        protected override void OnBarUpdate()
        {
         if (BarsInProgress != 0)
          return;
      
         if (CurrentBar < BarsRequiredToTrade)
          return;
      
         Log(" Strategy SMA : " + SMA1[1], LogLevel.Information);  
        }
      
        #region Properties
        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name="Periods", Order=1, GroupName="Parameters")]
        public int Periods
        { get; set; }
        #endregion
      
       }
      }

      here the result I am getting

      Click image for larger version

Name:	capture.JPG
Views:	205
Size:	185.3 KB
ID:	1092010

      Any advice ?

      Attached Files

      Comment


        #4
        I dit a little modification on the strategy and it look like I am getting data every 30 sec which match the time frame but still no data are getting received


        Code:
         
        if (IsFirstTickOfBar)
           {
           Log(" Strategy SMA : " + SMA1[1], LogLevel.Information);  
           }


        Click image for larger version

Name:	capture.JPG
Views:	200
Size:	152.6 KB
ID:	1092014

        I even tried to print 20 candle ago still no result.

        Advice ?

        Comment


          #5
          Hello madams212121,

          Keep in mind, bar closures are signaled on the first tick of a new bar. Checking BarsAgo 1 on the first tick of a new bar is the same thing as referencing BarsAgo 0 when calculating on bar closes.

          I had updated a different example I was preparing by mistake. I updated my post to include an updated sample and also a strategy that cna be used to test.

          Let me know if there is anything else I can do to help.
          JimNinjaTrader Customer Service

          Comment


            #6
            thank you that helps a lot.

            I can run your script and it looks like generating the right amount print in the window based on what is happening

            As you can see from the picture below the SMA print great , the function call the indicator also print the right value but the code in the strategy doesn't get the value.



            Click image for larger version

Name:	call.JPG
Views:	202
Size:	34.1 KB
ID:	1092048



            I have attached the code for both the strategy and the indicator


            Comment


              #7
              Hi NinjaTrader_Jim,

              I just want to be clear on the bar referencing logic you described. If a strategy updates on every tick, and IsFirstTickOfBar = true, then any reference using [1], such as Close[1], should refer to the close of the just-completed bar, and any reference using [0] such as Open[0] should refer to the bar that just started forming. Is that correct? Thanks.

              Gordon

              Comment


                #8
                Hello madams212121,

                My example sets the previous plot value on the first tick of a new bar and also assigns the current (developing) plot value to this value. The plot values only change on bar closes and the developing bar is also updated with this last calculated value. I recommend following this approach.

                I.E.

                Code:
                if (Entry_threshold == 1)
                {   
                    entry_signal_long = Close[1]; 
                    Entry_long [1] =  entry_signal_long;
                    Entry_long [0] = Entry_long [1];
                }
                grose, yes this is correct. IsFirstTickOfBar is the first tick of the developing bar, so referencing BarsAgo 1 here would represent the bar that had just closed.

                I look forward to assisting.
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                44 views
                0 likes
                Last Post jeronymite  
                Started by Barry Milan, Yesterday, 10:35 PM
                7 responses
                20 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by AttiM, 02-14-2024, 05:20 PM
                10 responses
                179 views
                0 likes
                Last Post jeronymite  
                Started by ghoul, Today, 06:02 PM
                0 responses
                10 views
                0 likes
                Last Post ghoul
                by ghoul
                 
                Started by DanielSanMartin, Yesterday, 02:37 PM
                2 responses
                13 views
                0 likes
                Last Post DanielSanMartin  
                Working...
                X