how to handle this situation

Questions about MultiCharts and user contributed studies.
salonbus
Posts: 8
Joined: 02 Jan 2013

how to handle this situation

Postby salonbus » 03 Jan 2013

hi u guys, my trading conditons are : high>upline then buy at market, low<dnline then sellshort at market, but i found if there is a bar which high>upline and low<dnline, and my marketposition=-1 , then after i covered my short position, there will be a short signal, which is not my wanted. My code is as follow, could u please kindly help me check it to avoid the above situation, or provide a exmple to me, thank you very much!

Code: Select all


if Date<>Date[1] then begin

if marketposition=0 and High>upline then
buy Lots contracts next bar at market;

if low<dnline then flag=1;
if marketposition=1 and flag=1 then
sell next bar at market;

if marketposition=0 and flag=1 then
sellshort Lots contracts next bar at market;
flag=0;

if marketposition=-1 and High>upline then
buytocover next bar at market;
Last edited by salonbus on 03 Jan 2013, edited 2 times in total.

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

Re: how to handle this situation

Postby TJ » 03 Jan 2013

hi u guys, my trading conditons are : high>upline then buy at market, low<dnline then sellshort at market, but i found if there is a bar which high>upline and low<dnline, and my marketposition=-1 , then after i covered my short position, there will be a short signal, which is not my wanted. My code is as follow, could u please kindly help me check it to avoid the above situation, or provide a exmple to me, thank you very much!

if marketposition=0 and High>upline then
buy Lots contracts next bar at market;


if low<dnline then flag=1;
if marketposition=1 and flag=1 then
sell next bar at market;

if marketposition=0 and flag=1 then
sellshort Lots contracts next bar at market;
flag=0;

if marketposition=-1 and High>upline then
buytocover next bar at market;
You have something not in the above code that is causing you the problem.


ps. please use code tag when posting codes. It makes reading easier.
I have tagged your code for you.

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

Re: how to handle this situation

Postby TJ » 03 Jan 2013

missing a BEGIN and END here:

Code: Select all

if marketposition=0 and flag=1 then
BEGIN
sellshort Lots contracts next bar at market;
flag=0;
END;

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 03 Jan 2013

thank you so much!

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 03 Jan 2013

the problem is still apeared..., could you please kindly help to check my code, thanks!

Code: Select all

[IntrabarOrderGeneration = True]

Inputs:
length(0.5);
vars:
myrange(0),upline(0),dnline(0),offset(0),flag(0),RiskPercent(0.15),TotalEquity(0.0),
Lots(0),MarginRatio(0.15);



if currentbar>=1 then begin
value1=HighD(1)-CloseD(1);

value2=CloseD(1)-LowD(1);
myrange=MaxList(value1,value2,0.2*(HighD(1)-lowD(1)));
upline=OpenD(0)+myrange*length;
dnline=OpenD(0)-myrange*length;
end;

TotalEquity=GetRTAccountEquity("1999_2-0000458");
Lots= IntPortion(TotalEquity * RiskPercent/(Close*BigPointValue*MarginRatio));


if currentbar>=1 and time>0900 then
begin

if h>upline and marketposition=0 then
buy Lots contracts next bar at market;


if l<dnline then flag=1;

if marketposition=1 and flag=1 then
sell next bar at market;

if marketposition=0 and flag=1 then begin
sellshort Lots contracts next bar at market;
flag=0;
end;

if marketposition=-1 and h>upline then
buytocover next bar at market;

end;

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 03 Jan 2013

the pic, for you reference
Attachments
1.jpg
(26.86 KiB) Downloaded 601 times

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

Re: how to handle this situation

Postby TJ » 03 Jan 2013

hi u guys, my trading conditons are : high>upline then buy at market, low<dnline then sellshort at market, but i found if there is a bar which high>upline and low<dnline, and my marketposition=-1 , then after i covered my short position, there will be a short signal, which is not my wanted. My code is as follow, could u please kindly help me check it to avoid the above situation, or provide a exmple to me, thank you very much!
...
you can set up a counter.

Code: Select all

var:
traded.this.bar(0);

if marketposition=1 and flag=1 then
begin
sell next bar at market;
traded.this.bar = currentbar;
end;

if marketposition=0 and flag=1
and traded.this.bar <> currentbar then
begin
sellshort Lots contracts next bar at market;
flag=0;
end;

