Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninja PairsTrader

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

    #16
    Vielan Dank zweistein!

    I had a brief look at your code (haven't imported it yet) and have a few quick questions:

    1. Type "Instrument" isn't supported per se in NinjaScript. Have you found an unsupported Instrument check or are you working around the exceptions that would be thrown? I've been doing the latter.

    2. Limit Orders are the goal of this project. I actually want to quote on one leg of the spread and only hedge if I'm filled, which is why I have IOrders in that code. My IOrders code throws exceptions in some cases in the last code I posted. I am weak at that kind of programming and welcome any and all input.

    3. At first glance the only difference between SpreadZScore and SpreadZScore_Zw is a commented out Instrument check. Is there something I missed?

    Again I haven't imported anything yet and was just looking at the cs files you posted. Thank you for your interest in this. Personally I consider the inability of NT7 to execute spreads safely is a challenge worth solving. It will save me $1450 a month .

    EDIT: Item 2. LIMIT orders are the goal of this project. And the ability to cancle/replace/keep psotion in queue.
    Last edited by MXASJ; 05-08-2010, 09:15 AM.

    Comment


      #17
      Hello MXASJ,

      1. Ideally there would be a typeconverter for Instrument. This is the cleanest solution but the most difficult to implement... Currently I stick to string also.

      2. You should be able to modify for limit orders easlily
      3. indicator is the same, I just renamed for import issues and future ideas.

      4. You do not need multi-instrument trading as long as the instrument in use is capable of long and short.
      If you are limited to long trades only then it is a different story. Then you have to manage both instruments in your code.

      P.S. What are the 1450USD monthly for? CQG or something like that?

      Comment


        #18
        It is specifically for multi instrument trading, long one 'abc' and short two 'xyz' for example, here is a copy paste from another forum where I started working on this publically:

        "If you think XYZ is worth $5.00 but is currently trading at $5.00, you might place a GTC order at $4.90 and another at $4.80. You are happy when you are filled at $4.90, and even happier when you are filled at $4.80 because you know it is worth $5.00 and expect the market to recognize that shortly so you can sell at $5.00 and pocket $0.30.

        Perhaps you notice that ABC has very similar price action to XYZ... they zig and zag together most of the time. Perhaps you notice that ABC trades at $7.00, and looking back in recent price history there is a $2.00 difference (or spread) between ABC and XYX roughly 95% of the time. The other 5% of the time this relatonship is out of whack briefly but eventually goes back to $2.00.

        If the market opens one day with XYZ at $5.00 and ABC at $8.00, your bet might be that ABC will decrease in price more than XYZ will increase in price (all things being equal), so you short ABC and buy XYZ.

        All well and good using end-of-day data and a calculator, but what if you wanted to be a bit more agressive? Your math tells you the "value" of the spread should be $2.00, and every time its $3.00 you want to buy XYZ and sell ABC. What you might consider is quoting to sell ABC at $3.00 below an Ask price of XYZ where there is enough Ask depth to ensure you get filled if somebody actually buys ABC from you.

        That would be difficult to manage manually in a fast-moving market... but let's make it even harder.

        If I like that spread at $3.00, I'd love it at $3.50, and be crying with joy at $4.00. So I actually want to quote at three different Bid price levels for ABC while watching my Ask depth of XYZ. As you can imagine this will involve a lot of cancel and replace orders which would impossible to do manually. Enter a auto spreader."


        That's where I'm coming from. The $ quote is X_Trader Pro. CQG with the spreader is a bit more where I live.

        Comment


          #19
          OK,

          so you want a real spread, buy X and sell Y , all the time.

          This becomes a bit too lengthy for something I looked into just because this saturday after lunch it was raining outside...

          I have never been much into spreads, as I do normal future trading usually and I am happy with it.

          Give me a PM if you want to pursue

          regards

          Andreas

          P.S. attached a file that opens real spreads using the multi-instrument approach. I didn't review your PairsZscore indicator though.
          Attached Files
          Last edited by zweistein; 05-08-2010, 11:14 AM.

          Comment


            #20
            Its sunny and 28 degrees C on my end so I'll be working on this later today .

            The SpreadZScore indicator looks at the Z Score of the price difference of two instruments over n periods. The general idea is that spread is "out of whack" if it is +/- two standard deviations from the mean (in this case the n period SMA).

            If anyone is interested attached is a single-instrument ZScore indicator, which is similar to the RSI indicator. It may be useful in support of other signals, but like the RSI things can remain overbought/oversold for some time, and you can't hedge by buying the SMA (hence the logic of pairs/spreads).

            Indicator is NT7 only.
            Attached Files

            Comment


              #21
              I was testing out PairsTraderBeta and I noticed some unusual activity in backtesting.

              Changing value "coverSpread" will change backtesting results slightly but it doesn't seem to change when the spread covers. It looks like the spread doesn't actually cover but instead reverses position when the ZScore changes, Zscore=2 the position changes from long to short and ZScore=-2 the position changes from short to long.

              Comment


                #22
                I get that too, chrisdc5, but only when doing things on, say, minute bars. I also get IExecution exceptions (NullReferenceException on IExecution execution) on minute bars.

                So on minute bars it seems to be "always in" which is wrong, but on daily bars it it opens positions on the +/- 2 line and exits on the 0 line as expected.

                I do not know why

                Comment


                  #23
                  Hello to all Pair-friends

                  my sample should work also in backtesting if ExitOnClose is set to false.


                  regards
                  andreas

                  Comment


                    #24
                    MXASJ

                    Good to hear it wasn't only me, but hopefully we can find a way to fix that as it would be much more interesting on minute bars. I haven't had much time yet but I will try helping as soon as possible.

                    Zweistein - I tried backtesting but it is only executing one side.

                    Comment


                      #25
                      @chrisdc5,

                      Yes , the first versions were executing only one side.

                      But the version from post#19 should execute both sides. at least it seemed in my tests..

                      Maybe I have not understood well what the pairstrade is supposed to do.

                      Any input/feedback from you is always welcome...


                      regards

                      Andreas

                      Comment


                        #26
                        zweistein - I just tried out the PairsTraderXY and the backtesting works great, even on minute bars.

                        MXASJ - I commented out the code below and the backtest worked, it allowed the code to cover. It may not be a good idea in production but for backtesting it should be fine.


                        // Log("TryCatch Error: STRATEGY PairsTrader. OnExecution/Long Logic.", LogLevel.Error);
                        // Print(Time[0] + " " + e.ToString());

                        Comment


                          #27
                          I changed some of the logic in OnBarUpdate and now this seems to work as it should on minute bars as well.

                          I'm still getting the unusual thing where what is displayed on the Strategies tab does not match what is shown on the Positions tab. The Positions tab is correct.

                          Try it in sim, minute bars, 1 ES against 2 NQ. That won't make you money but it should trade often enough to see what this is intended to do, and (hopefully) confirm the Strategies tab/Positions tab discrepency so I know its not just me .
                          Attached Files

                          Comment


                            #28
                            Hi,

                            I read your code (PairTradingBeta) but I have a question:

                            protected override void OnExecution(IExecution execution)/////////////////////////////////////////////////////////
                            {
                            try
                            {
                            if (entryOrderL != null && entryOrderL.Token == execution.Order.Token)
                            {
                            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled
                            || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                            {
                            entryOrderHedgeL = EnterShort(1, hedgeQuantity, "Long Spread Hedge");
                            if (execution.Order.OrderState != OrderState.PartFilled)
                            {
                            entryOrderL = null;
                            inventory ++;
                            }
                            }
                            }

                            Basically, you require to EnterShort the hedgeQuantity of security # 2 whenever you get a partial fill (which works fine in backtest), but when you trade on the market, if you get, say, 3 partial fills to fill completely your order, you will send 3 EnterShort hedgeQuantity on your second leg, which is wrong, isn't it?!

                            Thank you for your answer

                            Comment


                              #29
                              Hey,

                              I've looked through your code and it looks great so far. I have been working on a similar project but I like your approach better (using the OnExecution method to enter second leg of the trade). I will be testing it and I'll let you know if I find anything abnormal.

                              One quick note, if you try a spread on currencies (Auzzie/Cad 1:1 for example), you get a weird looking chart and most likely faulty indicator calculations because of the rounding in SpreadZScore. Changing the rounding to 4 decimal places solves the issue.

                              mySpread.Set(Math.Round(spreadRough, 4));

                              Cheers

                              Comment


                                #30
                                Thanks pmorissette and stefy for the interest. To be honest I haven't worked on this one for a while as I ran into some beta 14 and 15 bugs that made it more of a challenge than it should be.

                                I have one more code project to get out of the way and then I'll resurrect this one.

                                One thing I'll probably want to do first is seperate the alpha idea from the execution code. It is the safe, reliable, and consistent execution of a pairs/spread idea that is my goal.

                                Alpha (money making) ideas can be added once that is in place.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                649 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                370 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                109 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                574 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                576 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X