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(), stop loss not setting the current stop price.

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

    setstoploss(), stop loss not setting the current stop price.

    Hi,

    I am new to programing in general and ask for your patience.

    I was able to get my setprofittarget and breakeven working. However, the setstoploss is placing my stop 1000 pts away from my intended location.

    Calulationmode.OnEachTick

    here is my code:
    if (BarsInProgress == 0)
    {
    EntryLong = (High[1] + 1 * TickSize);
    //Enter Long
    if (SignalLong[0] == 2 && Close[0] > EMA1[0] && ADX1[0] > 25 && EntryLong >= GetCurrentAsk())
    {
    BreakEvenTrigger = true;
    EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), EntryLong, "Long1");
    EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), EntryLong, "Long2");
    SetStopLoss("Long1", CalculationMode.Ticks, Low[1] - StopPrice1, false);
    SetStopLoss("Long2", CalculationMode.Ticks, Low[1] - StopPrice2, false);
    }

    else if (SignalLong[0] == 2 && Close[0] > EMA1[0] && ADX1[0] > 25 && EntryLong < GetCurrentAsk())
    {
    BreakEvenTrigger = true;
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), EntryLong, "Long1");
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), EntryLong, "Long2");
    SetStopLoss("Long1", CalculationMode.Ticks, Low[1] - StopPrice1, false);
    SetStopLoss("Long2", CalculationMode.Ticks, Low[1] - StopPrice2, false);
    }

    EntryShort = (Low[1] - 1 * TickSize);
    //Enter Short
    if (SignalShort[0] == -2 && Close[0] < EMA1[0] && ADX1[0] > 25 && EntryShort <= GetCurrentBid())
    {
    BreakEvenTrigger = true;
    EnterShortStopMarket(Convert.ToInt32(DefaultQuanti ty), EntryShort, "Short1");
    EnterShortStopMarket(Convert.ToInt32(DefaultQuanti ty), EntryShort, "Short2");
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    }

    else if (SignalShort[0] == -2 && Close[0] < EMA1[0] && ADX1[0] > 25 && EntryShort > GetCurrentBid())
    {
    BreakEvenTrigger = true;
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short1");
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short2");
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    }
    }


    if (Position.MarketPosition == MarketPosition.Flat)
    {
    BreakEvenTrigger = true;
    SetStopLoss("Long1", CalculationMode.Ticks, Low[1] - StopPrice1, false);
    SetStopLoss("Long2", CalculationMode.Ticks, Low[1] - StopPrice2, false);
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    }

    if (Position.MarketPosition == MarketPosition.Long && BreakEvenTrigger)
    {
    if (Close[0] >= Position.AveragePrice + (BreakEven * TickSize))
    {
    SetStopLoss("Long2", CalculationMode.Price, Position.AveragePrice + 1 * TickSize, false);
    BreakEvenTrigger = false;
    }
    }

    if (Position.MarketPosition == MarketPosition.Short && BreakEvenTrigger)
    {
    if (Close[0] <= Position.AveragePrice - (BreakEven * TickSize))
    {
    SetStopLoss("Short2", CalculationMode.Price, Position.AveragePrice - 1 * TickSize, false);
    BreakEvenTrigger = false;
    }
    }
    }


    however when I am on live trading this is the stop loss that was placed (highlighted).


    how can I fix this?



    2) If I want to set my stop loss at the lowest of the preceding 3 bars do I just use "Lowestbar(3) + 1 * TickSize" instead?
    Attached Files
    Last edited by ssjaznguy; 01-04-2021, 02:22 PM.

    #2
    Hello ssjaznguy, thanks for your post.

    One thing I am noticing in the code is you are not setting the stop losses in the correct location. You must set the stop loss before the entry order, it looks like you are expecting the stop loss to get set afterward. When you do this the price of the old stop loss is initially used, change this to submit before the entry order:

    Code:
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short1");
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short2");
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    
    to 
    
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short1");
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short2");
    Also, for debugging purposes and understanding why we get the output, use Print statements throughout the code to see the data as the strategy is running e.g.

    Code:
    Print("Setting Stop Loss 1 " + (High[1] + StopPrice1) + "  " + Time[0]);
    Print("Setting Stop Loss 2 " + (High[1] + StopPrice2) + "  " + Time[0]);
    SetStopLoss("Short1", CalculationMode.Ticks, High[1] + StopPrice1, false);
    SetStopLoss("Short2", CalculationMode.Ticks, High[1] + StopPrice2, false);
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short1");
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), EntryShort, "Short2");
    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chris for the prompt reply.

      1) I moved the setstoploss to the location you suggested but is still confuse on why there are 1000 pt difference. I tested my strategy on live ES data today and the market was no where near the 4596.25 mark

      2)
      Print("Setting Stop Loss 1 " + (High[1] + StopPrice1) + " " + Time[0]);
      Print("Setting Stop Loss 2 " + (High[1] + StopPrice2) + " " + Time[0]);
      so for debugging purpose, I just use my string name "Short1" or "Short2" inside the " "?

      3) Can I use LowestBar(3) to set it to the lowest point of the last 3 bar?

      Comment


        #4
        Hello ssjaznguy, thanks for your reply.

        1. What is the output from the Print methods? To know why the stop loss is being set at a certain price you must print out the price being used. Add prints before every SetStopLoss to print out the price it is about to use.

        2. Any string can be used in the quotation marks. If it makes it easier to identify, it's fine to use Short1 and Short2.

        3. You can use the MIN (Minimum) function to get the lowest low in the last 3 bars e.g.

        Code:
        double x = MIN(Low, 3)[0];
        
        Print(x); //Print to see what the value is.
        Please let me know if I can assist any further.

        Chris L.NinjaTrader Customer Service

        Comment


          #5
          this is what was printed out for a simulated trade @ 4.00pm on live data, I see the print at the correct stop but that is not what is shown on the order tab (highlighted), I also see that there was two long1 entries but only one long2 entries.

          Attached Files

          Comment


            #6
            Hello ssjaznguy, thanks for your reply.

            ​​​​​​You will need to print out the data in OnOrderUpdate or OnExecutionUpdate. I don't have any way of reproducing what you are reporting, so its something wrong with your script.




            Please let me know if I can assist any further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              I just dont see why it would execute the setstoploss for far away.

              2021-01-05 06:44:27:206|1|64|Instrument='ES 03-21' Account='SimAlgo' Average price=3709.75 Quantity=1 Market position=Long Operation=Operation_Add
              2021-01-05 06:44:27:207|1|32|Order='34d671339a1e4cb584b723c5f d0f6b54/SimAlgo' Name='Long2' New state='Working' Instrument='ES 03-21' Action='Buy' Limit price=0 Stop price=3709.75 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
              2021-01-05 06:44:27:261|1|32|Order='34d671339a1e4cb584b723c5f d0f6b54/SimAlgo' Name='Long2' New state='Filled' Instrument='ES 03-21' Action='Buy' Limit price=0 Stop price=3709.75 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=1 Fill price=3709.75 Error='No error' Native error=''
              2021-01-05 06:44:27:261|1|8|Execution='0cf05a8336564856a5b9d2 d08d7f19b7' Instrument='ES 03-21' Account='SimAlgo' Exchange=Default Price=3709.75 Quantity=1 Market position=Long Operation=Operation_Add Order='34d671339a1e4cb584b723c5fd0f6b54' Time='1/5/2021 6:44 AM'
              2021-01-05 06:44:27:261|1|16|NinjaScript strategy 'SecondBE3/221101722' submitting order
              2021-01-05 06:44:27:279|1|32|Order='f3a6d932752747e297c5ac168 5c27524/SimAlgo' Name='Stop loss' New state='Submitted' Instrument='ES 03-21' Action='Sell' Limit price=0 Stop price=2783.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='0cf05a8336564856a5b9d2d08d7f19b7' Filled=0 Fill price=0 Error='No error' Native error=''
              2021-01-05 06:44:27:279|1|16|NinjaScript strategy 'SecondBE3/221101722' submitting order
              2021-01-05 06:44:27:283|1|32|Order='c47b3e3b26e3484598f379399 1e89bce/SimAlgo' Name='Stop loss' New state='Accepted' Instrument='ES 03-21' Action='Sell' Limit price=0 Stop price=2782.75 Quantity=1 Type='Stop Market' Time in force=DAY Oco='24005edcadd641a6bbad03ce424f0308' Filled=0 Fill price=0 Error='No error' Native error=''
              2021-01-05 06:44:27:284|1|32|Order='c47b3e3b26e3484598f379399 1e89bce/SimAlgo' Name='Stop loss' New state='Change submitted' Instrument='ES 03-21' Action='Sell' Limit price=0 Stop price=2783.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='24005edcadd641a6bbad03ce424f0308' Filled=0 Fill price=0 Error='No error' Native error=''
              2021-01-05 06:44:27:300|1|32|Order='3607177a72354207ae07dd299 98fe597/SimAlgo' Name='Profit target' New state='Submitted' Instrument='ES 03-21' Action='Sell' Limit price=3712.75 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='0cf05a8336564856a5b9d2d08d7f19b7' Filled=0 Fill price=0 Error='No error' Native error=''
              2021-01-05 06:44:27:300|1|64|Instrument='ES 03-21' Account='SimAlgo' Average price=3709.75 Quantity=2 Market position=Long Operation=Update


              should I be using SetStopLoss("Long2", CalculationMode.Price, Low[1] - StopPrice2, false); instead of using "calculationmode.ticks instead?
              Attached Files

              Comment


                #8
                Hello ssjaznguy, thanks for your reply.

                Using CalculationMode.Ticks with a value like Low[1] - StopPrice2 would set it very far away from the expected level, in this case, several thousand ticks. Use CalculationMode.Price to set the stop at a definitive price level.

                Please let me know if I Can assist any further.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chris,

                  That fixed the spot loss issue. now I need to figure out how to do trailing lost and fine tune my entry condition.

                  is there a way to code in to not take any order if signal is in the range of a congestion?

                  Comment


                    #10
                    Hello ssjaznguy, thanks for your reply.

                    You will need to define what the range of a congestion is in NinjaScript language, I'm not sure what that is. If you could give an example I will suggest the right tools to use. We have examples here on limiting trades based on a condition:




                    Please let me know if I can assist any further.

                    Chris L.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by futtrader, 04-21-2024, 01:50 AM
                    5 responses
                    56 views
                    0 likes
                    Last Post NinjaTrader_Eduardo  
                    Started by PeakTry, Today, 10:49 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post PeakTry
                    by PeakTry
                     
                    Started by llanqui, Today, 10:32 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post llanqui
                    by llanqui
                     
                    Started by StockTrader88, 03-06-2021, 08:58 AM
                    45 responses
                    3,992 views
                    3 likes
                    Last Post johntraderuser2  
                    Started by TAJTrades, Today, 09:46 AM
                    0 responses
                    8 views
                    0 likes
                    Last Post TAJTrades  
                    Working...
                    X