Hi everyone, I'm new to Amibroker and AFL. I'm not a dev but a self taught guy and I'm starting to code all my strategies. I started with pinescript and want to move all my scripts to AFL because I got a new job where I am asked for a robust backtest.
Amibroker ver 6.40.4
TimeFrame m5
Ticker MNQ1!
This is the idea I'm trying to code:
I've two Buy conditions and two Short conditions.
BuyCond1 and ShortCond1 are my main signals with fixed SL & TP, these are typical channel BO. If prev trade was also a main signal MUST be a bar between the prev and the new signal. In other words, once a trade finishes (SL or TP) for ex in the 11:55 bar the 12:00 bar must meet the condition for a new signal and the new trade will start at 12:05 Open. If 11:55 bar meet signal criteria it should not trigger a trade because is the same bar previous trade finished.
BuyCond2 and ShortCond2 are secondary signals and triggers ONLY if prev trade was a main signal. There's must NOT be two secondary trades in a row. The pattern that triggers a secondary signal must NOT be inside the prev trade but if the last bar of a sec signal also triggers a main this new signal IS VALID.
Unfortunately I don't have a (correctly) working code from which to present example images so I will share
with you what I studied and did so far..
I started with this code which I follow from this YT video.
_SECTION_BEGIN("DCH intraday trading System");
//realtime signals 0,0,0,0(immediate execution)
SetTradeDelays(0,0,0,0);
//Inputs controls
lenght = Param("Lenght",20,1,100,1);
stops = Param("Stoploss",50,1,1000,1);
target = Param("Target",100,1,1000,1);
displayline = ParamToggle("Show Target & Stoploss","HIDE|SHOW",1);
//Intraday controls
starttime = ParamTime("signalStartTime","10:30");
endtime = ParamTime("signalEndTime","17:00");
sqofftime = ParamTime("SquareoffTime","17:05");
//Build Donchinal Channel
upper = HHV(High,lenght);
lower = LLV(Low,lenght);
//One bar delay to the channel
upper = Ref(upper,-1);
lower = Ref(lower,-1);
//Plotting Donchian Channel
Plot(upper,"DCH upper",colorBlue,styleThick);
Plot(lower,"DCH lower",colorBlue,styleThick);
//Plot Candles
SetChartOptions(0,chartShowArrows | chartShowDates); //enable X - axis
Plot(Close,"Candles",colorDefault,styleCandle);
//Trading logic Buy & Sell
//Condicion de entrada para Longs
Buy = Cross(High,upper) AND TimeNum() >= starttime AND TimeNum() <= endtime;
//Condicion de salida
isell = Cross(lower,Low) OR TimeNum() == sqofftime;
Buy = ExRem(Buy,isell);
Sell = ExRem(isell,Buy);
//Calculating Long entry price - Gap UP considered
BuyPrice = ValueWhen(Buy,Max(Open,upper)); //Calculating Long entry price - Gap UP considered
//Calculo de SL y TP
BuyStop = BuyPrice - stops;
BuyTarget = BuyPrice + target;
//Condicion final de salida para Longs - Long Exits
Sell = isell OR Cross(High,BuyTarget) OR Cross(BuyStop,Low);
//Remove excessive signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Calculating sell price - Gap considered
SellPrice = ValueWhen(Sell,Min(Open,IIf(TimeNum()==sqofftime,Close,IIf(Cross(lower,Low),lower,
IIf(Cross(High,BuyTarget),BuyTarget,IIf(Cross(BuyStop,Low),BuyStop,Close))))));
//Trading logic Short & Cover
//Condicion de entrada para Shorts
Short = Cross(lower,Low) AND TimeNum() >= starttime AND TimeNum() <= endtime;
//Condicion de salida
icover = Cross(High,upper) OR TimeNum() == sqofftime;
Short = ExRem(Short,icover);
Cover = ExRem(icover,Short);
//Calculating Short entry price - Gap Down considered
ShortPrice = ValueWhen(Short,Min(Open,lower));
//Calculo de SL y TP
ShortStop = ShortPrice + stops;
ShortTarget = ShortPrice - target;
//Condicion final de salida para Shorts - Short Exits
Cover = icover OR Cross(ShortTarget,Low) OR Cross(High,ShortStop);
//Remove excessive signals
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
//Calculating cover price - Gap considered
CoverPrice = ValueWhen(Cover,Max(Open,IIf(TimeNum()==sqofftime,Close,IIf(Cross(High,upper),upper,
IIf(Cross(ShortTarget,Low),ShortTarget,IIf(Cross(High,ShortStop),ShortStop,Close))))));
//Plotting StopLoss and Target
if(displayline)
{
buycontinue = Flip(Buy,Sell); //nos permite graficar SL/TP mientras la posicion este activa
shortcontinue = Flip(Short,Cover);
Plot(IIf(buycontinue,BuyStop,Null),"BuyStop",colorRed,styleDashed);
Plot(IIf(buycontinue,BuyTarget,Null),"BuyTarget",colorGreen,styleDashed);
Plot(IIf(buycontinue,BuyPrice,Null),"BuyEntry",colorYellow,styleDashed);
Plot(IIf(shortcontinue,ShortStop,Null),"ShortStop",colorRed,styleDashed);
Plot(IIf(shortcontinue,ShortTarget,Null),"ShortTarget",colorGreen,styleDashed);
Plot(IIf(shortcontinue,ShortPrice,Null),"ShortEntry",colorYellow,styleDashed);
}
//Plotting Signals on chart
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
_SECTION_END();
Then I started adding my own conditions.
For Main signals I want Close breaking the channels and added an EMA to filter.
For secondary signal the EMA filter reverse and finally sum up all signals:
isUP = Close > EMA(Close,200);
isDOWN = Close < EMA(Close,200);
oppBuy = Close > Ref(upper,-1) AND isUP; //Main Buy signal
oppShort = Close < Ref(lower,-1) AND isDOWN; //Main Short signal
oprBuy = Ref(Close,-1) > Ref(Open,-1) AND Close > Open AND isDOWN; //Sec Buy signal
oprShort = Ref(Close,-1) < Ref(Open,-1) AND Close < Open AND isUP; //Sec Short signal
BuyCond1 = Ref(oppBuy,-1);
BuyCond2 = Ref(oprBuy,-1);
iBuy = BuyCond1 OR BuyCond2;
Buy = iBuy AND TimeNum() >= starttime AND TimeNum() <= endtime;
ShortCond1 = Ref(oppShort,-1);
ShortCond2 = Ref(oprShort,-1);
iShort = ShortCond1 OR ShortCond2;
Short = iShort AND TimeNum() >= starttime AND TimeNum() <= endtime;
As I said I use fixed SL and TP for mains but for secs I use the same SL but for my target I choose VWAP, so I add it to the code. Also tried my best to sum up all my exit conditions inside the Sell and Cover expressions, also for SellPrice and CoverPrice. My final version was:
_SECTION_BEGIN("Intraday test");
//realtime signals 0,0,0,0(immediate execution)
SetTradeDelays(0,0,0,0);
//Inputs controls
lenght = Param("Lenght",20,1,100,1);
stops = Param("Stoploss",50,1,1000,1);
target = Param("Target",50,1,1000,1);
displayline = ParamToggle("Show Target & Stoploss","HIDE|SHOW",1);
//Intraday controls
starttime = ParamTime("signalStartTime","10:30");
endtime = ParamTime("signalEndTime","17:00");
sqofftime = ParamTime("SquareoffTime","17:05");
//Build Donchinal Channel
upper = HHV(High,lenght);
lower = LLV(Low,lenght);
mid = (upper + lower) / 2;
//VWAP
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = IIf (BarIndex() >= StartBar, Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
//Plot Candles
SetChartOptions(0,chartShowArrows | chartShowDates); //enable X - axis
Plot(Close,"Candles",colorDefault,styleCandle);
isUP = Close > EMA(Close,200);
isDOWN = Close < EMA(Close,200);
//Trading logic Buy & Sell
oppBuy = Close > Ref(upper,-1) AND isUP; //Main Buy signal
oprBuy = Ref(Close,-1) > Ref(Open,-1) AND Close > Open AND isDOWN; //Sec Buy signal
BuyCond1 = Ref(oppBuy,-1);
BuyCond2 = Ref(oprBuy,-1);
iBuy = BuyCond1 OR BuyCond2;
Buy = iBuy AND TimeNum() >= starttime AND TimeNum() <= endtime;
//Calculating Long entry price
BuyPrice = ValueWhen(Buy,Open); //Calculating Long entry price - Gap UP considered
//Calculo de SL y TP
BuyStop = BuyPrice - stops;
BuyTarget = BuyPrice + target;
//Condicion final de salida para Longs - Long Exits
Sell = IIf(BuyCond1, Cross(High,BuyTarget) OR Cross(BuyStop,Low),IIf(BuyCond2, Cross(High,VWAP) OR Cross(BuyStop,Low),Null));
//Remove excessive signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Calculating sell price
SellPrice = ValueWhen(Sell,IIf(TimeNum()==sqofftime,Close,IIf(Cross(High,BuyTarget),BuyTarget,
IIf(Cross(BuyStop,Low),BuyStop,IIf(Cross(High,VWAP),VWAP,Close)))));
//Trading logic Short & Cover
oppShort = Close < Ref(lower,-1) AND isDOWN; //Main Short signal
oprShort = Ref(Close,-1) < Ref(Open,-1) AND Close < Open AND isUP; //Sec Short signal
ShortCond1 = Ref(oppShort,-1);
ShortCond2 = Ref(oprShort,-1);
iShort = ShortCond1 OR ShortCond2;
Short = iShort AND TimeNum() >= starttime AND TimeNum() <= endtime;
//Calculating Short entry price - Gap Down considered
ShortPrice = ValueWhen(Short,Open);
//Calculo de SL y TP
ShortStop = ShortPrice + stops;
ShortTarget = ShortPrice - target;
//Condicion final de salida para Shorts - Short Exits
Cover = IIf(ShortCond1, Cross(BuyTarget,Low) OR Cross(High,ShortStop),IIf(ShortCond2, Cross(VWAP,Low) OR Cross(High,ShortStop),Null));
//Remove excessive signals
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
//Calculating cover price - Gap considered
CoverPrice = ValueWhen(Cover,IIf(TimeNum()==sqofftime,Close,IIf(Cross(BuyTarget,Low),BuyTarget,
IIf(Cross(High,ShortStop),ShortStop,IIf(Cross(VWAP,Low),VWAP,Close)))));
//Plotting Donchian Channel
Plot(upper,"DCH upper",colorBlue,styleThick);
Plot(lower,"DCH lower",colorBlue,styleThick);
Plot(mid,"DCH lower",colorOrange,styleThick);
//Plotting VWAP & EMA
Plot (VWAP,"VWAP",colorYellow, styleLine);
Plot(EMA(Close,200),"EMA",colorRed,styleThick);
//Plotting StopLoss and Target
if(displayline)
{
buycontinue = Flip(Buy,Sell); //this let me plot while condition is true
shortcontinue = Flip(Short,Cover);
Plot(IIf(buycontinue,BuyStop,Null),"BuyStop",colorRed,styleDashed);
Plot(IIf(buycontinue,BuyTarget,Null),"BuyTarget",colorGreen,styleDashed);
Plot(IIf(buycontinue,BuyPrice,Null),"BuyEntry",colorYellow,styleDashed);
Plot(IIf(shortcontinue,ShortStop,Null),"ShortStop",colorRed,styleDashed);
Plot(IIf(shortcontinue,ShortTarget,Null),"ShortTarget",colorGreen,styleDashed);
Plot(IIf(shortcontinue,ShortPrice,Null),"ShortEntry",colorYellow,styleDashed);
}
//Plotting Signals on chart
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
_SECTION_END();
The final result was a plot mess
Looking for around I stumbled upon this thread and tried this out with the intention of resolving the SL and TP levels but leaving aside details of the logic such as the intermediate candle and the restrictions for secondary signals:
_SECTION_BEGIN("stopTest");
SetTradeDelays(0,0,0,0);
SetOption("MaxOpenPositions", 1);
SetOption("ActivateStopsImmediately", True);
//**************************************************************************************************************
//Inputs controls
//**************************************************************************************************************
lenght = Param("DCH Lenght",20,1,100,1);
oppStop = Param("OPP SL Ticks",50,1,1000,1);
oprStop = Param("OPR SL Ticks",50,1,1000,1);
target = Param("Target Ticks",100,1,1000,1);
displayline = ParamToggle("Show Target & Stoploss","HIDE|SHOW",1);
//Intraday controls
starttime = ParamTime("signalStartTime","10:30");
endtime = ParamTime("signalEndTime","17:00");
sqofftime = ParamTime("SquareoffTime","17:05");
TickSize = 0.25;
//**************************************************************************************************************
//Indicadores
//**************************************************************************************************************
//Build Donchinal Channel
upper = HHV(High,lenght);
lower = LLV(Low,lenght);
mid = (upper + lower) / 2;
//**************************************************************************************************************
//VWAP
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = IIf (BarIndex() >= StartBar, Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
//**************************************************************************************************************
//Plot Candles
SetChartOptions(0,chartShowArrows | chartShowDates); //enable X - axis
Plot(Close,"Close",colorDefault,styleCandle);
//**************************************************************************************************************
//Filter
inTime = TimeNum() >= starttime AND TimeNum() <= endtime;
isUP = Close > EMA(Close,200);
isDOWN = Close < EMA(Close,200);
//**************************************************************************************************************
//Condicion de entrada para OPP Longs
//**************************************************************************************************************
oppBuy = Close > Ref(upper,-1) AND isUP AND inTime;
oprBuy = Ref(Close,-1) > Ref(Open,-1) AND Close > Open AND isDOWN AND inTime;
isLong = oppBuy OR oprBuy;
//**************************************************************************************************************
//Condicion de entrada para OPP Shorts
oppShort = Close < Ref(lower,-1) AND isDOWN AND inTime;
oprShort = Ref(Close,-1) < Ref(Open,-1) AND Close < Open AND isUP AND inTime;
isShort = oppShort OR oprShort;
//**************************************************************************************************************
BuyCond1 = Ref(oppBuy,-1);
BuyCond2 = Ref(oprBuy,-1);
ShortCond1 = Ref(oppShort,-1);
ShortCond2 = Ref(oprShort,-1);
timeOut = TimeNum() == sqofftime;
Buy = BuyCond1 OR BuyCond2;
Short = ShortCond1 OR ShortCond2;
Sell = Cover = 0;
//ExRem(Buy, Sell);
//ExRem(Short, Cover);
//Levels
entryBuy = ValueWhen(Buy, Open);
entryShort = ValueWhen(Short, Open);
BuyStop = entryBuy - oppStop;
BuyTarget = entryBuy + target;
ShortStop = entryShort + oppStop;
ShortTarget = entryShort - target;
stopLine = IIf(isLong,BuyStop,IIf(isShort,ShortStop,Null));
targetLine = IIf(isLong,IIf(oppBuy,BuyTarget,VWAP),IIf(oppShort,ShortTarget,VWAP));
ApplyStop(stopTypeLoss, stopModePoint, stopLine, 1, False);
ApplyStop(stopTypeProfit, stopModePoint, targetLine, 1, False);
Equity(1,0);
//**************************************************************************************************************
//Plotting EMA
Plot(EMA(Close,200),"EMA",colorRed,styleThick);
//Plotting Donchian Channel
Plot(upper,"DCH upper",colorBlue,styleThick);
Plot(lower,"DCH lower",colorBlue,styleThick);
Plot(mid,"DCH mid",colorOrange,styleLine);
//Plotting VWAP
//Plot (VWAP,"VWAP",colorYellow, styleLine);
//Plotting levels and signals
buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);
Plot(IIf(buycontinue,BuyStop,Null),"BuyStop",colorRed,styleDashed);
Plot(IIf(buycontinue,BuyTarget,Null),"BuyTarget",colorGreen,styleDashed);
Plot(IIf(buycontinue,entryBuy,Null),"BuyEntry",colorYellow,styleDashed);
Plot(IIf(shortcontinue,ShortStop,Null),"ShortStop",colorRed,styleDashed);
Plot(IIf(shortcontinue,ShortTarget,Null),"ShortTarget",colorGreen,styleDashed);
Plot(IIf(shortcontinue,entryShort,Null),"ShortEntry",colorYellow,styleDashed);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0, H, Offset=-40);
_SECTION_END();
No luck and started to desperate
Then tried to wet my feet with flow control with IF-ELSE and made this attempt to assign each type of
signal their own stop and target (without trying the intermediate bar between main signals and sec trigger restriccion) I used this thread as base:
_SECTION_BEGIN("DCH WIP5");
SetTradeDelays(0,0,0,0);
SetOption("MaxOpenPositions", 1);
//**************************************************************************************************************
//Inputs controls
//**************************************************************************************************************
lenght = Param("DCH Lenght",20,1,100,1);
oppStop = Param("SL Pts",50,1,1000,1);
//oprStop = Param("OPR SL Ticks",200,1,1000,1);
target = Param("TP Pts",100,1,1000,1);
displayline = ParamToggle("Show Target & Stoploss","HIDE|SHOW",1);
//Intraday controls
starttime = ParamTime("signalStartTime","10:30");
endtime = ParamTime("signalEndTime","17:00");
sqofftime = ParamTime("SquareoffTime","17:05");
TickSize = 0.25;
//**************************************************************************************************************
//Indicadores
//**************************************************************************************************************
//Build Donchinal Channel
upper = HHV(High,lenght);
lower = LLV(Low,lenght);
mid = (upper + lower) / 2;
//**************************************************************************************************************
//VWAP
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = IIf (BarIndex() >= StartBar, Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
//**************************************************************************************************************
//Plot Candles
SetChartOptions(0,chartShowArrows | chartShowDates); //enable X - axis
Plot(Close,"Candles",colorDefault,styleCandle);
//**************************************************************************************************************
//Filters
inTime = TimeNum() >= starttime AND TimeNum() <= endtime;
isUP = Close > EMA(Close,200);
isDOWN = Close < EMA(Close,200);
//**************************************************************************************************************
//Condicion de entrada para OPP Longs
oppBuy = Close > Ref(upper,-1) AND isUP AND inTime;
oprBuy = Ref(Close,-1) > Ref(Open,-1) AND Close > Open AND isDOWN AND inTime;
//isLong = oppBuy OR oprBuy;
//**************************************************************************************************************
//Condicion de entrada para OPP Shorts
oppShort = Close < Ref(lower,-1) AND isDOWN AND inTime;
oprShort = Ref(Close,-1) < Ref(Open,-1) AND Close < Open AND isUP AND inTime;
//isShort = oppShort OR oprShort;
//**************************************************************************************************************
BuyCond1 = Ref(oppBuy,-1);
BuyCond2 = Ref(oprBuy,-1);
ShortCond1 = Ref(oppShort,-1);
ShortCond2 = Ref(oprShort,-1);
timeOut = TimeNum() == sqofftime;
Buy = BuyCond1 OR BuyCond2;
Short = ShortCond1 OR ShortCond2;
Sell = Cover = 0;
inTrade = False;
entryPrice = 0;
BuyStop = 0;
BuyTarget = 0;
ShortStop =0;
ShortTarget = 0;
isell = 0;
icover = 0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
for (i = 0; i < BarCount; i++)
{
if (inTrade)
{
if (BuyCond1[i])
{
BuyPrice[i] = Open[i];
entryPrice = BuyPrice [i];
BuyStop = entryPrice - oppStop;
BuyTarget = entryPrice + target;
//isell = Cross(BuyStop,Low) OR Cross(High,BuyTarget) OR TimeNum() == sqofftime;
if(BuyStop > Low[i])
Sell[i] = True;
SellPrice[i] = Low[i];
inTrade = False;
entryPrice = 0;
if(High[i] > BuyTarget)
Sell[i] = True;
SellPrice[i] = High[i];
inTrade = False;
entryPrice = 0;
}
else
{
if (BuyCond2[i])
BuyPrice[i] = Open[i];
entryPrice = BuyPrice [i];
BuyStop = entryPrice - oppStop;
BuyTarget = VWAP;
//isell = Cross(BuyStop,Low) OR Cross(High,BuyTarget) OR TimeNum() == sqofftime;
if(BuyStop > Low[i])
Sell[i] = True;
SellPrice[i] = Low[i];
inTrade = False;
entryPrice = 0;
if(High[i] > BuyTarget)
Sell[i] = True;
SellPrice[i] = High[i];
inTrade = False;
entryPrice = 0;
}
if (ShortCond1[i])
ShortPrice[i] = Open[i];
entryPrice = ShortPrice [i];
ShortStop = entryPrice + oppStop;
ShortTarget = entryPrice - target;
//icover = Cross(High,ShortStop) OR Cross(ShortTarget,Low) OR TimeNum() == sqofftime;
if(High[i] > ShortStop)
Sell[i] = True;
SellPrice[i] = High[i];
inTrade = False;
entryPrice = 0;
if(ShortTarget > Low[i])
Sell[i] = True;
SellPrice[i] = Low[i];
inTrade = False;
entryPrice = 0;
}
else
{
if (ShortCond2[i])
ShortPrice[i] = Open[i];
entryPrice = ShortPrice [i];
ShortStop = entryPrice + oppStop;
ShortTarget = VWAP;
icover = Cross(High,ShortStop) OR Cross(ShortTarget,Low) OR TimeNum() == sqofftime;
if(High[i] > ShortStop)
Sell[i] = True;
SellPrice[i] = High[i];
inTrade = False;
entryPrice = 0;
if(ShortTarget[i] > Low[i])
Sell[i] = True;
SellPrice[i] = Low[i];
inTrade = False;
entryPrice = 0;
}
if (timeOut[i])
{
Sell [i] = True;
Cover [i] = True;
SellPrice [i] = Close;
CoverPrice [i] = Close;
inTrade = False;
entryPrice = 0;
}
Buy [i] = 0;
Short [i] = 0;
if (Buy [i] OR Short [i])
{
inTrade = True;
}
}
//Remove excessive signals
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
/*
AddColumn(oppBuy,"oppBuy",1.0,colorDefault,colorDefault,-1,Null);
AddColumn(oppShort,"oppShort",1.0,colorDefault,colorDefault,-1,Null);
AddColumn(oprBuy,"oprBuy",1.0,colorDefault,colorDefault,-1,Null);
AddColumn(oprShort,"oprShort",1.0,colorDefault,colorDefault,-1,Null);
*/
//**************************************************************************************************************
//Plotting
//**************************************************************************************************************
//Plotting Donchian Channel
Plot(upper,"DCH upper",colorBlue,styleThick);
Plot(lower,"DCH lower",colorBlue,styleThick);
Plot(mid,"DCH mid",colorOrange,styleLine);
//Plotting VWAP
Plot (VWAP,"VWAP",colorYellow, styleLine);
Plot(EMA(Close,200),"EMA",colorRed,styleThick);
//Plotting StopLoss and Target
if(displayline)
{
buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);
Plot(IIf(buycontinue,BuyStop,Null),"BuyStop",colorRed,styleDashed);
Plot(IIf(buycontinue,BuyTarget,Null),"BuyTarget",colorGreen,styleDashed);
Plot(IIf(buycontinue,entryPrice,Null),"BuyEntry",colorYellow,styleDashed);
Plot(IIf(shortcontinue,ShortStop,Null),"ShortStop",colorRed,styleDashed);
Plot(IIf(shortcontinue,ShortTarget,Null),"ShortTarget",colorGreen,styleDashed);
Plot(IIf(shortcontinue,entryPrice,Null),"ShortEntry",colorYellow,styleDashed);
}
//Plotting Signals on chart
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
_SECTION_END();
This Doesn't work either. The first I noticed was it took several minutes to load the chart, may be my wonky code made it more difficult to calculate the whole thing. While coding I faced various errors produced by my wrong undertending using IF-ELSE statement, but also errors that I don't fully understand.
For ex why it flag an error in a sentence that in logic is exactly the same as a previous one and in that one I had no errors, obviously I missing something..
I must admint that other thing confuses me is read so many examples about IF-ELSE where some users writed very organized indented code while other doesn't indent at all or the statement is right next to the IF() condition.
Tell me if I need to document more about something also I can add tradingview images of this idea working for visual reference if anyone think it helps.
Thanks in advance!
















