Problem with ReverseSignalForcesExit

I am having trouble translating the following TradeStation code. I get only buy and short signals. I have Amibroker 6.41.0 64 bit program. Here is the TS code

Inputs: Price((H+L)/2),

alpha(.07),

RngFrac(.35),

RevPct(1.015);

Vars: Smooth(0),

ITrend(0),

Trigger(0);

ITrend = (alpha - alpha*alpha/4)*Price + .5*alpha*alpha*Price[1] – (alpha - .75*alpha*alpha)*Price[2] + 2 *(1 – alpha)*ITrend[1] – (1 - alpha)*(1 - alpha)*ITrend[2];

If currentbar < 7 then ITrend = (Price + 2*Price[1] + Price[2]) / 4;

Trigger = 2*Itrend - ITrend[2];

If Trigger Crosses Over ITrend then Buy Next Bar at Close – RngFrac*(High - Low) Limit;

If Trigger Crosses Under ITrend then Sell Short Next Bar at Close + RngFrac*(High - Low) Limit;

If MarketPosition = 1 and Close < EntryPrice/RevPct then Sell Short Next Bar On Open;

If MarketPosition = -1 and Close > RevPct*EntryPrice then Buy Next Bar on Open

********** My AFL code is

SetOption("ReverseSignalForcesExit", True);

Price = (High + Low) / 2;
alpha = 0.07;
RngFrac = 0.35;
RevPct = 1.015;

Smooth = 0;
ITrend = 0;
Trigger = 0;
Position = 0;
bi = BarIndex();

Buy = Sell = Short = Cover = 0;

for ( i = 0; i < BarCount; i++ )
{
if ( i < 2 )
{
ITrend[i] = Price[i];
continue;
}

ITrend[i] = ( alpha - alpha * alpha / 4 ) * Price[i]

  • 0.5 * alpha * alpha * Price[i - 1]
  • ( alpha - 0.75 * alpha * alpha ) * Price[i - 2]
  • 2 * ( 1 - alpha ) * ITrend[i - 1]
  • ( 1 - alpha ) * ( 1 - alpha ) * ITrend[i - 2];

ITrend = IIf(BarIndex() < 7, (Price + 2*Ref(Price, -1) + Ref(Price, -2)) / 4, ITrend);

Trigger = 2 * ITrend - Ref(ITrend, -2);

BuySetup = Cross(Trigger, ITrend);
SellSetup = Cross(ITrend, Trigger);

BuyPrice = Close - RngFrac * (High - Low);
ShortPrice = Close + RngFrac * (High - Low);

// --- Exit Conditions ---
SellCond = (Position == 1 AND Close < BuyPrice / RevPct);
CoverCond = (Position == -1 AND Close > ShortPrice * RevPct);

// --- Signals ---
Buy = BuySetup;
Short = SellSetup;
position = Flip( Buy, Sell ) - Flip( Short, Cover );

Sell = SellCond;
Cover = CoverCond;

// --- Plotting ---
Plot(Close, "Price", colorDefault, styleCandle);

I would appreciate any assistance

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Sorry about that.

SetOption("ReverseSignalForcesExit", True);
 
Price = (High + Low) / 2;
alpha = 0.07;
RngFrac = 0.35;
RevPct = 1.015;

Smooth = 0;
ITrend = 0;
Trigger = 0;
Position = 0;
bi = BarIndex();

Buy = Sell = Short = Cover = 0;
  
 // --- ITrend Calculation ---
ITrend = (alpha - alpha*alpha/4)*Price
        + 0.5*alpha*alpha*Ref(Price, -1)
        - (alpha - 0.75*alpha*alpha)*Ref(Price, -2)
        + 2*(1 - alpha)*Ref(ITrend, -1)
        - (1 - alpha)*(1 - alpha)*Ref(ITrend, -2);
        
ITrend = IIf(BarIndex() < 7, (Price + 2*Ref(Price, -1) + Ref(Price, -2)) / 4, ITrend);

Trigger = 2 * ITrend - Ref(ITrend, -2);

BuySetup  = Cross(Trigger, ITrend);
SellSetup = Cross(ITrend, Trigger);


BuyPrice  = Close - RngFrac * (High - Low);
ShortPrice = Close + RngFrac * (High - Low);

// --- Exit Conditions ---
SellCond  = (Position == 1 AND Close < BuyPrice / RevPct);
CoverCond = (Position == -1 AND Close > ShortPrice * RevPct);

// --- Signals ---
Buy  = BuySetup;
Short = SellSetup;

position = Flip( Buy, Sell ) - Flip( Short, Cover );

Sell  = SellCond;
Cover = CoverCond;

// --- Plotting ---
Plot(Close, "Price", colorDefault, styleCandle);

You posted some AI-generated (wrong) translation of Tradestation code. If you want HUMAN help, explain in PLAIN ENGLISH what YOU want to achieve. Write it yourself, don't use AI for explanation what YOU need.

Ok what I need is help with coding the ReverseSignalForcesExit function.
Once I have working Buy, Sell, Short and Cover codes, how does one program the last 4 Tradestation lines

If Trigger Crosses Over ITrend then Buy Next Bar at Close – RngFrac*(High - Low) Limit;
If Trigger Crosses Under ITrend then Sell Short Next Bar at Close + RngFrac*(High - Low) Limit;

If MarketPosition = 1 and Close < EntryPrice/RevPct then Sell Short Next Bar On Open;

If MarketPosition = -1 and Close > RevPct*EntryPrice then Buy Next Bar on Open

With regards to limit orders

Last two lines are not needed because AmiBroker does this automatically and doesn't require user to code it at all. It just knows that Short reverses Long pos and Long reverses short.

1 Like

@Tomasz, I read this thread and found it intriguing since I personally never use limit entries nor “all-in” positions with reversing trades.
Thanks for pointing to the KB article explaining how to set up the logic for the first two conditions of the EasyLanguage script.

However, I don’t understand why you wrote:

“Last two lines are not needed because AmiBroker does this automatically and doesn't require user to code it at all… ”

When I test the full EL code in TradeStation, I get more trades compared to the AFL version - if, during the porting process, we don’t account for the signals generated by the last two EL conditions. These extra signals, based on the actual EntryPrice of the last trade (Buy or Short), should not be ignored.

Is there a way to fully port the original code so that it replicates all the trades reported in TS? Would this require custom code using the CBT, or is there a simpler solution?

1 Like

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