Last active
August 27, 2020 05:43
-
-
Save botany02/e4bc94f57f5e83d264ceeb0a1d14cef6 to your computer and use it in GitHub Desktop.
Demo Trading System with SL Entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Demo Trading System with BO-SL Entry | |
ORB with SL Entry | |
Coded by ChokS | |
https://howutrade.in | |
[email protected] | |
*/ | |
_SECTION_BEGIN("CHART_OPTIONS"); | |
GfxSetBkMode(0); | |
SetChartOptions(0,chartShowArrows|chartShowDates); | |
SetChartBkColor( colorBlack); | |
SetChartBkGradientFill( colorBlack, colorBlack); | |
_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",IIf(C>O,colorLime,colorRed),styleCandle|styleNoTitle); | |
FS=Param("Font Size",30,11,100,1);GfxSelectFont("Times New Roman", FS, 700, True );GfxSetBkMode( colorWhite );GfxSetTextColor( ParamColor("Color",colorGreen) );Hor=Param("Horizontal Position",940,1,1200,1);Ver=Param("Vertical Position",12,1,830,1); | |
GfxTextOut(""+C, Hor , Ver );YC=TimeFrameGetPrice("C",inDaily,-1);DD=Prec(C-YC,2);xx=Prec((DD/YC)*100,2);GfxSelectFont("Times New Roman", 11, 700, True );GfxSetTextColor(ParamColor("Color",colorYellow) );GfxTextOut(""+DD+" ("+xx+"%)", Hor , Ver+45 ); | |
BarT = BarIndex() - Nz(ValueWhen(Day()!= Ref(Day(),-1) ,BarIndex()),0) + 1; | |
DayH = HHV(H,BarT); | |
DayL = LLV(L,BarT); | |
_SECTION_END(); | |
_SECTION_BEGIN("ORB"); | |
Exch = ParamList("Exchange","NSE",0); | |
Symbol = ParamStr("Symbol (Ex: ICICIBANK)","ICICIBANK"); | |
Qty = Param("Quantity",1,1,10000,1); | |
SqOffValue = Param("Target Points",10,0.05,10000,0.05); | |
StoplossValue = Param("Stoploss Points",3,0.05,10000,0.05); | |
TrailingStoploss = Param("Trailing Points",1,0,10000,0.05); | |
//Difference between TrgPrice and LmtPrice | |
//Use higher value to Simulate MARKET order | |
Haircut = Param("Haircut for SL Entry",1,0,100,0.05); | |
StgyCode = ParamList("StgyCode","R1|R2|R3|R4|R5|R6|R7|R8|R9",0); | |
OrdType = "SL"; | |
IsLive = True; | |
IsAsync = True; | |
TrdSym = Symbol; //CHECK | |
//System related | |
OrbTime = ParamTime("ORB Time","09:45:00",0); | |
TrdEndTime = ParamTime("Trade End Time","14:45:00",0); | |
SqOffTime = ParamTime("Square Off Time","15:10:00",0); | |
IsTrdTime = TimeNum()>=OrbTime && TimeNum()<=TrdEndTime; | |
IsSqOffTime = TimeNum()>=SqOffTime; | |
IsEndTime = TimeNum() > TrdEndTime; | |
//Tags | |
BuyTag = StgyCode + "-BUY"; | |
ShortTag = StgyCode + "-SHORT"; | |
//ORB | |
ORBCandle = (TimeNum() == OrbTime); | |
ORBHigh = ValueWhen(ORBCandle,DayH); | |
ORBLow = ValueWhen(ORBCandle,DayL); | |
ORBBar = ValueWhen(ORBCandle,BarT); | |
Plot(ORBHigh, "ORBHigh", colorBlue, styleLine); | |
Plot(ORBLow, "ORBLow", colorRed, styleLine); | |
//Signals | |
Buy = (BarT > ORBBar AND H >= ORBHigh AND IsTrdTime); | |
Short = (BarT > ORBBar AND L <= ORBLow AND IsTrdTime); | |
Buy = ExRem(Buy,ORBCandle); | |
Short = ExRem(Short,ORBCandle); | |
BuyPrice = ValueWhen(Buy, ORBHigh); | |
ShortPrice = ValueWhen(Short,ORBLow); | |
BuyTgtPrice = BuyPrice + SqOffValue; | |
BuySlPrice = BuyPrice - StoplossValue; | |
ShortTgtPrice = ShortPrice - SqOffValue; | |
ShortSlPrice = ShortPrice + StoplossValue; | |
Sell = (H >= BuyTgtPrice OR L <= BuySLPrice OR IsSqOffTime OR Short) AND Buy == 0; | |
Cover = (H >= ShortSlPrice OR L <= ShortTgtPrice OR IsSqOffTime OR Buy) AND Short == 0; | |
Sell = IIf(Cum(Buy)==0,0,ExRem(Sell,Buy)); | |
Cover = IIf(Cum(Short)==0,0,ExRem(Cover,Short)); | |
BuyExit = IIf(H >= BuyTgtPrice, BuyTgtPrice, IIf(L <= BuySLPrice, BuySLPrice, IIf(Short, ShortPrice, O))); | |
ShortExit = IIf(L <= ShortTgtPrice, ShortTgtPrice, IIf(H >= ShortSLPrice, ShortSLPrice, IIf(Buy, BuyPrice, O))); | |
SellPrice = ValueWhen(Buy, BuyExit); | |
CoverPrice = ValueWhen(Short,ShortExit); | |
PlotShapes(IIf(Buy, shapeHollowUpArrow, shapeNone),colorGreen, 0,L, Offset=-30); | |
PlotShapes(IIf(Cover, shapeHollowStar, shapeNone),colorDarkGreen, 0,L, Offset=-50); | |
PlotShapes(IIf(Short, shapeHollowDownArrow, shapeNone),colorRed, 0,H, Offset=-30); | |
PlotShapes(IIf(Sell, shapeHollowStar, shapeNone),colorDarkRed, 0,H, Offset=50); | |
_SECTION_END(); | |
_SECTION_BEGIN("BRIDGE_CODE"); | |
//Initialize Bridge | |
Bridge = CreateStaticObject("KiteNet.Bridge"); | |
/* | |
Flow | |
1. Place Buy and Short SL Order on OrbTime | |
2. Track High or Low Break in AFL (i.e Buy and Short Signals) | |
3. If High Break, then cancel the Short BO entry and Vice versa | |
4. Track Exit Signal in AFL (i.e. Target, Stoploss or Sqoff hit) | |
5. On Exit Signal, Call ExitBO to Close Open position (in live this position may be closed by OMS by Trail points etc) | |
6. If No Break Till EndTime, Cancel both Entries | |
Use Static Variables to Restrict multiple requests | |
*/ | |
//On ORB Time | |
SlPlaceStaus = Nz(StaticVarGet(StgyCode + Symbol + "SLPLACE")); | |
if (LastValue(ORBCandle) && SlPlaceStaus == 0) { | |
StaticVarSet(StgyCode + Symbol + "SLPLACE", 1); | |
//Place BUY BO | |
SignalType = "BUY"; | |
TagApi = "BUY"; | |
TrgPrice = LastValue(ORBHigh); | |
Lmtprice = TrgPrice + Haircut; //CHECK | |
CTag = BuyTag; | |
Resp = Bridge.PlaceBOBridge(Exch, TrdSym, SignalType, Qty, LmtPrice, SqOffValue, StoplossValue, TrailingStoploss, OrdType, TrgPrice, CTag, IsLive, StgyCode, IsAsync, TagAPI); | |
//Place SHORT BO | |
SignalType = "SHORT"; | |
TagApi = "SHORT"; | |
TrgPrice = LastValue(ORBLow); | |
Lmtprice = TrgPrice - Haircut; //CHECK | |
CTag = ShortTag; | |
Resp = Bridge.PlaceBOBridge(Exch, TrdSym, SignalType, Qty, LmtPrice, SqOffValue, StoplossValue, TrailingStoploss, OrdType, TrgPrice, CTag, IsLive, StgyCode, IsAsync, TagAPI); | |
} | |
BuyStaus = Nz(StaticVarGet(StgyCode + Symbol + "BUY")); | |
if (LastValue(Buy) && BuyStaus == 0) { | |
StaticVarSet(StgyCode + Symbol + "BUY", 1); | |
//Upside Break, lets Cancel SHORT BO | |
CTag = ShortTag; | |
OrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
Resp = Bridge.CancelBOMainBridge(OrderId,IsAsync); | |
} | |
SellStaus = Nz(StaticVarGet(StgyCode + Symbol + "SELL")); | |
if (LastValue(Sell) && SellStaus == 0 && BuyStaus == 1) { | |
StaticVarSet(StgyCode + Symbol + "SELL", 1); | |
//Got Exit Signal | |
CTag = BuyTag; | |
POrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
ChildOrders = Bridge.GetChildOrders(POrderId, IsLive); | |
Resp = Bridge.ExitBOBridge(ChildOrders, IsAsync, StgyCode, IsLive); | |
} | |
ShortStaus = Nz(StaticVarGet(StgyCode + Symbol + "SHORT")); | |
if (LastValue(Short) && ShortStaus == 0) { | |
StaticVarSet(StgyCode + Symbol + "SHORT", 1); | |
//Dnside Break, lets Cancel BUY BO | |
CTag = BuyTag; | |
OrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
Resp = Bridge.CancelBOMainBridge(OrderId,IsAsync); | |
} | |
CoverStaus = Nz(StaticVarGet(StgyCode + Symbol + "COVER")); | |
if (LastValue(Cover) && CoverStaus == 0 && ShortStaus == 1) { | |
StaticVarSet(StgyCode + Symbol + "COVER", 1); | |
//Got Exit Signal | |
CTag = ShortTag; | |
POrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
ChildOrders = Bridge.GetChildOrders(POrderId, IsLive); | |
Resp = Bridge.ExitBOBridge(ChildOrders, IsAsync, StgyCode, IsLive); | |
} | |
EntryStaus = Nz(StaticVarGet(StgyCode + Symbol + "ENTRY")); | |
if (LastValue(IsEndTime) && EntryStaus == 0) { | |
StaticVarSet(StgyCode + Symbol + "ENTRY", 1); | |
if (BuyStaus == 0 && ShortStaus == 0 && SlPlaceStaus == 1) { | |
//Cancel BUY BO Entry | |
CTag = BuyTag; | |
OrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
Resp = Bridge.CancelBOMainBridge(OrderId,IsAsync); | |
//Cancel SHORT BO Entry | |
CTag = ShortTag; | |
OrderId = Bridge.GetOrderCTag(Exch, TrdSym, CTag, IsLive); | |
Resp = Bridge.CancelBOMainBridge(OrderId,IsAsync); | |
} | |
} | |
_SECTION_END(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment