Is it possible to open 2 positions on one bar in 2 stocks in pair trading?
Author: akuzn
Creation Date: 7/21/2012 2:17 PM
profile picture

akuzn

#1
In real time trading and in testing mode i see that my strategy doesnt open pair positions. Only one stock.
And another problem - it puts orders and cancels them at the same time.
I ve got an explanation that SetContext and RestoreContext cancel alerts of previous symbol so orders are canceled too.
I saw how it is happening in real time. Strategy creates and cancels orders at the same time.
I use SetScaleCompressed and RestoreScale too an it seems to me that RestoreScale has not only effect of restoring scale but also of restoring context.

1. Is there anything wrong in following logic?

SetScaleCompressed
// computing some DataSeries for symbol 1
RestoreScale
SetContext ( stock2 )
// computing some DataSeries for symbol 2
// computing stats functions based on relationship between DataSeries of symbol 1 and symbol 2
order ( Buy, Short, Cover or Sell ) // for symbol 2
RestoreScale
RestoreContext()

Order ( Buy, Short, Cover or Sell ) // for symbol 1
// orders of symbol 1 are at the end because it gives time economy of minus 1 use of SetContext.


2. Is there any way to avoid orders canceling?
profile picture

Eugene

#2
Double check that you are not wrapping trading functions inside SetScaleCompressed (or SetScale* for that matter) as it will lead to incorrect results. Note what the QuickRef states:
QUOTE:
Important!

You must call RestoreScale() to return to the original time scale particularly for plotting and executing trading signals. In general, only remain in a compressed scale to create indicators and immediately revert to the base scale by calling RestoreScale().

The 2nd, orhpaned RestoreScale call makes me wonder if your code suffers from this oversight.
profile picture

Eugene

#3
QUOTE:
// orders of symbol 1 are at the end because it gives time economy of minus 1 use of SetContext.

Sounds right, for two symbols there's no need for two SetContext's - having just one is enough. RestoreContext isn't need too I think, if you organize the workflow like this:

SetScaleCompressed
...
RestoreScale
SetContext ( stock1 )
Order for symbol 1

SetContext ( stock2 )
SetScaleCompressed ?
+
RestoreScale - depending on your script's logic

order for symbol 2
no need for RestoreContext if sending order for pair symbol is your last action in the loop by bars
profile picture

akuzn

#4
Certainly no trades before restoring scale.

After many test i ve found the main problem.
Unfortunately in quick ref manual i didnt find some answers wich could help me.

When i switch context which position is in
IsLastPositionActive - the latest active position of context symbol or latest of all symbols?
LastPosition, LastActivePosition etc.

Why i ask about it.

For example:
SetContext ( stock2, true );
................
if (p.Active)
sell = conditions....;
cover = conditions....;
if (p.Long.... && sell )
SellAtLimit ( bar + 1, p,...);
....
RestoreContext();

similar logic...

The main problem in which object concerning position i can find information concerning only contexted symbol?

When i try to use IsLastPositionActive in any context seems it has information about last active position of all symbols and i cant use 2 stocks
i can be in position only in one stock.

profile picture

Eugene

#5
Take a look at the "Pair Trading" Strategy.
profile picture

akuzn

#6
I ve looked there.
they dont manage position. only one bar holding or exit by CloseAllPosiitons
profile picture

Eugene

#7
I see. Since what you're building essentially is a multi-position Strategy, you can't rely on LastPosition logic anyway.

Have you considered replacing that in your script with the "MP Strategy Template" from the WealthScript Guide, Programming Trading Strategies > Multi-Position Strategies?
profile picture

akuzn

#8
Where i can find this template?

Nor in my wealthscript ref nor in wiki search i have not found
"MP Strategy Template" from the WealthScript Guide, Programming Trading Strategies > Multi-Position Strategies?

I dont use LastPosition - just asked what it contains after switching context - last position of contexted symbol or lastest position info of 2 symbols?
So Positions contains information about all symbols or context symbol ?
profile picture

Eugene

#9
The WealthScript Guide is located under Help menu. Carefully read that short chapter, and try applying the MP template. LastPosition and IsLastPositionActive are relevant for single-position strategies.
profile picture

akuzn

#10
Yes i have this menu).
Seems after many tests i ve got what i wanted.
Thank You very much. Result is surpassing any waitings.
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).