Gamestudio Links
Zorro Links
Newest Posts
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (EternallyCurious, AndrewAMD, TipmyPip, Quad), 889 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Need help resolving trading issues with script #433222
11/26/13 15:59
11/26/13 15:59
Joined: Jul 2013
Posts: 110
B
bfleming Offline OP
Member
bfleming  Offline OP
Member
B

Joined: Jul 2013
Posts: 110
I've managed to finally put together my first test script and am running it in demo, but I need some clarification on some things that don't make sense.

The script is just a test script to practice coding, so please don't worry about profitability and such. I've written it the way I have in order to build to something bigger later, so that's why it's only looping one algo, but is set up to add algos later on.

The algo is just the one in Workshop 4, with optimization as in Workshop 5 and looping assets as in Workshop 6 with some small changes:

function tradeTrend()
{
TimeFrame = 1;
vars Price = series(price());
vars Trend = series(LowPass(Price,optimize(1000,500,1500)));
vars Signals = series(0);

Stop = 10*ATR(20);
TakeProfit = 20*ATR(20);
Entry = -2*PIP;
EntryDelay = 6*30;
if(valley(Trend)) {
Signals[0] = 1;
if(Sum(Signals+1,3) == 0)
enterLong();
} else if(peak(Trend)) {
Signals[0] = 1;
if(Sum(Signals+1,3) == 0)
enterShort();
}
}

function run()
{
set(PARAMETERS+TICKS+TESTNOW);
BarPeriod = 240;
LookBack = 500;
StartDate = 2002;
NumWFOCycles = 10;

if(ReTrain) {
UpdateDays = -1;
SelectWFO = -1;
}

while(asset(loop("AUD/USD","USD/CHF")))
while(algo(loop("TRND1A")))

{
if(strstr(Algo,"TRND1A") and strstr(Asset,"AUD/USD"))
tradeTrend();
else if(strstr(Algo,"TRND1A") and strstr(Asset,"CHF/USD"))
tradeTrend();
}

PlotWidth = 600;
PlotHeight1 = 300;
}

So here are my questions, which I hope someone can help me with:

1) As you can see, the algo uses stops and take profit: SL 10*ATR, TP 20*ATR. Should these stops and limits appear on the broker's platform, or do they function internally within Zorro?

2) In line with the above, the algo opened two trades as follows:

[AUD/USD:TRND1A:L8153] Long 1@0.9113 Risk 345.
[USD/CHF:TRND1A:L8194] Long 1@0.9093 Risk 391.

a) the algo is trading 0.01 lots – I assume the risk is in pips not $?

b) in the case of of AUDUSD, 10*ATR at the time of opening the trade was 345 pips, so this is correct, but 10*ATR at the time of opening USDCHF was 220 pips, so this obviously doesn't match Zorro's risk – what is going on here?

c) should take profit appear in the Zorro window as well as risk? i.e. how do I know Zorro is getting the TP right, esp. since it apparently didn't get the SL right in the case of CHF?

d) In the case of both trades, stops did appear on my broker platform when the trades opened, but the stops are completely unrealistic and don't align in any way with ATR:

- in the case of AUDUSD the stop is 0.39362, compared with open price of 0.9113, i.e. 5,177 pips away (15x the risk in the Zorro window)

- in the case of USDCHF the stop is 0.37616, compared with open price of 0.9093, i.e. 5,332 pips away (13.6 times the risk in the Zorro window and 24x what the risk should be)

What is going on with this? Also, no TP appeared on the broker platform. In case it matters, I haven't set up any risk or margin sliders or such.

3) On another note, how do I change the algo to trade 0.1 lots instead of 0.01?

I hope someone can help me with this. Many thanks in advance!!

Re: Need help with script [Re: bfleming] #433226
11/26/13 17:28
11/26/13 17:28
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
How stops and limits work is explained in much better detail in the manual than I could explain here. Look here: http://manual.zorro-trader.com/stop.htm. The profit, risk, loss etc. in the messages are money, not pips. Setting different trade sizes is dealt with in workshop 6. You can use the "Lots" or "Margin" variables.

Your manual has a search function that leads you directly to the page that you need. Once you've read it and do not understand something, just ask here.

Re: Need help with script [Re: jcl] #433277
11/27/13 13:52
11/27/13 13:52
Joined: Jul 2013
Posts: 110
B
bfleming Offline OP
Member
bfleming  Offline OP
Member
B

Joined: Jul 2013
Posts: 110
OK, I've been scouring the manual again and again, but it unfortunately it doesn't answer most of my questions:

1) If the risk in the Zorro window is in account currency units, that means Risk 345 = $345 in the case of a $ account. With a trade size of 0.01 lots (i.e. $0.10 / pip), that would mean Risk 345 = SL 3,450 pips. The ATR at the time was 34.5 pips, so 10*ATR = 345 pips = $34.50. So the risk should be Risk 34.5, not Risk 345. Why would it be off by a factor of 10?

