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;
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;