Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLoss & Profit Target

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

    StopLoss & Profit Target

    Normally I put SetProftTarget & SetStopLoss under State.Configure. However for the strategy I am working on it has multiple variations of profit targets and stop losses based on certain variables. Am I unable to use the SetProftTarget & SetStopLoss under the OnBarUpdate? I can't get it to work and I am not sure why.

    HTML Code:
    if(Position.MarketPosition == MarketPosition.Long && Trigger122BullTargets == true)
                {
                    SetProfitTarget(CalculationMode.Currency,Bull122TargetPrice);
                    SetStopLoss(CalculationMode.Currency,BullStopPrice);
                    return;
                }
                
                else if(Position.MarketPosition == MarketPosition.Long && Trigger122BullTargets == false)
                {
                    SetProfitTarget(CalculationMode.Currency,BullTargetPrice);
                    SetStopLoss(CalculationMode.Currency,BullStopPrice);
                    return;
                }​
    Instead the only way I can figure out a similar way of doing it by using the below code in OnBarUpdate. Just looking for clarification why the above code wouldn't work.
    HTML Code:
    if(Position.MarketPosition == MarketPosition.Long && Trigger122BullTargets == true)
                {
                    if(Close[0]>=Bull122TargetPrice)
                    {
                        ExitLong();
                    }
                    else if(Close[0]<BullStopPrice)
                    {
                        ExitLong();
                    }
                    return;
                }
                if(Position.MarketPosition == MarketPosition.Long && Trigger122BullTargets == false)
                {
                    if(Close[0]>=BullTargetPrice)
                    {
                        ExitLong();
                    }
                    else if(Close[0]<BullStopPrice)
                    {
                        ExitLong();
                    }
                    return;​
    Thanks

    #2
    Hello algospoke,

    SetStopLoss() and SetProfitTarget() can be called within OnBarUpdate(). These need to be called before calling an entry order method.

    Below is a link to the ProfitChaseStopTrailSetMethodsExample example which demonstrates.


    Note, the CalculationMode.Currency means you are looking for an amount of loss or gain, not a specific price.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

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