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

SetStopLoss() and SetProfitTarget()

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

    SetStopLoss() and SetProfitTarget()

    My question is related to Forex trading. Which fomat is best to use with currency trading to set stop loss and profit target? Would it be CalculationMode.Price or CalculationMode.Ticks

    I have used following code but can't seem to make it work. My strategy does not execute either stop loss or profit target orders.

    SetStopLoss("Short", CalculationMode.Ticks, 50, true);

    SetProfitTarget("Short",CalculationMode.Ticks, 100);

    I have tried CalculationMode.Price also but that did not help either.

    I am placing these stoploss and profittarget conditions in the Initialize() section.

    Thanks,

    Sami


    #2
    Either one is fine.

    Please remove the reference to "Short".

    SetProfitTarget(CalculationMode.Ticks, 50);

    I suspect that you do not have an entry signal with the name "Short" and this is why not stops/targets are submitted.
    RayNinjaTrader Customer Service

    Comment


      #3
      Actually I do have a entershort order by the name of "short" but the setstoploss and setprofittarget still doesn't work.

      Comment


        #4
        It may be case sensitive, can you try "Short" for both? Right now you have "Short" and "short".

        Thanks.
        RayNinjaTrader Customer Service

        Comment


          #5
          It is "Short" and "Short" in both place. It was a typo in my last post. Any other suggestions?

          Comment


            #6
            As a last resort, take the attached script which demonstrates the functionality discussed in this thread and modify it to make it your own.
            Attached Files
            RayNinjaTrader Customer Service

            Comment


              #7
              Ray,

              Thank you for the code. I think I have isolated the problem. The strategy seems to work when I apply it to the chart but it does not reflect the same results in the backtesting mode. On the chart I have multiple position opened and stoped (loss or profit) but when I backtest the same currency pair I am only getting one order which open at the begining of the testing period and closes at the end. Why the code works on the chart but not in the backtesting mode?

              Sami

              Comment


                #8
                How can I set a dynamic stoploss? One that re-calculate with each bar.

                I have used the following code but not getting any order, and not even the strategy seems to be executing at all.

                protectedoverridevoid Initialize()
                {
                CalculateOnBarClose =
                true;
                ExitOnClose =
                false;
                ExcludeWeekend =
                false;

                double stopPrice=ATR(45)[1];
                SetStopLoss(
                "Long",CalculationMode.Price,Close[0]-stopPrice,false);
                }

                Need help.

                Sami

                Comment


                  #9
                  Hi Sami,

                  You will have to debug your code. The reference sample I provided works as expected in both real-time and backtest. What I do when I run into walls is strip things down to a core working state and then layer my logic on step by step until I reach a point where it does not work as expected. Then I know what code is causing the problem. I use Print() statements to print out key data at at different points in the script.

                  In your last post, I suspect accessing value in ATR() causes an exception. You will see this in the Log tab of the Control Center window. I believe you can not access indicator values in Initialize().

                  To dynamically change a stop loss, you can call the SetStopLoss() method in OnBarUpdate() and pass in a new price. You must remember to reset the SetStopLoss() values in OnBarUpdate() when you are flat so that on opening a position, they are set to some default "ticks away" value.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Ray,

                    I followed you suggestion of layering the logic step by step and got stuck when I added the SetStopLoss statement. The work around I found was to use the ExitLongStop and ExitShortStop instead of SetStopLoss and it seems to have worked.

                    Thank you for your help,

                    Sami

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by rtwave, 04-12-2024, 09:30 AM
                    2 responses
                    20 views
                    0 likes
                    Last Post rtwave
                    by rtwave
                     
                    Started by tsantospinto, 04-12-2024, 07:04 PM
                    5 responses
                    68 views
                    0 likes
                    Last Post tsantospinto  
                    Started by cre8able, Today, 03:20 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post cre8able  
                    Started by Fran888, 02-16-2024, 10:48 AM
                    3 responses
                    49 views
                    0 likes
                    Last Post Sam2515
                    by Sam2515
                     
                    Started by martin70, 03-24-2023, 04:58 AM
                    15 responses
                    115 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X