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

Declaring variables and performing calculations

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

    Declaring variables and performing calculations

    New to NT and programming here. I would like my profit target to equal my stop loss on every trade and obviously on each trade the values are going to be different. My entry is a stop order triggered when the best value of either Close[0] or SMA[0] is hit. Stop loss for a buy would be the lowest low of previous 10 bars. This is not ideal because I really only care about the lowest low of the 2 bars right before the entry, but don't know how to program that.(Somehow using BarsSinceEntry in MIN(Low, 2)[BarsSinceEntry] maybe? Tried but failed.)

    Anyway, the price value for a long profit target would then be LongEntry+(LongEntry-Stop Loss). Profit:Loss=1:1 Here is my code:

    protected override void OnBarUpdate()
    {
    // Assign Long Stop Entry Price
    double LongStopEntry=(SMA(5)[0]<Close[0]? SMA(5)[0]: Close[0]);
    // Assign Short Stop Entry Price
    double ShortStopEntry= (SMA(5)[0]>Close[0]? SMA(5)[0]: Close[0]);

    // Assign Profit Targets
    double LongProfitTarget=LongStopEntry+(LongStopEntry-MIN(Low, 10)[0]);
    double ShortProfitTarget=ShortStopEntry-(MAX(High, 10)[0]-ShortStopEntry);

    // Assign Stop Losses
    double LongStopLoss=LongStopEntry-(LongProfitTarget-LongStopEntry);
    double ShortStopLoss=ShortStopEntry+(ShortStopEntry-ShortProfitTarget);

    SetProfitTarget("Enter LONG", CalculationMode.Price, LongProfitTarget);
    SetProfitTarget("Enter SHORT", CalculationMode.Price, ShortProfitTarget);



    My problem is that the profit target values don't calculate correctly at all when I backtest with no apparent pattern and I can't figure out why. It works for entries but not profit targets.
    Do I need to declare a method in order for it to calculate? I can only go off what Help says.
    Would be glad to hear your suggestions.

    #2
    MIN(Low, 2)[1];// would give you the lowest value of the prior two bars.

    You could set this to a variable when your enter condition was met so that its not being called each bar update.

    In the code above your targets are being set based off of the entry value. This entry value however is changing with each bar that passes. This is causing your targets to be based off of a different entry price each update.

    You will need to section your code in a way that prevents it from being changed every bar update.

    For useful debugging tips: http://www.ninjatrader.com/support/f...ead.php?t=3418
    LanceNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Lance View Post
      MIN(Low, 2)[1];// would give you the lowest value of the prior two bars.

      You could set this to a variable when your enter condition was met so that its not being called each bar update.

      In the code above your targets are being set based off of the entry value. This entry value however is changing with each bar that passes. This is causing your targets to be based off of a different entry price each update.

      You will need to section your code in a way that prevents it from being changed every bar update.

      For useful debugging tips: http://www.ninjatrader.com/support/f...ead.php?t=3418
      Thank you for your reply.
      So are you basically saying that the problem is in the bold segment of the code:

      protected override void OnBarUpdate()
      {
      // Assign Long Stop Entry Price
      double LongStopEntry=(SMA(5)[0]<Close[0]? SMA(5)[0]: Close[0]);
      // Assign Short Stop Entry Price
      double ShortStopEntry= (SMA(5)[0]>Close[0]? SMA(5)[0]: Close[0]);

      If so, how do I code it so that this calculation is used only when my conditions are true?
      Also I guess another way (if there is one) would be if somehow the entry price of a currently open position was stored and could be retrieved and used in the calculations, you wouldn't have to worry about OnBarUpdate since the entry price doesn't change. Is there something like this in NT?

      Comment


        #4
        Yes the entry price is stored once the order is placed.

        You can use Position.AvgPrice to get the price that was filled


        At first glance it looks like this would resolve your issue, however there could be other logic that needs debugging. You can always use different variables for state tracking as needed.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Lance View Post
          Yes the entry price is stored once the order is placed.

          You can use Position.AvgPrice to get the price that was filled


          At first glance it looks like this would resolve your issue, however there could be other logic that needs debugging. You can always use different variables for state tracking as needed.
          Ok here's what the it looks now but still doesn't work. When I backtest it , it exits the position on the very next bar after which it was filled every single time. without profit target or stop loss being reached. Do you have any ideas why?


          // Assign Long Stop Entry Price
          double LongStopEntry=(SMA(5)[0]<Close[0]? SMA(5)[0]: Close[0]);
          // Assign Short Stop Entry Price
          double ShortStopEntry= (SMA(5)[0]>Close[0]? SMA(5)[0]: Close[0]);

          // Assign Profit Targets
          double LongProfitTarget=Position.AvgPrice-MIN(Low, 10)[0];
          double ShortProfitTarget=MAX(High, 10)[0]-Position.AvgPrice;



          SetProfitTarget("Enter LONG", CalculationMode.Ticks, LongProfitTarget);
          SetProfitTarget("Enter SHORT", CalculationMode.Ticks, ShortProfitTarget);


          I have another question.
          I can't get tick historical data, only minute. My strategy requires that the price close beyond a certain point before I exit with a loss. However it's different when it comes to profit targets. Say for instance, I 'm in a long position and have a profit target. If my profit target is within a High/Low range but the bar didn't necessarily close above that price level, is there a way in NT to take me out of the position as soon as the price ticks up the target it hit?? Because in real life I would have exited the position as soon as the price ticked up to the target and would't have waited for the close.

          If there is, this would also solve my problem

          Thanks

          Comment


            #6
            Originally posted by rdaoogle View Post
            Ok here's what the it looks now but still doesn't work. When I backtest it , it exits the position on the very next bar after which it was filled every single time. without profit target or stop loss being reached. Do you have any ideas why?


            // Assign Long Stop Entry Price
            double LongStopEntry=(SMA(5)[0]<Close[0]? SMA(5)[0]: Close[0]);
            // Assign Short Stop Entry Price
            double ShortStopEntry= (SMA(5)[0]>Close[0]? SMA(5)[0]: Close[0]);

            // Assign Profit Targets
            double LongProfitTarget=Position.AvgPrice-MIN(Low, 10)[0];
            double ShortProfitTarget=MAX(High, 10)[0]-Position.AvgPrice;



            SetProfitTarget("Enter LONG", CalculationMode.Ticks, LongProfitTarget);
            SetProfitTarget("Enter SHORT", CalculationMode.Ticks, ShortProfitTarget);
            Showing a chart will clarify what you are saying much better.
            I have another question.
            I can't get tick historical data, only minute. My strategy requires that the price close beyond a certain point before I exit with a loss. However it's different when it comes to profit targets. Say for instance, I 'm in a long position and have a profit target. If my profit target is within a High/Low range but the bar didn't necessarily close above that price level, is there a way in NT to take me out of the position as soon as the price ticks up the target it hit?? Because in real life I would have exited the position as soon as the price ticked up to the target and would't have waited for the close.

            If there is, this would also solve my problem

            Thanks
            Look again. Even in BackTest, if the bar encompasses the target, the exit is scored as having happened at the target, not the extreme of the bar. However, to do that you must use tick data.

            No computer can accurately use data that does not exist. (Extrapolated data is ipso facto inaccurate). If you have only minute data, the only data exposed is the OHLC of the bar, and the computer can use that only .

            You might want to look at using Market Replay for your test, in that case. Not as convenient for backtesting, but in the absence of tick data, just about the only real choice.
            Last edited by koganam; 05-30-2013, 07:43 PM. Reason: Corrected spelling.

            Comment


              #7
              Originally posted by koganam View Post
              Showing a chart will clarify what you are saying much better.

              Look again. Even in BackTest, if the bar encompasses the target, the exit is scored as having happened at the target, not the extreme of the bar. However, to do that you must use tick data.

              No computer can accurately use data that does not exist. (Extrapolated data is ipso facto incacurate). If you have only minute data, the only data exposed is the OHLC of the bar, and the computer can use that only .

              You might want to look at using Market Replay for your test, in that case. Not as convenient for backtesting, but in the absence of tick data, just about the only real choice.

              So the conditions work but no the calculations above


              If NT has OCHL minute data only (no ticks) and my long target just happens to be somewhere in between H and C of an up bar but not exactly either one of those values, will ninja trader count the profit target as having been executed when backtesting? In real life the order would have likely been filled so wouldn't it make sense for the computer to be built to assume the same thing?
              Are you saying that when there is no tick data, computer can only use 1 of 4 OCHL values to execute trades in the past?
              Attached Files
              Last edited by rdaoogle; 05-30-2013, 02:34 PM.

              Comment


                #8
                Your limit/stop orders will be filled historically at the price submitted if they are contained within the OHLC

                This becomes an issue when both your stop and target are contained within the OHLC of a single bar. NinjaTrader has no way of knowing whether the stop or the target was hit first. In this case it will default to assuming the stop was hit first even if in live trading the target was hit first.

                I'd suggest playing around with the simulated data feed to generate trades for your strategy so that you can better understand how fills are handled in real time vs historical
                LanceNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by burtoninlondon, Today, 12:38 AM
                0 responses
                5 views
                0 likes
                Last Post burtoninlondon  
                Started by AaronKoRn, Yesterday, 09:49 PM
                0 responses
                13 views
                0 likes
                Last Post AaronKoRn  
                Started by carnitron, Yesterday, 08:42 PM
                0 responses
                11 views
                0 likes
                Last Post carnitron  
                Started by strategist007, Yesterday, 07:51 PM
                0 responses
                13 views
                0 likes
                Last Post strategist007  
                Started by StockTrader88, 03-06-2021, 08:58 AM
                44 responses
                3,982 views
                3 likes
                Last Post jhudas88  
                Working...
                X