My formula for intraday trading not work

Hi guys,
I need some help on the Intraday (1-minute timeframe) code below.

my rules:
buy/short at 9:35 every morning

  • when getting 50% profit(i used applystop)

  • sell at 3:55(force close).

However, my below code always had problem: prices always are daily close price.

  1. applystop never works. it always sell at 16:00

i'm very confused that this is a Intraday strategy and my data are 1 minutes from iqfeed.


// starting equity
SetOption("InitialEquity", 10000 );

// commission setting
SetOption( "CommissionMode", 3 ); /* set commissions AND costs as $ per trade*/
SetOption( "CommissionAmount", 0.0035 ); /* commissions AND cost */


// setting both position size and maximum number of open positions so equity is spread equally among trades:
PosQTY = 5; // You can define here how many open positions you want
SetOption("MaxOpenPositions", PosQTY );
PositionSize = -100/PosQTY; // invest 100% of portfolio equity divided by max. position count

// trading price setting



Buy = mycondition1;
Sell = TimeNum() > 155000;
Short =mycondition2;
Cover= TimeNum() > 155000;

//ApplyStop( stopTypeNBar, stopModeBars, 380 );
ApplyStop(stopTypeProfit, stopModePercent, 0.5);

PlotShapes(IIf(Buy,shapeDigit1, Null),colorGreen);
PlotShapes(IIf(Sell,shapeDigit2, Null),colorAqua);
PlotShapes(IIf(Short,shapeDigit3, Null),colorPink);
PlotShapes(IIf(Cover,shapeDigit4, Null),colorAqua);

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

Filter = Buy OR Sell OR Short OR Cover; 	
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( Short, "Short", 1 );
AddColumn( Cover, "Cover" ,1);

Incorrect. ApplyStop does work.

  1. FYI, value of 0.5 you inserted is not 50% but 0.5% (And FYI, "...50% profit..." would mean that e.g. a stock price of 100 would have to rise to 150).
  2. ApplyStop is backtester function.
    So to evaluate Stops in non backtest environment such as chart, explorer, scan you would have to add Equity() function.

Please read ApplyStop documentation.
Also KB has examples already:
Also this forum has examples already:
So not sure why not using existing sources?


I would also check absolute basics like selecting "1-minute" periodicity from Analysis Settings window.

1 Like

Yes, I found my analysis setting was daily, instead of 1 minute, this is the major problem. I assumed the analysis would be 1 minute since I used I minute data.

@fxshrat also pointed out some very important mistakes in the code.
thank you guys so much.

1 Like

@Tomasz some setting can be done via both analysis option and AFL. Should AFL overwrite analysis option setting, corrert?

Use File->Save to save Analysis window to Analysis PROJECT file (.APX). It stores BOTH formula and settings and you don't need to set anything from AFL as all settings are already good when you load APX.

1 Like