Avoid opening two different position on new bar
Author: bishop
Creation Date: 3/2/2012 7:07 AM
profile picture

bishop

#1
Hello,

What is the best way to avoid opening a long and a short position on the same bar for SP strategies?

I have an entry condition, which can be true for both long and short entries on one bar. But my main purpose is to have only one position, either long or short (depending on which was opened first).

I apologize in advance for the silly question (i'm only in the beginning of my Wealth-Lab coding experience).


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

Eugene

#2
Hi Vitaly,

Each trading method used to enter long and short Positions returns a new Position object that represents the newly established position. The idea is to create a Position with the different "sign" if you haven't yet established a Position, as indicated by the returned Position being equal to null:
CODE:
Please log in to see this code.

For more illustrations of this design pattern, see the built-in Strategies "Bandwagon Trade" and "Channel Breakout VT".
profile picture

bishop

#3
Eugene, thank you very much. All clear now.

I have one more question related to plotting DataSeries which was converted to double.
In the code I have created:
DataSeries mid = Close - Close;
mid[bar] = (High[bar-period] + Low[bar-period])/2;

What is the best way to plot mid? Or where is the best way to insert PlotSeries?

In my code I realized it right before closing the bracket for "for loop", but I see very long list label "Close-Close" on the chart. Where am I wrong?

Thanks.

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

Eugene

#4
QUOTE:
What is the best way to plot mid?

The best way to plot the mid is to simply use AveragePrice instead:

Standard Indicators > AveragePrice
QUOTE:
In my code I realized it right before closing the bracket for "for loop", but I see very long list label "Close-Close" on the chart. Where am I wrong?

Do not place a PlotSeries call in the loop. Move that line outside the for loop. This applies to any DataSeries, be it the one created on-the-fly (e.g. mid) or a 'formal' indicator like AveragePrice.
profile picture

bishop

#5
Great, thanks for all advises.
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).