Simple Coding Help - No Promise.

trash

Well-Known Member
Here is another alternative

added to chart
Code:
su = Study( "SU", GetChartID() ); // support

nm         = Name(); 
uniqueID   = "12345";
uniqueName = nm + uniqueID; // static variable of support line depends on symbol and id
StaticVarSet( "SUMaster_" + uniqueName, su); // set static variable dependent on set unique name
StaticVarSetText( "ChartSymbol_" + uniqueID, nm );
Analysis code
Code:
uniqueID = "12345";
nm = StaticVarGetText( "ChartSymbol_" + uniqueID );
uniqueName = nm + uniqueID;

su  = LastValue( Nz( StaticVarGet( "SUMaster_" + uniqueName ) ) );

Close_ = Foreign( nm, "C" );
SUcond = Cross( su, Close_ ); // cross of price below suppport

Filter = Status( "LastBarInRange" );

AddTextcolumn( nm, "Chart Ticker", 1 );
AddTextcolumn( WriteIf( SUcond, "True", "False" ), "Support Line Cross DN", 1 );
Addcolumn( Close_, "Chart Price", 1.2 );
Addcolumn( su, "Support", 1.2 );
 

pratapvb

Well-Known Member
Code line is not correct because there is a syntax error
This is correct sell = cross( study( "SU", GetChartID() ), close );

Secondly if he right-clicks a hand drawn line then it is not params but properties. If he wants to choose support then he has to set SU in Study ID drop down menu. But actually any 2 letter codes are possible but they have to be the same in StudyID code and in Properties window.

Thirdly since Analysis is the slave and chart is the master you have to set static variable in your chart code to receive the study in the analysis via staticvarget. In analysis code you can not use sell = cross( study( "SU", GetChartID() ), close ). It makes not sense.

So for example in your chart code you set

.....

You should also take care of the timeframe in chart and analysis! For example if cross in chart happens in 1 minute time frame but in analysis periodicity is set to daily you will get problematic output there.
1. I have never done AFLs with study but I knew it was there in ami

2. the code sample is not my code....it was copy pasted from ami help and I did not check it

3. As I had not done that coding myself, my idea was only to point the person to that area of help in ami

sorry if you think that I tried to misguide someone.....better for me to refrain if future
 
Last edited:

pratapvb

Well-Known Member
Please show me where this line sell = cross( study( "SU" ), close, GetChartID() ); is to be found in the AB help file. Thanks!
you seem to still think that my quoting that it was copy pasted from ami help is more an excuse.....I don't think I need to make excuses and whatever I say on this forum is sincere and to the best of my ability.

It could be a mistype in amihelp....as I said I did not go into the details.... I just searched for the appropriate help and reference it and suggested to the person how he can get to that help.....

anyway since you asked for ami help reference here it is

 

KelvinHand

Well-Known Member
Last edited:
A new request for Afl, please find below my strategy.

Intraday setup:
It is based on previous day high low breakout strategy, after markets open today, we go long when previous day high is broken provided today open = low, we do not go long if todays open is not equal to today's low when the previous day high is broken.
Stop loss is today's open price.

We go short when previous day low is broken provided todays open = high, we do not go short if todays open is not equal to todays high when previous day low is broken.
Stop loss is days open price.
No trailing stop loss.
No traget, we close the position at 3.20pm.

Please let me know if the strategy is clear, if not I can explain again with example
 
It might re-paint signals in case when the Low/High of the day changes and goes beyond open of the day after a buy/short signal has been generated. Will fix it and update soon. No stop-loss have been included.

Code:
// Written by: Abhishek Gupta

RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;
PointValue = 500;
SetPositionSize(4,spsShares);
SetBarsRequired(100);


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


_SECTION_BEGIN("Trading signals");
NewDay = IIf(Day() != Ref(Day(), -1) OR BarIndex() == LastValue(BarIndex()), 1, 0);
Plot(NewDay, "", 47, 2 + 32768 + 4096, Minvalue = 0, Maxvalue = 1);
Closetime	= ParamTime("Closing Time", "15:20");