Notes:
1. see how I indent the BEGIN/END for easy reading.
2. avoid generic variable names at all costs. (ie value1, condition1, etc.,)

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 04 Jan 2013

Code: Select all

traded.this.bar <> currentbar
hi TJ, does this code means it will short at next bar? I'd like to short immediately after covered the long position, could u please tell me how can i achieve that purpose.

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

Re: how to handle this situation

Postby TJ » 04 Jan 2013

hi u guys, my trading conditons are : high>upline then buy at market, low<dnline then sellshort at market, but i found if there is a bar which high>upline and low<dnline, and my marketposition=-1 , then after i covered my short position, there will be a short signal, which is not my wanted. My code is as follow, could u please kindly help me check it to avoid the above situation, or provide a exmple to me, thank you very much!
You will need to add a filter for the condition "high>upline and low<dnline".

I do not fully understand your intention. You should draw a flow chart, as well as a permutation table, so that you can have all the situation covered.

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 04 Jan 2013

please refer the attachment. from the pic, you can see there is red bar, which high>upline and low<dnline, and before that bar, i have a long position, so when come to the red bar, i should short right after covered my long position. The signal that "buy & cover" is not needed. i think maybe "Time" can filter the above condition, but i don't know how to write the code.

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

Re: how to handle this situation

Postby TJ » 04 Jan 2013

please refer the attachment. from the pic, you can see there is red bar, which high>upline and low<dnline, and before that bar, i have a long position, so when come to the red bar, i should short right after covered my long position. The signal that "buy & cover" is not needed. i think maybe "Time" can filter the above condition, but i don't know how to write the code.
If you want to reverse a LONG position to SHORT,
a single SELLSHORT statement will cover your long and sell short at the same time.

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 04 Jan 2013

i want to cover the long position first, then short, as if i cover long and sell short at the same time, it will use double margin in china market. secondly, i'll add some filter in my condition, so i think use sellshort is not a good choice.

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

Re: how to handle this situation

Postby TJ » 04 Jan 2013

i want to cover the long position first, then short, as if i cover long and sell short at the same time, it will use double margin in china market. secondly, i'll add some filter in my condition, so i think use sellshort is not a good choice.
Then you have to draw a flow chart to document your logic path.

Image

salonbus
Posts: 8
Joined: 02 Jan 2013

Re: how to handle this situation

Postby salonbus » 04 Jan 2013

i wondered if i could use "time" to filter my condition, for example, after covered my long position, if low<dnline and the time of "low<dnline" newest than the time of "high>upline" then short.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: how to handle this situation

Postby JoshM » 22 Jan 2013

You should draw a flow chart, as well as a permutation table, so that you can have all the situation covered.
What is a permutation table? I couldn't find much useful info with Google, and thefreedictonairy.com says
In computers, a table designed for the systematic construction of code groups; it may also be used to correct garbles in groups of code text.
Which doesn't make it clearer for me. :) Since a flow chart is such a handy tool, a 'permutation table' might also be helpful.

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

Re: how to handle this situation

Postby TJ » 22 Jan 2013

You should draw a flow chart, as well as a permutation table, so that you can have all the situation covered.
What is a permutation table? I couldn't find much useful info with Google, and thefreedictonairy.com says
In computers, a table designed for the systematic construction of code groups; it may also be used to correct garbles in groups of code text.
Which doesn't make it clearer for me. :) Since a flow chart is such a handy tool, a 'permutation table' might also be helpful.
here's an example...
you populate the table with your variables,
then go through your chart bar by bar,
and fill the table with all the possible scenarios (ie all the OHLC possibilities),
to determine the permutations with your logic tree.

One of the common stumbling block in programming is, people try to do something that is mutually exclusive. A table with all the variables can help you to sort out the permutations in your logic.

Image
Attachments
table.jpg
(14.26 KiB) Downloaded 636 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: how to handle this situation

Postby JoshM » 23 Jan 2013

here's an example...
you populate the table with your variables,
then go through your chart bar by bar,
and fill the table with all the possible scenarios (ie all the OHLC possibilities),
to determine the permutations with your logic tree.

One of the common stumbling block in programming is, people try to do something that is mutually exclusive. A table with all the variables can help you to sort out the permutations in your logic.
Ah I see. Thanks TJ, that's indeed helpful.


Return to “MultiCharts”