The PEEK flag and tradeMFE / tradeMAE

Posted By: Smon

The PEEK flag and tradeMFE / tradeMAE - 10/07/18 14:57

I'm trying to export signals for Deep Learning with this script and I have an issue:

I want to predict the maximum favorable excursion of a trade (TradeMFE). So I export only long trades and if the trade was a loser, I take the maximum adverse excursion instead (TradeMAE). What I get seems to be the data from the trade before. The data I exported is now in this format:

DateTime | EMA200 (D1) | TradeMFE | TradeMAE | My Objective | Trade Return (for comparison)

The second last column (MAE / MFE) should always have the same sign as the last column (Profit). However, if the last column changes its sign, the second last column reacts with a delay of one row:

Code:
function run()
{
	set(RULES+PEEK);
	StartDate = 20150101;
	EndDate = 20150115;
	BarPeriod = 15; 

	while(asset(loop("EUR/USD")))
		
	{
		//Append asset name to csv file to avoid overwriting when exporting with SIGNALS
	
		static string prevAsset;
		if (is(INITRUN)) {
				char dest[50];
				string ass = strx(prevAsset,"/","");
				sprintf(dest,"Data%s_%d_target.csv",ass,BarPeriod);
				if(file_length("Datatarget.csv") != 0)
				{
					file_copy(dest,"Datatarget.csv");
				}
				prevAsset = Asset;
		}
		
		
		Spread = RollLong = RollShort = Commission = Slippage = 0;
		
		LifeTime = 1;
	
		vars myMFE = series(TradeMFE);
		vars myMAE = series(TradeMAE);
		
		int Offset = ifelse(Train,-1,0);
		
		var Objective;
		if(TradeProfit >= 0) Objective = myMFE[Offset];
		if(TradeProfit <  0) Objective = -myMAE[Offset];
		
		
		// generate the signals
		adviseLong(SIGNALS, 0, wdate(0), myMFE[Offset], myMAE[Offset], Objective);
		enterLong();
	}
}



I set Objective = 0 in the adviseLong call just for debugging purposes.

With Offset = -1, I get only Zero values.

Without this PEEK and Offset attempt, I'm getting this (see attached screenshot for better readability:


Quote:

42006 0 0 0 -0.24987
42006.01042 0.00018 0.00033 -0.00033 0.11199
42006.02083 0.00031 0.00001 0.00031 -1.15484
42006.03125 0.00002 0.00163 -0.00163 -0.44817
42006.04167 0.00002 0.00057 -0.00057 0.09483
42006.05208 0.00047 0.00043 0.00047 -1.02569
42006.0625 0 0.0015 -0.0015 0.87918
42006.07292 0.00102 0.00052 0.00102 -0.00863
42006.08333 0.00028 0.0004 -0.0004 -0.37923


It's evident, that by default, tradeMFE and tradeMAE don't relate to the trade that gets opened at the current bar, but to the next trade. So the whole Objective column lags by 1 row. I will use a non-fixed LifeTime in my final script and I already learned that rows are written to the file in the sequence of the closed trades. Therefore I'm a little hesitant to just work around and shift the whole Objective column. Any help/suggestions highly appreciated!

Attached picture MFEMAE.PNG
Posted By: Smon

Re: The PEEK flag and tradeMFE / tradeMAE - 10/07/18 15:16

By the way, the plan is to predict a decent distance for SL and TP (two models, one for each) and enter a trade when the predicted risk/reward is high enough. I'll combine this with support and resistance. If you got a better idea of how to build the objective variable... I'm open to suggestions.
Posted By: Smon

Re: The PEEK flag and tradeMFE / tradeMAE - 10/09/18 04:40

Really? Did nobody ever try to predict this target?
Posted By: Smon

Re: The PEEK flag and tradeMFE / tradeMAE - 04/09/20 13:40

Solution:

Code
set(PEEK);
		
		var up_potential, down_potential = price();
		
		for(i= -20; i<=0; i++)
		{
			up_potential = max(up_potential, priceHigh(i));
			down_potential = min(down_potential, priceLow(i));
		}
   
//until here, up_potential / down_potential is ony the highest / lowest price during that upcoming period of 20 bars.

		up_potential = abs(up_potential - priceOpen())/ATR(30);
		down_potential = abs(down_potential - priceOpen())/ATR(30);


up_potential: how many ATRs the price will move up during the next 20 Bars.
up_potential: how many ATRs the price will move down during the next 20 Bars.
© 2024 lite-C Forums