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

barUpdate resets Position

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

    barUpdate resets Position

    Hi,
    In My strategy, i am always trying to check if I am long or short. If I am already long then even though the signal indicates to go long, the strategy shouldnt enter any trade. To use this logic, I am using position.marketposition variable to check if I am already long at the current bar.
    But I observed, that when the on the bar update, even though my position is not exited, the Position.MarketPosition indicates that I am flat.

    Q. Should I be using MarketPosition variable.
    Q. Is there any other variable / value i can use.

    thanks,
    Madhav

    #2
    madhav.

    Keep in mind that the Position.MarketPosition variable will not be updated until your order is filled.

    If you're using Stop / Limit orders, the order may be sent by your strategy, Accepted by the broker, and never filled b/c the specified price is not reached. In that case, MarketPosition will remain flat and you may process the entry signal again and again.

    I would suggest using an IOrder variable and overriding the onexecution method.

    For example

    private IOrder entryOrderL = null;

    protected override void OnExecution(IExecution execution)
    {
    Print("OnExecution");

    //Long Execution Check?
    if (entryOrderL != null && entryOrderL.Token == execution.Order.Token)
    {

    //-----------------------------------------------------
    // PENDING ORDERS (PendingSubmit / Working / Accepted)
    //-----------------------------------------------------
    if (
    execution.Order.OrderState == OrderState.PendingSubmit ||
    execution.Order.OrderState == OrderState.Working ||
    execution.Order.OrderState == OrderState.Accepted
    )
    {
    Print("Order Long State = " + execution.Order.OrderState.ToString());
    //Save the current bar number
    barNumberOfOrder = CurrentBar;
    }

    //----------------------------
    // Filled Order Response?
    //----------------------------
    if (execution.Order.OrderState == OrderState.Filled)
    {
    //clear the order variable
    Print("Order Long Filled");
    entryOrderL = null;
    }

    //----------------------------
    //Rejected Long Order?
    //----------------------------
    if (execution.Order.OrderState == OrderState.Rejected)
    {
    Print("Order Long Rejected");
    //clear the order variable
    entryOrderL = null;
    }

    return;
    }


    protected override void OnBarUpdate()
    {
    //some logic

    if(entryOrderL != null)
    return;
    //entry logic
    entryOrderL = EnterLong();

    ...
    }

    Get the idea? While an order is being processed you can add logic to keep specific functionality from taking place (like, entrances again and again). That said, you must be aware that your order may NEVER get filled, in which case you have to add some logic to cancel the order after some criteria.

    Make sense?

    I hope this is helpful. Its late and has been a long day of development.

    Anthony
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      Thanks,

      That was the problem. My order was never filled and hence the Position was flat.

      Thank you Anthony.

      Comment


        #4
        Originally posted by madhav_ab View Post
        Hi,
        In My strategy, i am always trying to check if I am long or short. If I am already long then even though the signal indicates to go long, the strategy shouldnt enter any trade. To use this logic, I am using position.marketposition variable to check if I am already long at the current bar.
        But I observed, that when the on the bar update, even though my position is not exited, the Position.MarketPosition indicates that I am flat.

        Q. Should I be using MarketPosition variable.
        Q. Is there any other variable / value i can use.

        thanks,
        Madhav
        If the strategy reports that the position is flat then the strategy itself is not holding a position.

        In addition, you really don't need to check for position since any order method is ignored if you already have a position in the same direction dependant on the Entries per direction and Entry handling parameters. You could however, check for market position to make your strategy more efficient and skip a bunch of code etc...
        RayNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Aviram Y, Today, 05:29 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by TradeForge, Today, 02:09 AM
        1 response
        22 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by elirion, Today, 01:36 AM
        2 responses
        14 views
        0 likes
        Last Post elirion
        by elirion
         
        Working...
        X