Opening position % above/below signal

Hi,

I am testing following strategy on AFL.

Buy 0.5% below Close when its more than 3 StDev lower than intraday VWAP (based on OHLC) between 11AM-3:30PM EST, sell when Close is higher than VWAP.

I sorta figured the AFL code, I just want to double check if I am writing correctly.

Here's the code:

/////START

//Define starting capital and position size
SetOption("initialequity", 100000);
SetPositionSize(20,spsPercentOfEquity);
SetTradeDelays(1,0,0,0);
SetChartBkColor(colorwhite); // color of outer border
SetChartBkGradientFill(colorlightorange,colorPaleGreen,colorBlack); // color of inner panel
Plot( C, "Portfolio Equity", ColorBlend( colorBrightGreen, colorBlack ), styleGradient | styleLine, Null, Null, 0, -1 );
SetOption("MaxOpenPositions", 5);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

StartTime = 110000;
Endtime = 153000;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Bars_so_far_today = 1 + BarsSince(Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 400000, BarIndex());
TodayVolume = Sum(V, Bars_so_far_today);
VWAP_OHLC = Sum((O + H + L + C) * V, Bars_so_far_today) / (4 * TodayVolume);

// Calculate standard deviation
SumOfSquaredDeviations = sum(((O + H + L + C) / 4 - VWAP_OHLC) * ((O + H + L + C) / 4 - VWAP_OHLC) * V, Bars_so_far_today);
StandardDeviation = sqrt(SumOfSquaredDeviations / TodayVolume);

// Calculate upper and lower bands
UpperBand = VWAP_OHLC + 2 * StandardDeviation;
LowerBand = VWAP_OHLC - 2 * StandardDeviation;
UpperBand3 = VWAP_OHLC + 3 * StandardDeviation;
LowerBand3 = VWAP_OHLC - 3 * StandardDeviation;
UpperBand4 = VWAP_OHLC + 4 * StandardDeviation;
LowerBand4 = VWAP_OHLC - 4 * StandardDeviation;
UpperBand5 = VWAP_OHLC + 5 * StandardDeviation;
LowerBand5 = VWAP_OHLC - 5 * StandardDeviation;
UpperBand6 = VWAP_OHLC + 6 * StandardDeviation;
LowerBand6 = VWAP_OHLC - 6 * StandardDeviation;

ibs = (C-L)/(H-L);

newl = C == LLV(C,4);

volumefilter = MA(V,90) > 100000;

barsbelow = C > VWAP_OHLC;

nbars = 3;

condition = Sum(barsbelow,nbars) >= nbars;

TimeFrameSet(inDaily);
Trendfilter = Ref(C,-1) > MA(C,200);
TimeFrameRestore();
Trendfilter 			= TimeFrameExpand(Trendfilter, inDaily, expandFirst);


StartTime = 110000;
Endtime = 153000;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


N = 1;

dn = DateNum();
newDay = dn != Ref( dn,-1);


Buy = C < LowerBand3 AND volumefilter AND Trendfilter AND timeOK;
BuyPrice = Cross(C, Buy * 0.995);
Sell = C > VWAP_OHLC; 
SellPrice = Close;

Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

Buy = ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

/////END

I used this thread as guide: Buy when current price is 1% above day opening

I'm no expert coder, but I'll share what works for me in these situations. Run a backtest on your code and track the individual trades. Walk through a trade and see if the system is doing what you expect. If you're able to walk through the trades and everything looks good in a backtest try moving to paper trades or small position sizes to check if you are able to get the actual results that your system produces.

If you find the system doesn't do what you expect then you can troubleshoot to see what part of your code isn't performing.

I figured solution, with a little help from others.

This is from another my strategy

basically once main buy condition is met, my limit price has be "sandwiched" between H and L values of the last bar, as soon as L value is triggered, trade would be executed.

Buylimitprice = yestH2 - 0.98;

buy = Cross(yesth2,L) AND trendfilter AND condition AND timeOK;
BuyPrice = Buylimitprice;
Sell = C >= Ref(H,-1);
SellPrice = Close;

//trades limit
N = 1;

//identify new day
dn = DateNum();
newDay = dn != Ref( dn,-1);


Buy = Buy = ((H >= Buylimitprice AND L <= Buylimitprice ) OR L <= Buylimitprice) AND condition AND timeOK;			
BuyPrice = Buylimitprice;																		
Sell = H >= Ref(H,-1);																	
SellPrice = Ref(H,-1);	

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.