stop-price variable is not working
Author: Musashi1970
Creation Date: 1/16/2010 1:28 AM
profile picture

Musashi1970

#1
Hi
I want to define an initial stop after opening a long position which depends on the entry price and the then actual ATR. Right after the buy-order, I define the variable inistop accordingly. But somehow WLD does not recognize the value, because the stop is never executed. What is my error, and how has the code to be arranged ?

Thx & Kind Regards

QUOTE:

protected override void Execute()
{
double inistop=0;

PlotStops();

for(int bar = 100; bar < Bars.Count; bar++)
{


if (IsLastPositionActive)
{
//code your exit rules here
SellAtStop(bar,LastPosition,inistop,"Initial-Stop");
}
else
{
//code your entry rules here
if ( .... )
{
BuyAtLimit(bar+1,Bars.Close[bar],"Long-Entry");
inistop=LastPosition.EntryPrice - ATR.Value(bar, Bars, 20);
}
}
}
profile picture

Eugene

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

Eugene

#3
Trading methods return a Position object if it was established. In other words, first you need to check that the Position has been created - by making sure it's not null.
profile picture

Musashi1970

#4
Hello Eugene
thx again for the quick post, but I have to admit, I am bit lost now.

1. in my version, when it has executed the buyatlimit-line, it should know that a position is established and therefore should be able to calculate the inistop-variable ?
2. when I substitute my inistop-line with your suggestion, the stop is placed correctly, but mysteriously, it opens not one but two long-positions (and establishes only one stop) ???

Your support is highly appreciated
profile picture

Eugene

#5
I don't believe in mystery, rather in an error somewhere else in the code.

P.S. That SellAtStop(bar..., initstop) alone looks very suspicious i.e. the variable is set at bar+1 and its value is used at bar+0, although I'm not telling it's the culprit.
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).