Simple Coding Help - No Promise.

Please help,

Hello Happy sir, Pratap sir, Santosh sir & other experts here,

"There are 4 conditions in the strategy, but their usage depends on the market conditions and capital of the account, market volatility and gapups and gapdowns, some times all condtions should be satisfied, some times only 2 or 3 conditions should be met, "

excel file attached.
 
Team..Need help.. Does anyone has a code to count the number of bars in Rally/Decline..

Rally (higher high and higher low) & decline (lower high and lower low)..

For every 3,4,5 consecutive rally bars it should mark 1,2,3 respectively as support and vice versa..i.e. 1,23 as resistence in decline..

tks for help
 
Hello Amit

All the 4 conditions are given in the post requesting the AFL . . .
Just convert line for line English to AFL :)

Code:
range = Param("ADX Periods", 10, 2, 200, 1 );
r1 = Param( "Macd Fast avg", 12, 2, 200, 1 );
r2 = Param( "Macd Slow avg", 26, 2, 200, 1 );
r3 = Param( "Macd Signal avg", 9, 2, 200, 1 ); 
myADX = ADX(range);
myMacd = MACD(r1,r2);
mySignal = Signal(r1,r2,r3);

//1. Buy when MACD gives bullish Signal AND ADX is above 25 .
Buy = myMacd > mySignal AND myADX > 25;
//2. Sell when MACD gives negative Signal OR u get decent 25 points OR market is closed. 
Sell = myMacd < mySignal OR Cross(H, ValueWhen(Buy,Close)+25) OR TimeNum()>152500;
//3. Short when MACD gives bearish Signal AND ADX is above 25. 
Short = myMacd < mySignal AND myADX > 25;
//4. Cover when MACD gives bullish Signal OR u get decent 25 points OR market is closed.
Cover = myMacd > mySignal  OR Cross(ValueWhen(Short,Close)-25,L) OR TimeNum()>152500; 

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short, Cover);
Cover= ExRem(Cover,Short);
// exploration
Filter = Buy OR Short; 
AddColumn(Close, "Close", 1.2);
AddColumn(Buy, "Buy", 1.0);
AddColumn(Short, "Short",1.0);
_SECTION_END();
Happy :)


Happy ji..

thanx you for efforts..

This above scanner scans all stocks above 25 Adx and macd crossover.. it is giving 200 stocks (its lots of stocks for intraday trading).


My request is to modify it a bit..

I need

Macd crossover up.. but Adx between 20-30 = Buy

Macd crossover down but adx moving range of 20-30 = Sell

(hope u understood my point as i want limited stocks)



If adx is around 22-23.. once it crosses 25.. will be able to buy or sell stocks.


Please help me for this..

Thanx you
 

amitrandive

Well-Known Member
Happy ji..

thanx you for efforts..

This above scanner scans all stocks above 25 Adx and macd crossover.. it is giving 200 stocks (its lots of stocks for intraday trading).


My request is to modify it a bit..

I need

Macd crossover up.. but Adx between 20-30 = Buy

Macd crossover down but adx moving range of 20-30 = Sell

(hope u understood my point as i want limited stocks)



If adx is around 22-23.. once it crosses 25.. will be able to buy or sell stocks.


Please help me for this..

Thanx you
Shruti

I think I have modified the code as you wanted.But I would suggest you limit yourself to a limited number of stock.


Code:
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
range = Param("ADX Periods", 10, 2, 200, 1 );
r1 = Param( "Macd Fast avg", 12, 2, 200, 1 );
r2 = Param( "Macd Slow avg", 26, 2, 200, 1 );
r3 = Param( "Macd Signal avg", 9, 2, 200, 1 ); 
myADX = ADX(range);
myMacd = MACD(r1,r2);
mySignal = Signal(r1,r2,r3);

