Autotrading question

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

Autotrading question

Postby larssan » 31 Aug 2011

Hello,
I trade mostly mechanical and so far I have put on trades manually but in order to learn more about programming I was thinking of automate my systems.
Some of it I figured out but now I've run in to some problems.

When using my "Backtesting" code for automatic trading I found lot of logical errors when the orders are executed.
I'm no experienced programmer so I try here to see if anyone can help me out.

Code: Select all

if marketposition = 1 and shorttrade1 = false then begin
longtrade1 = true;
lstp1 = InitialLongStop1 ;


if UseMoveToBreakEven and h >= entryprice+BreakEvenFactor
then begin
lstp1 = MaxList(lstp1, entryprice);
sell ("ExitLong1BE") from Entry ("Long1") next bar at lstp1 stop;
end;

if UseTrailing and time >= TrailingStartTime and TrailLevel > lstp1
then begin
lstp1 = MaxList(lstp1, TrailLevel );
sell ("ExitLong1 TRAIL") from Entry ("Long1") next bar at TrailLevel stop;
end;

if time >= CloseTime then
sell ("ExitLong1 EOD") from Entry ("Long1") this bar at c;

if time < NoNewPositionsTime and shorttrade1 = false then begin
sellshort ("Short2") 1 contract next bar at SellLevel stop;
sstp2 = InitialShortStop2;
BuyToCover ("InitExit Short2") from Entry ("Short2") next bar at sstp2 stop;

end;
The main problem is that to many orders are sent at the same time.

The problems I found so far:

1. If MarketPosition = 1 and UseTrailing = true
(I'm long and have a initial stop&reverse in the market and also using a trailing stop)
If the trailing stop level moves one tick above the initial stop level the trailing is active.
Now I have a stop order at the trailinglevel and a stop&reverse order one tick below.
The stop&reverse order is twice as big as my current position. (flat+short)
The problem i see is that if the market moves really fast I'll be flat at the trailing level and short twice the initial position at the stop&reverse level.
When I trade this manually I put in one order at the trailing level to make me flat and one order at the stop&reverse order to make me short.
I'll attach a pic to make it easier to understand what I mean. (It shows a short position but the issue is the same)

Auto Trading:
Image
Manual Trading:
Image

2. If MarketPosition = 1 and UseMoveToBreakEven = true
Basically same problem.

I'll try to explain the logic I'm looking for:
- If I'm long 1 contract I want to have a stop&reverse at a specified level below the entryprice.
(Flat and Short at the same level.)

- If I'm long 1 contract and using UseTrailing and the trailing stop is above the initial stop&reverse I want to Sell 1 contract at the trailing level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)

- If I'm long 1 contract and using MoveToBreakEven and the BreakEven level is above the initial stop&reverse I want to Sell 1 contract at the BreakEven level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)

- If I'm long 1 contract and using MoveToBreakEven and UseTrailing I want to sell 1 contract at the highest of the two and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)

Hope you understand what I'm trying to describe....

If anyone can give me some ideas I'd really appreciate it.

By the way, if someone feels the urge to write that learning EasyLanguage is a huge project and requires time and effort to understand and recommending some "Learn the basics" book.
Please don't, It's not what I'm after with this post.. ;)


Thanks, Lars

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

Re: Autotrading question

Postby escamillo » 01 Sep 2011

1). It is generally easier to manage code and signals by having separate Signal worksheets for Entry code (Buy and/or SellShort) and Exit code (Sell and BuyToCover). Also consider separate Buy and SellShort Signal worksheets that you can separately turn on and off. Rather than directly reversing a position, have an exit that will get you flat before reversing.
2). Calculated stops such as ratchet stops are fine and can work well. But consider using MC trading engine managed SetStopLoss and SetBreakEven, which work well, will account for position direction (flat, long or short) for you and are a good place to start. And with them you are perhaps less likely to get what you consider to be erroneous orders.

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

Re: Autotrading question

Postby JoshM » 01 Sep 2011

Hi Lars,

I don't know if I can help in some way since you seem quite knowledgeable about MultiCharts, but I've taken the liberty to take your four points that summarize the logic you are looking for, and coded it like I would code it (which it's not to say that's its correct ;) ). I've included your original logic points in comments in the code, to illustrate what it's doing. (Please note, this code is untested)

Code: Select all

variables: PositionSize(0), stopPrice(0), stopPrice(0), initialStopReverse(0), breakEvenLevel(0),
MoveToBreakEven(False), UseTrailing(False), highestPrice(0);

PositionSize = MaxContracts(0);

if MarketPosition = 1 and PositionSize = 1 then begin

stopPrice = 123.45;
initialStopReverse = 122.34;
breakEvenLevel = 143.21;

