Doubt about TimeFrame and timing of trading functions

Posted By: Mithrandir77

Doubt about TimeFrame and timing of trading functions - 08/19/15 04:13

I am demo trading since two weeks and this experience is challenging some of the concepts I thought I knew.

For instance now as you can see in the log there was a reverse in the TCT algo (similar to workshop of mean reversion) but the trade from the MAMA (moving average crossover) algo didn't reverse although as pointed in the plot below with a red circle the green line crossed under the blue one.

Is it because MAMA algo has a Timeframe of 8 with a BarPeriod of 60 minutes (8 hour timeframe)? But if so, weren't price(), enterLong(), reverseLong() called at the end of each bar? Or are they called at the end of timeframe? What I would really like to know is what is the reason that EUR/USD:MAMA1:L7191 has not been reversed yet? Below are the code of MAMA algo, the recent trade log and the equity plot. Hedge is at default just in case. Thanks!

Code:
TimeFrame = 8;
vars Price = series(price());	
	Stop = optimize(4,2,8) * ATR(100);	
	MAMA(Price,fastLimit,slowLimit);
	vars MAMAs = series(rMAMA);
	vars FAMAs = series(rFAMA);
	plot("fm",FAMAs,MAIN|LINE,BLUE);
	plot("ma",MAMAs,MAIN|LINE,GREEN);
	Trail = 0;
	if( crossUnder(FAMAs,MAMAs) )
		reverseShort(1);
	else if( crossOver(FAMAs,MAMAs) )
		reverseLong(1);



Code:
[Tue 18.08.15 21:00]  50322 +341 +128 //
[Tue 18.08.15 22:00]  50323 +341 +129 //
BrokerSell EUR/USD: 837 ms
[EUR/USD:TCT:S4040] Reverse 7@1.1027: +99.12 at 22:00
BrokerBuy EUR/USD: 647 ms
[EUR/USD:TCT:L2004] Long 9@1.1027 Risk 254$ t at 22:00

[Tue 18.08.15 23:00]  50321 +440 +28 /\

Tuesday 18.08.15  Profit +8.28 ----
[EUR/USD:MAMA1:L7191] +29$ s1.0828 c1.1026 e1.0968
[EUR/USD:TCT:L2004] -1$ s1.0747 c1.1026 e1.1027

[Wed 19.08.15 00:00]  50326 +440 +33 //
[Wed 19.08.15 01:00]  50337 +440 +45 //
[Wed 19.08.15 02:00]  50337 +440 +45 //
[EUR/USD:MAMA1:L7191] +38$ s1.0828 c1.1044 e1.0968
[EUR/USD:TCT:L2004] +15$ s1.0747 c1.1044 e1.1027

[Wed 19.08.15 03:00]  50350 +440 +57 //



Posted By: jcl

Re: Doubt about TimeFrame and timing of trading functions - 08/19/15 11:23

I can not comment about MAMA, but a time frame behaves just like a bar. You can only reverse a trade at the end of the time frame, not in the middle. So the crossover can only have an effect at the end of the time frame where it happened.
Posted By: Mithrandir77

Re: Doubt about TimeFrame and timing of trading functions - 08/20/15 04:10

Originally Posted By: jcl
I can not comment about MAMA, but a time frame behaves just like a bar. You can only reverse a trade at the end of the time frame, not in the middle. So the crossover can only have an effect at the end of the time frame where it happened.


Thanks a lot! Indeed now it reversed the trade:

Code:
[Wed 19.08.15 17:00] 50364 +440 +72 //
[Wed 19.08.15 18:00] 50451 +440 +159 //
[Wed 19.08.15 19:00] 50459 +440 +167 //
[Wed 19.08.15 20:00] 50462 +440 +170 //
[Wed 19.08.15 21:00] 50451 +440 +158 //
[Wed 19.08.15 22:00] 50452 +440 +161 //
[Wed 19.08.15 23:00] 50445 +440 +153 //

Wednesday 19.08.15 Profit +126$ ----
[EUR/USD:MAMA1:L7191] +74$ s1.0828 c1.1116 e1.0968
[EUR/USD:TCT:L2004] +80$ s1.0747 c1.1116 e1.1027

[Thu 20.08.15 00:00] 50449 +440 +157 //
[Thu 20.08.15 01:00] 50480 +440 +189 //
[Thu 20.08.15 02:00] 50470 +440 +178 //
[EUR/USD:MAMA1:L7191] Reverse 5@1.1133: +82.75 at 02:00
[EUR/USD:MAMA1:S3361] Short 6@1.1134 Risk 145$

[Thu 20.08.15 03:00] 50469 +523 +95 //
[Thu 20.08.15 04:00] 50469 +523 +96 /\

Posted By: Mithrandir77

Re: Doubt about TimeFrame and timing of trading functions - 08/26/15 04:04

Now there is another issue that I can't understand. As you can see in the image below, there was a second crossover of the green line over the blue one and I would like to know why the short trade was not reversed, how can I debug this?



The code of that strategy is:

