Simple Coding Help - No Promise.

I need simple code for buy sel rules. I tried some codes but not true in bactest.


Buy rule1 = rsi > 70
Buy rule2 = Cross( MACD(), Signal() );

Sell rule1 = rsi < 30
Sell rule2 = Cross( Signal, MACD() );

Buy= if buy1 and buy2
sell = if sell1 and sell2

short=sell
cover=buy

Thank you...
 

amitrandive

Well-Known Member
I need simple code for buy sel rules. I tried some codes but not true in bactest.


Buy rule1 = rsi > 70
Buy rule2 = Cross( MACD(), Signal() );

Sell rule1 = rsi < 30
Sell rule2 = Cross( Signal, MACD() );

Buy= if buy1 and buy2
sell = if sell1 and sell2

short=sell
cover=buy

Thank you...
Found on the net again.Think RSI rules are reversed in your requirement.Please check.

Code:
Buy = Cover=(Cross( MACD(), Signal() ) OR Cross( RSI(), 30 )) ;
Sell = Short=(Cross(Signal(), MACD() ) OR Cross( RSI(), 70 )) ;
//exploration
Filter = Buy OR Sell;
AddColumn( Buy, "Buy", 1);
AddColumn( Sell, "Sell", 1);
AddColumn( Volume, "Volume", 1);
AddColumn( Cross( MACD(), Signal() ), "MACD/Signal cross", 1);
AddColumn( Cross( RSI(), 30 ), "RSI / 30 cross", 1);
AddColumn( Cross( RSI(), 70 ), "RSI / 70 cross", 1);
 

extremist

Well-Known Member
Here is alternative to add "near MA" occurrences

Code:
// by trash

MAequalval = 0.05;  // MA equality in percent, 0.01 means within 0.01% of MA
MAequalval = MAequalval * 100000;

MA_ = EMA( C, 20 );

// don't touch but be near MA
MAequalityL = Nz( AlmostEqual( L, MA_, MAequalval ) ) AND L > MA_;
MAequalityH = Nz( AlmostEqual( H, MA_, MAequalval ) ) AND H < MA_;

// touch MA
touch = H > MA_ AND L < MA_;

condition = touch OR MAequalityL OR MAequalityH;

Plot( MA_, "MA", colorGold, styleLine );
PlotShapes( condition*shapeSmallSquare, colorGold, 0, MA_, 0 );
EDIT: removed redundant code parts


great thanks to u mr trash.
 
Guyss... Need a small modification on the below code. Its ORB strategy, after the initial 5 minutes, based on the breakout/breakdown it shows signal.

However, i would like to add the below condition.

1. we go long when opening range high is broke with opening range low as stop loss.

when stop loss hits, the system exits the position. But I want to reverse the trade, by exiting the long position and going short with opening range high as stop loss, when this trade's stop loss hit, we should exit the trade, no more trades.

2. we go short when opening range low is broke with opening range high as stop loss.

when stop loss hits, the system exits the position. But I want to reverse the trade, by exiting the short position and going long with opening range low as stop loss, when this trade's stop loss hit, we should exit the trade, no more trades.

code:
Code:
function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}




_SECTION_BEGIN("Augubhai's ORB System v1.1"); 

//--Intraday time frame 
TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes 
TimeFrameInMinutes = 5;

//--Define all params 
EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 0, 0);
SLPct = ParamOptimize("SL %", 0, 0, 0, 0);
TargetPct = ParamOptimize("Target %", 0, 0, 0, 0);
MaxTarget = 100;
TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct); 
EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 425, 10, 180, 5);
EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);  

//--Plot Price Candle Chart
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", colorBlack, styleNoTitle | GetPriceStyle() ); 

//--New Day & Time. End Day & Time . End Day & Time is null till end of day 1  
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0; 
printf("\n NewDay  : " + NewDay ); 
EndDay = (Day()!= Ref(Day(), 1)); 
printf("\n EndDay  : " + EndDay );
FirstBarTime = ValueWhen(NewDay,TimeNum(),1); 
EndTime = ValueWhen(EndDay,TimeNum(),1);
SquareOffTime = 152000;

//--Calculate ORB, and SL
HighestOfDay = HighestSince(NewDay,H,1); 
LowestOfDay = LowestSince(NewDay,L,1); 
BarsSinceNewDay = BarsSince(NewDay);
ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100)); 
ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100)); 
//ORBHSL = ORBH * (1-(SLPct/100)); 
ORBHSL = ORBL; 
//ORBLSL = ORBL * (1+(SLPct/100));
ORBLSL = ORBH; 
ORBHTarget = ORBH * (1+(TargetPct/100));
ORBLTarget = ORBL * (1-(TargetPct/100));


//--Find Buy, Sell, Short & Cover Signals
BuySignal = (H >= ORBH) AND (BarsSinceNewDay  > EntryBarStart); 
printf("\nBuySignal : " + BuySignal ); 
ShortSignal = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) ; 
printf("\nShortSignal  : " + ShortSignal ); 
BarsSinceLastBuySignal = (BarsSince(Ref(BuySignal,-1)) + 1);
BarsSinceLastShortSignal = (BarsSince(Ref(ShortSignal,-1)) + 1);
BarsSinceLastEntrySignal = Min(BarsSinceLastBuySignal, BarsSinceLastShortSignal);
BothEntrySignalsNull = IsNull(BarsSinceLastBuySignal) AND IsNull(BarsSinceLastShortSignal); //true for start of Day 1
printf("\n\nBarsSinceNewDay : " + BarsSinceNewDay ); 
printf("\n BarsSinceLastEntrySignal : " + BarsSinceLastEntrySignal); 
Buy = (H >= ORBH) AND (BarsSinceNewDay  > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < 

BarsSinceLastEntrySignal) OR BothEntrySignalsNull ); 
Sell = (L <= ORBHSL) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastBuySignal); 
Short = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < 