if UseTrailing = True and stopPrice > initialStopReverse then begin

sell("ExitLongStop") PositionSize contracts next bar at stopPrice stop;
SellShort ("EnterShortAtInitialStopReverse") PositionSize contracts next bar at initialStopReverse limit;

{If I'm long 1 contract and using UseTrailing and the trailing stop is above the initial stop&reverse I want to Sell 1 contract at the
trailing level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)}

end else if MoveToBreakEven = True and breakEvenLevel > initialStopReverse then begin // Perhaps here should also be included: 'and UseTrailing = False' to prevent conflicts with the if statement following this one?

Sell("ExitLongBreakEvenLevel") PositionSize contracts next bar at breakEvenLevel limit;
SellShort("EnterShortAtInitialStopReverse") PositionSize contracts next bar at initialStopReverse limit;

{If I'm long 1 contract and using MoveToBreakEven and the BreakEven level is above the initial stop&reverse I want to Sell
1 contract at the BreakEven level and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)}

end else if MoveToBreakEven = True and UseTrailing = True then begin

highestPrice = MaxList(initialStopReverse, breakEvenLevel);
Sell("ExitLongAtHighest") PositionSize contracts next bar at highestPrice limit;
SellShort("EnterShortAtInitialStopReverse") PositionSize contracts next bar at initialStopReverse limit;

{If I'm long 1 contract and using MoveToBreakEven and UseTrailing I want to sell 1 contract at the highest of the
two and 1 contract at the initial stop&reverse level.
(Flat and Short at two different levels.)}

end else

Sell ("ExitLongStop") PositionSize contracts next bar at stopPrice stop;
SellShort ("ReverseLong") PositionSize contract next bar at stopPrice limit;

{If I'm long 1 contract I want to have a stop&reverse at a specified level below the entryprice.
(Flat and Short at the same level.)}

end;
end;
It's probably a matter of taste what to do regarding splitting the strategy in more than one signal and using the build-in SetStopLoss() and SetBreakEven() functions. Escamillo made some good points about this, but personally I disagree and do exactly the opposite. More roads lead to Rome, or whatever the saying was. :)

Best regards,
Josh

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Autotrading question

Postby Henry MultiСharts » 01 Sep 2011

Hello, Lars.
Unfortunately the provided script does not correspond to the screenshots. The order names on the chart are different from ones in the script. It seems to us that Buy Stop order with Volume 4 was not placed by the provided script.
Please attach a screenshot of the Order and position tracker on the Order history tab so that we can trace the placed orders. Or please provide the proper script and screenshots. What we can recommend at the moment is to use "buy to cover" instead of "buy" if you do not want to reverse. Also check that this order was not formed by any other strategy or from the other chart.

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

Re: Autotrading question

Postby larssan » 01 Sep 2011

Thanks for helping!

escamillo,
I would really like the simplicity with multiple codes for buy, sell and stops etc.
But if I need to turn them off and on manually it will not work for me.

Josh,
Thank you for the effort.
I changed my code to look like yours.
The main difference from my code as I see was that you use "end else" between the different scenarios.
I got it to work and it looks good to start off with...

However the issue with "double orders" remains.
You would think that using Sell and SellShort would fix it but it doesn't.
I thought that if you use a Sell Stop before the SellShort Stop MC would understand that you'll be flat before the SellShort Stop is hit.

In the pic below I've used your code...
Image

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

Re: Autotrading question

Postby larssan » 01 Sep 2011

see below.
Last edited by larssan on 01 Sep 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: Autotrading question

Postby larssan » 01 Sep 2011

Hello, Henry.
Hello, Lars.
Unfortunately the provided script does not correspond to the screenshots. The order names on the chart are different from ones in the script. It seems to us that Buy Stop order with Volume 4 was not placed by the provided script.
Please attach a screenshot of the Order and position tracker on the Order history tab so that we can trace the placed orders. Or please provide the proper script and screenshots. What we can recommend at the moment is to use "buy to cover" instead of "buy" if you do not want to reverse. Also check that this order was not formed by any other strategy or from the other chart.
The picture was only to describe the issue. Thats why I wrote this in the post:
I'll attach a pic to make it easier to understand what I mean. (It shows a short position but the issue is the same)
Sell, SellShort, Buy or BuyToCover does not make any different.
That was the first I tried and I also tried to specify every Sell and BuyToCover to the initial position. However that does not make any difference either.

You can try it using your own "Channel Trailing LX" and "Channel Breakout" signals.
- Get long 5 contracts using "Channel Breakout".
- Start "Channel Trailing LX" as a trailing stop.
- When "Channel Trailing LX" moves above the lower channel of "Channel Breakout" you'll have two stops in the market.
One for 5 contracts at the "Channel Trailing LX" and one for 10 contracts at "Channel Breakout". (Stop&Reverse)

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

