I have coded the below simple strategy, which seeks to buy everytime price is higher than the 20 day High and sell everytime price is lower than the 20 day low. It seems to be working from the long side - but it doesnt open any short trades. Any ideas on what i might be doing wrong. If you load it in any instrument, you will notice it opens only long trades...
Additionally it would be nice to code this using a Donchian Indicator instead. So write something like this in pseudocode : Buy when price is crossing Donchian Channel High from below / Sell when price is crossing Donchian Channel Low from above. Any ideas on that front also appreciated.
Thanks
Code: Select all
inputs: Price(Close), RSIlength(14), atrlength(14), TgtATR(1.5), StpATR(1.5);
vars: ATR(0), RSIVAL(0), CurrentEquity(InitialCapital), MP(0), Cts(1);
ATR = AvgTrueRange(atrlength);
MP = marketposition;
CurrentEquity = InitialCapital + netprofit;
cts = 10*(CurrentEquity/O);
//trading rules
// Long
if Price > Highest(H,20) then buy Cts contracts next bar at market;
// short
if Price < Lowest(L,20) then sell Cts contracts next bar at market;
//Stops
if MP=1 then Sell ("Longstop") Next Bar at (EntryPrice - (StpATR*ATR)) Stop;
if MP=-1 then buytocover ("Shortstop") Next Bar at (EntryPrice + (StpATR*ATR)) Stop;
//TargetS
If MP=1 then Sell ("LongTarget") Next bar at (entryprice + (TgtATR*ATR)) Limit;
If MP=-1 then buytocover ("ShortTarget") Next bar at (entryprice - (TgtATR*ATR)) Limit;