I checked the log file from the test and in the log file the risk was mostly accurately displayed. That is to say, if ATR at the time of the trade was 37 pips, the risk was listed as, e.g. Risk 41. I can understand a difference of a few pips, but not a factor of 10.

2) In the case of the USDCHF trade, ATR at the time of opening the trade was 22 pips, so 10*ATR = 220 pips = $22, which means the risk should be Risk 22. But the Zorro window says Risk 391. This is a factor of 19.5. Why would it be so far off?

I again checked the log file, and in this case I did notice greater discrepancies between the listed risk and the ATR at the time of a trade. For example, in one case ATR was 37 pips, but the risk was Risk 51, a difference of 14 pips. Still, it's not a factor of 10 or 20.

3) OK, I found the part of the manual explaining that SL and TP are handled internally by Zorro and not displayed to the broker. But what then are the huge 5K pip stops that appear on the broker platform?

I ran the tests using the data downloaded from the Zorro site. I checked the ATR against FXCM's charts using the ATR indicator.

I've been looking all through the manual, and I can't find anything that addresses the above issues, so I hope you can help me with this.

Many thanks!


Last edited by bfleming; 11/27/13 13:55.
Re: Need help with script [Re: bfleming] #433280
11/27/13 14:02
11/27/13 14:02
Joined: Jul 2013
Posts: 522
D
dusktrader Offline
User
dusktrader  Offline
User
D

Joined: Jul 2013
Posts: 522
You could be simulating the wrong type of broker account, which could explain why your figures are off by a factor of 10.

If you expect $0.10/pip then I believe that it a micro account. Therefore, you should use a line like this to make sure you are simulating that type of account:
Code:
AssetList = "AssetsMicro.dta"



There is more info about this on this manual page

Re: Need help with script [Re: dusktrader] #433285
11/27/13 14:16
11/27/13 14:16
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
The risk displayed in the message is the real risk - this money will be really gone from your account if your trade hits the SL. If it is by factor 10 higher than you expect, you're trading ten times more risky than you intended.

What dusktrader said is correct: First you need to know what account you have. From your post, I suspect you wanted a micro lot account, but have opened a mini lot account instead. This explains the factor 10. You're also not trading 0.1 or 0.01 lot as you think, but 1 lot. 1 lot is the default and the minimum order size for your account. The 22/391 discrepancy might be some other calculation mistake, like forgetting the pip cost. You can find all formulae in the manual:

Quote:
The risk of a trade - the maximum possible loss at a given Stop distance - is Lots * (Stop/PIP) * PIPCost. The number of lots equivalent to a given margin is (Margin/MarginCost).

If you do not know what account you have, you can find a short script under "Tips&Tricks" that prints the lot size of your account. Do not continue trading until you are sure that you got the right account and the displayed risk is exactly the value that you expect. You can easily test trade parameters by running a little script on a demo account that opens trades with a fixed SL and TP. You can then see how your account changes after every trade.

Re: Need help with script [Re: jcl] #433311
11/27/13 17:45
11/27/13 17:45
Joined: Jul 2013
Posts: 110
B
bfleming Offline OP
Member
bfleming  Offline OP
Member
B

Joined: Jul 2013
Posts: 110
Thank you for the links and help, but this still doesn't resolve the issue.

I simulated the strategy on a micro account and the broker account is also a micro account. And the strategy correctly opened micro lots. So this does not explain the factor of 10.

As for the factor of 19.5, I'm not sure what calculation mistake there can be in 10*ATR unless Zorro is calculating ATR incorrectly. It would be very helpful if the log file would show where the strategy has placed the SL and TP, that way I could verify whether it's placing the stop correctly and by deduction, if it's calculating ATR correctly. Is there anyway to do that?

The code to the strategy is above. As I said, it's basically the code from Workshops 4 & 5, so if there are any miscalculations in the code, they should appear there. If not, then Zorro is getting something wrong.

This is what I know to be true:

Zorro shows Risk 345
Zorro opens 0.01 lots with the broker
The pip value of 0.01 lots is $0.10

This means either:

a) all the above is true and the stop is 3,450 pips away, which means Zorro is miscalculating ATR by a factor of 10;

b) Zorro is correctly calculating ATR and placing the stop, but is miscalculating the risk; or

c) Zorro is calculating ATR and risk correctly, but thinks it's trading mini lots while in fact opening micro lots with the broker.

I tried adding "AssetList = "AssetsMicro.dta" to the code to test it, but got a syntax error, so I'm not sure how to ensure that Zorro knows it's trading micro lots, not minis.

