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 not working correctly

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

    setstoploss not working correctly

    I have a strategy (with2 instruments) in which I am setting stop loss at .4% immediately after a buy order gets filled. When I run this startegy in the morning, the first order for each instrument always has the incorrect stop loss. Subsequent orders after that get the correct value.

    Relevent section from the code:
    --------------------------------
    ======================
    //on some condition
    entryOrder = EnterLong(DefaultQuantity, "FAS Purchase");

    In OnExecution()
    buyprice=order.AvgFillPrice;
    sellprice=buyprice*.996;
    SetStopLoss("FAS Purchase", CalculationMode.Price, sellprice, false);
    ======================

    I HAVE ATTACHED THE SECTION FROM THE TRACE FILE WHEN WRONG STOP WAS PLACED:
    ======================
    1. Buy order was placed at 6:44:45 AM (PST) for 472 stocks of FAS. Fill price = $84.72. Order got filled in 4 partial fills of 100,200,100 and 72.
    2. Stop sell SHOULD have been placed for $84.38
    3. But the stop loss was placed for $85.05
    4. Since stop was higher than the current price, it sold all the stocks at the market right away.
    5. The next order was filled to buy 2033 stocks of FAZ. Fill price was $19.67 and filled in batches of 1200 and 833.
    6. Trace shows 2 stop loss settings - one for $19.53 (incorrect) and $19.59 (correct).

    your help will be highly appreciated.


    Attached Files

    #2
    Hi cheeky, can you please post the full code you're using to set the stop loss?

    From your post:
    Fill price = 84.72
    actual stop = 85.05 = 84.72 * (1 / 0.996)
    where stop should be = 84.38 = 84.72 * 0.996

    From the above calculations, it looks like the stop is being set for the wrong "direction" (a "short" stop was placed for your long position).

    I would recommend adding logic to your strategy that sets the stop price depending on the direction (long/short) instead of just one stop price (in your case, fill price * 0.996).
    AustinNinjaTrader Customer Service

    Comment


      #3
      I am not entering any short position. Only longs.

      My logic is simple -

      1. Buy first instrument if it is above .6% of open.
      2. Sell if stopped out at .4%. Immediately buy the other instrument. Repeat the same cycle.
      3. In back test everything works fine. But in live trading sometimes stops are incorrectly getting calculated.

      HERE iS the code ---

      Attached Files

      Comment


        #4
        cheeky123, I didn't see you resetting the SetStopLoss to some default value when you're flat, please try this and then see if the stop loss is entered as expected.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          How do I set the default value? My strategy is flat only at the beginning of the day..

          Comment


            #6
            cheeky123,

            To set a default value, just call SetStopLoss() prior to your next entry, but after your previous trade.
            Josh P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by haas88, 03-21-2024, 02:22 AM
            19 responses
            220 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by ZeroKuhl, Yesterday, 04:31 PM
            4 responses
            30 views
            0 likes
            Last Post ZeroKuhl  
            Started by cupir2, Yesterday, 07:44 PM
            3 responses
            22 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by reynoldsn, Yesterday, 07:26 PM
            2 responses
            16 views
            0 likes
            Last Post reynoldsn  
            Started by MartinT, 05-17-2023, 06:00 AM
            18 responses
            175 views
            0 likes
            Last Post flybuzz
            by flybuzz
             
            Working...
            X