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

Trailing Stop, StopLoss, Profit Target Combo (without risk of daytrading)

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

    Trailing Stop, StopLoss, Profit Target Combo (without risk of daytrading)

    I have a strategy that uses daily bars and I want to add Trailing Stop, StopLoss, and Profit Targets, but I do not want one of them to get hit on the same day the order is placed. Is there any reasons why something like this would not work?

    Code:
     
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] useStopLossOrTrailingStop;[/SIZE]
    [SIZE=2][SIZE=2][[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Category[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Parameters"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])][/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Exit_SlOrTs_OnOff {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] { [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] useStopLossOrTrailingStop; }[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] { useStopLossOrTrailingStop = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]value[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; }[/SIZE]
    [SIZE=2]}[/SIZE]
    [/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OnBarUpdate() {[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (dayTrade == 0 && Position.MarketPosition != [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MarketPosition[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Flat && BarsSinceEntry(0, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0) == 1) {[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].useProfitTarget == 1) [/SIZE]
    [SIZE=2]SetProfitTarget([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, Position.AvgPrice *([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].profitTarget / 100));[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (useStopLossOrTrailingStop) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 1: [/SIZE]
    [SIZE=2]   SetTrailStop([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, Position.AvgPrice *([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].stopLoss / 100), [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 2: [/SIZE]
    [SIZE=2]   SetStopLoss([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, Position.AvgPrice * ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].stopLoss / 100), [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]default[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]: [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
     
    [SIZE=2]TryEnter();[/SIZE]
     
    [SIZE=2]}[/SIZE]
     
    private void TryEnter() {
     
    ...
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (dayTrade == 0) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].useProfitTarget == 1) [/SIZE]
    [SIZE=2]  SetProfitTarget([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, Price * 5);[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (useStopLossOrTrailingStop) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 1: SetTrailStop([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, 0, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 2: SetStopLoss([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]CalculationMode[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Price, 0, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  default[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]: [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
     
     
    ...
    //Place order
     
    }
    Thanks,
    Erik

    #2
    Please ignore this post I already figured out it is not going to work that well and I am rewriting the code.

    Thanks,
    Erik

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ETFVoyageur, Today, 12:52 AM
    0 responses
    2 views
    0 likes
    Last Post ETFVoyageur  
    Started by Board game geek, Yesterday, 02:20 AM
    2 responses
    21 views
    0 likes
    Last Post Board game geek  
    Started by jackiegils, Yesterday, 11:05 PM
    0 responses
    5 views
    0 likes
    Last Post jackiegils  
    Started by cre8able, 05-09-2024, 09:15 PM
    2 responses
    17 views
    0 likes
    Last Post cre8able  
    Started by Trader146, Yesterday, 09:17 PM
    0 responses
    10 views
    0 likes
    Last Post Trader146  
    Working...
    X