Also, this doesn't explain the 22/391 discrepancy, which can only be explained by miscalculating ATR. It also doesn't explain the 5K pip SL placed with the broker.

Would it matter in any way that it's a 5-digit broker?

Thanks again for your help!

Re: Need help with script [Re: bfleming] #433314
11/27/13 18:29
11/27/13 18:29
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
No, it does not matter if it is a 5 digits broker. Unless the broker has set up wrong parameters to his MT4 server. This is theoretically possible, but I have not yet heard of such a case.

If Zorro or the Workshop 4 script would calculate something "wrong", I would expect that this happened with all Zorros and not only just with yours. So this is not a very likely scenario either.

I think for making progress we should stop guessing and agree first about what account you have. If you can't run a test script, please post the Assets.dta file content - it also contains your basic account parameters. Afterwards I'll show you how to run a script that prints the SL distance and calculates the risk.

Re: Need help with script [Re: jcl] #433324
11/28/13 07:10
11/28/13 07:10
Joined: Jul 2013
Posts: 110
B
bfleming Offline OP
Member
bfleming  Offline OP
Member
B

Joined: Jul 2013
Posts: 110
I agree, if Zorro were calculating something wrong, it wouldn't just be happening with mine. And no point in guessing.

Not sure what test script I should try to run, but here's the Assets.dta file content:

Name Price Spread RollLong/Short PIP PIPCost Margin Lot
AUD/USD 0.92020 0.00024 0.3100 -0.6400 0.0001 0.07599 5.000 1000
EUR/CHF 1.23651 0.00024 0.0200 -0.0600 0.0001 0.08089 5.000 1000
EUR/USD 1.31610 0.00024 -0.0300 0.0100 0.0001 0.07599 5.000 1000
GBP/USD 1.52900 0.00026 0.0500 -0.0900 0.0001 0.07599 7.000 1000
GER30 8367.00 1.00000 -0.7454 0.0000 1.0000 1.00000 60.000 1
NAS100 3055.00 2.0000 -0.2130 0.0000 1.0000 0.75985 20.000 1
NZD/USD 0.79219 0.00026 0.1400 -0.7300 0.0001 0.07599 4.000 1000
SPX500 1697.60 0.50000 -0.1141 0.0000 0.1000 0.75985 80.000 1
UK100 6642.00 1.00000 -0.7378 0.0000 1.0000 1.16177 60.000 1
US30 15581.00 4.0000 -1.0724 0.0000 1.0000 0.75985 60.000 1
USD/CAD 1.03539 0.00022 -0.2200 0.1000 0.0001 0.07340 4.500 1000
USD/CHF 0.93961 0.00024 0.0400 -0.0900 0.0001 0.08089 4.500 1000
USD/JPY 100.042 0.02400 0.0100 -0.0300 0.0100 0.07597 4.500 1000
USOil 108.250 0.05000 0.0000 0.0000 0.0100 0.75985 140.000 1
XAG/USD 19.9510 0.04400 -0.0008 0.0000 0.0100 0.37995 5.000 50
XAU/USD 1316.73 0.47000 -0.0230 0.0000 0.0100 0.00760 3.000 1

Re: Need help with script [Re: bfleming] #433332
11/28/13 09:28
11/28/13 09:28
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
This is indeed a micro account, as you can see from the lot size of 1000 contracts. In the next step we'll deal with how to calculate the risk of a trade. Afterwards we can check if your MT4 version behaves as it should.

You now need to run a test script. You can use the "TradeTest" script that you should have in your folder. Please modify it so that the parameters you had problems with, especially the risk, are now manually calculated and printed:

Code:
function run()
{
	BarPeriod = 1;
	LookBack = 0;
	NumYears = 1;
	set(LOGFILE);

	Lots = 10;
	Stop = TakeProfit = 10*PIP;
	if(NumOpenTotal == 0) {
		printf("\nLots %.0f, Stop %.4f, PIPCost %.4f, Risk %.2f",
			Lots, Stop, PIPCost, Lots*(Stop+Spread)/PIP*PIPCost);
		if(random() > 0) 
			enterLong();
		else 
			enterShort();
	} 
}



Run this script by clicking "Test", and then check the log file. Compare the risk that you've calculated in the script with the risk displayed by Zorro. Let me know if you do not understand something in the script.

After you did that, the next step would be trading this script live and checking if your MT4 version does everthing correctly.

Re: Need help with script [Re: jcl] #433347
11/28/13 12:16
11/28/13 12:16
Joined: Jul 2013
Posts: 110
B
bfleming Offline OP
Member
bfleming  Offline OP
Member
B

Joined: Jul 2013
Posts: 110
OK, so when I ran the above script exactly as you have it, I got this result:

