# Display Sell (and Cover-) Values when using ApplyStop and Sell = 0 ;

Hi,
I want to visualize my Entry and Exitpoints.
(see the code below)

``````
// Insidebar; Farbe egal

b = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarLong = b ;

k = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarShort = k ;

TradeDelay = 0; // set it to 0 for no delays

TickSize = 0.01;

// sample entry rules

// Equity(1); // THIS EVALUATES STOPS

BuyPrice = Max(Ref(H,-1) , Open) ;
SellPrice = Open ;
ShortPrice = Min(Ref(L,-1) , Open) ;
CoverPrice = Open ;

// Long //
Buy = InsidebarLong AND H > Ref(H,-1);

stopLevelLong = Ref( L, -1 ); // use previous bar low  // define stop level
stopAmountLong = BuyPrice - stopLevelLong + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick

Sell = 0 ;

// Short
Short = InsidebarShort  AND L < Ref(L,-1) ;

stopLevelShort = Ref( H, -1 );  // use previous bar high  // define stop level
stopAmountShort = stopLevelShort - ShortPrice + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick

Cover = 0 ;

// assign stop amount conditionally by checking if there is a Buy signal on given bar
stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort );
stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong );

Risk = 1; // Risk in %
U = Risk/100 ; // umgewandelt in %
InitialEquity = 100000 ;
Z = U * InitialEquity ; // realtives --> absolutes Risiko
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
TickSize  = 0.01 ;
TickValue = 0.01 ;
MarginDeposit = 100 ;
PointValue = TickValue / TickSize ;
size = Z/(stopAmount*(TickValue/TickSize)) ;  // Z = size*stopAmount
SetPositionSize( size, spsShares ); // number of shares/contracts per trade

ApplyStop( stopTypeLoss, stopModePoint, stopAmountShort, True );
ApplyStop( stopTypeProfit, stopModePoint, 1*stopAmountShort, True );

// problem with this one, it couldn't work
PlotShapes( IIf(Sell, shapeCircle, shapeNone),colorRed, 0, SellPrice, 0 );
``````

If I have my Buy-Condition and BuyPrice then it's no problem, but
what's when Sell = 0; SellPrice = Open (e.g.) and I'm using ApplyStop?

Must I write the ApplyStop, like I did in the following formula, into the sell-array and into the SellPrice ?
I tried it, but it doesn't work like it should. (see below)
Isn't it possible to write the applystop into the sell-array and SellPrice ?

Need some help.

``````// Insidebar; Farbe egal

b = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarLong = b ;

k = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarShort = k ;

TradeDelay = 0; // set it to 0 for no delays

TickSize = 0.01;

// sample entry rules

// Long //
Buy = InsidebarLong AND H > Ref(H,-1);
BuyPrice = Max(Ref(H,-1) , Open) ;

stopLevelLong = Ref( L, -1 ); // use previous bar low  // define stop level
stopAmountLong = BuyPrice - stopLevelLong + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick

// Zwischeneinschub
Equity(1,0) ; // evaluate stops, all quotes
// SetOption("EveryBarNullCheck", True) ;   // checks for null bars
// intradeShort = Flip(Short, Cover); // True when Short; False when Cover

// Short
Short = InsidebarShort  AND L < Ref(L,-1) ;
ShortPrice = Min(Ref(L,-1) , Open) ;

stopLevelShort = Ref( H, -1 );  // use previous bar high  // define stop level
stopAmountShort = stopLevelShort - ShortPrice + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick

Cover = 0 ;
CoverPrice = Open ;

// assign stop amount conditionally by checking if there is a Buy signal on given bar
stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort );
stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong );

Risk = 1; // Risk in %
U = Risk/100 ; // umgewandelt in %
InitialEquity = 100000 ;
Z = U * InitialEquity ; // realtives --> absolutes Risiko
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
TickSize  = 0.01 ;
TickValue = 0.01 ;
MarginDeposit = 100 ;
PointValue = TickValue / TickSize ;
size = Z/(stopAmount*(PointValue/TickSize)) ;  // Z = size*stopAmount
SetPositionSize( size, spsShares ); // number of shares/contracts per trade

PlotShapes( IIf(Sell, shapeCircle, shapeNone),colorRed, 0, SellPrice, 0 );
``````

With this sentence I meant in this case not the attempt to code the Stop into the code like I did in the second code, I meant to write the whole ApplyStop into a Sell-Array - like this:

