Flip(FirstEntry, Cover) when using Equity(1)

Will Flip(FirstEntry, Cover) works to set FirstEntry back to False when Cover is True under the condition that I used Equity(1) which returns the result and set the Cover as value of 2 (max_loss)?

I have searched for quite some time but am still not able to figure out the solution. Can somebody give me hint?

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

Hi Tomasz, sorry for my ambiguity. I am trying to write codes for scaling in. (I assume the first scale in means the first trade position when normal trading signal is met. And when I am holding the position and met more criteria, e.g. market panic, then I will enter the contingent second trade position). I tried to follow the TPS strategy template and wrote codes which I think could get to the goal.

But after I wrote the following code snippet and checked through exploration, I found that the InFirstPos wasn't able to get set back from 1 to 0 when I wrote the code:

InFirstPos = Flip(FirstEntry, Cover);

One may help check through the attached photo that on 30/5/2001 I entered a short trade when short signal is 1. Then, according to the code above, the InFirstPos should get set back to 0 from 1 starting on 7/6/2021 but it just kept being 1 onwards.

P.S.: The following is the whole codes for any reference if required:

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

//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;

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

RSI_param = 5;
RSI_threshold = 80;
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);
//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);

SetStopPrecedence( stopTypeLoss,  stopTypeTrailing, stopTypeNBar, stopTypeProfit );


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



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(Short,"Short Signal",1.2);
AddColumn(Cover,"Cover Signal",1.2);




First of all, scale-in/out is supported ONLY by portfolio backtester.

Equity() is OLD (v4.5) single-security backtester and it does not support scale/in so you should REMOVE Equity() calls from your formula. They are NOT needed.

Thanks for your reminding. However, as I might need to use equity(1) to write back the stop loss mechanism to my cover signal, and without it the cover signal in exploration will just be 0 instead of other value when a stop loss mechanism is hit. Thus, the InFirstPos will continue as 1 onwards. How should I complement this action?

Originally 7/6/2021 should be covered.

No, you don't need Equity function. Don't use it if you use scaling. It is for backward compatibility with formulas written before 2005.

This post has already deleted Equity().

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