BackTest: TradeTest_1 EUR/USD 2013
[1: Wed 02.01. 04:09] 1.32728
Lots 10, Stop 0.0010, PIPCost 0.0760, Risk 9.42
[EUR/USD::S0101] Short 10@1.3269 Risk 9 at 04:09

[2: Wed 02.01. 04:10] 0p 1/0
[EUR/USD::S0101] Cover 10@1.3264: +1.20 at 04:10

[3: Wed 02.01. 04:11] 16p 1/0
Lots 10, Stop 0.0010, PIPCost 0.0760, Risk 9.42
[EUR/USD::S0302] Short 10@1.3271 Risk 9 at 04:11

[4: Wed 02.01. 04:12] 16p 1/1
[EUR/USD::S0302] Cover 10@1.3275: -4.86 at 04:12

[5: Wed 02.01. 04:13] -48p 1/1
Lots 10, Stop 0.0010, PIPCost 0.0760, Risk 9.42
[EUR/USD::S0503] Short 10@1.3274 Risk 9 at 04:13

The displayed risk matches the manually calculated risk.

Then I ran the script with this code, using an ATR stop like in my strategy:

function run()
{
BarPeriod = 1;
LookBack = 0;
NumYears = 1;
set(LOGFILE);

Lots = 10;
Stop = 10*ATR(20);
TakeProfit = 10*ATR(20);

if(NumOpenTotal > 0) {
exitLong();
exitShort();
} else {
printf("\nLots %.0f, Stop %.4f, PIPCost %.4f, Risk %.2f",
Lots, Stop, PIPCost, Lots*(Stop+Spread)/PIP*PIPCost);
if(random() > 0)
enterLong();
else
enterShort();
}
}

I got this result:

BackTest: TradeTest_2 EUR/USD 2013
[1: Wed 02.01. 04:09] 1.32728
Lots 10, Stop 0.0000, PIPCost 0.0760, Risk 1.82
[61: Wed 02.01. 05:09] 1.32658
Lots 10, Stop 0.0000, PIPCost 0.0760, Risk 1.82
[EUR/USD::S6101] Short 10@1.3267 Risk 103 at 05:09

[62: Wed 02.01. 05:10] 0p 0/1
[EUR/USD::S6101] Cover 10@1.3265: -0.46 at 05:10

[63: Wed 02.01. 05:11] -6p 0/1
Lots 10, Stop 0.0000, PIPCost 0.0760, Risk 1.82
[EUR/USD::L6302] Long 10@1.3267 Risk 103 at 05:11

[64: Wed 02.01. 05:12] -6p 0/2
[EUR/USD::L6302] Sell 10@1.3269: -0.68 at 05:12

[65: Wed 02.01. 05:13] -15p 0/2
Lots 10, Stop 0.0000, PIPCost 0.0760, Risk 1.82
[EUR/USD::L6503] Long 10@1.3269 Risk 103 at 05:13

[66: Wed 02.01. 05:14] -15p 0/3
[EUR/USD::L6503] Sell 10@1.3269: -1.75 at 05:14

[67: Wed 02.01. 05:15] -38p 0/3
Lots 10, Stop 0.0000, PIPCost 0.0760, Risk 1.82
[EUR/USD::L6704] Long 10@1.3268 Risk 103 at 05:15

Here the manually calculated risk does not match the displayed risk.

Then I traded the first script. Attached is a screenshot showing Zorro and MT4 (TestScript1). Here you can see that the manually calculated risk - 12.00 - and displayed risk - 12 -match and that MT4 opened the trade with the correct risk - 12 pips + spread.

Then I traded the second script with the ATR stop and profit. As with the test, the attached screenshot (TestScript2_10*ATR) again shows a mismatch between the manually calculated risk - 2.0 - and the displayed risk - 138 - and MT4 has the stop 2,718 pips away.

I then traded this script with Stop = 1*ATR(20) and TakeProfit = 1*ATR(20). The attached screenshot (TestScript2_1*ATR) shows a similar mismatch as with 10*ATR above, and MT4 again placed the stop 2,700 pips away.

Finally I traded the 1*ATR script on a FXCM mini account (as opposed to micro account). The attached screenshot (TestScript2_1*ATR_FXCM) agains shows the same discrepancy between the manually calculated risk - 17.0 - and the displayed risk - 1,375 (although in this case both are a factor of 10 greater because of trading a full standard lot). FXCM trade station didn't indicate any stop loss.

As one final test, I traded the same script on a FXCM MT4 micro account. The attached screenshot (TestScript2_1*ATR_FXCM_MT4) shows the same discrepancy, but the platform doesn't show a stop.

Hope this helps. And once again, many thanks!

Attached Files TestScript1.pngTestScript2_10*ATR.pngTestScript2_1*ATR.pngTestScript2_1*ATR_FXCM.pngTestScript2_1*ATR_FXCM_MT4.png
Page 1 of 3 1 2 3

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1