# Fail to use flip to reset the first-in position from 1 back to 0

Hi all, I am not able to use Flip to reset the first-in position from 1 back to 0 after coming across with a cover signal and this led to negligence of a numbers of trades who originally supposed to trade. I suppose it is due to the Equity(1) function which send a value of 2 back to the Cover and therefore the True is not valid but I am not sure whether this is the reason.

Does anyone know where I went wrong with the following main part?:

``````FirstEntry = RSI(RSI_param) < RSI_threshold and Ref(RSI(RSI_param),-1) > RSI_threshold and (EMA(Close,EMA_param) < ref(EMA(Close,EMA_param),-1)) and (Close < Ref(Close,Close_param)) AND notmarketpanic;
InFirstPos = Flip(FirstEntry, Cover);
FirstTrigger = ExRem(InFirstPos, Cover);
``````

This is the whole code for reference:

``````//Backtesting Only Code will not plot any signals
_SECTION_BEGIN("Simple RSI Momentum Long Only Strategy");

//Short = RSIa(Close,5) < 40 and Ref(RSIa(Close,5),1) > 40 and (EMA(Close,50) < ref(EMA(Close,50),-1)) and (Close < Ref(Close,21));
//RSI_param = Optimize("rsi_param",5,5,250,10);
//RSI_threshold = Optimize("rsi_threshold",40,3,50,1);
//EMA_param = Optimize("ema_param",50,5,250,10);
//Close_param = Optimize("close_param",-21,-100,-3,1);
MaxPos = 2;
MaxPositionSizePercent = 100;
SetOption("InitialEquity",1000000);
SetOption("MaxOpenPositions",MaxPos);

PC_ratio = Foreign("PC_ratio","Close");
marketpanic = PC_ratio > Optimize("PC",28,10,40,1);
notmarketpanic = NOT marketpanic;

RSI_param = 5;
RSI_threshold = 40;
EMA_param = 50;
Close_param = -21;
trailing_stop = 1.02;
//time_stop = 17;
initial_stop = 3;

//RSI_param = Optimize("RSI_param",5,5,100,5);
//RSI_threshold = Optimize("RSI_threshold",40,5,100,5);
//EMA_param = Optimize("EMA",50,5,100,5);
//Close_param = Optimize("Close_param",-21,-100,-1,5);
//trailing_stop = Optimize("trailing_stop",1.02,1.01,1.1,0.01);
time_stop = Optimize("time_stop",17,5,100,5);
//initial_stop = Optimize("initial_stop",3,1,10,1);

RSI_param_display = RSI(RSI_param);
EMA_param_display = EMA(Close,EMA_param);
Close_comparison_display = (Close < Ref(Close,Close_param));

FirstScaleIn = 0.5;
SecondScaleIn = 0.5;

SL_above_previous_high = Ref(High,-1) * trailing_stop;
Cover = H > SL_above_previous_high;
CoverPrice = IIf(Cover==1, Max(O, Ref(High,-1)*trailing_stop), C);
//CoverPrice = Max(0, Ref(High,-1)*1.02);

FirstEntry = RSI(RSI_param) < RSI_threshold and Ref(RSI(RSI_param),-1) > RSI_threshold and (EMA(Close,EMA_param) < ref(EMA(Close,EMA_param),-1)) and (Close < Ref(Close,Close_param)) AND notmarketpanic;
InFirstPos = Flip(FirstEntry, Cover);
FirstTrigger = ExRem(InFirstPos, Cover);

SecondEntry = RSI(RSI_param) < RSI_threshold and Ref(RSI(RSI_param),-1) > RSI_threshold and (EMA(Close,EMA_param) < ref(EMA(Close,EMA_param),-1)) and (Close < Ref(Close,Close_param)) AND marketpanic AND InFirstPos AND Ref(InFirstPos,-1);;
InSecondPos = Flip(SecondEntry,Cover);
SecondTrigger = ExRem(InSecondPos,Cover);

//---
//---

if( FirstScaleIn + SecondScaleIn == 1.0 )
{
//PositionScore = PositionScore = 100 - CurrentRSI ;  // favour low RSI

Short = IIf( FirstTrigger, 1,
IIf( SecondTrigger, sigScaleIn, 0  ) );

SetPositionSize(IIf( FirstTrigger, MaxPositionSizePercent*FirstScaleIn,MaxPositionSizePercent*SecondScaleIn), IIf( Short > 0, spsPercentOfEquity, spsNoChange ) );

Cover = ExRem(Cover,Short);
}

ApplyStop(stopTypeLoss,	stopModePercent,initial_stop,1);

ApplyStop( stopTypeNBar, stopModeBars,time_stop,1);

//ApplyStop( stopTypeTrailing, stopModePercent, SL_above_previous_high ,1);

ApplyStop( stopTypeProfit, stopModePercent, 20,1);

Equity(1,-1);

SetStopPrecedence( stopTypeLoss,  stopTypeTrailing, stopTypeNBar, stopTypeProfit );

//Position Size
//PositionSize = MarginDeposit = 1;

_SECTION_END();

PlotShapes(Cover*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Short*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);
Plot(SL_above_previous_high , "trailing stop line", colorRed );

//SetPositionSize( 50, spsPercentOfEquity );
//SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

Filter = 1;