Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Buying/Selling in the right tick

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

    Buying/Selling in the right tick

    I am trying to develop a trading system by writting my own code, since I think that perhaps the Wizard will not give me all the instruments I need. Please, correct me if I'm wrong.

    I have wrote the following code (just as a sample):

    protectedoverridevoid OnBarUpdate()
    {
    if (FirstOperation)
    {
    EnterLong(100000, "Operation 1");
    FirstEntryPrice = Close[0]; // Here I just want to register the price to which I have bought
    Print ("First Entry Price: " + FirstEntryPrice.ToString());
    FirstOperation = false;
    SecondOperation = true;
    }

    NewTrigger = FirstEntryPrice - Distance;

    if (SecondOperation && Close[0] < NewTrigger)
    {
    Print ("Close Long and Enter Short if current tick Price less than: " + NewTrigger.ToString());
    ExitLong("Operation 1");
    EnterShort(100000, "Operation 2");
    SecondEntryPrice = Close[0];
    Print ("Second Entry Price: " + SecondEntryPrice.ToString());
    SecondOperation = false;
    }
    }

    So, I need to know the exact price to which I bought/sold and place and order the sooner the price reach the NewTrigger level.

    I need to to do backtesting with database when the strategy is finished, then, I think I have to dismiss Event Driven programming. Is it right?

    I have been trying to execute this code on backtest mode (EURUSD tick-to-tick) and I find strange results:

    - On the graph (Strategy Analyzer), it appears I buy Operation1 for 1.2774, but in my own output screen says: "First Entry Price: 1.2775" which is the previous tick.

    - Similar results with se Short position.

    Could you please tell me how can I avoid this delay error? Will the corrected code be different when I test the strategy with real-time feed-data?

    Thank you very much for your comments.

    #2
    Hi swingAB,

    Welcome to the NinjaTrader forum and thanks for posting!

    Depending on which strategies you plan to code, the wizard will give you a very good starting point by generating a lot of core code that you can always enhance manually, just be aware that once you have unlocked the code to do custom programming, you cannot edit the same strategy using the wizard anymore.

    Please also review this link -



    What is important to understand: during backtesting, every strategy will enter on the next bar, after the current bar is fully completed. Since you record

    Code:
    FirstEntryPrice = Close[[SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][SIZE=2]]; [/SIZE]
    as your reference entry price, there will always be the difference you are witnessing right now.

    You can instead use this to record your entry prices -

    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] myEntry = Position.AvgPrice; [/SIZE]
    [/SIZE]
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you Bertrand

      I used Position.AvgPrice successfully within OnExecution() to fix the problem: now, data on my output window is equal to the one on my graph.

      My only point now is: is it (físically or algorithmically) possible to launch an entry order at the current tick?

      Is it only during backtesting that the entry is executed at the next bar or is also in real-time data feed running?

      The idea is: as soon I reach a trigger price level, I want NT to execute the order at that bar/tick in order to buy it at that price.

      Is there a way?

      Thank you very much for your usefull comments,

      swingAB

      Comment


        #4
        Hi swingAB,

        You are welcome - this is possible when you choose to let your strategy run on CalculateOnBarClose = False.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thank you Bertrand,

          I am sorry that I did ask you too many different questions in a single post, and now I don't know if your answer is a global respond for all my questions or just to one.

          I tried CalculateOnBarClose = false and I have no better results.

          I am suspecting that it is not (fisically) possible to enter at the current tick.

          So, lets say that I see BID price at 1.2900 and ASK price at 1.2902 in a forex market.

          Is the tick volume always 1 contract? in this case: 1 contract = 1.000.000 money units?

          - That means that the previous tick has different BID and ASK price necessarily, if a 1M contract order enters? and if it is 0.1M? It waits for 10 mini contracts to change the tick?

          - And also the next tick will have different BID and ASK price necessarily?

          I don't understand is it is ok that GAIN historical data gives always a tick volume of 1 or there is a lack of information.

          I'm sorry if I am not clear enough. Just tell me!

          Thanx!

          swingAB

          Comment


            #6
            Hi swingAB,

            You can never enter at the current tick because to receive that tick it is already a matter of the past.

            You can get your order out there for the next tick though.

            Bid and ask prices may move accordingly. You may be viewing a lack of historical data from GAIN. Also please consider, there is no real volume / contract data in Forex present as it for example in Futures.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by benmarkal, Yesterday, 12:52 PM
            3 responses
            22 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by helpwanted, Today, 03:06 AM
            1 response
            16 views
            0 likes
            Last Post sarafuenonly123  
            Started by Brevo, Today, 01:45 AM
            0 responses
            11 views
            0 likes
            Last Post Brevo
            by Brevo
             
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            6 views
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            244 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Working...
            X