Auto trading

Questions about MultiCharts and user contributed studies.
User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Auto trading

Postby larssan » 17 May 2011

Hi,

I have found a strange bug when using stop orders and auto trading.
I'll attach the code in the bottom.


1. Say you have a BuyStop for 5 contracts @ 1100 and a SellStop for 5 contracts @ 1050.
2. 1100 is filled and a new SellStop for 5 contracts @ 1051 is generated by the code.
3. Now you should have the original SellStop for 5 contracts @ 1050 + the new sellstop for 5 contracts @ 1051.
But what happens is that the original SellStop @ 1050 doubles to 10 contracts.
4. So now you have a SellStop for 10 contracts @ 1050 + a SellStop for 5 contracts @ 1051.
If the market moves very fast below 1050 you will be short 10 contracts instead of 5.
5. What it should look like is one SellStop for 5 contracts @ 1051 + one SellStop for 5 contracts @ 1050.

I understand why it happens as you're Long five it adds 5 to make you flat before, but it disregards the fact that you also have a stop order BEFORE and you will be flat when it comes down to 1050.

I've added some pictures to show the bug...

Hope anyone can help with this.


Edit: Is it known that the auto trader can double the position when using stop orders ?
It will not happen when backtesting but I fail to see the point of autotrading if it's not similar to backtesting.

Edit: I've added a img of how the orders should be placed.

Thanks, Lars

Code: Select all

[IntrabarOrderGeneration = false]
inputs: Length( 3 ), NoOfContracts(2) ;
Buy ( "Long1" ) NoOfContracts contracts next bar at HighestFC( High, Length ) + 1 point stop ;
Sell ("Init Exit Long1") from Entry ("Long1") next bar at LowestFC( Low, Length ) + 1 point stop;
Sell Short ( "Short1" ) NoOfContracts contracts next bar at LowestFC( Low, Length ) - 1 point stop ;
BuyToCover ("Init Exit Short1") from Entry ("Short1") next bar at HighestFC( High, Length ) - 1 point stop;
Image

Image

Image
Attachments
correct orders.png
How it should look like...
(20.63 KiB) Downloaded 1266 times
Before order is filled.png
(16.4 KiB) Downloaded 1297 times
After order is filled.png
(20.69 KiB) Downloaded 1304 times
Last edited by larssan on 23 May 2011, edited 3 times in total.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: MegaBug: Auto trading doubles the position!!

Postby larssan » 20 May 2011

Anyone?

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 302 times

Re: MegaBug: Auto trading doubles the position!!

Postby Stan Bokov » 20 May 2011

Dear larrsan,

Your code has no marketposition checks, so it will always send the whole package. We've modified the code, so that it checks the market position and sends only appropriate orders.

Also, I don't understand why you would like TWO sellstops - it would make sense to keep only one. The modified code is attached.

Code: Select all

[IntrabarOrderGeneration = false]
inputs: Length( 3 ), NoOfContracts(2) ;

if marketposition = 0 then
Buy ( "Long1" ) NoOfContracts contracts next bar at HighestFC( High, Length ) + 1 point stop ;

if marketposition > 0 then
Sell ("Init Exit Long1") from Entry ("Long1") next bar at LowestFC( Low, Length ) + 1 point stop;

if marketposition = 0 then
Sell Short ( "Short1" ) NoOfContracts contracts next bar at LowestFC( Low, Length ) - 1 point stop ;

if marketposition < 0 then
BuyToCover ("Init Exit Short1") from Entry ("Short1") next bar at HighestFC( High, Length ) - 1 point stop;
P.S. The title of this topic is very alarming, while in reality you just need to modify your script so that it functions the way you want. We would appreciate it if such strong claims were researched, substantiated with evidence and proved to be true, prior to being posted - otherwise they may ward off potential users. If it's a bug we'll immediately fix it, but that doesn't appear the case, in this case.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: MegaBug: Auto trading doubles the position!!

Postby larssan » 20 May 2011

I got no reply after three days so I changed the subject to something more catchy...
As I had spoken to support before posting and they told me that I can't do anything about this issue other than switch to IB as they have some "built-in" safety feature that won't allow double fills.

That sounded so strange that I believed this to be a bug.

Back to the real issue, the code you provided does not work correctly either.
After one level is hit it creates a stop order on the opposite side, double the size first entered.

If you look at the code I attached the stop orders are on two different levels.
One is 1 tick above the other. (Thats why I use two sellstops)

It's so simple and I can't understand why it's even a problem ?

It should simply not double the position size....

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

Re: MegaBug: Auto trading doubles the position!!

