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

Attaching a StopLoss to the Swing indicator in strategy builder

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

    Attaching a StopLoss to the Swing indicator in strategy builder

    I am building a strategy and I am trying to incorporate the Swing indicator to set a trailing style stop loss, but I can't seem to get it figured out. I am using the Wizard as I have no coding experience, but I have also made a separate one using the NinjaScript Editor trying to resolve my issue. I'll input the code from the wizard, I have removed the other indicators i have added to try and cut back on the reading. Thank you for any help.


    {
    private double StopPrice;


    private CDSwing CDSwing1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    StopPrice = 1;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    else if (State == State.DataLoaded)
    {
    ATR1 = ATR(Close, 14);
    EMA1 = EMA(Close, 10);
    CDSwing1 = CDSwing(Close, 5);
    SetProfitTarget("", CalculationMode.Currency, 800);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;


    // Set 2
    if (Position.MarketPosition == MarketPosition.Long)
    {
    StopPrice = CDSwing1.SwingLow[0];
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), StopPrice, "", "");
    }

    // Set 3
    if (Position.MarketPosition == MarketPosition.Short)
    {
    StopPrice = CDSwing1.SwingHigh[1];
    ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), StopPrice, "", "");
    }


    }



    #2
    Hello Critical_Catch,

    Thanks for your post and welcome to the Forums.

    In the code you shared I see you are using Set methods and Exit methods in the same script. This would go against the Managed Approach Internal Order Handling Rules linked below.

    You would need to program your strategy to use either Exit methods or Set methods, but not both.

    Managed Approach Internal Order Handling Rules: https://ninjatrader.com/support/help...antedPositions

    That said, you would need to pass in the indicator for the price of your Exit order method so the order is placed at the indicator value.

    Below is a link to an example.

    Hello support team, I have 2 dataseries, a 1minute to detect the ATR value. Now I would like to set the stoploss and profit target based on the actual ATR Value Example: Stoploss 2xATR Profittarget 3xATR value 1. How can this be done with builder, I have no coding experience 2. Later I would like to have the possibility to


    Further, I also see the script is not calling an Enter method, such as EnterLong(), to place an entry order. The script will need to call an Entry order method in the strategy to place an order. Then, once that entry order fills you could check if you are in a Long market position and call your Exit order method with the indicator passed in for the price of the order.
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AaronKoRn, Today, 08:44 PM
    0 responses
    1 view
    0 likes
    Last Post AaronKoRn  
    Started by trader252, Today, 08:32 PM
    0 responses
    1 view
    0 likes
    Last Post trader252  
    Started by Randy Hotcakes, Today, 08:10 PM
    0 responses
    2 views
    0 likes
    Last Post Randy Hotcakes  
    Started by joseph323, Today, 07:05 PM
    0 responses
    6 views
    0 likes
    Last Post joseph323  
    Started by kidpoker007, Today, 03:58 PM
    1 response
    17 views
    0 likes
    Last Post AndyM
    by AndyM
     
    Working...
    X