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

StopLoss coding

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

    StopLoss coding

    Hello,

    I am running into a brick wall by attempting to finish this block of code:

    if (condition)
    {double high = High[0] + (5 * TickSize);
    BarBrush = Brushes.Lime;
    EnterLongLimit(Convert.ToInt32(ContractSize), (high) );

    double stop = Low[1] - (5 * TickSize);
    SetStopLoss(CalculationMode.Price, stop);
    double low = Low[1] - (5 * TickSize);

    double risk =Math.Abs(high-low);

    int number = 2;
    double profittarget = Math.Abs(risk*number);
    SetProfitTarget(CalculationMode.Price, profittarget);
    }

    The logic of this suppose to be: Once the limit order is placed, the stop goes 5 ticks above the high of the closed bar. The stop goes to 5 ticks below the low of one bar ago.
    The risk gets calculated in ticks based on the current (entry - stoploss) difference and gets multiplied by 2. Once that is done, a profit target gets generated. Basically, its a dynamic 2:1 risk reward ratio system. Does anyone know why it is not working? Im still very new to programming.

    #2
    Originally posted by mikethelen View Post
    . Does anyone know why it is not working? Im still very new to programming.
    Do you have errors in the output window or log?

    Perhaps something about CurrentBar?

    Comment


      #3
      Hello mikethelen,

      Thank you for your post.

      What is not working as far as the code?
      Are you returning an error in the Output window or the Log tab of the Control Center as sledge asked?
      Are the orders not at the expected prices?

      I look forward to your response.

      Comment


        #4
        It places the entry and the stoploss at the correct locations, but does not generate the profit target.

        Comment


          #5
          Hello Mikethelen

          I will need to see your log and trace for today look further into this inquiry.

          Could you please send me your log and trace files from within NinjaTrader. Go to Control Center-->Help-->EMail Support in NinjaTrader. Put 'Att AlanP', in the subject field and enable 'Log and trace files'.

          I look forward to your message.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Trace logs sent. I've tested the strategy on live market feed and the orders are being rejected for some reason. Maybe that is the problem.

            Comment


              #7
              Hello Mike,

              The issue is that your profit target is being submitted at a price of 0.00082, which is being rejected.

              2016-11-14 11:58:59:579|1|32|Order='526cf309c79542838ba6e0b02 374f799/Sim101' Name='Profit target' New state='Rejected' Instrument='EURUSD' Action='Sell' Limit price=0.00082 Stop price=0 Quantity=100,000 Type='Limit' Time in force=GTC Oco='b470da0d3a2c49e4a0cf51fe05893564' Filled=0 Fill price=0 Error='Order rejected' Native error='Limit price can't be smaller than current bid.'

              2016-11-14 11:58:59:580|0|32|Sim101, Limit price can't be smaller than current bid. affected Order: Sell 100000 Limit @ 0.00082

              You may wish to change the CalculationMode.Price to CalculationMode.Ticks, which would in the case above place your profit target .00082 ticks/pips better than your entry price, rather than at a price of .00082.

              Please let us know if you need further assistance.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                The order gets placed now, but it is instantly cancelled by the stoploss.

                Comment


                  #9
                  Hello Mikethelen,

                  I will need to see your log and trace for today look further into this inquiry.

                  Could you please send me your log and trace files from within NinjaTrader. Go to Control Center-->Help-->EMail Support in NinjaTrader. Put 'Att AlanP', in the subject field and enable 'Log and trace files'.

                  I look forward to your message.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello,

                    I've inquired about this a couple of years ago and somehow, I forgot to follow up on my own request.

                    Can someone please take look at this block of code and tell me why my orders get instantly cancelled at the same price?

                    If (condition)

                    {


                    EnterLongLimit(Convert.ToInt32(OrderSize), (high) );


                    double stop = Low[2] - (5 * TickSize);

                    SetStopLoss(CalculationMode.Ticks, stop);

                    double low = Low[2] - (5 * TickSize);


                    double risk =Math.Abs(high-low);


                    int number = 2;

                    double profittarget = Math.Abs(risk*number);

                    SetProfitTarget(CalculationMode.Ticks, profittarget);
                    }

                    It suppose to place an order 5 ticks above the high that created the signal and the stop suppose to go to a low 2 bars ago - 5 ticks below that low.The profit target suppose to be 2:1

                    Comment


                      #11
                      Hello mikethelen,

                      Are you wanting to set the stop to a number of ticks from entry or are you trying to place this at a specific price?

                      What is Low[2] - (5 * TickSize) evaluating as?

                      Is this making sense for a number of ticks?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Low of 2 bars ago and 5 ticks below that low. The end result is for for the code to look at the high and low, determine the stoploss in ticks and then multiply that number by 2 and set a profit target.

                        Comment


                          #13
                          Hello mikethelen,

                          It sounds like you are trying to set the stop loss to a specific calculated price and not to a number of ticks.

                          If so, you need to use CalculationMode.Price instead of using CalculationMode.Ticks.

                          Below is a link to the help guide.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I tried that, it still not working. Something is wrong with that syntax as it is not calculating properly.

                            Comment


                              #15
                              Hello mikethelen,

                              One thing to keep in mind is that you need to call SetStopLoss() before you call the entry order so that it is not using an old price.

                              ProfitChaseStopTrailSetMethodsExample_NT8 demonstrates this.


                              When you mentioned you tried that, are you saying you have use CalculationMode.Price and the order is not being placed at the price you are supplying?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post alifarahani  
                              Started by junkone, Today, 11:37 AM
                              3 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by pickmyonlineclass, Today, 12:23 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post pickmyonlineclass  
                              Started by frankthearm, Yesterday, 09:08 AM
                              12 responses
                              44 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              5 responses
                              35 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X