BarsSinceLastEntrySignal) OR BothEntrySignalsNull ); 
Cover = (H >= ORBLSL) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal); 
printf("\nBuy : " + Buy ); 
printf("\nSell : " + Sell ); 
printf("\nShort : " + Short ); 
printf("\nCover : " + Cover ); 

//--Handle if ORB broken both sides on same bar
//--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
orbBothSides = IIf(Buy AND Short, 1, 0); 
Buy = IIf(orbBothSides AND C <= O, 0, Buy); 
Short = IIf(orbBothSides AND C > O, 0, Short); 
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell); 
Sell = IIf((BarsSince(Buy) < (BarsSince(Ref(Sell,-1))+1)) OR (BarsSince(Buy) AND IsNull(BarsSince(Ref(Sell,-1)))),Sell,0);
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover); 
Cover = IIf((BarsSince(Short) < (BarsSince(Ref(Cover,-1))+1)) OR (BarsSince(Short) AND IsNull(BarsSince(Ref(Cover,-

1)))),Cover,0);
printf("\n\norbBothSides : " + orbBothSides); 
printf("\nBuy : " + Buy ); 
printf("\nSell : " + Sell ); 
printf("\nShort : " + Short ); 
printf("\nCover : " + Cover ); 

//--Special Condition for 18 & 19 May 2009 for the Indian Market
Buy =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Buy );
Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );

//--Set prices
BuyPrice = IIf(Buy, ORBH, Null); 
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null); 
ShortPrice = IIf(Short, ORBL, Null); 
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null); 

//--Plot ORB, and SL 
Plot(ORBHSL,"",colorRed,styleDashed); 
Plot(ORBLSL,"",colorRed,styleDashed); 
Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorBlack, styleCloud); 
 
//--Plot Signals
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow; 
PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) ); 
shape2 = Cover * shapeUpArrow + Short * shapeDownArrow; 
PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) ); 
GraphXSpace = 5; 

//--Restore time frame 
TimeFrameRestore(); 
_SECTION_END();
Hello Cubt

This will take quite a bit of work/time, setting up a loop for tracking trades etc . . .
Maybe Augubhai' has already tested this type of version that takes second trade, if he has then we can request him to post it or send it to you over pm.

Another alternative is to look for a code that keeps taking all the trades (not just first 2 trades ) on break of ORB Levels . . .


Happy :)
 
Hello Cubt

This will take quite a bit of work/time, setting up a loop for tracking trades etc . . .
Maybe Augubhai' has already tested this type of version that takes second trade, if he has then we can request him to post it or send it to you over pm.

Another alternative is to look for a code that keeps taking all the trades (not just first 2 trades ) on break of ORB Levels . . .


Happy :)
Thanks for the input, Happy. I tried coding it myself with amibroker help and with help of other afl codes form google.. but still i couldn't do it. I shall check with Agubhai on this.
 
Found on the net again.Think RSI rules are reversed in your requirement.Please check.

Code:
Buy = Cover=(Cross( MACD(), Signal() ) OR Cross( RSI(), 30 )) ;
Sell = Short=(Cross(Signal(), MACD() ) OR Cross( RSI(), 70 )) ;
//exploration
Filter = Buy OR Sell;
AddColumn( Buy, "Buy", 1);
AddColumn( Sell, "Sell", 1);
AddColumn( Volume, "Volume", 1);
AddColumn( Cross( MACD(), Signal() ), "MACD/Signal cross", 1);
AddColumn( Cross( RSI(), 30 ), "RSI / 30 cross", 1);
AddColumn( Cross( RSI(), 70 ), "RSI / 70 cross", 1);
:clapping::clapping::clapping: Thank you
 
Code:
 NewDay = Day()!= Ref(Day(), -1);
 afterbreakout0 = Cross(TimeNum(),093000);
 afterbreakout1 = TimeNum()>= 093000 AND TimeNum()<=151500;
 highestoftheday = HighestSince(newday,H,1);
 Lowestoftheday =LowestSince(newday,L,1);
 ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1);
 ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1);
 Buy= Cross(C,orbhigh) AND afterbreakout1;
 Sell = Cross(orblow,C) AND afterbreakout1 OR Cross(TimeNum(),151500);

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


Short=Cross(ORblow,C) AND afterbreakout1;
Cover=Cross(C,ORbhigh) AND afterbreakout1  OR Cross(TimeNum(),151500);

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


 Plot(C,"",colorYellow,styleBar);
 PlotShapes( shapeUpArrow * Buy, colorGreen,0,L,-12);
 PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);
 Plot(afterbreakout0,"",colorBlue,styleHistogram|styleOwnScale);
 Plot(ORBHigh,"",colorGreen,styleDots);
 Plot(ORBLow,"",colorRed,styleDots);
Found this very old file, this will take all trades, me thinks . . . .

Adjust the BO time as your requirements . . .


Happy :)


Thanks for the input, Happy. I tried coding it myself with amibroker help and with help of other afl codes form google.. but still i couldn't do it. I shall check with Agubhai on this.
 
Last edited:
Hello

This one is for Augubhai :thumb:

Why ORB works better on historical charts and not so well now?

The story goes that in older days, the big orders from institutions used to take some time after open, before they start hitting the floor . . .

This order flow used to decide the initial intraday trend for the market, and that's where the ORB trades used to get their traction from.

That latency in the order flow is no more valid . . . with automation, algos and what not . . .

In Indian context, when the markets used to open at 9:55, the first 5 minutes candle was the best range for trading.



Happy :)
 

Similar threads