Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Programming manual trailstop

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

    Programming manual trailstop

    hi since we cant use setstoploss and trailstop together, i'm trying to program the logic from trailerstop

    Code:
            private void ManageOrders1()
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetStopLoss(CalculationMode.Ticks, 2);
                    LongStopPrice = 0; //initial long stop as small as possible.
                }
     
                // If a long position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Math.Max(LongStopPrice, Position.AvgPrice + target1))
                {                                
     
                    if (Close[0] >= Position.AvgPrice + target1) SetStopLoss(CalculationMode.Price, Close[0]-(Close[0] - Position.AvgPrice));
     
                   LongStopPrice = Close[0]; //update the StopPrice
     
                }
            }
    for some reason the stoploss does not update as price goes up. it only sets the stoploss once and then forgets about it

    #2
    Hello calhawk01,

    Thanks for your note.

    Before we continue, please add Print() statements one line before the SetStopLoss call.

    Let me know if these print statements are printing as expected.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      i added print, and it works.

      here is the code that i am working with right now, i think it's working but i dont think it's the most efficient way.

      what do you think?

      Code:
              private double LongStopPrice = 0;    
              private double diff = 0;    
      
              private void ManageOrders1()
              {
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                      SetStopLoss(CalculationMode.Ticks, 2);
                      LongStopPrice = 0; //initial long stop as small as possible.
                  }
       
                  // If a long position is open, allow for stop loss modification
                  else if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Math.Max(LongStopPrice, Position.AvgPrice + target1))
                  {                                
       
                      if (Close[0] >= Position.AvgPrice + target1) SetStopLoss(CalculationMode.Price, Close[0]-((Close[0] - Position.AvgPrice)-diff));
       
                     LongStopPrice = Close[0]; //update the StopPrice
                     diff = Close[0]-(target1+Position.AvgPrice); //update the StopPrice
       
                  }
              }

      Comment


        #4
        Hi calhawk01,

        Thanks for your reply.

        May have the entire script (with the print statement) so that I may test this on my end?

        To export your script do the following:
        1. Click File -> Utilities -> Export NinjaScript
        2. Enter a unique name for the file in the value for 'File name:'
        3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
        4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


        By default your exported file will be in the following location:
        • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


        Below is a link to the help guide on Exporting NinjaScripts.


        Once exported please attach the exported file to your reply. If you would like to keep the code private between us, please send the script to support[at]ninjatrader[dot]com. In the body of the email write Attn: Chelsea #880778.

        I look forward to receiving the file.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        64 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        35 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        59 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        62 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        51 views
        0 likes
        Last Post CarlTrading  
        Working...
        X