Issue running pair trading strategy
Author: stolmax
Creation Date: 8/31/2009 11:18 PM
profile picture

stolmax

#1
I am having an issue running a pair trading strategy provided by Wealth-lab. It is not really an issue w/ a strategy, since this happens with my own pair trading strategy as well, but rather with the execution context. Her is the issue. For example, I decided to trade GDX against GOLD. I hardcode these symbols in the strategy and run the strategy after previously clicking on the GDX symbol in my data set. As the result of running my strategy, I get a profit of X for 3 years of back testing. Now, I click on the AAPL symbol and the strategy is being re-executed by the Wealth-lab pro automatically. Unfortunately, now my profit for the same strategy is not X but Y, eventhough all of the strategy symbols were hardcoded inside the strategy and trading only happens with hard-coded symbols. Can you please let me know what I am doing wrong and why a profit calculation for the strategy is being effected by the active symbol, eventhough this symbol is not being used for trading inside the strategy. So you don't have to pull up your code for pair trading strategy, here it is:

CODE:
Please log in to see this code.
profile picture

Cone

#2
CODE tags when posting code, please?

See the "true" in SetContext? WealthScript Programming Guide: DataSeries > Accessing Secondary Symbols > Secondary Series Synchronization

profile picture

stolmax

#3
Thanks Cone! Sorry for the tags
profile picture

nexial_1002002

#4
Good luck with that. You're still several steps from a viable long term version.
profile picture

stolmax

#5
Hi Cone,

I tried your suggestion, but unfortunately they did not work. Here are the steps I tried:

1. Changed synchronized flag on all SetContext() calls to false. Got the following exception:

Runtime error: Plotted DataSeries has fewer bars than the Symbol being charted
at WealthLab.Strategies.PairsTrading.Execute()
at WealthLab.WealthScript.PlotSeries(ChartPane pane, DataSeries series, Color color, LineStyle style, int32 width)
at WealthLab.WealthScript.a(DataSeries A_0)

Commented all Plot() calls
Runtime error: DataSeries do not have equal number of values for mathematical operation
at WealthLab.Strategies.PairsTrading.Execute()
at WealthLab.DataSeries.op_Division(DataSeries ds1, DataSeries ds2)

Changed synchronized flag for initial 2 SetContext() calls to true
Runtime error: Bar number must be 2155 or less
at WealthLab.Strategies.PairsTrading.Execute()
at WealthLab.WealthScript.BuyAtClose(Int32 bar, String signalName)
at WealthLab.WealthScript.a(Int32 A_0, Int32 A_1, Int32 A_2)
profile picture

stolmax

#6
At some point I even got the following exception:

Runtime error: Basis price for Position entry cannot be zero
at WealthLab.Strategies.PairsTrading.Execute()
at wealthLab.WealthScript.BuyAtClose(Int32 bar, String signalName)
at WealthLab.WealthScript.a(Double A_0)

Can you please update my code above to remove the dependency between stocks being traded by the strategy and the actual "Active" symbol user clicks on to run a strategy. Thanks

Max
profile picture

Eugene

#7
QUOTE:
1. Changed synchronized flag on all SetContext() calls to false. Got the following exception:

You're missing a call to Synchronize (e.g. close1 = Synchronize( close1 ); etc).
QUOTE:
Runtime error: Basis price for Position entry cannot be zero

You need to start the main loop like this:
CODE:
Please log in to see this code.

Please see the QuickRef for FirstActualBar for a detailed explanation.
profile picture

stolmax

#8
hi Eugene,

I understood wat you suggested above and tried to implement your suggestion, but I still cannot get it right. First I changed all SetContext() calls to have sync flag set to false. With this flag set to false, I retrieved data for both stock symbols above and for one there were 4474 bars and for another 2157. This made it difficult to calculate moving averages because of unequal number of bars. Nevertheless, my Bars.FirstActualBar was 2157 and not 0. Because I could not calculate ratios, I followed your second suggestion and coded a Synchronized(Bars.Close) calls for each stock. This automatically increased number of bars in the second data series to 4474; now all ratios and SMA were calculated correctly. Unfortunately, now I started to get exception:

Runtime error: Bar number must be 2157 or less
at WealthLab.Strategies.PairsTrading.Execute()
at WealthLab.WealthScript.BuyAtClose(...)

I checked my FirstActualBar value and it is 0, since I had to make Synchronize() to get the same number of bars b/w 2 stocks in question. One one point, I was able to make it work, but still every-time I would click on a new symbol my Net Profit value would change.

I am sorry, but without a good example on how to work with 2 stocks at the same time and how to prevent an "Active" series from getting input Net Profit calculations, and without a standard debugging tools it is difficult to get this working. I am also surprised that this is the case with a standard strategy that you guys host on the site.

Can you please help me out and update the pairs trading strategy to show me how to accomplish what I want, such as:

1. Working with stock coded in the strategy and not the stock I am clicking on to run the series.
profile picture