//1. Buy when MACD gives bullish Signal AND ADX is above 20 and below 30 .
Buy = myMacd > mySignal AND myADX > 20 AND myADX<30;
//2. Sell when MACD gives negative Signal OR u get decent 25 points OR market is closed. 
Sell = myMacd < mySignal OR Cross(H, ValueWhen(Buy,Close)+25) OR TimeNum()>152500;
//3. Short when MACD gives bearish Signal AND is above 20 and below 30 
Short = myMacd < mySignal AND myADX > 20 AND myADX<50;
//4. Cover when MACD gives bullish Signal OR u get decent 25 points OR market is closed.
Cover = myMacd > mySignal  OR Cross(ValueWhen(Short,Close)-25,L) OR TimeNum()>152500; 

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short, Cover);
Cover= ExRem(Cover,Short);
// exploration
Filter = Buy OR Short; 
AddColumn(Close, "Close", 1.2);
AddColumn(Buy, "Buy", 1.0);
AddColumn(Short, "Short",1.0);
_SECTION_END();
PlotShapes(Buy* shapeUpArrow,colorBlue, 0,L, Offset=-45); 
PlotShapes(Short* shapeDownArrow,colorRed, 0,H, Offset=-45);
PlotShapes(Sell*shapeHollowDownArrow,colorRed,0,L,-51);
PlotShapes(Cover*shapeHollowUpArrow,colorBlue,0,H,-51);
A link I got from this forum

1)ADX is below 20 - USE OSCILLATOR BASED SYSTEM(RSI,STOCHASTIC ETC.)

2)ADX is above 30 - USE TREND FOLLOWING SYSTEM(MOVING AVERAGE,MACD ETC.)

3)ADX is at 45 or above it - BOOK PROFITS.(Wait for new entry)

4)ADX IS RISING FROM LEVEL BELOW 18 AND THEN GOING ABOVE 23 - USE TREND FOLLOWING SYSTEM.

5)ADX DECLINING FROM LEVEL ABOVE 30 AND THEN GOING BELOW 27 - USE OSCILLATOR BASED SYSTEM.

The above mentioned method will help you select right system at right time. It is not a holy grail
 
Strategy works based on highest and lowest price and PVI so I wrote below code, but was not able to code for same for PVI as I was not able to find TimeFrameGetPrice equivalent for PVI.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot(C, "", IIf(O>=C, colorRed, colorGreen), ParamStyle("Price Style",styleCandle,maskPrice));

PevH = TimeFrameGetPrice("H", inDaily, -1);
PevL = TimeFrameGetPrice("L", inDaily, -1);
PevClose = TimeFrameGetPrice("C", inDaily, -1);
GfxTextOut( ("Prev Day High: " + PevH + " Low : " + PevL + " Close : " + PevClose ), 30 ,30);
Plot(PevH ,"",colorBlue,styleStaircase|styleSwingDots|styleNoRescale);
Plot(PevL ,"",colorRed,styleStaircase|styleSwingDots|styleNoRescale);
Plot(PevClose ,"",colorGreen,styleStaircase|styleSwingDots|styleNoRescale)

But what I require is same high and low for PVI which is nothing but Positive Volume Index indicator which is by default available as AFL in Amibroker Indicators.

If you find it confusing, even if u can provide code for previous days Highest and Lowest RSI, it would be of great help for me, I can make requried changes later with PVI
Can anyone help me please for the code I requested
 
Hello. First, I want to congratulate you for your initiative. Really thank you!

Now for the questions:

First, i want to trade futures bovespa index. It is measured in points, with minimum tick size of 5 points. When I put 100, I meam 100 points, or 20 ticks (of 5 points each).

I am trying to set up an automatic system thats buy at the touch of a MA, put its stop loss and gain (lets say, 100 and 200 respectively, but I want to be able to automaticly optimize these values), and move the stop to breakeven point after some profit (again optimizable). The short process is the same. I will show it in picture so it can be really clear:





So, I tried using this code for my first backtest:

//MA first backtest

Period = Optimize( "MA Period" , 66 , 2 , 100 , 1 );
Field = ParamField( "Field" , 3 );
Field_Opt = Optimize( " Field " , 3 , 0 , 4 , 1 );
MA_1 = MA( Field_Opt , Period );




Plot( MA_1 , "MA" , colorOrange );



Buy = Cross( MA_1 , Low );

BuyPrice = MA_1 - 10;

BuyStopLoss = BuyPrice - 100;
BuyTakeProfit = BuyPrice + 200;


Sell = BuyStopLoss OR BuyTakeProfit;

SellPrice = BuyTakeProfit OR BuyStopLoss ;




Short = Cross( High , MA_1);

SellPrice = MA_1 + 10;

SellStopLoss = SellPrice + 100;
SellTakeProfit = SellPrice - 200;

Cover = SellStopLoss OR SellTakeProfit;

CoverPrice = SellStopLoss OR SellTakeProfit;