Code:
function MAMA1_Strat(var fastLimit,var slowLimit)
{
	TimeFrame = 8;
	vars Price = series(price());	
	Stop = optimize(4,2,8) * ATR(100);	
	MAMA(Price,fastLimit,slowLimit);
	vars MAMAs = series(rMAMA);
	vars FAMAs = series(rFAMA);
	plot("fm",FAMAs,MAIN|LINE,BLUE);
	plot("ma",MAMAs,MAIN|LINE,GREEN);
	Trail = 0;
	if( crossUnder(FAMAs,MAMAs) )
		reverseShort(1);
	else if( crossOver(FAMAs,MAMAs) )
		reverseLong(1);
}



And for allocating Margin I use:

Margin = OptimalF * Capital * sqrt(1 + max(0,WinTotal-LossTotal)/Capital);

This is the trade log, in which it can be seen that the Short trade was stopped on 21st but it should have been reversed at the end of the 8 hour - timeframe more or less in the 20th

Click to reveal..

[Wed 19.08.15 23:00] 50445 +440 +153 //

Wednesday 19.08.15 Profit +126$ ----
[EUR/USD:MAMA1:L7191] +74$ s1.0828 c1.1116 e1.0968
[EUR/USD:TCT:L2004] +80$ s1.0747 c1.1116 e1.1027

[Thu 20.08.15 00:00] 50449 +440 +157 //
[Thu 20.08.15 01:00] 50480 +440 +189 //
[Thu 20.08.15 02:00] 50470 +440 +178 //
BrokerSell EUR/USD: 361 ms
[EUR/USD:MAMA1:L7191] Reverse 5@1.1133: +82.75 at 02:00
BrokerBuy EUR/USD: 524 ms
[EUR/USD:MAMA1:S3361] Short 6@1.1134 Risk 145$ at 02:00

[Thu 20.08.15 03:00] 50469 +523 +95 //
[Thu 20.08.15 04:00] 50469 +523 +96 /\
[Thu 20.08.15 05:00] 50469 +523 +96 /\
[Thu 20.08.15 06:00] 50466 +523 +94 //
[Thu 20.08.15 07:00] 50467 +523 +96 /\
[Thu 20.08.15 08:00] 50463 +523 +92 //
[Thu 20.08.15 09:00] 50469 +523 +99 /\
[Thu 20.08.15 10:00] 50473 +523 +102 /\
[Thu 20.08.15 11:00] 50480 +523 +109 /\
[Thu 20.08.15 12:00] 50480 +523 +109 /\
[Thu 20.08.15 13:00] 50473 +523 +102 /\
[Thu 20.08.15 14:00] 50477 +523 +107 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0747 at 14:20

[Thu 20.08.15 15:00] 50485 +523 +114 /\
[Thu 20.08.15 16:00] 50486 +523 +115 /\
[Thu 20.08.15 17:00] 50485 +523 +114 /\
[Thu 20.08.15 18:00] 50485 +523 +114 /\
[Thu 20.08.15 19:00] 50486 +523 +115 /\
[Thu 20.08.15 20:00] 50492 +523 +121 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0779 at 20:04

[Thu 20.08.15 21:00] 50496 +523 +126 /\
[Thu 20.08.15 22:00] 50496 +523 +125 /\
[Thu 20.08.15 23:00] 50496 +523 +125 /\

Thursday 20.08.15 Profit +54.67 ----
[EUR/USD:TCT:L2004] +185$ s1.0802 c1.1234 e1.1027
[EUR/USD:MAMA1:S3361] -60$ s1.1364 c1.1234 e1.1134

[Fri 21.08.15 00:00] 50497 +523 +126 /\
[Fri 21.08.15 01:00] 50498 +523 +127 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0802 at 01:08

[Fri 21.08.15 02:00] 50500 +523 +129 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0811 at 02:24

[Fri 21.08.15 03:00] 50508 +523 +137 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0837 at 03:04

[Fri 21.08.15 04:00] 50509 +523 +138 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0847 at 04:51

[Fri 21.08.15 05:00] 50513 +523 +142 /\
[EUR/USD:TCT:L2004] Trail 9@1.1027 Stop 1.0849 at 05:13

[Fri 21.08.15 06:00] 50512 +523 +141 /\
BrokerSell EUR/USD: 359 ms
[EUR/USD:TCT:L2004] Reverse 9@1.1283: +230$ at 06:00
BrokerBuy EUR/USD: 525 ms
[EUR/USD:TCT:S0633] Short 9@1.1283 Risk 255$ t at 06:00

[Fri 21.08.15 07:00] 50509 +753 -92 \\
[Fri 21.08.15 08:00] 50558 +753 -43 /\
[Fri 21.08.15 09:00] 50549 +753 -52 /\
[Fri 21.08.15 10:00] 50532 +753 -69 /\
[Fri 21.08.15 11:00] 50497 +753 -105 \\
[Fri 21.08.15 12:00] 50528 +753 -75 /\
[Fri 21.08.15 13:00] 50508 +753 -95 \\
[Fri 21.08.15 14:00] 50494 +753 -110 \\
[Fri 21.08.15 15:00] 50471 +753 -133 \\
BrokerSell EUR/USD: 436 ms
[EUR/USD:MAMA1:S3361] Stop 6@1.1365: -138$ at 15:56

