This is a mobile optimized page that loads fast, if you want to load the real page, click this text.

Amibroker: Optimize stops for multiple symbols

Joined
24 April 2012
Posts
2
Reactions
0
Hi,

I am trying to write an AFL, where for a given strategy, I can optimize the stops for multiple symbols based on the ATR of the stock. Using DebugView, I see the program works fine for the first loop, but then the BarCount value seems to reset to some crazy number, and that throws all the other calcualtions off.

I thought BarCount would be static for a given chart. Is that not correct? Code is below. Thanks for any advice.

// Set variables
MyDefault = 1;
AtrArray = ATR(15);
MyATR = AtrArray[BarCount-1];
_TRACE("ATR = " + MyATR);
_TRACE("BarCount = " + BarCount);

if (MyATR >= 1 AND MyATR < 50)
{
MyMinStop = 1;
MyMaxStop = round(MyATR/10);
MyStopIncrement = 0.25;
MyMinTgt = 0.75 * MyATR;
MyMaxTgT = 2 * MyATR;
MyTgtIncrement = 1;
}

if (MyATR >= 50 AND MyATR < 100)
{
MyMinStop = 1;
MyMaxStop = round(MyATR/10);
MyStopIncrement = 1;
MyMinTgt = 0.75 * MyATR;
MyMaxTgT = 2 * MyATR;
MyTgtIncrement = 2;
_TRACE("Entered Function ");
}

if (MyATR >= 100 AND MyATR < 200)
{
MyMinStop = 1;
MyMaxStop = round(MyATR/10);
MyStopIncrement = 1;
MyMinTgt = 0.75 * MyATR;
MyMaxTgT = 2 * MyATR;
MyTgtIncrement = 5;
}
 
MyStopLoss = Optimize("Stop",MyDefault,MyMinStop,MyMaxStop,MyStopIncrement);

_TRACE("Max Stop = " + MyMaxStop);
_TRACE("Min Tgt = " + MyMinTgt);
_TRACE("Max Tgt = " + MyMaxTgt);
_TRACE("Stop Incr = " + MyStopIncrement);
_TRACE("MyStopLoss = " + MyStopLoss);

// Strategy
YesterdayClose = Ref(Close,-1);
GapSize = 1;
Buy = Long = Open > (YesterdayClose+GapSize);
BuyPrice = Open;
Condition1 = ApplyStop(stopTypeLoss,stopModePoint,myStopLoss,1,False,0);
Sell = Condition1 OR Close;
 
Hi Amita --

I did not check all of your code, but this statement might be causing part of the problem:

Sell = Condition1 OR Close;

In AmiBroker, logical variables are either True or False. False is exactly zero, True is anything else. Since Close is a price, it is not zero, hence True. Anything OR True is always True. So the statement above is equivalent to:

Sell = True;

Thanks,
Howard
 
Hi Howard,

Thank for the suggestion. I tried making the last statement

Sell = Condition1;

and still got the same error. It works fine for the first iteration, but then messes up. Even if I explicitly change the MyATR statement to something like this,

MyATR = AtrArray[16];

the MyATR value changes on the second iteration of the loop. I cant figure out why...I expect this to be static throughout the Optimize loop.

Amit.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more...