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

Entry and Exit Price are same

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

    Entry and Exit Price are same

    Hello, i wrote some code.

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 3)
    return;

    // Set 1
    inSideBar = (High[1] < High[2] && Low[1] < Low2);
    OxO = (High[1] - Low[1]);

    if (inSideBar)
    {
    if (Close[0] < (Low[1] - (1 * TickSize)));
    {
    Print (Time[0] + " H1:" + High[1] + " H2:" + High[2] + " C1:" + Close[1] + " C0:" + Close[0] + " 1x1:" + OxO);
    CandleOutlineBrushes [1]= Brushes.Yellow;
    EnterShort(Convert.ToInt32(DefaultQuantity), "inSideBar");
    }

    }


    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    SetStopLoss(@"inSideBar", CalculationMode.Price, Close [0] - OxO, false);
    SetProfitTarget(@"inSideBar", CalculationMode.Price, Close [0] -
    OxO);
    Print(OxO);
    }
    }
    }

    Some problems for me is showing in the attachment.
    It is the result after I turn off and on the tick replay in Strategy Analyzer

    Actually, I want to entry just right after every "insidebar" once.

    Thx
    Attached Files

    #2
    Hello,

    Thank you for the post.

    This looks to be the prices you are using. I see that you are Subtracting the OxO variable from the Close price for both the Target and Stop, you would need to adjust the Target and Stop prices to make a Band around the order price rather than being the same price.

    Here is a sample which uses Addition and Subtraction from the entries price:

    Code:
    if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
    {
    	SetStopLoss(@"inSideBar", CalculationMode.Price, execution.Price + OxO, false);
    	SetProfitTarget(@"inSideBar", CalculationMode.Price, execution.Price - OxO);
    }
    This would assume you are only entering short orders. Because you are using CalculationMode.Price you would need to manually calculate the Price you want. If you use Long orders you would likely need to add a condition to check if the order filled was long or short to adjust the addition/subtraction on the prices.

    Looking at the example it seems you may need to add some additional filtering to the OnExecution logic. Also, you may want to utilize the AvgEntry price in your calculations as the order may not have entered directly at Close[0]. The execution.Price is shown above in the example and you can locate a sample of OnExecutionUpdate that Filters Filled orders here: http://ninjatrader.com/support/helpG...ub=onexecution

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thx, and i also want to ask

      if (RSI1.Avg[0] > 20)
      {
      if (CrossAbove(MACD1.Default, MACD1.Avg, 20))
      {

      can i set the "MACD" condition no matter how many bars after the "RSI"?

      Comment


        #4
        Hello,

        Thank you for the post.

        I wanted to clarify, do you mean that once the are you asking that once the RSI condition becomes true, check the MACD condition every bar? If so, your logic should already do that.

        If you are instead asking to know how many bars since the RSI condition became true to adjust the LookBak period you are using, that would be more complex. Can you provide more detail on what specifically you want to do with these conditions?

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          1. It seem correct bcos I want to got the 30mins RSI and MACD under 5 mins chart, pls advise.
          2. i am still confuse about current bar and look back period,


          RSI1 = RSI(BarsArray[1], 9, 3);
          MACD1 = MACD(BarsArray[1], 10, 21, 7);

          protected override void OnBarUpdate()
          {
          if (CurrentBars[0] < 5 || CurrentBars[1] < 1) return;

          if (RSI1.Avg[0] < 80)
          {
          if (CrossBelow(MACD1.Diff, MACD1.Default, 20))

          Comment


            #6
            Hello Auntthree30,

            I am a little confused by your first question.

            Do you want to have the RSI function off of a 30 minutes data series and have the MACD function off of a 5 minute data series? As you are using BarsArray[1] (the first added secondary data series) in both indicators, both will use the same data series as input instead of 5 minute and 30 minute data series'.

            I am not sure where you are facing confusion with the look bar period and the current bar. Please elaborate on what you are confused with if my explanation does not provide further direction.

            You will need to make sure that a certain number of bars iterate for both added data series so the information that you are referencing in the indicators will exist. For example if the primary data series (BarsInProgress == 0 and BarsArray[0]) is a 5 minute data series and the second data series is 30 minutes, OnBarUpdate() for BarsInProgress == 0
            will iterate 6 times before one of the 30 minute bars (BarsInProgress == 1) completes. You will need as many bars completed to begin calculations in the script as you require for the look back period for each indicator.

            For more information on referencing barsAgo, referencing additional data series in an indicator, and using additional data series in a NinjaScript in general, please see the documentation linked below.

            Using [] brackets (barsAgo reference) - http://ninjatrader.com/support/forum...ad.php?t=19346

            Accessing Price Data in a Multi Series NinjaScript - http://ninjatrader.com/support/helpG...arsninjascript

            Bars Object as input to Indicator Methods - http://ninjatrader.com/support/helpG...dicatorMethods

            Complete Multi Time Frame and Instruments documentation - http://ninjatrader.com/support/helpG...nstruments.htm
            JimNinjaTrader Customer Service

            Comment


              #7
              Thx all of your help, the updated code as follow.

              1. It seemingly only appear in the same bar, how can i set the "MACD" can be trigger within ten bars after "RSI"
              2. I am add "Calculate = Calculate.OnBarClose;" and "Calculate = Calculate.OnEachTick;", is it Useful for reduce the computer resource depletion?

              Thx.


              else if (State == State.Configure)
              {
              AddDataSeries("NQ 09-17", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
              }
              else if (State == State.DataLoaded)
              {
              RSI1 = RSI(BarsArray[1], 9, 3);
              MACD1 = MACD(BarsArray[1], 10, 21, 7);
              }

              }

              protected override void OnBarUpdate()
              {
              if (BarsInProgress != 0) return;
              if (CurrentBars[0] < 5 || CurrentBars[1] < 30) return;

              if (RSI1.Avg[0] <= 80)

              {
              Draw.VerticalLine(this, @"DwRSI1", 0, Brushes.Red, DashStyleHelper.Solid, 5);

              if (CrossBelow(MACD1.Default, MACD1.Avg, 1))

              {
              Draw.VerticalLine(this, @"DwMACD1", 0, Brushes.Green, DashStyleHelper.Solid, 5);

              Calculate = Calculate.OnBarClose;

              inSideBar = (High[1] < High[2] && Low[1] < Low2);
              OxO = (High[1] - Low[1]);

              Calculate = Calculate.OnEachTick;
              if (inSideBar)
              {

              ShortBB = (Close[0] < (Low[1] - (1 * TickSize)));

              if (ShortBB)
              {
              Print (Time[0] + " H1:" + High[1] + " H2:" + High[2] + " C1:" + Close[1] + " C0:" + Close[0] + " 1x1:" + OxO);
              CandleOutlineBrushes [1]= Brushes.Yellow;
              EnterShort(Convert.ToInt32(DefaultQuantity), @"DwMB");
              }
              }

              Comment


                #8
                Hello Auntthree30,

                Thanks for your additional questions.

                1. It seemingly only appear in the same bar, how can i set the "MACD" can be trigger within ten bars after "RSI"
                I would recommend to check the number for CurrentBar value when the RSI condition occurs and save it to a variable. Let's call this variable SavedCurrentBar. You can then see if CurrentBar > SavedCurrentBar + 10 along side your MACD condition to restrict that condition to occur 10 bars after the RSI condition became true.

                2. I am add "Calculate = Calculate.OnBarClose;" and "Calculate = Calculate.OnEachTick;", is it Useful for reduce the computer resource depletion?
                Calculate.OnBarClose will use less resources as Calculate.OnEachTick as OnBarUpdate() will be called on the close of each bar rather than on each incoming tick that forms these bars.

                Further explanations and tips on Calculate can be found here: https://ninjatrader.com/support/help...?calculate.htm

                If you have any additional questions, please don't hesitate to ask
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ScottWalsh, 04-16-2024, 04:29 PM
                6 responses
                27 views
                0 likes
                Last Post ScottWalsh  
                Started by frankthearm, Today, 09:08 AM
                10 responses
                35 views
                0 likes
                Last Post frankthearm  
                Started by GwFutures1988, Today, 02:48 PM
                0 responses
                3 views
                0 likes
                Last Post GwFutures1988  
                Started by mmenigma, Today, 02:22 PM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by NRITV, Today, 01:15 PM
                2 responses
                9 views
                0 likes
                Last Post NRITV
                by NRITV
                 
                Working...
                X