Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check if my last StopLoss is from a Long/Short position

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

    Check if my last StopLoss is from a Long/Short position

    How do I check if my last StopLoss is from a Long/Short position?

    This is my Stop Loss function:
    Code:
    // ************ Stop Loss *****************************
    
                if     (
                    Profit <= -StopLoss
                    )                             
                {                        
                    ExitLong(Position.Quantity,"StopLoss(Long)","EnterLong");    
                    ExitShort(Position.Quantity,"StopLoss(Short)","EnterShort");    
                }
    I would like to

    if (LastExit == StopLoss(Long))
    EnterShort

    if(LastExit == StopLoss(Short))
    EnterLong

    Thank you!

    #2
    Hello johnnybegoode,

    The most simple way would be to use OnExecutionUpdate and look for Filled orders. When you locate an order with one of the names you supplied you could set a variable to the direction order that was.



    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
      if(execution.Order.Name == "StopLoss(Long)")
      {
          //set a variable or do an action, last stop loss was long 
      }
      Print(execution.ToString());
    }
    I look forward to being of further assistance.

    Comment


      #3
      ...or, set an int to 1 when entering a long or -1 when going short. Check that number when exiting at a stop loss. After exit reset to 0;
      eDanny
      NinjaTrader Ecosystem Vendor - Integrity Traders

      Comment

      Latest Posts

      Collapse

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