Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLoss is not static

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

  • Heikoman
    replied
    Hello Jim,

    I can`t believe it, that was the point, compiling was successful.

    Thank you very much!

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Heikoman,

    If the code cannot compile, the syntax would be incorrect. Please note exactly how the sample code checks for a cancelled order. You may observe that OrderState.Cancelled is used when order.state.Cancelled would be incorrect.

    I.E.

    if (order.OrderState == OrderState.Cancelled)

    where "order" is the Order object that is passed by parameter from OnOrderUpdate, order.OrderState is the order state of that order, and OrderState.Cancelled is the OrderState value that means cancelled.

    We look forward to assisting.

    Leave a comment:


  • Heikoman
    replied
    Hello Jim,

    thanks for your post!

    First of all, I`ve tried to cancel orders, if the market breaks out into the "wrong direction" and added the following to my trading conditions (long trades):

    else if (entryOrder != null && Low[0] < RangeMin)
    {
    CancelOrder(entryOrder);
    }

    Of course, in "public class : strategy", I´ve created a new Order:

    private Order entryOrder = null;

    and under "if (State == State.SetDefaults)" the following:

    else if (State == State.Realtime)
    {
    if (entryOrder != null)
    {
    entryOrder = GetRealtimeOrder(entryOrder);
    }
    }

    Compiling was successful, but it doesn`t work in backtests. So I decided to set Order to null, as shown in the sample (long and short trades):

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState,
    DateTime time, ErrorCode error, string comment)
    {
    if(order.Name == "Long")

    entryOrder = order;

    if (entryOrder != null && entryOrder == order)
    {
    if(entryOrder.OrderState == order.State.Cancelled)
    {
    entryOrder = null;
    }
    }
    if(order.Name == "Short")

    entryOrder = order;

    if (entryOrder != null && entryOrder == order)
    {
    if(entryOrder.OrderState == order.State.Cancelled)
    {
    entryOrder = null;
    }
    }
    }

    After this step, it is not possible to compile (something wrong with Order.State)
    Sorry for such a long question, but all my ideas to find a mistake didn`t work. Hope you can find something wrong in my strategy.
    Best Regards

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Heikoman,

    You can use the Unmanaged Approach to work outside the internal rules of the Managed Approach. I have attached an example that demonstrates bracketed entries with the Unmanaged Approach.

    Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

    The example models the behaviors described in the SampleOnOrderUpdate strategy. This is an advanced approach, and I recommend becoming familiar with Order object handling and the usage of OnOrderUpdate and OnExecutionUpdate that is demonstrated in SampleOnOrderUpdate before reviewing this UnmanagedTemplate strategy.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    If you would like to be able to cancel orders programmatically, this is also possible with the Managed Approach when Order objects are tracked for the order, and calling CancelOrder to cancel the order. An example is included below.

    SampleCancelOrder - https://ninjatrader.com/support/help...thod_to_ca.htm

    Let me know if there is anything else we can do to help.
    Attached Files

    Leave a comment:


  • Heikoman
    replied
    Hello Jim,

    thanks for your post.

    I found out, that the orders are ignored, because an active entry order is waiting in the opposite direction.

    Moreover, in Managed Approach I`ve read, that this behaviour is "pre-installed" in Ninjatrader. Is there a way to avoid this?

    Another idea (or maybe in combination) is just to cancel the entry order, when a certain price is reached. I can determine the price, but is possible to cancel an order in this way?

    Best regards

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Heikoman,

    Thanks for your question.

    If your strategy was scaling into new positions, the position check would prevent those additional entries, as the entry condition now requires that you are flat before entering again.

    If you are trying to understand if the code is reaching the order submission methods, debugging prints should be used to confirm the order method is reached. If it is not reached, prints should be used outside of the condition so you can monitor why the parts of the condition are not allowing the action to be taken

    If you see that the order method is reached, but the order is ignored, please test again using TraceOrders and be sure to reference the internal rules of the Managed Approach.

    TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

    Managed Approach (Internal Rules) - https://ninjatrader.com/support/help...antedPositions

    We look forward to assisting.

    Leave a comment:


  • Heikoman
    replied
    Hello Jim,

    thank you very much, I`ve added

    && (Position.MarketPosition == MarketPosition.Flat)

    to my trading conditions, so that trading (and setting orders) is just allowed, if there is no open position, and it works great!

    Moreover, in the backtest I´ve seen, that many trades are ignored. Although all conditions are complied, there is no trade when the market breaks out of the range.

    Is there any correlation between this behaviour and the new code line?

    Best regards

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Heikoman,

    Thanks for your post.

    Set methods prep NinjaTrader to submit target and stop upon the execution of the associated entry order. As such, we should ensure that the Set methods are called before the entry order is submitted.

    To prevent the code from calling the Set methods again once you are in a position, you can check if the Strategy Position is flat in addition to your other entry conditions.

    Position.MarketPosition - https://ninjatrader.com/support/help...etposition.htm

    If the strategy is not listi89ng in the Strategy Analyzer, please check the log tab of the Control Center for any errors. You may then use debugging prints to find the exact line that is throwing an error after opening the Strategy Analyzer and trying to select the strategy. If you have questions on the error, please include the error from the Log tab of the Control Center (you can use Ctrl + C to copy the error) as well as the line of code that generates the error.

    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

    If you do not see errors, please confirm that you are looking for the strategy that has the same Name property in OnStateChange under if (State == State.SetDefaults).

    We look forward to assisting.

    Leave a comment:


  • Heikoman
    started a topic StopLoss is not static

    StopLoss is not static

    Hello,

    I have created a strategy, that trades the breakout from a certain range (I use two variables: RangeMax, RangeMin).

    For example, I trade a long position by stop order, if the market breaks over "RangeMax", the StopLoss is at "RangeMin", and the profit target is as far as the Stoploss.

    It works, as long as the strategy doesn`t find another Range (new "RangeMax" and "RangeMin") during an open trade.

    In this case, the strategy changes StopLoss from the first "RangeMin" to the new "RangeMin".

    I`ve tried the following code:

    if (.....)
    {
    EnterLongStopMarket(0, true, KontraktzahlLong, RangeMax, @"Long");
    SetStopLoss(@"Long", CalculationMode.Price, RangeMin, false);
    SetProfitTarget(CalculationMode.Ticks, RangeTicks);
    }

    I`ve tried to write

    SetStopLoss(@"Long", CalculationMode.Price, RangeMin, false);

    in the OnStateChange() method (when the current market position is long or short). After compiling I couldn`t find the strategy in Strategy Analyzer.

    Unfortunately I`m not a programmer (learning by doing), but I hope we will find a solution.

    Best regards



Latest Posts

Collapse

Topics Statistics Last Post
Started by hunghnguyen2016, Today, 08:00 PM
0 responses
6 views
0 likes
Last Post hunghnguyen2016  
Started by cbentrikin, Today, 03:49 PM
0 responses
11 views
0 likes
Last Post cbentrikin  
Started by MiCe1999, 04-14-2025, 06:54 PM
7 responses
76 views
0 likes
Last Post b.j.d
by b.j.d
 
Started by NISNOS69, Today, 02:20 PM
0 responses
18 views
0 likes
Last Post NISNOS69  
Started by hunter7, Today, 01:09 PM
0 responses
27 views
0 likes
Last Post hunter7
by hunter7
 
Working...
X