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 not hitting per strategy

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

  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your reply.

    When you are using Calculate.OnEachTick and using live data, the bar references change so that [0] is the currently forming bar.

    If you want to use the previous bar then you would change it to [1] which would be the last/just closed bar.

    Leave a comment:


  • pyraxic
    replied
    Thank you, Paul. What is the best way to set stoploss one tick below the last closed bar? I am using
    Code:
    SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
    but that doesn't seem to be working as expected.

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your reply.

    What you can do is to create a class level double type variable and store the stop price into it in OnOrderUpdate. Then in the OnBarUpdate() you can add a print statement to print out the time, the stop price, and the close price (current price) (you can comment out the print statement in OnOrderUpdate() as you will now be printing the same thing except in OnbarUpdate()). This then should help you see what is happening on a per-trade basis, to understand where the price is relative to the stop price as well as what the stop price is.

    Leave a comment:


  • pyraxic
    replied
    It gives me output like the following which isn't helpful.

    stop price is: 3577.5
    stop price is: 3577.5
    stop price is: 3577.5
    stop price is: 3577.5
    stop price is: 3577.25
    stop price is: 3577.25
    stop price is: 3577.25
    stop price is: 3577.25
    stop price is: 3577
    stop price is: 3577
    stop price is: 3577
    stop price is: 3577
    stop price is: 3576.75
    stop price is: 3576.75
    stop price is: 3576.75
    stop price is: 3576.75
    stop price is: 3576.5
    stop price is: 3576.5
    stop price is: 3576.5
    stop price is: 3576.5
    stop price is: 3576.25
    stop price is: 3576.25
    stop price is: 3576.25
    stop price is: 3576.25
    stop price is: 3576
    stop price is: 3576
    stop price is: 3576
    stop price is: 3576
    stop price is: 3575.75
    stop price is: 3575.75
    stop price is: 3575.75
    stop price is: 3575.75
    stop price is: 3575.5
    stop price is: 3575.5
    stop price is: 3575.5
    stop price is: 3575.5
    stop price is: 3575.25
    stop price is: 3575.25
    stop price is: 3575.25
    stop price is: 3575.25
    stop price is: 3575
    stop price is: 3575
    stop price is: 3575
    stop price is: 3575
    stop price is: 3575
    stop price is: 3575
    stop price is: 3575
    stop price is: 3578.75
    stop price is: 3578.75
    stop price is: 3578.75
    stop price is: 3578.75
    stop price is: 3578.75
    stop price is: 3578.75

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your reply.

    Everything looks correct.

    Please recompile, remove strategy from the chart and then reapply the strategy and enable the strategy.

    Leave a comment:


  • pyraxic
    replied
    Hmm...it's not providing any output in the NinjaScript Output window. Here is my code. Am I missing anything?

    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;
    
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    if (IsFirstTickOfBar){
    TradeTaken = false;
    }
    
    // Enter Long
    if ((CrossAbove(MACD1.Default, MACD1.Avg, 1)) && Close[0] > Close[1] && TradeTaken == false)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    TradeTaken = true;
    }
    
    SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
    
    
    // Enter Short
    if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && Close[0] < Close[1] && TradeTaken == false)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    TradeTaken = true;
    }
    SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false);
    }
    
    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    if (order.Name == "Stop loss")
    {
    Print ("stop price is: " + order.StopPrice);
    }
    }

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your reply.

    Yes, it will work in the Strategy analyzer as well as live.

    Leave a comment:


  • pyraxic
    replied
    Originally posted by NinjaTrader_PaulH View Post
    Hello pyraxic,

    Thanks for your reply.

    I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.

    In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.

    Here is an example:

    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    if (order.Name == "Stop loss")
    {
    Print ("stop price is: "+order.StopPrice);
    }
    }


    The print statement sends it output to the new>Ninjascript output window.
    Would this work for Strategy Analyzer as well? I tried using it in Strategy Analyzer but there was no output. Trying it out in sim trading now.

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your reply.

    I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.

    In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.

    Here is an example:

    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    if (order.Name == "Stop loss")
    {
    Print ("stop price is: "+order.StopPrice);
    }
    }


    The print statement sends it output to the new>Ninjascript output window.

    Leave a comment:


  • pyraxic
    replied
    There is another trade that it took this morning with similar behavior for a long position. (see attached)
    Attached Files

    Leave a comment:


  • pyraxic
    replied
    Originally posted by NinjaTrader_PaulH View Post
    Hello pyraxic,

    Thanks for your post.

    Please check the "log" tab of the control center for any errors related to your strategy.

    Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?

    What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)

    Do you have any other code that could be setting the stop-loss to another value unexpectedly?
    There are no errors in the logs. This is a live simulated trade but Strategy Analyzer has shown similar results in the past. The calculate settings is Calculate.OnEachTick and there is no other code that would be causing the stop loss. I have do have a StopLoss set for the long position but the signal name is different
    Code:
    SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello pyraxic,

    Thanks for your post.

    Please check the "log" tab of the control center for any errors related to your strategy.

    Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?

    What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)

    Do you have any other code that could be setting the stop-loss to another value unexpectedly?

    Leave a comment:


  • pyraxic
    started a topic Stoploss not hitting per strategy

    Stoploss not hitting per strategy

    Hello,

    I am trying to set the Stop-Loss one tick above the last bar and have the following code.

    Code:
    // Enter Short
    if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && TradeTaken == false && Close[0] < Close[1])
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    TradeTaken = true;
    }
    SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false);
    For some reason, the Stop-Loss isn't triggering when it's supposed to and hitting the profit target (see attached). Should it hit the stoploss (marked in the screenshot) instead?
    Attached Files

Latest Posts

Collapse

Topics Statistics Last Post
Started by bortz, 11-06-2023, 08:04 AM
47 responses
1,609 views
0 likes
Last Post aligator  
Started by jaybedreamin, Today, 05:56 PM
0 responses
9 views
0 likes
Last Post jaybedreamin  
Started by DJ888, 04-16-2024, 06:09 PM
6 responses
19 views
0 likes
Last Post DJ888
by DJ888
 
Started by Jon17, Today, 04:33 PM
0 responses
6 views
0 likes
Last Post Jon17
by Jon17
 
Started by Javierw.ok, Today, 04:12 PM
0 responses
16 views
0 likes
Last Post Javierw.ok  
Working...
X