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.
InFirstPos
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);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceCover, Ref(C,-BarsSinceFirstTrigger),0);

This is the whole code for reference:

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


//Trading Logic
//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);

BarsSinceCover = BarsSince(Cover);

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);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceCover, Ref(C,-BarsSinceFirstTrigger),0);

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);
BarsSinceSecondTrigger = BarsSince(SecondTrigger);
SecondTriggerPrice = IIf(BarsSinceSecondTrigger < BarsSinceCover, Ref(C,-BarsSinceSecondTrigger), 0);

//---
// Trade signals
//---

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;
AddColumn(PC_ratio,"PC_param",1.2);
AddColumn(ShortPrice,"ShortPrice",1.2);
AddColumn(CoverPrice,"CoverPrice",1.2);
AddColumn(Short,"Short Signal",1.2);
AddColumn(Cover,"Cover Signal",1.2);
AddColumn(RSI_param_display,"RSI_param_display",1.2);
AddColumn(EMA_param_display,"EMA_param_display",1.2);
AddColumn(Close_comparison_display,"Close_comparison_display",1.2);
AddColumn(FirstEntry,"FirstEntry",1.2);
AddColumn(InFirstPos,"InFirstPos",1.2);
AddColumn(FirstTrigger,"FirstTrigger",1.2);
AddColumn(FirstTriggerPrice,"FirstTriggerPrice",1.2);

AddColumn(SecondEntry,"SecondEntry",1.2);
AddColumn(InSecondPos,"InSecondPos",1.2);
AddColumn(SecondTrigger,"SecondTrigger",1.2);
AddColumn(SecondTriggerPrice,"SecondTriggerPrice",1.2);

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