Simple Auto Strategy Help

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

Simple Auto Strategy Help

Postby Zhorny » 01 Nov 2012

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
buytocover next bar at market

end;
end;
Attachments
Example.png
(9.11 KiB) Downloaded 374 times

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

Re: Simple Auto Strategy Help

Postby TJ » 01 Nov 2012

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

Postby Zhorny » 01 Nov 2012

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];

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;

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

Re: Simple Auto Strategy Help

Postby TJ » 01 Nov 2012

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?

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

Re: Simple Auto Strategy Help

Postby TJ » 01 Nov 2012

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

Postby Zhorny » 01 Nov 2012

Thanks TJ for your help.

The flow chart is as such:

For BUY orders
Buy and keep buying (repeat) each time: Close[0] > Close[1]
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.

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

Re: Simple Auto Strategy Help

Postby TJ » 01 Nov 2012

Thanks TJ for your help.

The flow chart is as such:

For BUY orders
Buy and keep buying (repeat) each time: Close[0] > Close[1]
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:

Image


Return to “MultiCharts”