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

Multiple profit targets and BE point - BigMike

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

    Multiple profit targets and BE point - BigMike

    Hi, I am trying to develop a code for initial stop loss, moving the stop loss to breakeven point (if price goes my way), move stop loss to target 1, move stop loss to target 2, move stop loss to target 3

    I was browsing around and found this link:
    private void GoLong() { SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false); SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize)); EnterLong("target1"); } private void ManageOrders() { if (Position.MarketPosition == MarketPosition.Long) { if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize)) SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice …


    It is a video tutorial from bigmike. Very good tutorial.

    His stop losses, breakeven points and targets are based on Points. I want to use percentages.

    I edited the code, but It seems It's not working properly. Please help as I am not a expert programmer and all of this is very new to me!

    {
    /// <summary>
    /// Big Mike - 1/26/10 - http://www.bigmiketrading.com/beginn...-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")]
    public class CrossoverTargets : Strategy
    {
    //#region Variables

    // targets based on percentage, when I change the targets to 0.01, I get an error b/c of ''int"
    private int target1 = 12;
    private int target2 = 10;
    private int target3 = 24;

    //stop loss based on points/ticks. I want to make the stoploss 1 penny, so 0.01.

    private int stop = 12;
    private bool be2 = true;
    private bool be3 = true;


    //#endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {

    CalculateOnBarClose = true;
    EntryHandling = EntryHandling.UniqueEntries;

    }

    private void GoLong()
    {

    // stoplosses based on entry price minus the stoploss points/ticks that I've assigned above.

    SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice - (Stop*TickSize), false);
    SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice - (Stop*TickSize), false);
    SetStopLoss("target3", CalculationMode.Price,Position.AvgPrice - (Stop*TickSize), false);

    //profit targets based on percentage. So if price moves up 0.01% I want that to be my first target.. and so on. This is why I added a 1+ next to all the targets. In match.. 1.001*Entryprice should give me the target.

    SetProfitTarget("target1", CalculationMode.Price, Position.AvgPrice * ((1+Target1)*TickSize));
    SetProfitTarget("target2", CalculationMode.Price, Position.AvgPrice * (((1+Target1)+(1+Target2))*TickSize));
    SetProfitTarget("target3", CalculationMode.Price, Position.AvgPrice * (((1+Target1)+(1+Target2)+(1+Target3))*TickSize));

    EnterLong("target1");
    EnterLong("target2");
    EnterLong("target3");


    }
    private void GoShort()
    {
    SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Stop*TickSize), false);
    SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice + (Stop*TickSize), false);
    SetStopLoss("target3", CalculationMode.PricePosition.AvgPrice + (Stop*TickSize), false);

    SetProfitTarget("target1", CalculationMode.Price, Position.AvgPrice * ((1-Target1)*TickSize));
    SetProfitTarget("target2", CalculationMode.Price, Position.AvgPrice * (((1-Target1)+(1-Target2))*TickSize));
    SetProfitTarget("target3", CalculationMode.Price, Position.AvgPrice * (((1-Target1)+(1-Target2)+(1-Target3))*TickSize));

    EnterShort("target1");
    EnterShort("target2");
    EnterShort("target3");


    }

    private void ManageOrders()
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
    SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);

    if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
    SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);

    }
    if (Position.MarketPosition == MarketPosition.Short)
    {
    if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
    SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);

    if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
    SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);

    }

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    EntryHandling = EntryHandling.UniqueEntries;

    ManageOrders();

    // Condition set 1
    if (CrossAbove(CCI(14), 250, 1))
    EnterShort();

    // Condition set 2
    if (CrossBelow(CCI(14), 250, 1))
    EnterLong();
    }

    //#region Properties
    [Description("")]
    [Category("Parameters")]
    public int Target1
    {
    get { return target1; }
    set { target1 = Math.Max(1, value); }
    }
    [Description("")]
    [Category("Parameters")]
    public int Target2
    {
    get { return target2; }
    set { target2 = Math.Max(1, value); }
    }
    [Description("")]
    [Category("Parameters")]
    public int Target3
    {
    get { return target3; }
    set { target3 = Math.Max(1, value); }
    }
    [Description("")]
    [Category("Parameters")]
    public int Stop
    {
    get { return stop; }
    set { stop = Math.Max(1, value); }
    }
    [Description("")]
    [Category("Parameters")]
    public bool BE2
    {
    get { return be2; }
    set { be2 = value; }
    }
    [Description("")]
    [Category("Parameters")]
    public bool BE3
    {
    get { return be3; }
    set { be3 = value; }
    }


    //#endregion
    }
    }
    The private "int" target 1, obviously need to be a lot smaller (but for some reason when I try to change it to .. let's say 0.001, I get an error) , since I am trying to obtain target 1 by doing "day's opening price *1.001" (.01% target).

    Thank you for all the help!
    Last edited by staycool3_a; 05-15-2013, 09:56 PM.

    #2
    Hi Calhawk01,

    Thank you for posting.

    All SetStopLoss and ProfitTargets have the ability to set the calculation mode, to either Price, Points, Percent.

    Code:
    SetStopLoss("target1", [B]CalculationMode.Percent[/B], Position.AvgPrice - (Stop*TickSize), false);
    You will need to change all you SetStopLoss and SetProfitTargets to have the percent calculation mode.

    For the smaller number on the private int target1, I would recommend using a private double instead, it will allow you have a decimal number and is required to use for Percent since it's a double.

    Code:
    private double target1 = .01;
    Please let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      hi CAL, how come the below does not work?

      //stoploss below 1 cent to the open pirce

      SetStopLoss("", CalculationMode.Price, CurrentDayOHL(open).CurrentOpen - 0.01;

      also there does not seem to be an explanation on calcmode.price/tick/percent, i kind of have an idea but am guessing bc i cant find an exact definition anywhere
      Last edited by staycool3_a; 05-16-2013, 08:55 AM.

      Comment


        #4
        Calhawk01,

        The calculation mode price, will be the price that you are seeing coming in the data.
        Example, if i were trading the S&P Mini and the current open was 1555.25. The stop loss would be set to 1555.24, however the tick size of the S&P is .25. Therefore if the price were to drop one tick it would become 1555.00, surpassing my stop loss.

        If I were trading stocks and the tick size was .01 then I would move 1 cent below the price.
        Your code should work, however it is missing a closing parenthesis.
        Code:
        SetStopLoss("", CalculationMode.Price, CurrentDayOHL().CurrentOpen - 0.01[B])[/B];
        Also, try using the Print() function to see what values are being generated.
        Code:
        Print(CurrentDayOHL().CurrentOpen - 0.01)
        Below is a link to the online help guide on the Print() function
        http://www.ninjatrader.com/support/h...html?print.htm

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Cal View Post
          Calhawk01,

          The calculation mode price, will be the price that you are seeing coming in the data.
          Example, if i were trading the S&P Mini and the current open was 1555.25. The stop loss would be set to 1555.24, however the tick size of the S&P is .25. Therefore if the price were to drop one tick it would become 1555.00, surpassing my stop loss.

          If I were trading stocks and the tick size was .01 then I would move 1 cent below the price.
          Your code should work, however it is missing a closing parenthesis.
          Code:
          SetStopLoss("", CalculationMode.Price, CurrentDayOHL().CurrentOpen - 0.01[B])[/B];
          Also, try using the Print() function to see what values are being generated.
          Code:
          Print(CurrentDayOHL().CurrentOpen - 0.01)
          Below is a link to the online help guide on the Print() function


          Let me know if I can be of further assistance.
          If i want to get set my stoploss below opening price i guess it would be smarter to calculate how many ticks you are away from opening price and subtract an additional tick

          SetStopLoss("", CalculationMode.Tick, (Position.AvgPrice - CurrentDayOHL().CurrentOpen) - 1;

          Is the formula above correct?

          Also how do I print things on my chart? I looked at the link you sent me but that does not explain how I actually go about adding a print on my chart to verify my codes

          Comment


            #6
            Calhawk01,

            Keep the code the same as before,
            Code:
            SetStopLoss("", CalculationMode.Price, CurrentDayOHL().CurrentOpen - 0.01);
            However, add in the TickSize to the calculation portion and change the number to 1 -
            Code:
            SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen - (1 * TickSize))));
            This will multiply 1 by the Tick Size of the instrument, in other words one tick size below the current open.

            If you want to draw things on the chart you will need to add draw objects. Below is a reference sample on drawing objects on the chart
            http://www.ninjatrader.com/support/f...ead.php?t=3419

            The Print() will print out the values in the Output window, Tools>Output Window, and not o the chart.

            Please let me know if I can be of further assistance.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              CAL, thx for the help

              however,

              SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen - (1 * TickSize))));

              is giving me an error, "Operator '-' cannot be applied to operands of type 'NinjaTrader.Data.DataSeries' and 'double'''

              Comment


                #8
                Calhawk01,

                Apologies, there is an index missing for the CurrentDayOHL.

                Here is the correct Syntax, I have highlighted the corrected part -

                Code:
                SetStopLoss(CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[B][0][/B] - (1 * TickSize))));
                Please let me know if I can be of further assistance.
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by calhawk01 View Post
                  CAL, thx for the help

                  however,

                  SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[0] - (1 * TickSize))));

                  is giving me an error, "Operator '-' cannot be applied to operands of type 'NinjaTrader.Data.DataSeries' and 'double'''
                  You must query a DataSeries by an index, in order to get a value.

                  Comment


                    #10
                    Originally posted by NinjaTrader_Cal View Post
                    Calhawk01,

                    Apologies, there is an index missing for the CurrentDayOHL.

                    Here is the correct Syntax, I have highlighted the corrected part -

                    Code:
                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[B][0][/B] - (1 * TickSize))));
                    Please let me know if I can be of further assistance.
                    Hi CAL, no worries.

                    Right now I'm using

                    SetStopLoss(CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[0] - (1 * TickSize))));

                    The above compiles perfectly. I get zero errors. HOWEVER, when I try to add this strategy backtesting or real time testing, I'm getting an unusual behavior.

                    For some strategy reason, even though I select this strategy, the settings for this strategies do not change, and when backtested or realtime testing, the orders are not based on the variables from this strategy.. they are based on different strategies that I have saved.

                    I have already made sure that the code for this strategy is not referring to any other strategy.. I even started from scratch a simple strategy with the above stoploss setting.

                    Any idea what's going on?


                    Here is my entire code:

                    Code:
                    #region Using declarations
                    using System;
                    using System.ComponentModel;
                    using System.Diagnostics;
                    using System.Drawing;
                    using System.Drawing.Drawing2D;
                    using System.Xml.Serialization;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Data;
                    using NinjaTrader.Indicator;
                    using NinjaTrader.Gui.Chart;
                    using NinjaTrader.Strategy;
                    #endregion
                    
                    // This namespace holds all strategies and is required. Do not change it.
                    namespace NinjaTrader.Strategy
                    {
                        /// <summary>
                        /// Enter the description of your strategy here
                        /// </summary>
                        [Description("Enter the description of your strategy here")]
                        public class XYZ: Strategy
                        {
                            #region Variables
                            // Wizard generated variables
                            // User defined variables (add any user defined variables below)
                            #endregion
                    
                            /// <summary>
                            /// This method is used to configure the strategy and is called once before any strategy method is called.
                            /// </summary>
                            protected override void Initialize()
                            {
                                SetProfitTarget("", CalculationMode.Percent, 0.005);
                                SetStopLoss(CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[0] - (1 * TickSize))));
                                CalculateOnBarClose = false;
                            }
                    
                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            protected override void OnBarUpdate()
                            {
                                // Condition set 1
                         if (CrossAbove(CCI(14), 250, 1))
                                {
                                    EnterLongLimit(DefaultQuantity, Close[0], "");
                                }
                    
                                // Condition set 2
                                if (ToTime(Time[0]) == ToTime(15, 55, 0))
                                {
                                    ExitLong("", "");
                                }
                            }
                    
                            #region Properties
                            #endregion
                        }
                    }
                    Last edited by staycool3_a; 05-16-2013, 11:03 AM.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      You must query a DataSeries by an index, in order to get a value.
                      how do I do that?

                      Comment


                        #12
                        Originally posted by calhawk01 View Post
                        how do I do that?
                        Did you read my response?

                        I clearly marked the addition in bold red, matched to the word "index".

                        Comment


                          #13
                          Originally posted by koganam View Post
                          Did you read my response?

                          I clearly marked the addition in bold red, matched to the word "index".
                          SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[index] - (1 * TickSize))));

                          i'm pretty sure that is not what you meant lol

                          do I create a user variable or something?

                          Comment


                            #14
                            Originally posted by calhawk01 View Post
                            SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[index] - (1 * TickSize))));

                            i'm pretty sure that is not what you meant lol

                            do I create a user variable or something?
                            Hm. I wrote in there the exact correction as:
                            Code:
                            SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[COLOR=red][B][0][/B][/COLOR] - (1 * TickSize))));
                            Then in my directions I pointed you to that correction by using matching colored text. I guess I needed to explain that the text was matched by color?

                            Comment


                              #15
                              Originally posted by koganam View Post
                              Hm. I wrote in there the exact correction as:
                              Code:
                              SetStopLoss("", CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[COLOR=red][B][0][/B][/COLOR] - (1 * TickSize))));
                              Then in my directions I pointed you to that correction by using matching colored text. I guess I needed to explain that the text was matched by color?
                              Oh I think you might've missed my previous message. When I use this code:

                              Code:
                              SetStopLoss(CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[0] - (1 * TickSize))));
                              I get this behavior:

                              The above compiles perfectly. I get zero errors. HOWEVER, when I try to add this strategy backtesting or real time testing, I'm getting an unusual behavior.

                              For some strategy reason, even though I select this strategy, the settings for this strategies do not change, and when backtested or realtime testing, the orders are not based on the variables from this strategy.. they are based on different strategies that I have saved.

                              I have already made sure that the code for this strategy is not referring to any other strategy.. I even started from scratch a simple strategy with the above stoploss setting.

                              Any idea what's going on?
                              And when I use your suggested code: bold is the only difference between what CAL suggested and what you are suggesting

                              Code:
                              SetStopLoss([B]""[/B], CalculationMode.Price, (Position.AvgPrice - (CurrentDayOHL().CurrentOpen[0] - (1 * TickSize))));
                              I get this error when compiling:
                              "No overload for method 'SetStopLoss' takes '3' arguments"

                              I've posted my entire code in the previous messages..

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ETFVoyageur, Today, 04:00 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post ETFVoyageur  
                              Started by AaronKTradingForum, Today, 03:44 PM
                              1 response
                              5 views
                              0 likes
                              Last Post AaronKTradingForum  
                              Started by Felix Reichert, 04-26-2024, 02:12 PM
                              11 responses
                              75 views
                              0 likes
                              Last Post Felix Reichert  
                              Started by junkone, 04-28-2024, 02:19 PM
                              7 responses
                              82 views
                              1 like
                              Last Post junkone
                              by junkone
                               
                              Started by pechtri, 06-22-2023, 02:31 AM
                              11 responses
                              136 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Working...
                              X