Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

assign order quantity

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

    assign order quantity

    Hi NinjaTrader

    pls help me programatically assign # of shares to my orders.
    What I do:

    Code:
    #region Variables
    // introduce variables
    private double value = 1000;
    private double Dqty;
    private int    Iqty;
    Code:
    protected override void OnBarUpdate()
    {
    
    //calculate number of shares to buy based on the 
    Dqty = value / Ask[0];                                                      //code line: 84
    Iqty = Convert.ToInt32(Dqty);
    
    <...>
    
            {
            EnterLong("Long 1a", Iqty);                                   //code line: 111
            EnterLong("Long 1b", Iqty);                                   //code line: 112
            }
    }
    Then I get error message 1 (attached - error1).
    If I try to remove Iqty from EnterLong and try o compile, then I get another (attached - error2)


    If you can't help me debug this, can you please let me know what I am doing wrong here?

    Alternatively, can you please advise if there is an easier way to set order quantity based on this logic: $ ammount / share price.

    THANK YOU
    Attached Files
    Last edited by ionaz; 02-06-2013, 12:56 AM.

    #2
    Originally posted by ionaz View Post
    Hi NinjaTrader

    pls help me programatically assign # of shares to my orders.
    What I do:

    Code:
    #region Variables
    // introduce variables
    private double value = 1000;
    private double Dqty;
    private int    Iqty;
    Code:
    protected override void OnBarUpdate()
    {
     
    //calculate number of shares to buy based on the 
    Dqty = value / Ask[0];                                                      //code line: 84
    Iqty = Convert.ToInt32(Dqty);
     
    <...>
     
            {
            EnterLong("Long 1a", Iqty);                                   //code line: 111
            EnterLong("Long 1b", Iqty);                                   //code line: 112
            }
    }
    Then I get error message 1 (attached - error1).
    If I try to remove Iqty from EnterLong and try o compile, then I get another (attached - error2)


    If you can't help me debug this, can you please let me know what I am doing wrong here?

    Alternatively, can you please advise if there is an easier way to set order quantity based on this logic: $ ammount / share price.

    THANK YOU
    I think the errors are self-explanatory.

    There is no DataSeries called Ask, so Ask[0] is meaningless to the compiler. You are probably looking for GetCurrentAsk().

    You have the parameters for EnterLong() in the wrong order, and the error is telling you so. Consult the NT Help for the correct syntax.

    SetPosition is likely declared with the wrong scope, or does not exist. Without seeing the code that references SetPosition, it is impossible to say with certitude what is wrong.

    Comment


      #3
      Thanks koganam

      I fixed the order of parameters in the EnterLong() and changed Ask[0] to GetCurrentAsk().
      But this gave only partial result:

      • OrderQuantity - I changed the code to:

      Code:
      Dqty = (value*0.03) / GetCurrentAsk();
      Iqty = Convert.ToInt32(Dqty);
      So it works only partially, looks like whenever the result of the above exceeds 0.5, it works!
      But if the result is below 0.5 (didn't do exact calculations, but when (value*0.03) is clearly insuficient to buy 1 share) it then buys the default 100 shares, as if it switched from "by strategy" to "by default quantity".

      • StopLoss stopped working: on all stocks the postions are closed on the last bar. Do you know why?

      • Question on GetCurrentAsk() defintion: A double value representing the current ask price. Note: When accessed during a historical backtest, the close price of the evaluated bar is substituted. As Engish - not my 1st language, can you pls explain the note above, specifically what does it mean by "substituted". How I understand it is: if one day there's a signal to buy smth, then in my case to arrive at order quantity the ask price of that same day will be used. why would it need to be substituted? and with what?
      Last edited by ionaz; 02-06-2013, 03:01 PM.

      Comment


        #4
        Originally posted by ionaz View Post
        Thanks koganam

        I fixed the order of parameters in the EnterLong() and changed Ask[0] to GetCurrentAsk().
        But this gave only partial result:

        • OrderQuantity - I changed the code to:
        Code:
        Dqty = (value*0.03) / GetCurrentAsk();
        Iqty = Convert.ToInt32(Dqty);
        So it works only partially, looks like whenever the result of the above exceeds 0.5, it works!
        But if the result is below 0.5 (didn't do exact calculations, but when (value*0.03) is clearly insuficient to buy 1 share) it then buys the default 100 shares, as if it switched from "by strategy" to "by default quantity".
        "Iqty = Convert.ToInt32(Dqty);" is asking to convert to an integer. A quantity less than 0.5 will be truncated to zero. If you want to not enter in such a case, then that is what you should code.
        Code:
         
        if (Iqty > 0) EnterLong();
        • StopLoss stopped working: on all stocks the postions are closed on the last bar. Do you know why?
        1. Did you use a Set statement to set your StopLoss?
        2. If so, have you reset that Set statement when you went flat?
        • Question on GetCurrentAsk() defintion: A double value representing the current ask price. Note: When accessed during a historical backtest, the close price of the evaluated bar is substituted. As Engish - not my 1st language, can you pls explain the note above, specifically what does it mean by "substituted". How I understand it is: if one day there's a signal to buy smth, then in my case to arrive at order quantity the ask price of that same day will be used. why would it need to be substituted? and with what?
        You cannot access data that is not available. In a backtest, the only available data are OHLC. Therefore, it you try to use the Ask price, the Close price is used instead, because there is no record of the Ask price.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        4 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Working...
        X