Hello Forum members,

I want you to just take a second and help look into what I'm probably doing wrong. I'm building a strategy where if a valley is predicted, the algo enters a long, and if a peak is predicted, it enters a short. However, after some time into the trade, and there are 2 closes over the peak, the algo should cancel the short order and change it to a long. Also, if two bars close below a valley, the long order should be cancelled and changed to a short.

Below is my script.

Code:
int checkPosition(var current, var previous){ 
printf("n %d - Open Price", TradePriceOpen);
	for(open_trades) {
		if(TradePriceOpen < current && TradePriceOpen < previous && TradeIsShort){
			printf("nAbout to process long signal");
			exitShort();
			enterLong(); 
		}
		
		if(TradePriceOpen > current && TradePriceOpen > previous && TradeIsLong){
			printf("nAbout to process short signal");
			exitLong();
			enterShort();  
		}
	}
	return 8;
}

 

function run()
{ 
	set(TICKS|FAST);
	BarPeriod = 60;
	NumYears = 1;
	assetList("AssetsFix");
	asset("EUR/USD");
	
	vars PClose = series(priceClose());  
	vars Price = series(price());
	var LP30 = LowPass(Price,30);
	var LP100 = LowPass(Price,100);
	vars R = series(LP30);
	int Limit = -5; // predict 5 bars in advance
	var shortEntry = 0;
	var longEntry = 0;

	ColorUp = ColorDn = 0; // suppress the price curve
	PlotScale = 8; // bigger symbols
	PlotWidth = 1000;
	PlotHeight1 = 300;
	set(PLOTNOW);
	plot("LP30",LP30,0,RED);
	plot("LP100",LP100,0,BLUE);
	plot("Price",Price,0,ORANGE);
		
// Peaks / Valleys ////////////////////////////////////////	
	var Peak = predict(PEAK,R,10,0.5*PIP);
	var Valley = predict(VALLEY,R,10,0.5*PIP);
	static int Reversal = 0; // plot only 1 symbol per prediction
	if(Peak > Limit) {
		if(Reversal <= 0) 
			plot("Peak",LP30+ATR(30),TRIANGLE4,RED);
			enterShort(checkPosition, PClose[0], PClose[1] ); 
			 
		Reversal = 1;
	}
	if(Valley > Limit) {
		
		if(Reversal >= 0) 
			plot("Valley",LP30-ATR(30),TRIANGLE,GREEN);
			enterLong(checkPosition, PClose[0], PClose[1] );
			 
		Reversal = -1;
	}
	if(Peak < Limit && Valley < Limit)
		Reversal = 0;
 
	
	
}




The behaviour that I see as result doesn't represent what I hope to create. Kindly help look into this.

Thanks.