I don't know what I'm doing wrong, but the backtest just doesnt even work. Can you help me?

Thanks for your time!
 

trash

Well-Known Member
_SECTION_BEGIN("Test_code");
Lot1=Param("Lot1",1,1,20,1);
Lot2=Param("Lot2",1,1,20,1);
Lot3=Param("Lot3",1,1,20,1);
Lot4=Param("Lot4",1,1,20,1);

GAP1=Param("GAP1",2,1,20,1);
GAP2=Param("GAP2",6,1,20,1);
GAP3=Param("GAP3",12,1,20,1);
GAP4=Param("GAP4",1,1,20,1);
Tar1=Param("Tar1",3,1,20,1);
Tar2=Param("Tar2",6,1,20,1);
Tar3=Param("Tar3",9,1,20,1);
Tar4=Param("Tar4",1,1,20,1);
Plot( C, "Close", colorWhite, styleCandle, Zorder = 1);
SetChartOptions(0,chartShowArrows | chartShowDates);


//to calculate the avg price
function Calculate_Avg_Price(Avg_price,Gap_Price)
{
Avg_price1= (Avg_price +Gap_Price)/2;
return Avg_price1;
}




Gap=Lot=1;
//Buy=TimeFrameGetPrice("C",inWeekly,-1,expandFirst);
global Avg_Price;
//Avg_Price=0;
//consider the first trade price is
BuyPrice=270;
Avg_Price=BuyPrice;
//Avg_Price
PTar1=BuyPrice+Tar1;
//PTar2=BuyPrice+Tar2;
//PTar3=BuyPrice+Tar3;
//PTar1=Tar1+Avg_Price;
PTar2=Tar2+Avg_Price;
PTar3=Tar3+Avg_Price;



PGap1=BuyPrice-Gap1;
PGap2=BuyPrice-Gap2;
PGap3=BuyPrice-Gap3;

printf("\n P Tar1 value is =>"+PTar1);
printf("\n P Tar2 value is =>"+PTar2);
dist = 1.5*ATR(10);
flag_gap=flag_tar=0;

for(i=0;i<BarCount;i++)
{
// printf("\n ith value is =>"+i);

//Calculating the first target
//i.e. Verify the expected target is between the Value Of target1 AND target 2
if (C>=PTar1 AND C<=PTar2 )
{
Lot=Lot+1;
printf("\n Current log size is =>"+Lot);
//PlotText( "\n Target 1 price is " +PTar1 , i, H[ i ]-dist, colorYellow );
}
//calculate the gap
//if we take a Buy AND the price is moving downwards then add 1 more Lot for the FIRst gap
//also after addiding the first lot we need to calculate the avg price i.e. buyprice +gap price /2
if ( C<PGap1 AND C>PGap2 )
{
Gap=Gap+1;
Avg_Price=Calculate_Avg_Price(BuyPrice,PGap1);
//PlotText( "\n Gap 1 price is " +PGap1 , i+1, H[ i+1 ]-dist, colorWhite );
//PlotText( "\n Avg price is " +Avg_Price , i, H[ i ]-dist, colorWhite );
flag_gap=1;
}

//Calculate the second target
if (C>=PTar2 AND C<=PTar3 AND flag_tar!=2)
{
Lot=Lot+1;
//PlotText( "\n Target 1 price is " +PTar2 , i, H[ i ]-dist, colorYellow );

}

//Calculate the Second gap
if ( C<PGap2 AND C>PGap3 AND flag_gap!=2 )
{

Gap=Gap+1;
Avg_Price=Calculate_Avg_Price(Avg_Price,PGap2);

//PlotText( "\n Gap 2 price is " +PGap2 , i, H[ i ]-dist, colorYellow );
flag_gap=2;
//PlotText( "\n Avg price is " +Avg_Price , i+1, H[ i+1 ]-dist, colorWhite );



}



}

Plot(PTar1,"",colorGreen,styleDots) ;
Plot(PTar2,"",colorGreen,styleLine) ;
Plot(PGap1,"",colorYellow,styleDots) ;
Plot(PGap2,"",colorYellow,styleLine) ;


I am not able to plat gap value line .as per the avg price value in the above
code .
The above plot function is able to plot the Fix gap value .Its not considering the AVg_price value while draw the line .Verified with print statement
avg price calculation is correct
 

Similar threads