FLIP function not working as expected

I'm tinkering with a simple system to investigate rotational trading and planned to use FLIP to register the trend condition within the system. When I run the code below, I do get buys and sells as expected but when I run Explore, the UpTrend variable never has a value of 1. The UpTrend variable isn't needed right now for this system to run but I would use it when rotational is turned on.

System is run on weekly data.

Any insight you can offer would be much appreciated.


NumPos = 25; 	//Number of allowed positions
SetPositionSize(100/NumPos,spsPercentOfEquity);
SetOption( "MaxOpenPositions", NumPos );

// trade on next day open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;
SellPrice = Open;
SetOption("AllowPositionShrinking", True ); 
//EnableRotationalTrading() ;
//SetOption("WorstRankHeld",100);


MA_length = Optimize("MA length", 30, 5, 31, 2);
MinPrice = 15;

Trend = MA(C, MA_length) > Ref( MA(C, MA_length), -2);

UpTrend = Flip(Trend, ! Trend);

Buy_sig = Trend;
Sell_sig = ! Trend ;

Buy = 	Buy_sig AND
		C> MinPrice;
		
Sell = Sell_sig;

MOMO = ( ROC(C,6) + ROC(C,13) + ROC(C,26) + ROC(C,52)) /4;

PositionScore = IIF(UpTrend, 1000+momo,0); 


//******************************
// info for Exploration
//*****************************
filter = 1; /* all symbols and quotes accepted */

AddColumn(UpTrend,"UpTrend",1.4);


Seems to work for me. However, as you can see from the output below, the Flip command is serving no purpose in this context. Perhaps you had something else in mind or this is an oversimplified example.

Oh no. Well, thanks very much for testing it.

Any idea what settings might impact the result? I've used FLIP before in similar situations and it worked fine. This problem is a new one for me. I'm running version 6.00. Not sure if an upgrade to 6.4 would impact this or not.

No it got nothing to do with upgrading AmiBroker

Flip function converts to state signal from impulse signal.
But Trend variable is returning state signal already.
So that's why Uptrend variable returns same results as Trend variable.

Use Cross function for Trend variable to get impulse signals.
Then you will see the differences.

Just ran test this again on a single stock (CAT) from 1/1/2020 to 12/31/2023 (weekly data, Norgate).

I added a single line to Exploration as below to check values for the Trend variable in addition to the UpTrend variable line already there.

AddColumn(UpTrend,"Trend",1.4);

The backtest generates 7 or 8 trades (buys and sells) but when I run Exploration, the Trend variable that doesn't use FLIP does equal 1 in many cases but the UpTrend variable using FLIP never does. Real head-scratcher here. No idea why this isn't working.

Would very much appreciate any input other coders and testers have out there.

Thanks in advance.

Because it is always Trend being true the whole time so there is nothing to flip.

I don't see how that is possible. mradtke showed an exploration above on $SPX where both values are the same (Trend and UpTrend) which they should be. When I run the same thing, Trend varies between 1 and zero as expected but UpTrend, which uses FLIP, is always zero.

See,

1 Like

I am not talking about varying.
If Trend does not change from true to false then there isn't anything to flip.

Anyway Flip function works as expected here.

I would agree if I saw that Trend did not change but it clearly does. Another Exploration example is below.

image

And the code below one more time for convenience. I used FLIP with a similar moving average comparison line a few months ago and it worked fine. That same strategy no longer works and suffers from the same problem here: FLIP never changes from zero.


NumPos = 25; 	//Number of allowed positions
SetPositionSize(100/NumPos,spsPercentOfEquity);
SetOption( "MaxOpenPositions", NumPos );

// trade on next day open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;
SellPrice = Open;
SetOption("AllowPositionShrinking", True ); 
//EnableRotationalTrading() ;
//SetOption("WorstRankHeld",100);

MA_length = Optimize("MA length", 30, 5, 31, 2);
MinPrice = 15;

Trend = MA(C, MA_length) > Ref( MA(C, MA_length), -2);

UpTrend = Flip(Trend, ! Trend);

Buy_sig = Trend;
Sell_sig = ! Trend ;

Buy = 	Buy_sig AND
		C> MinPrice;
		
Sell = Sell_sig;

MOMO = ( ROC(C,6) + ROC(C,13) + ROC(C,26) + ROC(C,52)) /4;

PositionScore = IIF(UpTrend, 1000+momo,0); 


//******************************
// info for Exploration
//*****************************
filter = 1; /* all symbols and quotes accepted */
LastUptrend = BarsSince(Uptrend = 0);

AddColumn(UpTrend,"UpTrend",1.4);
AddColumn(Trend,"Trend",1.4);


Again, many thanks to those of you that have reviewed this.

Is it possible that I accidentally changed some global setting that would impact how FLIP operates?

That one is wrong coding.
You have added assignment there within Barssince function instead of equality check.
So that's why Uptrend outputs zero all the time.

Correction

LastUptrend = BarsSince(Uptrend == 0);
1 Like

Just for kicks I upgraded from version 6.00.2 32-bit to 6.40 32-bit. No change. The FLIP function is not working here still for some reason. Only produces zeros in the code example I shared above.

Hopefully someone out there can help solve the mystery.

Do you actually read carefully?
If not then re-read post above your one again and correct the coding mistake (since there isnt any confirmation you have done so).

Yes! This is it.

My apologies, fxshrat, I had the web page open and replied below before the page had updated so didn't see your comment.

The change you recommended does fix the problem. Such a tiny but dangerous mistake there.

Thanks so much for your patience! !

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.