stolmax

#9
Also, can you please let me know, how I can create a sub-set of the Bars object, so that I can create a new Bars object from an original object that contains data for only 100 days back and not all data.

I tried creating 2 DataSeries objects with only data for the last year, but when I the tried to calculate their ratio, ds1/ds2, the application threw an exception that Object reference was not found.

This is just extremely frustrating.

Thanks in advance.
profile picture

Eugene

#10
QUOTE:
Also, can you please let me know, how I can create a sub-set of the Bars object, so that I can create a new Bars object from an original object that contains data for only 100 days back and not all data.

CODE:
Please log in to see this code.
profile picture

stolmax

#11
Thanks Eugene.
profile picture

stolmax

#12
Hi Eugene,

I finally was able to modify the program to generate "consistent" results and then modified it again to work only on the predefined bar interval and use active symbol as one of the stocks in the pair trading. At first, it looked like everything was working properly, but then I started to review trades and nothing made sense or may be I was just not getting it. In my program that I will post below, you will see that I am only trying to analyze data for the last 500 bars which should really correspond to only data for the past 2 years. Unfortunatly, either by design or my mistake that I cannot see, all trades by the application were placed from 1992 til 2003 and not in the last 2 years. This really does not make sense to me. Can you please review my program and let me know what I am doing wrong? Thanks in advance.

Here are the debug statements from a sample run:

Number of bars1: 500
Number of bars2: 500
Performing calculations
Done calculating
Total bars: 500
StartBar: 30
First Actual bar: 0
Date of first bar: 9/13/2007 12:00:00 AM
Date of second bar: 9/4/2009 12:00:00 AM

As you can see, I am starting my calculations on 9/13/2007 and therefore don't understand why my trades were placed in 1992. I would really appreciate any help I can get on this. Thanks

CODE:
Please log in to see this code.


Max
profile picture

Cone

#13
1. FirstActualBar is broken in 5.4
2. You SetScaleDaily(), but never reverted to RestoreScale(). (See QuickRef for the Important! note.) Trades must be created in the chart's base time frame, otherwise you're sure to get results that make no sense.
profile picture

quaresma

#14
Hi, I am trying to make a pair trading strategy for some instruments.
But at the time of run strategy, wealth lab drop(picture in the attachment). There are 2 tools in the dataset that I use in the code. Tell me, what am I doing wrong? Thanks
CODE:
Please log in to see this code.
profile picture

Eugene

#15
Hi,
QUOTE:
But at the time of run strategy, wealth lab drop(picture in the attachment).

I ran your code on a few instruments in Minute scale and Wealth-Lab didn't crash for me. You might want to provide more details:

1. Single symbol or Multi-symbol mode
2. Data Loading settings
3. Symbol(s) and Data provider
profile picture

quaresma

#16
Hi, Eugene.
1) I'm using single symbol mode and choosing instrument inside the dataset
2) TimeFrame 5 min and range 3 month.
3) i'm using finam data provider and choose cryptocurrency: longSymbol = ETH-USD, shortSymbol = BTC-USD
Sometimes it runs fine, but once I uncomment this part in my code:
CODE:
Please log in to see this code.

and everything falls
profile picture

Eugene

#17
Still no crash running your code on the two cryptos by Finam provider in 5-min scale over recent 3 months and with the four lines above uncommented:

CODE:
Please log in to see this code.
profile picture

quaresma

#18
Okay, thanks for your input. I will try to solve this problem
profile picture

Eugene

#19
Any effect from changing the position size (Raw Profit, Port.Sim, PosSizers)?

What about different DataSets, symbols and bar scale (to exclude Finam's data)?
profile picture

quaresma

#20
And one more question.
I changed the logic a bit and on some data I have everything running stably and I run my code. But I have a question with the equity display, after a certain period it goes in a straight line as if there were no trades, but in fact there were trades. What could it be? I use SetShareSize and in Position size select "overridescript". date I attached in attache

CODE:
Please log in to see this code.


profile picture

quaresma

#21
TF 10 minutes, range 3 months.
Please try to run my code by Porfolio Simulation Mode "WealthScript Override (SetShareSize), whether equity will be displayed correctly?
profile picture

Eugene

#22
The equity gets displayed for me up until 11/22/2019 (when the system has stopped trading).
profile picture

quaresma

#23
Eugene, yes close last trades was 22/11/2019. I open "Equity+" and and equity changes ends in early October. Although in early October until November there were deals and she had to re-evaluate the position at every bar, right? Do you have the same equity?

profile picture

Eugene

#24
No idea about the custom performance visualizer you're using but for me the equity curve looked different (when appeared in the Equity tab). Sorry, I don't have time for troubleshooting your custom Strategy at the moment. Please use debugging tools available like PrintDebug and Visual Studio.

Good luck with your project.
profile picture

quaresma

#25
The Equity+ tab (part of the MS123 Visualizers library). I understand, thanks for your help
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).