ApplyStop functionality in automated trading

I am trying to automate my Amibroker signals with trading terminal in Upstox, which is an Indian Broker. Placing order has following syntax -

PlaceOrder(APILicenseKey, symbol, transactionType, orderComplexity, orderType, product, duration, quantity, price, triggerPrice, squareOff, stopLoss, trailingTicks, disclosedQuantity);

I am now able to perform Buy & Sell using above syntax automatically but no idea how can I automate ApplyStop functionality when I have a Chandelier type Trailing Stop Loss -

ApplyStop(stopTypeTrailing, stopModePoint, 3*ATR(14),True,True );

Unable to automate ApplyStop in order placing, I tried to use a Sell condition, when Close touches or crosses Trailing Stop Loss line but this makes the process slower and for daily time frame back-testing is giving still a decent return but in hourly time-frame or below is only complete failure.
Is there a process where i can describe Chandelier exit in a trailing ticks manner? I am not working in intraday but mainly i work in hourly time-frame and i want to automate the total buy sell & stop signals.
Can anyone help me on this.
Also can any body suggest that the best way of applying stop as trailing tick basis or my way of doing Chandelier TSL for full automation? I am a bit confused as in the order placing format only tick basis option is available. I am novice and need suggestion and guidance from others
Thanks & regards
Samik

The idea behind ApplyStop is to simulate in backtest automatic stops NATIVELY supported by the brokerage, such as "TRAIL" stop order at Interactive Brokers.
https://www.interactivebrokers.com/en/index.php?f=605

So in RT trading you place native stop directly @ broker instead of using ApplyStop.

If broker does not support that, you can use Equity(1) to write ApplyStop triggered stops to Sell array: http://www.amibroker.com/f?equity

5 Likes

Dear Sir,
Thanks for your reply and now I am using Sell conditions instead of ApplyStop and comparing them both in backtest and graphically. Pic%20comparison
The upper Image using ApplyStop and the lower one is using Sell condition equivalent to ApplyStop.

Differences are –

  1. When ApplyStop triggers after that one Buy signal does not appears whereas using equivalent Sell Signal, there is the next Buy signal available
  2. ApplyStop equivalent Sell signal appears or delayed by One bar than ApplyStop Signal. So using Sell condition instead of ApplyStop is a delayed process. Even SetTradeDelay 0 is giving same result
  3. Backtest Performance in a daily timeframe for 3.5 years data shows almost same performance for both (39.66 CAR for 1.75 CAR/MDD with 65 trades for upper Chart and 36.55 CAR for 1.07 CAR/MDD with 90 trades) but when I go to Hourly Chart, ApplyStop Equivalent Sell signal(The Lower Chart/Indicator) runs in loss but using ApplyStop makes still a profitable condition of 14.25 CAR with 0.24 CAR/MDD.
    My Questions are –
  4. Am I applying ApplyStop equivalent Sell signal in a proper manner? How to use a Sell function after Equity(1)
  5. Whether a 35-40% CAR is a moderate return for a Daily timeframe? I am a novice in this path and need some guidance from seniors.
  6. Also when I shift my time frame from Daily to Hourly or 15 mins. My CAR falls sharply and I don’t find any advantage for intraday trading. I understand that this is my limitation but if somebody can tell me a moderate amount of CAR for a trader who lives on trading only performs in Daily/hourly/15min/5min chart so that I can take that CAR as a milestone and will try to reach that level.

Below is the indicator using Sell condition instead of ApplyStop

SetTradeDelays( 1 ,1, 1, 1 );
BuyPrice=Close;
SellPrice=Close;
ShortPrice=Close;
CoverPrice=Close;
Buy1= Cross( StochK (range,Ksmooth) , StochD (range,Ksmooth, Dsmooth));
Buy2= Cross(StochK(range,Ksmooth),OS);

Sell1=Cross( StochD(range, Ksmooth,Dsmooth) , StochK(range, Ksmooth));
sell2=StochK(range,Ksmooth)<OB;
Sell3=Close<stop*BuyPrice;

Buy= Buy1   OR  Buy2;
stopline = HighestSince( Buy, H - 3*ATR(10) );
Sell4 = Cross(stopline,Close); //Sell condition in place of ApplyStop

Sell = Sell1  AND Sell2 OR Sell3 OR Sell4 ;//

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);


Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, H - Z*ATR(R) ), Null );


Here is the indicator using ApplyStop

SetTradeDelays( 1 ,1, 1, 1 );
BuyPrice=Close;
SellPrice=Close;
ShortPrice=Close;
CoverPrice=Close;
Buy1= Cross( StochK (range,Ksmooth) , StochD (range,Ksmooth, Dsmooth));
Buy2= Cross(StochK(range,Ksmooth),OS);

Sell1=Cross( StochD(range, Ksmooth,Dsmooth) , StochK(range, Ksmooth));
sell2=StochK(range,Ksmooth)<OB;
Sell3=Close<stoploss*BuyPrice;

Buy= Buy1   OR  Buy2;
stopline = HighestSince( Buy, H - 3*ATR(10) );
Sell4 = Cross(stopline,Close); //Sell condition in place of ApplyStop

Sell = Sell1  AND Sell2 OR Sell3 OR Sell4 ;//

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);


Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, H - 3*ATR(10) ), Null );


}
1 Like

could you please paste your full code?

Could you please share your full afl code with applystop. If you wish you can hide your trade strategy (i.e buy-sell-short-cover rules) as I am interested only in the part of correct applystop coding part.

Please help.

Both code are the same. Please check again. Could you please share a full code for a strategy trading. I want to study write a strategy with Buy -> TakeProfit or StopLoss; Sell->TakeProfit or StopLoss. But difficult for me! Please help!