[Fri 21.08.15 16:00] 50399 +615 -66 \
[Fri 21.08.15 17:00] 50395 +615 -70 \
[Fri 21.08.15 18:00] 50396 +615 -69 \
[Fri 21.08.15 19:00] 50399 +615 -66 \

Friday 21.08.15 Loss -99.78 ----
[EUR/USD:TCT:S0633] -66$ s1.1565 c1.1357 e1.1283

Weekend at 21.08. 20:00.................................
[EUR/USD:TCT:S0633] -100$ s1.1565 c1.1394 e1.1283
....
Weekend ends at 23.08. 23:00
[Sun 23.08.15 23:00] 50368 +615 -98 \

Sunday 23.08.15 Loss -31.68 ----
[EUR/USD:TCT:S0633] -98$ s1.1565 c1.1392 e1.1283

[Mon 24.08.15 00:00] 50381 +615 -84 \
[Mon 24.08.15 01:00] 50373 +615 -92 \
[Mon 24.08.15 02:00] 50336 +615 -130 \
[Mon 24.08.15 03:00] 50295 +615 -171 \
[Mon 24.08.15 04:00] 50310 +615 -155 \
[Mon 24.08.15 05:00] 50310 +615 -155 \
[Mon 24.08.15 06:00] 50328 +615 -138 \
[Mon 24.08.15 07:00] 50302 +615 -164 \
[Mon 24.08.15 08:00] 50285 +615 -181 \
[Mon 24.08.15 09:00] 50296 +615 -170 \
[Mon 24.08.15 10:00] 50289 +615 -177 \
[Mon 24.08.15 11:00] 50279 +615 -187 \
[Mon 24.08.15 12:00] 50266 +615 -200 \
BrokerSell EUR/USD: 717 ms
[EUR/USD:TCT:S0633] Stop 9@1.1567: -252$ at 12:46

[Mon 24.08.15 13:00] 50214 +363 +0
[Mon 24.08.15 14:00] 50214 +363 +0
[Mon 24.08.15 15:00] 50214 +363 +0
[Mon 24.08.15 16:00] 50214 +363 +0
[Mon 24.08.15 17:00] 50214 +363 +0
[Mon 24.08.15 18:00] 50214 +363 +0
[Mon 24.08.15 19:00] 50214 +363 +0
[Mon 24.08.15 20:00] 50214 +363 +0
[Mon 24.08.15 21:00] 50214 +363 +0
[Mon 24.08.15 22:00] 50214 +363 +0
[Mon 24.08.15 23:00] 50214 +363 +0

[Tue 25.08.15 00:00] 50214 +363 +0
[Tue 25.08.15 01:00] 50214 +363 +0
[Tue 25.08.15 02:00] 50214 +363 +0
[Tue 25.08.15 03:00] 50214 +363 +0
[Tue 25.08.15 04:00] 50214 +363 +0
[Tue 25.08.15 05:00] 50214 +363 +0
[Tue 25.08.15 06:00] 50214 +363 +0
[Tue 25.08.15 07:00] 50214 +363 +0
[Tue 25.08.15 08:00] 50214 +363 +0
[Tue 25.08.15 09:00] 50214 +363 +0
[Tue 25.08.15 10:00] 50214 +363 +0
[Tue 25.08.15 11:00] 50214 +363 +0
[Tue 25.08.15 12:00] 50214 +363 +0
[Tue 25.08.15 13:00] 50214 +363 +0
[Tue 25.08.15 14:00] 50214 +363 +0
[Tue 25.08.15 15:00] 50214 +363 +0
[Tue 25.08.15 16:00] 50214 +363 +0
[Tue 25.08.15 17:00] 50214 +363 +0
[Tue 25.08.15 18:00] 50214 +363 +0
[Tue 25.08.15 19:00] 50214 +363 +0
[Tue 25.08.15 20:00] 50214 +363 +0
[Tue 25.08.15 21:00] 50214 +363 +0
[Tue 25.08.15 22:00] 50214 +363 +0
[Tue 25.08.15 23:00] 50214 +363 +0

[Wed 26.08.15 00:00] 50214 +363 +0
[Wed 26.08.15 01:00] 50214 +363 +0
[Wed 26.08.15 02:00] 50214 +363 +0
[Wed 26.08.15 03:00] 50214 +363 +0


The only guess that I have so far is that it is because there was no margin to allocate to that component at that time, is that correct? if there is no margin, a reverseLong, reverseShort does not happen?

Thanks!
Posted By: jcl

Re: Doubt about TimeFrame and timing of trading functions - 08/26/15 07:52

Maybe it was just a weekend? For debugging a strategy, the fastest way is printing the relevant variables to the log file.

In your case it's FAMAs[0], MAMAs[0], frame(0), and the crossUnder and crossOver returns. Go to the day in question in the log file, and look how the variables behaved. frame(0) is important as it tells you the end of the time frame - only then a trade can be entered.

This way you can see in 5 minutes if your script does what it should.
© 2024 lite-C Forums