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


Reverse Order in Strategy

  • Filter
  • Time
  • Show
Clear All
new posts

    Reverse Order in Strategy

    I'm trying to carry out this type of operation within a strategy, but it passes the line and doesn't execute. I'm posting the onPositionUpdate part...

    The Position is Long... and i have Order Short

    protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition){
    double reverso = averagePrice - 20;
    if(position.MarketPosition == MarketPosition.Long){
    EnterShortStopMarket(1,reverso,"Venda");   // Passed by here
    Print("entrando Vendido agora:"+averagePrice+" Preço médio:"+reverso);
    Print(position.MarketPosition+" "+MarketPosition.Long); // Flat

    Hello Rafaelseki,

    Thank you for your post.

    Have you checked if there are any errors appearing on the Log tab of the Control Center?

    To understand why the script is behaving as it is, such as placing orders or not placing orders (or drawing objects or other actions) when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    This will print to the output window. Backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Output from prints will appear in the NinjaScript Output window.
    NT8: New -> NinjaScript Output

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

    Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
    NT8 —

    It is also helpful to set TraceOrders to true in State.Configure as well as print the order object in OnOrderUpdate().

    TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.

    Printing the order object in OnOrderUpdate() will allow you to track the progression of the order from submitted, to working, to filled, cancelled, or rejected.

    These tools will let you know what happens to the order.

    TraceOrders -
    OnOrderUpdate() -

    I'm also including a link to a forum post with further suggestions on debugging a script.

    Save the output from the output window to a text file. Let me know if you need assistance creating a print or enabling TraceOrders.

    I am happy to assist with analyzing the output from prints and TraceOrders.​
    Gaby V.NinjaTrader Customer Service


    Latest Posts


    Topics Statistics Last Post
    Started by rbeckmann05, Today, 12:19 PM
    0 responses
    1 view
    Last Post rbeckmann05  
    Started by SuneSorgenfrei, Today, 09:56 AM
    2 responses
    Last Post SuneSorgenfrei  
    Started by samish18, Today, 11:44 AM
    1 response
    Last Post NinjaTrader_Jesse  
    Started by _Zero_, 01-06-2020, 01:16 PM
    66 responses
    Last Post NinjaTrader_ChelseaB  
    Started by RedDuke, 10-31-2023, 11:18 PM
    42 responses
    Last Post NinjaTrader_Emily