Postby TJ » 20 May 2011

I got no reply after three days so I changed the subject to something more catchy...
As I had spoken to support before posting and they told me that I can't do anything about this issue other than switch to IB as they have some "built-in" safety feature that won't allow double fills.

That sounded so strange that I believed this to be a bug.

Back to the real issue, the code you provided does not work correctly either.
After one level is hit it creates a stop order on the opposite side, double the size first entered.

If you look at the code I attached the stop orders are on two different levels.
One is 1 tick above the other. (Thats why I use two sellstops)

It's so simple and I can't understand why it's even a problem ?

It should simply not double the position size....
You should start with a flow chart.
You need to document the logic you have in mind...
then code the permutations with step-by-step instructions.

Writing an autotrade is a major undertaking.
Attention to detail is the key;
computers are dumb, it will only do what you tell it to do.
You will spend more time on designing, documenting, and debugging than coding,
a good flow chart will set you in the right direction, and saves you lots of headaches and debugging pains.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: MegaBug: Auto trading doubles the position!!

Postby janus » 20 May 2011

All very good and valid points TJ. One other point is the programer has to understand the various peculiarities of coding in MC's language. I'm not referring to the language itself but the way MC processes and submits orders to the broker in question. I've found this task not very intuitive and alien to how manual traders operated with their brokers over the phone in the old days (and still do). I understand the reasons, but it takes some getting used to and it's key to becoming a successful trader with MC. Unfortunately, there's no documentation on all this, at least as far as I know, and one has to learn through experience.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: Auto trading doubles the position!!

Postby larssan » 20 May 2011

Thanks for the tips but this issue has nothing to do with the code itself.
The problem here is that the Easy Language code says buy 5 contract and Multicharts creates an order for 10 contracts.
It does not matter if you use Market posistion or not.