Re: Autotrading question

Postby larssan » 01 Sep 2011

Here is a picture describing the issue using MC built-in strategies "Channel Breakout" and "Channel Trailing" using 1 contract.

Image

/Lars

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

Re: Autotrading question

Postby JoshM » 01 Sep 2011

Josh,
Thank you for the effort.
I changed my code to look like yours.
The main difference from my code as I see was that you use "end else" between the different scenarios.
I got it to work and it looks good to start off with...

However the issue with "double orders" remains.
You would think that using Sell and SellShort would fix it but it doesn't.
I thought that if you use a Sell Stop before the SellShort Stop MC would understand that you'll be flat before the SellShort Stop is hit.

In the pic below I've used your code...
Hi Lars,

I'm afraid I don't follow you. The picture does indeed show an 'incorrect' situation, since the highest buy order should be for 2 contracts and not 4. (Btw, how do you get these orders to display on the chart? I only have that display when I use manual chart trading). I do not mean to offend you, but have you double-checked that there is not exit strategy auto applied to new positions? That could explain the double orders.

Btw, I've put the code in a strategy to backtest it, and I only get orders with a position size of 1 (833 trades with a position size of 1), so no double orders. Judging from you new picture the problem resides not in the strategy logic, since the default strategies also have it.

Best regards,
Josh

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

Re: Autotrading question

Postby larssan » 01 Sep 2011


Hi Lars,

I'm afraid I don't follow you. The picture does indeed show an 'incorrect' situation, since the highest buy order should be for 2 contracts and not 4. (Btw, how do you get these orders to display on the chart? I only have that display when I use manual chart trading). I do not mean to offend you, but have you double-checked that there is not exit strategy auto applied to new positions? That could explain the double orders.

Btw, I've put the code in a strategy to backtest it, and I only get orders with a position size of 1 (833 trades with a position size of 1), so no double orders. Judging from you new picture the problem resides not in the strategy logic, since the default strategies also have it.

Best regards,
Josh
The orders have always been displayed like that. I thought it was default?
Did you start autotrading, maybe some setting in "Strategy Options" ?

I agree that the strategy logic is not the issue with the double orders.
That is more a limitation in MC I think..

There was however a few other logical errors but when running the code as you suggested corrected it. It now looks okay. I'll let it run a while and see that everything happens as it should.

If you apply "Channel Breakout LE/SE" and "Channel Trailing LX/SX" to a chart and start AutoTrade do you not see the orders as in my picture ?

Oh, I'm not offended and I have no other strategies luring in the background... :)

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

Re: Autotrading question

Postby JoshM » 01 Sep 2011

I can confirm this behaviour Lars, so you're not the only one who has this behaviour with the mentioned default strategies, but I don't know if it's expected behaviour (i.e. the default turn-and-reverse from these strategies) - and I haven't seen this behaviour in other strategies, so I don't think it's a bug.

Image

With the following default strategies:
Image

And the Strategy Properties:
Image

And the Order and Position Tracker:
Image
Attachments
OPT.PNG
(22.29 KiB) Downloaded 833 times
stratProperties.PNG
(36.09 KiB) Downloaded 838 times
strategies.PNG
(16.87 KiB) Downloaded 827 times
doubleOrders.PNG
(14.5 KiB) Downloaded 2244 times

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

Re: Autotrading question

Postby larssan » 01 Sep 2011

Ok, thanks!

I don't think it's a bug and probably it isn't a major issue either.
But I would feel more comfortable with the correct order size though.
So I'm only trying to find a way around it...

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Autotrading question

Postby Henry MultiСharts » 02 Sep 2011

The first order (buy stp 2) is the entry order. The second order (buy stp 1) is the exit order in the opposite direction (reverse). That is why the amount of contracts is doubled-to close the current position and enter a desired position.

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

Re: Autotrading question

Postby larssan » 02 Sep 2011

The first order (buy stp 2) is the entry order. The second order (buy stp 1) is the exit order in the opposite direction (reverse). That is why the amount of contracts is doubled-to close the current position and enter a desired position.
I agree to that Henry and I understand the logic.

However, if (buy stp 2) and (buy stp 1) are 1 tick from each other and the market is moving fast there is a great possibility that you'll get filled double the contracts you expect.
Agree?

So I'm trying to find a way to remove that possibility.
Maybe by changing the code or something else, I don't know yet.
So, if anyone more experienced then I has an idea for a workaround I would be interested to hear about it.

Cheers!


Return to “MultiCharts”