Implementing a simple Donchian Channel Strategy - Issues with entry  [SOLVED]

Questions about MultiCharts and user contributed studies.
siou83
Posts: 7
Joined: 28 Oct 2019
Has thanked: 2 times

Implementing a simple Donchian Channel Strategy - Issues with entry

Postby siou83 » 12 Nov 2019

Hi All,

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;

User avatar
TJ
Posts: 7752
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2228 times

Re: Implementing a simple Donchian Channel Strategy - Issues with entry

Postby TJ » 12 Nov 2019

Go to the Wiki and look up the keyword:

SELLSHORT

User avatar
TJ
Posts: 7752
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2228 times

Re: Implementing a simple Donchian Channel Strategy - Issues with entry

Postby TJ » 12 Nov 2019

You might find this FAQ useful:

[FAQ] Autotrade / Backtest / Optimization
viewtopic.php?t=10811

siou83
Posts: 7
Joined: 28 Oct 2019
Has thanked: 2 times

Re: Implementing a simple Donchian Channel Strategy - Issues with entry  [SOLVED]

Postby siou83 » 13 Nov 2019

Thank you TJ -i actually figured it out myself yesterday - the sell short command was indeed the issue.
Here is the corrected code that works fine now (have included an RSI filter as well)

Code: Select all

inputs: Price(Close), RSIlength(20), atrlength(14), TgtATR(1.5), StpATR(1.5);

vars: ATR(0), RSIVAL(0), CurrentEquity(InitialCapital), MP(0), Cts(1);



ATR = AvgTrueRange(atrlength);
RSIVAL = RSI(Price, RSIlength);



MP = marketposition;

CurrentEquity = InitialCapital + netprofit;
cts = 100;
//cts = 10*(CurrentEquity/O);

//trading rules

// Long
if C > Highest(High,20)[1] and RSIVAL > 65 then buy Cts contracts next bar at market;

// short

if C < Lowest(Low,20)[1] and RSIVAL < 35 then sellshort 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;


Return to “MultiCharts”