My real code are a bit more complex and I've tried all kinds of different logics but the bug persists.
The little snippet I've attached is merely to show the problem.
(it's from the signals included in MC)

If it can't be solved in this simple code I can't see any reason for the logic in my code beeing wrong.

Please, could someone explain why it doubles the position?
Is it just so that it's a limitation in MultiCharts and you can't use breakout strategies with levels so close ?

/Lars

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Auto trading doubles the position!!

Postby escamillo » 20 May 2011

.
Last edited by escamillo on 21 May 2011, edited 1 time in total.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: Auto trading doubles the position!!

Postby larssan » 20 May 2011

Hi, if you can figure it out on the code below it would help a lot.
Intrabar or not doesn't matter.

Basically what I'm trying achieve is a very simple breakout strategy.
One high level and one low level. Buy 1 tick above/below the high/low level and when filled create a stop order at the opposite level. The stop order 1 tick below/above the opposite level should remain intact.
So for instance after going Long you have two stop orders in the market.
One at the low level and the original one 1 tick below.
If the stop is hit and price moves one more tick down you're short and a new stop is created at the high level.

After that no more trades are to be taken until you restart the hole procedure manually.

It's not that complicated but the problem is that MultiCharts doubles the original stop order after the opposite one is filled.
Even though you specify tha no. Of contracts to 5, Multicharts puts in 10.
This to me must be an error in the software.

You can see what i mean in the attached picture.

Thanks, Lars

Code: Select all

inputs: Length( 3 ), NoOfContracts(5) ;
Buy ( "Long1" ) NoOfContracts contracts next bar at HighestFC( High, Length ) + 1 point stop ;
Sell ("Init Exit Long1") from Entry ("Long1") next bar at LowestFC( Low, Length ) + 1 point stop;
Sell Short ( "Short1" ) NoOfContracts contracts next bar at LowestFC( Low, Length ) - 1 point stop ;
BuyToCover ("Init Exit Short1") from Entry ("Short1") next bar at HighestFC( High, Length ) - 1 point stop;

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

Re: Auto trading doubles the position!!

Postby TJ » 20 May 2011

Moderator's note:
For easy viewing,
I have formatted all the codes with code tag.

Please use the code tag whenever you post codes.
The code tag button is located next to the "Quote" button in the reply message window.

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

Re: Auto trading doubles the position

Postby JoshM » 21 May 2011

All very good and valid points TJ. One other point is the programmer has to understand the various peculiarities of coding in MC's language. I'm not referring to the language itself but the way MC processes and submits orders to the broker in question. I've found this task not very intuitive and alien to how manual traders operated with their brokers over the phone in the old days (and still do). I understand the reasons, but it takes some getting used to and it's key to becoming a successful trader with MC. Unfortunately, there's no documentation on all this, at least as far as I know, and one has to learn through experience.
Hi Janus, that are indeed important points, and as a beginner myself I would like to learn more about these quirks and their (possible) workaround. I've searched this forum for more information about this, but came up with not much concrete information for automated trading. Can/would you elaborate some more on these peculiarities?

(If you would, perhaps we can 'collect' these in a different thread, so not to go too much off-topic here).

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Auto trading doubles the position!!

Postby escamillo » 21 May 2011

.
Last edited by escamillo on 21 May 2011, edited 1 time in total.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: Auto trading doubles the position!!

Postby larssan » 21 May 2011

Escamillo, no thats not it. It's a code that is incl. In MC.
It's the same code that was in the first post in this thread.

It's just to show the problem.

You clearly don't understand what the issue here is.
It has nothing to do with the code itself, it's all in the posts above.

I can't spend any more time explaining the problem to you.
Thanks for the effort but you're not helping.

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: Auto trading doubles the position!!

Postby larssan » 21 May 2011

Stan, thanks for the updated code.
However it does not work.

If marketposition = 0 all is good, one buystop and one sellstop is placed correctly.
But after one order is filled it still doubles the other order.

So if you go long 5 contracts and marketposition > 0 is true you'll find that the stop order below now is twice as large as it was when marketposition = 0 was true.

/Lars

User avatar
larssan
Posts: 61
Joined: 26 Feb 2011
Location: Sweden
Has thanked: 8 times
Been thanked: 8 times

Re: Auto trading doubles the position!!

Postby larssan » 23 May 2011

Here's a code that maybe demonstrates the problem better.

Code: Select all

inputs: Length( 3 ), NoOfContracts(5) ;

if marketposition = 0 then begin
Buy ( "Long1" ) NoOfContracts contracts next bar at HighestFC( High, Length ) + 1 point stop ;
Sell Short ( "Short1" ) NoOfContracts contracts next bar at LowestFC( Low, Length ) - 1 point stop ;
end;


if marketposition > 0 then begin
Sell ("Init Exit Long1") next bar at LowestFC( Low, Length ) stop ;
Sell Short ( "Short2" ) NoOfContracts contracts next bar at LowestFC( Low, Length ) - 1 point stop ;
end;


if marketposition < 0 then begin
BuyToCover ("Init Exit Short1") next bar at HighestFC( High, Length ) stop ;
Buy ( "Long2" ) NoOfContracts contracts next bar at HighestFC( High, Length ) + 1 point stop ;
end;

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 489 times

Re: Auto trading

Postby Dave Masalov » 26 May 2011

Dear larssan,

Entries are:

Buy is the entry to a long position,

Sell Short is the entry to a short position.

Exits are:

Sell is the exit from a long position ,

Buy to Cover is the exit from a short position.

If you are in the position other than flat, you should send an entry to the opposite direction, then the number of contracts will increase for the value (size) of the position which you are in.

For example, you’re in LONG + 5 and at the calculation your script generates: Sell Short 3 Contract next bar at market; then the order will be sent to the broker as SELL 8 Contract in order after its execution you’ll be in SHORT -3

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: Auto trading doubles the position

Postby janus » 29 May 2011

Hi JoshM. Sorry for the late reply; for some reason I didn't get an email alert - I suspect there's a bug in the notification system as it has happened a few times lately. Anyway, as for listing the quirks, I actually would love to. I have already started building a sort of quirk list but it's not very well sorted. When I get some more time I will clean it up and post it somewhere more appropriate. Just today I discovered another one, where a combination of having "max number of bars study will reference" on auto or too low and a study using LastBarOnChart_s, and then referencing data too many bars back causes it to end part way through without notice. It's not a bug in MC as it's working as designed but it got me stumped for an hour while debugging it before I realized what was happening. The "fix" was very simple; just increased the max bars setting. I wish we could turn this feature off. I know it would cause problems in studies trying to reference past the start of a data series but that's OK - the study should just abort the same way as if a study tries to reference an array outside its bounds. I often find this feature more of a pain than a benefit for my studies. Setting it to 0 does not help.

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

Re: Auto trading

Postby JoshM » 06 Jun 2011

Thanks for replying Janus - I also did not receive an email with updates to this thread so I'm also a little bit late.

Thanks for mentioning the LastBarOnChart_s quirk, I've taken note of it and hopefully this will prevent me from making this 'mistake'.

I'd love to read a list of these little things. I'm still a MC/EL beginner, but if you could use any help with such a list, feel free to PM me. I would be glad to help and could learn a lot from it.

Regards,
Josh


Return to “MultiCharts”