``````// Equity(1)
Sell = Cross(L, ApplyStop( stopTypeLoss, stopModePoint, stopAmountShort, True ));
``````

@euroundusd this previous post may help you. In particular I've seen written,

"And no, ApplyStop can’t return value because ApplyStop is NOT a function, it does not do any calculation and does not produce any output.
ApplyStop is merely instruction to the backtester (like turning checkboxes in the settings) and stops are calculated and applied during trading simulation, which happens much later AFTER formula execution finished."

and

"ApplyStop() does not generate any values. It is just instruction or ‘switch’ for the backtester that says “hey backtester, when you will be running portfolio backtest, would you please turn on stop X and handle it internally”. All logic and processing is done during actual backtest, not during your formula execution."

1 Like

Now It works nearly.
It's only not working, if their's a same bar exit.
Then it can't calculate the stopline.
Is anyone able to solve this?
Or am I forced to use Intra-Bar data for this endeavo(u)r ?

(Drag and Drop into the Chart - Insidebar with Targetpoints / Stoppoints = 1)

``````
// Insidebar; Farbe egal

b = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarLong = b ;

k = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) ;
InsidebarShort = k ;

TradeDelay = 0; // set it to 0 for no delays

TickSize = 0.5;

// Long //
Buy = InsidebarLong AND H > Ref(H,-1);

stopLevelLong = Ref( L, -1 ); // use previous bar low  // define stop level
stopAmountLong = BuyPrice - stopLevelLong + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick

BuyPrice = Max(Ref(H,-1) , Open) ;

// Sell //
Sell = 0 ;
ApplyStop( stopTypeLoss, stopModePoint, stopAmountLong, True );
ApplyStop( stopTypeProfit, stopModePoint, 1*stopAmountLong, True );

Equity( 1, 0 ); // evaluate stops, all quotes
SetOption("EveryBarNullCheck", True );

SellPrice = IIf(L <= stopline, Min(Open, stopline), Max(Open, targetline)) ;
//SellPrice = IIf( L <= Ref(stopline, -1), Min(Open, Ref(stopline, -1)), Max(Open, Ref(targetline, -1))) ;

// Short //
Short = InsidebarShort  AND L < Ref(L,-1) ;
ShortPrice = Min(Ref(L,-1) , Open) ;

stopLevelShort = Ref( H, -1 );  // use previous bar high  // define stop level
stopAmountShort = stopLevelShort - ShortPrice + 0.05*(Ref(ATR(14),-1));  // calculate stop amount
stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick

// Cover //
Cover = 0 ;
CoverPrice = Open ;

PlotShapes( IIf(Sell, shapeCircle, shapeNone),colorRed, 0, SellPrice, 0 );

PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( stopline, "trailing stop line", colorRed, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = -1, Zorder = 0, width = 4 ) ;
Plot( targetline, "profit target line", colorGreen, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = -1, Zorder = 0, width = 4 ) ;

// assign stop amount conditionally by checking if there is a Buy signal on given bar
stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort );
stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong );

Risk = 1; // Risk in %
U = Risk/100 ; // umgewandelt in %
InitialEquity = 100000 ;
Z = U * InitialEquity ; // realtives --> absolutes Risiko
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
TickSize  = 0.5 ;
TickValue = 0.5 ;
MarginDeposit = 5000 ;
PointValue = TickValue / TickSize ;
size = Z/(stopAmount*(PointValue/TickSize)) ;  // Z = size*stopAmount
SetPositionSize( size, spsShares ); // number of shares/contracts per trade

``````

@euroundusd sorry I don't have an answer but there is a lot of information on same bar exits in the user guide,

http://www.amibroker.com/guide/h_portfolio.html

Scroll down to the section, RESOLVING SAME BAR, SAME SYMBOL SIGNAL CONFLICTS

Applystop codes are there for sell strategy, similarly where they for cover strategy?

Got it.
Thanks a lot!

One new question:

Is it possible to make something like this in AB?
If yes, which function must I use?
The line between the entry is no problem, but how could I reach this "mouse-recognition" ?

(8:14 - 8:25)

*between the entry and exit

Gui -.....? But which?

You might find this example interesting:

... it makes use of GuiButtons() and detection of mouse hovering. It should give you some idea what you need.

1 Like

... all those new functions were described in details in releasing notes of the last few AmiBroker Beta versions: http://www.amibroker.com/devlog/

Besides you can find many example codes with GuiButtons() on the forum - use the Search functionality. Just a few examples: