How would you usually use order_target_percent using Mean Reversion?

Ive tried to use the log of the raw data and use a ratio of weights(order_target_percent(i, (j/Total)))  the portfolio but its performance negative. I also tried using raw data and multiplying the raw asset price by the amount of the asset at that time and that its performance also was negative. How do I utilize targeting orders based on percentages of my equity using Johansen Cointegration

Hello Jane,



Let me try to understand what you are asking:


  1. Are trying to ask how to use the hedge ratio cash value into the order target percent?


  2. Are trying to ask why the performance of your strategy is negative?


  3. Are you asking both questions?



    Please answer which questions you're asking to help you,



    Thanks and regards,



    José Carlos
  1. Are you asking both questions? Yes, both.

Hello Jane,



I guess you are using the code you sent to us previously in another query, right?



In case that's right, I'll check your code to help you.



Thanks



José Carlos

No that one uses Engle Granger. But it has a similar issue, in which im trying to find a way to target based on a percent of my balance. I made an imporvement to the code btw I send it to you.  

The below example would help you understand the working of order_target_percent



If you have $1000 of capital, the below code will try to buy $500 worth of Apple stocks.



order_target_percent(symbol('AAPL'),0.5)



Mean-reversion



Regression of log prices will give you the hedge ratio in dollar amount. If you have two assets A & B, you get the following equation



spread = A - hr * B



Let's say the hr or hedge ratio is 2. the equation becomes

spread = A - 2 *B



In the above equation, you want to buy $1 worth of A for every $2 worth of B sold. The ratio of capital is 1/3 for A and 2/3 for B. You can specify these in the order_target_percent as below



order_target_percent(A, 0.33)

order_target_percent(B, 0.67)



I hope this helps.

I understand this perfectly, my problem is that it works really well with raw prices and order target in share amount. But doesnt work well with logs and cash value ratios(used as percentages of cash value).

You can run both the backtests for the same 1-2 days and print context.portfolio.positions. These will show you the actual quantity and price at which the order was executed. And then you can compare both methods. You can also check the positions taken by the algorithm with what you expect and see if there is any discrepancy.



Thanks

Still waiting for Jose Carlos Gonzales Tanaka to check my code btw I emailed quantra@quantinsti.com

Hello Jane, 



Thanks for the reminder. Our excuses.



Your code has been checked. It really looks great. 



