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

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 sofortune, 05-10-2024, 10:28 AM
    6 responses
    46 views
    0 likes
    Last Post sofortune  
    Started by sofortune, Today, 10:05 AM
    1 response
    2 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Started by ETFVoyageur, Today, 08:54 AM
    3 responses
    28 views
    0 likes
    Last Post ETFVoyageur  
    Started by ETFVoyageur, Today, 02:15 AM
    3 responses
    23 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Andrei60, Today, 08:56 AM
    0 responses
    15 views
    0 likes
    Last Post Andrei60  
    Working...
    X