I'm trying to make a simple short selling code. The rules are as follows:
- Below the 100MA, short after 2 up bars
- Cover after any 3 down bars following the short
I have a counter to add up 3 down bars any time after the short, but each time it sees an additional short signal, it resets the counter to 0.
I think because it is self referencing that I have to use a loop but I'm not sure how I can do that. Would a competent coder be able to help me? I'd certainly be willing to offer some modest compensation.
This is my attempt to code it:
//short when you have two up burs and the price is below the 100 day moving average
//cover after any three down bars, following the short
entryDelay = 0;
exitDelay = 0;
SetTradeDelays( entryDelay, exitDelay, entryDelay, exitDelay );
BuyPrice = ShortPrice = IIf( entryDelay == 0, Close, Open );
SellPrice = CoverPrice = IIf( exitDelay == 0, Close, Open );
TrendMA = MA( Close, 100 );
DownTrend = Close < TrendMA;
DownBar = Close < Open;
UpBar = Close > Open;
UpTwoDays = BarsSince( DownBar ) > 2;
Short = DownTrend AND UpTwoDays;
DownBarCount = SumSince( Short, DownBar );
Cover = DownBarCount > 3;
Cover = ExRem( Cover, Short );
Short = ExRem( Short, Cover );
Buy = Sell = 0;
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( Close, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
Plot( TrendMA, "TrendMA", IIf( DownTrend, colorRed, colorGreen ) , Styleline | Stylethick );
Plot( DownBarCount, "DownBarCount", colorYellow, styleOwnScale );
Filter = 1;
AddColumn( Open, "Open", 1.2 );
AddColumn( High, "High", 1.2 );
AddColumn( Low, "Low", 1.2 );
AddColumn( Close, "Close", 1.2 );
AddColumn( DownBarCount, "DownBarCount", 1.0 );
AddColumn( IIF( Short, 'S', ' ' ), "Short", formatChar, colorDefault, IIf( Short, colorRed, colorDefault ) );
AddColumn( IIF( Cover, 'C', ' ' ), "Cover", formatChar, colorDefault, IIf( Cover, colorGreen, colorDefault ) );
I've also included a screen shot illustrating what I'm trying to do and the problem that I run into.