HighY	= TimeFrameGetPrice("High", inDaily, -1);
LowY	= TimeFrameGetPrice("Low", inDaily, -1);

OpenD	= TimeFrameGetPrice("Open", inDaily);
LowD	= TimeFrameGetPrice("Low", inDaily);
HighD	= TimeFrameGetPrice("High", inDaily);

Plot(HighY, "Y's High", ParamColor("Y's High Color", colorYellow), ParamStyle("Y's High Style", styleDashed));
Plot(LowY, "Y's Low", ParamColor("Y's Low Color", colorYellow), ParamStyle("Y's Low Style", styleDashed));
Plot(OpenD, "2day's Open", ParamColor("2day's Open Color", colorRed), ParamStyle("2day's Open Style", styleDashed));
Plot(LowD, "2day's Low", ParamColor("2day's Low Color", colorGreen), ParamStyle("2day's Low Style", styleDashed));
Plot(HighD, "2day's High", ParamColor("2day's High Color", colorGreen), ParamStyle("2day's High Style", styleDashed));

Buy		= OpenD==LowD AND Cross(High, HighY);
Sell	= Cross(TimeNum(), CloseTime);
Short	= OpenD==HighD AND Cross(LowY, Low);
Cover	= Cross(TimeNum(), CloseTime);


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

BuyPrice		= ValueWhen(Buy, Close);
ShortPrice		= ValueWhen(Short, Close);
CoverPrice		= ValueWhen(Cover, Close);
SellPrice		= ValueWhen(Sell, Close);


dist	= 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
	if (Cover[i]) {
		PlotText( "\nCover short: " + CoverPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		PlotText( "\n\nProfit: " + (ShortPrice[i]-CoverPrice[i]), i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if (Sell[i]) {
		PlotText( "\nSell bought: " + SellPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		PlotText( "\n\nProfit: " + (SellPrice[i]-BuyPrice[i]), i+1.5, H[ i ]+dist[i]+5, colorOrange);
	}
	if(Buy[i]) {
		PlotText( "Buy: " + BuyPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if( Short[i]) {
		PlotText( "Short: " + ShortPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
 	}
}

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

printf("\nSignal came " + IIf(BarsSince(Short)>BarsSince(Buy), BarsSince(Buy), BarsSince(Short)) + " bars ago");
WriteIf(BarsSince(Short)>BarsSince(Buy), "\nBuy@ " + BuyPrice, "\nShort@ " + ShortPrice);

printf("\n\nPossiblities ");
printf("\nMax Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (HighD-BuyPrice), (ShortPrice-LowD)));
printf("\nMin Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (OpenD-BuyPrice), (ShortPrice-OpenD)));


// Write Messages
printf("\n\nLet the profit run.");
printf("\nClose a call only when trailing SL hits");
_SECTION_END();
Abi, the code did not show entry and exit signals and also when backtested the results were not correct. only 8 trades were showing up for one year of data. not sure where do i need to correct, could you please check
 
Working on it. Need to fix a lot more things.
At what time the trade will be started?
What happens when the high or low of yesterday is very close to open of today?
Will it be just one trade per day in case a stop-loss hits?
At what time the trade will be started?

No specific time for entry.. As soon as buy/sell criteria is met, we should buy.

What happens when the high or low of yesterday is very close to open of today?
This scenario happens during gap up or gap down days, in that case mostly open could be above or below yesterday's high. If that's the case, we need to buy at open, provided Open=Low for buy and open=high for short.

Will it be just one trade per day in case a stop-loss hits?
yes, just one trade, when stop loss is hit- trade is closed and no reversal.
 

trash

Well-Known Member
OK thanks! Have reported it to AB support with picture telling that help section "Using studies in AFL formulas" shows wrong code line. Strange that no one did a precise error report before. Pretty easy.

PS: AB has already noted it just minutes after reporting. You see, it's pretty easy.
 

Similar threads