# Simple Auto Strategy Help

Questions about MultiCharts and user contributed studies.
Zhorny
Posts: 5
Joined: 01 Nov 2012

### Simple Auto Strategy Help

Hi Team,

I have very little experience in programming and want to create a very simple trading strategy in MultiCharts.

BUY = If Close (0) > Close (-1) -- buy if close of the current bar is greater than the close of the previous bar AND keep buying if this rule continues i.e. if there are 5 bars in a row each with the close higher than the previous bar then you will have a net LONG 5 lot position.

SELL = If Close (0) < Close (-1) -- sell if close of the current bar is less than the close of the previous bar AND keep selling if this rule continues i.e. if there are 5 bars in a row each with the close lower than the previous bar then you will have a net SHORT 5 lot position.

CLOSE entire LONG position when a SELL signal appears and open a SHORT as per the rule
CLOSE entire SHORT position when a BUY signal appears and open a LONG position as per the rule

I've highlighted two examples in the attached screenshot of how this rule should work.

Default Quantity = 1 lot i.e. every time I open or add to a current position I will only trade 1 lot.

When a reverse signal comes into effect i.e. "Close Longs - Open Short" I would sell off my entire long position and then open 1 position short.

Feedback appreciated.
Thank you

EDIT:
I forgot to add my attempt at the code:

inputs: Quantity( 1 );

condition1 = Close > Close[1]
condition2 = Close < Close[1]

if condition1 then
buy ( "BTO" ) Quantity next bar at market;

if condition2 then
sell next bar at market

end;

if condition2 then
sellshort ( "STO") Quantity next bar at market;

if condition1 then

end;
end;
Attachments
Example.png (9.11 KiB) Viewed 243 times

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Simple Auto Strategy Help

Rule #1: Nothing is simple

Rule #2: Start with a flow chart. This is the only way you can see clarity in your "simple" logic.

Zhorny
Posts: 5
Joined: 01 Nov 2012

### Re: Simple Auto Strategy Help

I got to here (see code below) but with this error. Any help appreciate:

ERROR

------ Compiled with error(s): ------
Orders cannot be inside a loop.
errLine 8, errColumn 5, errLineEnd 8, errColumnEnd 5
causal study: (Function)

CODE

Code: Select all

`inputs: Quantity( 1 );         condition1 = Close[0] > Close[1];condition2 = Close[0] < Close[1];Repeatif condition1 then    buy ( "BTO" ) this bar on close;Until condition2;if condition2 then    sell ( "STC" ) this bar on close;   Repeatif condition2 then    sellshort ( "STO" ) this bar on close;Until condition1;if condition1 then    buytocover ( "BTC" ) this bar on close;`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Simple Auto Strategy Help

Zhorny wrote:I got to here (see code below) but with this error. Any help appreciate:

ERROR

------ Compiled with error(s): ------
Orders cannot be inside a loop.
errLine 8, errColumn 5, errLineEnd 8, errColumnEnd 5
causal study: (Function)

CODE

inputs: Quantity( 1 );

condition1 = Close[0] > Close[1];
condition2 = Close[0] < Close[1];

Repeat
if condition1 then
buy ( "BTO" ) this bar on close;
Until condition2;

if condition2 then
sell ( "STC" ) this bar on close;

Repeat
if condition2 then
sellshort ( "STO" ) this bar on close;
Until condition1;

if condition1 then
buytocover ( "BTC" ) this bar on close;

Have you drawn a flow chart?

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Simple Auto Strategy Help

ps. please use code tag when posting codes, it makes reading easier.
To tag codes, highlight the codes you are going to post, then click on the "Code" button at the top of the message window.

I have tagged your code in the above post.

Zhorny
Posts: 5
Joined: 01 Nov 2012

### Re: Simple Auto Strategy Help

The flow chart is as such:

Stop buying when: Close[0] < Close[1]
Close entire long position when: Close[0] < Close[1]

For SELL orders
Sell and keep selling (repeat) each time: Close[0] > Close[1]
Stop selling when: Close[0] > Close[1]
Close entire short position when: Close[0] > Close[1]

i.e. you would never have two countermanding positions open at the same time. you would either be SHORT or LONG.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Simple Auto Strategy Help

Zhorny wrote:Thanks TJ for your help.

The flow chart is as such:

Stop buying when: Close[0] < Close[1]
Close entire long position when: Close[0] < Close[1]

For SELL orders
Sell and keep selling (repeat) each time: Close[0] > Close[1]
Stop selling when: Close[0] > Close[1]
Close entire short position when: Close[0] > Close[1]

i.e. you would never have two countermanding positions open at the same time. you would either be SHORT or LONG.

Try this:

These users thanked the author TJ for the post:
JoshM