I'm going to copy the following part of your code:

 

                if df1['numUnits'].iloc[-1]==1:
                    print("len(list_sect)",len(list_sect))    
                    print("{} Long entry".format(get_datetime()))
                    print("x.values[0]---",x.values[-1])
                    print("df1['hr'].iloc[0]",df1['hr'].iloc[-1])
                    print("+---",((1/(abs(((df1['hr'].iloc[-1])))+1)))*(1/len(list_sect)))
                    #order_target(s1,df1['hr'].iloc[-1])
                    order_target_percent(s1, ((1/(abs(((df1['hr'].iloc[-1])))+1)))*(1/len(list_sect)))
                    print("{} Short entry".format(get_datetime()))
                    #order_target(s2,1)
                    order_target_percent(s2, ((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))
                    print("+---",((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))
                if df1['numUnits'].iloc[-1]==-1:
                    print("len(list_sect)",len(list_sect))
                    print("{} Short entry".format(get_datetime()))
                    print("df1['hr'].iloc[-1]",df1['hr'].iloc[-1])
                    #order_target(s1,-1*df1['hr'].iloc[-1])
                    order_target_percent(s1, -1*((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))
                    print("+---",((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))
                    print("{} Long entry".format(get_datetime()))
                    #order_target(s2,-1*1)
                    order_target_percent(s2, -1*((1/(abs(((df1['hr'].iloc[-1])))+1)))*(1/len(list_sect)))
                    print("+---",((1/(abs(((df1['hr'].iloc[-1])))+1)))*(1/len(list_sect)))

I'm going to explain the error in your code based on what has been explained in another post you created called "Determining the optimal lag period using unrestricted VAR". The explanations were given by Mr Shah and me.


  1. Remember that spread = Y - hedge_ratioX. So, if the hedge ratio is 2, then spread = Y - 2X. If hedge ratio is -2, then spread = Y +2*X. So, in the former example, when you go long the spread, you buy Y and short-sell 2 X; when you go short the spread, you short-sell X. In the latter example, when you go long the spread, you buy 1 unit of Y and 2 units of X, when you short the spread, you short-sell 1 unit of Y and short-sell 2 units of X.



    So following this example, let's change your code:

     
                if df1['numUnits'].iloc[-1]==-1:
                    print("len(list_sect)",len(list_sect))
                    print("{} Short entry".format(get_datetime()))
                    print("df1['hr'].iloc[-1]",df1['hr'].iloc[-1])
                    #order_target(s1,-1*df1['hr'].iloc[-1])
                    order_target_percent(s1, -1*((1/(abs(((df1['hr'].iloc[-1])))+1)))*(1/len(list_sect)))
                    print("+---",((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))
                    print("{} Long entry".format(get_datetime()))
                    #order_target(s2,-1*1)
                    order_target_percent(s2, -1*((df1['hr'].iloc[-1]/(abs(((df1['hr'].iloc[-1])))+1)))*((1/len(list_sect))))

As you see, I only change the shorting-of-the-spread code. If you make a comparison with your previous code, you see that I replaced the order target percent of s1 with the one you gave for s2. I also replace the order target percent of s2 with the one you gave for s1. 



Let me explain your error with an example. Imagine the hedge ratio is 2. So the spread = Y - 2*X. The code you gave when you go long the spread was ok (you would buy Y and short-sell 2 X). Now, when you go short the spread you must short-sell 1 unit of Y and buy 2 X. But you did the following: you short-sell 2 units of Y and buy 1 unit of X. Do you see what was your problem?



I hope this helps,



Thanks and regards,



José Carlos





 

Can you email me back the entire code and a backtest showing ts performance. Thank you.

Also the code in question was the Johansen Cointegration not Engle Grandger. Please see other code I sent through the same email thread to quantra@quantinsti.com

Still waiting on you to send/show your performance that " really looks great "

Hello Jane,



My last response to your code was responding to your query regarding how to deal with the order function and the hedge ratio combination. 



Now, could you please tell me what your question is regarding the Johansen Cointegration test?



You sent a code on July 7th in which you applied the ADF test. This is not the Johansen cointegration test.



Please let me know what you need



Thanks



José Carlos

  1. Using Engle Granger as well as  the Johansen  method I have the same issue and I also said to email me your solution of the fully working code and backtest. This code in question the Engle Grander pair trading strategy sent to you 28 Jun at 18:22 


  2. Using the Johansen cointegration on 2 Jul at 08:11 I sent you the code Im talking about where the goal is to use percent based orders using a ratio of cash valued weights.

Hello Jane,


  1. Using Engle-Granger as well as the Johansen method I have the same issue and I also said to email me your solution of the fully working code and backtest. The code in question is the Engle Grander pair trading strategy sent to you on 28 Jun at 18:22.



    Answer: To help you, please let me know exactly in which part of your code you have a problem. The use of the Engle-Granger test, the Johansen test, a different hedge ratio, or whatever minor change that your strategy can have, in case everything's alright with your code, might have several results for your strategy performance. 



    So, let's give some advice:
  2. Make sure your code doesn't have any errors.
  3. In case there is no error, don't expect your strategy (with different tests, a different combination of the use of different types of hedge ratios and different types of orders, etc) will have similar results for each small change you make to it. 
  4. Performance will change as per any small or big change you make to your strategy. It can be true that small changes might produce similar performance, but you have to be sure that in each section of your strategy outputs the changed inputs are returning similar results.
  5. Don't forget that the Engle-Granger test is a test for univariate equations, the Johansen cointegration test is for multivariate equations. Each test has a different form to calculate the critical values and their own proper distributions. So don't necessarily believe they will make you have similar results.



    I hope this helps
  1. For the Engle Grander strategy sent to you 28 Jun at 18:22 please send me a performance of the corrections you made. Also make sure to run it without your changes to see the contrast before you do.





    These changes you made Jose Carlos Gonzales Tanaka on this blog post 06 Jul 2022.


  2. For the Johansen cointegration I sent on 2 Jul at 08:11 Im not sure where the error is but its in the money mangement area.

Still waiting.

 

Hello Jane,



Unfortunately, we are having issues running your code in the Blueshift platform. Consequently, I cannot help you up to now. Once we resolve the issue. We can do the following:



For 1): Please run your code in the Blueshift platform and if you see there's an error, let us know.



For 2) Can you tell us what error output you get from the Blueshift platform?



Thanks



José Carlos

Please reffer to the code sent to you just now (16th July) for the Johansen cointegration.

Its the same code and its has no error.