Description
This indicator can be used to plot KillSessions zones on the graph and also to plot the Asia,CBDR,FLOUT zone with standard extension deviations.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class KillSessions : Indicator
{
[Parameter("# max lines", DefaultValue=10, MinValue=0)]
public int maxLines { get; set; }
[Parameter("Show NewYork", DefaultValue=true)]
public bool showNY { get; set; }
[Parameter("Show Londre", DefaultValue=true)]
public bool showLondre { get; set; }
[Parameter("Show Londre Close", DefaultValue=true)]
public bool showLondreC { get; set; }
[Parameter("Show Asia", DefaultValue=true)]
public bool showASIA { get; set; }
[Parameter("Show CBDR", DefaultValue = true)]
public bool showCBDR { get; set; }
[Parameter("CBDR take wicks", DefaultValue = true)]
public bool cbdrTakeWicks { get; set; }
[Parameter("Show FLOUT", DefaultValue = true)]
public bool showFLOUT { get; set; }
[Parameter("Font size", DefaultValue = 8)]
public int fontSize { get; set; }
[Parameter("Show OpenRange",DefaultValue = false)]
public bool showOpenRange { get; set;}
[Parameter("Line Thickness", DefaultValue = 2, Group = "Line")]
public int thickness { get; set; }
[Parameter("Style", Group = "Line", DefaultValue = LineStyle.Dots)]
public LineStyle lineStyle { get; set; }
[Parameter("Color", DefaultValue = "#84CD9C0D", Group = "Line")]
public Color lineColor { get; set; }
[Parameter("Show CBDR Std", DefaultValue=true, Group="CBDR,ASIA,FLOUT")]
public bool showCBDRStd { get; set; }
[Parameter("Show ASIA Std", DefaultValue=true, Group="CBDR,ASIA,FLOUT")]
public bool showASIAStd { get; set; }
[Parameter("Show FLOUT Std", DefaultValue=true, Group="CBDR,ASIA,FLOUT")]
public bool showFLOUTStd { get; set; }
[Parameter("Auto SD", DefaultValue = true, Group="CBDR,ASIA,FLOUT")]
public bool autoSD { get; set; }
[Parameter("SD Count", DefaultValue = 4, MinValue=0, Group="CBDR,ASIA,FLOUT")]
public int sdCount { get; set; }
[Parameter("Line Style", Group = "CBDR,ASIA,FLOUT", DefaultValue = LineStyle.DotsRare)]
public LineStyle lineSD { get; set; }
[Parameter("Color", DefaultValue = "#84CD9C0D", Group = "CBDR,ASIA,FLOUT")]
public Color lineColorSD { get; set; }
[Parameter("Line Thickness", DefaultValue = 1, Group = "CBDR,ASIA,FLOUT")]
public int thicknessSD { get; set; }
[Parameter("Max zones", DefaultValue = 1, Group = "CBDR,ASIA,FLOUT", MinValue = 1)]
public int maxZonesStd { get; set; }
[Parameter("Liste", Group = "Indices", DefaultValue = "NAS100,AUS200,E35,EUSTX50,FRA40,HK50,JPN225,UK100,US30,US500,NETH25,CHINAH,CA60,GER40,US2000")]
public string indicesList { get; set; }
[Output("Main")]
public IndicatorDataSeries Result { get; set; }
private Bars minute;
private bool isIndice = false;
protected override void Initialize()
{
if (indicesList.Contains(Symbol.Name.Split(".")[0])) isIndice = true;
if (Symbol.Name.StartsWith("X")) isIndice = true;
minute = MarketData.GetBars(TimeFrame.Minute);
}
public enum CurrentAuto {
CBDR,
ASIA,
FLOUT,
none
}
public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
return (input >= date1 && input < date2);
}
private int lastIndex=-1, startNy=-1, startASIA=-1, startLondre=-1, startLondreC=-1, cptASIA=0, cptNy=-1, cptLondre=-1, cptLondreC=-1, cptOpRan;
private int startCBDR=-1, cptCBDR=-1, startFLOUT=-1, cptFLOUT=-1;
private double maxNy=-1, minNy=-1, maxASIA=-1, minASIA=-1, maxLondre=-1, minLondre=-1, maxLondreC=-1, minLondreC=-1, maxNYOpen=-1, minNYOpen=-1;
private double maxCBDR=-1, minCBDR=-1, maxFLOUT=-1, minFLOUT=-1;
private int lastDay = -1, idxNYInd9h30=-1;
public override void Calculate(int index)
{
if (lastIndex == index || index < 2) return;
lastIndex = index;
index -= 1; // bougie d'avant qui est terminée
var barLocalDateTime = Bars[index].OpenTime.Add(Application.UserTimeOffset);
var day = barLocalDateTime.Day;
if (lastDay == -1 || Math.Abs(lastDay - day)>1) // weekend
{
lastDay = day;
}
day %= maxZonesStd;
// Open Range pour les indices
if (showOpenRange && isIndice && showNY && (TimeFrame < TimeFrame.Hour || TimeFrame < TimeFrame.HeikinHour))
{
if (idxNYInd9h30==-1 && Between(new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,9,30,0), barLocalDateTime, Bars[index+1].OpenTime.Add(Application.UserTimeOffset)))
{
idxNYInd9h30 = index;
maxNYOpen=-1;
minNYOpen=-1;
cptOpRan++;
if (maxLines != 0) cptOpRan %= maxLines;
}
if (idxNYInd9h30 != -1)
{
if (maxNYOpen==-1 || maxNYOpen < Bars[index].High) maxNYOpen = Bars[index].High;
if (minNYOpen==-1 || minNYOpen > Bars[index].Low) minNYOpen = Bars[index].Low;
var lbl = Chart.DrawText("Txt_ind9h30_"+cptOpRan, "Open Range", idxNYInd9h30, maxNYOpen, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine("maxind9h30_"+cptOpRan, idxNYInd9h30, maxNYOpen, index, maxNYOpen, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("minind9h30_"+cptOpRan, idxNYInd9h30, minNYOpen, index, minNYOpen, lineColor, thickness, lineStyle);
// verticales
Chart.DrawTrendLine("leftind9h301_"+cptOpRan, idxNYInd9h30, maxNYOpen, idxNYInd9h30, Bars[idxNYInd9h30].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("leftind9h302_"+cptOpRan, idxNYInd9h30, Bars[idxNYInd9h30].Low, idxNYInd9h30, minNYOpen, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightind9h301_"+cptOpRan, index, maxNYOpen, index, Bars[index].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightind9h302_"+cptOpRan, index, Bars[index].Low, index, minNYOpen, lineColor, thickness, lineStyle);
if (Between(new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,11,0,0), Bars[index+1].OpenTime.Add(Application.UserTimeOffset),Bars[index+2].OpenTime.Add(Application.UserTimeOffset)))
{
idxNYInd9h30 = -1;
}
}
}
// NY AM
if (showNY && (!isIndice && Between(barLocalDateTime, new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,7,30,0), new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,10,0,0)) && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2) ||
isIndice && Between(barLocalDateTime, new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,8,30,0), new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,11,0,0)) && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2)))
{
if (maxNy==-1 || maxNy < Bars[index].High) maxNy = Bars[index].High;
if (minNy==-1 || minNy > Bars[index].Low) minNy = Bars[index].Low;
if (startNy == -1)
{
startNy = index;
cptNy++;
if (maxLines != 0) cptNy %= maxLines;
}
var lbl = Chart.DrawText("Txt_NyAM_" + cptNy, "NY AM", startNy, maxNy, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine("maxNyAM_" + cptNy, startNy, maxNy, index, maxNy, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("minNyAM_" + cptNy, startNy, minNy, index, minNy, lineColor, thickness, lineStyle);
// verticales
Chart.DrawTrendLine("leftNyAM1_" + cptNy, startNy, maxNy, startNy, Bars[startNy].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("leftNyAM2_" + cptNy, startNy, Bars[startNy].Low, startNy, minNy, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightNyAM1_" + cptNy, index, maxNy, index, Bars[index].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightNyAM2_" + cptNy, index, Bars[index].Low, index, minNy, lineColor, thickness, lineStyle);
}
else
// NY PM
if (isIndice && showNY && Between(barLocalDateTime, new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,13,30,0), new DateTime(barLocalDateTime.Year,barLocalDateTime.Month,barLocalDateTime.Day,16,0,0)) && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (maxNy==-1 || maxNy < Bars[index].High) maxNy = Bars[index].High;
if (minNy==-1 || minNy > Bars[index].Low) minNy = Bars[index].Low;
if (startNy == -1)
{
startNy = index;
cptNy++;
if (maxLines != 0) cptNy %= maxLines;
}
var lbl = Chart.DrawText("Txt_NyPM_" + cptNy, "NY PM", startNy, maxNy, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine("maxNyPM_" + cptNy, startNy, maxNy, index, maxNy, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("minNyPM_" + cptNy, startNy, minNy, index, minNy, lineColor, thickness, lineStyle);
// verticales
Chart.DrawTrendLine("leftNyPM1_" + cptNy, startNy, maxNy, startNy, Bars[startNy].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("leftNyPM2_" + cptNy, startNy, Bars[startNy].Low, startNy, minNy, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightNyPM1_" + cptNy, index, maxNy, index, Bars[index].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightNyPM2_" + cptNy, index, Bars[index].Low, index, minNy, lineColor, thickness, lineStyle);
}
else{
maxNy = -1;
minNy = -1;
startNy = -1;
}
if (barLocalDateTime.Hour >= 16 && barLocalDateTime.Hour <= 24-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
// CBDR
if (!isIndice && barLocalDateTime.Hour >= 16 && barLocalDateTime.Hour <= 21-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (cbdrTakeWicks)
if (maxCBDR==-1 || maxCBDR < Bars[index].High) maxCBDR = Bars[index].High;
if (minCBDR==-1 || minCBDR > Bars[index].Low) minCBDR = Bars[index].Low;
else
{
if (maxCBDR==-1 || maxCBDR < Math.Max(Bars[index].Open,Bars[index].Close)) maxCBDR = Math.Max(Bars[index].Open,Bars[index].Close);
if (minCBDR==-1 || minCBDR > Math.Min(Bars[index].Open,Bars[index].Close)) minCBDR = Math.Min(Bars[index].Open,Bars[index].Close);
}
if (startCBDR == -1)
{
startCBDR = index;
cptCBDR++;
if (maxLines != 0) cptCBDR %= maxZonesStd;
}
}
// FLOUT
if (!isIndice && barLocalDateTime.Hour >= 16 && barLocalDateTime.Hour <= 24-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (maxFLOUT==-1 || maxFLOUT < Bars[index].High) maxFLOUT = Bars[index].High;
if (minFLOUT==-1 || minFLOUT > Bars[index].Low) minFLOUT = Bars[index].Low;
if (startFLOUT == -1)
{
startFLOUT = index;
cptFLOUT++;
if (maxLines != 0) cptFLOUT %= maxZonesStd;
}
}
// Asie
if (barLocalDateTime.Hour >= 20 && barLocalDateTime.Hour <= 24-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (maxASIA==-1 || maxASIA < Bars[index].High) maxASIA = Bars[index].High;
if (minASIA == -1 || minASIA > Bars[index].Low) minASIA = Bars[index].Low;
if (startASIA == -1)
{
startASIA = index;
cptASIA++;
if (maxLines != 0) cptASIA %= maxZonesStd;
}
}
}
else
{
startASIA = -1;
startCBDR = -1;
startFLOUT = -1;
maxASIA=-1;
minASIA=-1;
maxCBDR=-1;
minCBDR=-1;
maxFLOUT=-1;
minFLOUT=-1;
}
// auto SD
var _currentAuto = CurrentAuto.none;
if (autoSD && !isIndice) {
var factor = Symbol.TickSize*10;
if (Symbol.Digits == 2)
{
factor = Symbol.PipSize*10;
}
if (maxCBDR-minCBDR > 15*factor && maxCBDR-minCBDR <= 30*factor) // pas plus que 40, pas de meche, idéalement 20 à 30
_currentAuto = CurrentAuto.CBDR;
else
if (maxASIA-minASIA >= 20*factor && maxASIA-minASIA <= 30*factor) // doit être en 20 et 30
_currentAuto = CurrentAuto.ASIA;
else
if (maxCBDR-minCBDR > 15*factor && maxCBDR-minCBDR <= 40*factor) // pas plus que 40, pas de meche, idéalement 20 à 30
_currentAuto = CurrentAuto.CBDR;
else
_currentAuto = CurrentAuto.FLOUT;
}
if (startCBDR >= 0 && (_currentAuto == CurrentAuto.none && showCBDR || _currentAuto == CurrentAuto.CBDR) && barLocalDateTime.Hour <= 21-1)
{
var id = autoSD ? "AUTOSD"+day : "CBDR"+cptCBDR;
var lbl = Chart.DrawText(id, "CBDR", startCBDR, maxCBDR, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine(id+"up", startCBDR, maxCBDR, index, maxCBDR, lineColor, thickness, lineStyle);
Chart.DrawTrendLine(id+"down", startCBDR, minCBDR, index, minCBDR, lineColor, thickness, lineStyle);
if (showCBDRStd)
{
var diff = maxCBDR - minCBDR;
for (int i=1; i<= sdCount; i++)
{
Chart.DrawTrendLine(id+"up"+i, startCBDR, maxCBDR+i*diff, index, maxCBDR+i*diff, lineColorSD, thicknessSD, lineSD);
Chart.DrawTrendLine(id+"down"+i, startCBDR, minCBDR-i*diff, index, minCBDR-i*diff, lineColorSD, thicknessSD, lineSD);
}
}
}
if (startASIA >= 0 && (showASIA || _currentAuto == CurrentAuto.ASIA))
{
var id = _currentAuto == CurrentAuto.ASIA? "AUTOSD"+day: "ASIA"+cptFLOUT;
if (isIndice) id="ASIA"+startASIA;
var lbl = Chart.DrawText(id, "Asie", startASIA, maxASIA, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine(id+"up", startASIA, maxASIA, index, maxASIA, lineColor, thickness, lineStyle);
Chart.DrawTrendLine(id+"down", startASIA, minASIA, index, minASIA, lineColor, thickness, lineStyle);
if (true)
{
Chart.DrawTrendLine(id+"upleft", startASIA, maxASIA, startASIA, minASIA, lineColor, thickness, lineStyle);
Chart.DrawTrendLine(id+"upright", index, maxASIA, index, minASIA, lineColor, thickness, lineStyle);
}
if (!isIndice && showASIAStd && (_currentAuto == CurrentAuto.ASIA || _currentAuto == CurrentAuto.none))
{
var diff = maxASIA - minASIA;
for (int i=1; i<= sdCount; i++)
{
Chart.DrawTrendLine(id+"up"+i, startASIA, maxASIA+i*diff, index, maxASIA+i*diff, lineColorSD, thicknessSD, lineSD);
Chart.DrawTrendLine(id+"down"+i, startASIA, minASIA-i*diff, index, minASIA-i*diff, lineColorSD, thicknessSD, lineSD);
}
}
}
if (startFLOUT >= 0 && (_currentAuto == CurrentAuto.none && showFLOUT || _currentAuto == CurrentAuto.FLOUT))
{
var id = autoSD ? "AUTOSD"+day : "FLOUT"+cptFLOUT;
var lbl = Chart.DrawText(id, "FLOUT ", startFLOUT, maxFLOUT, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine(id+"up", startFLOUT, maxFLOUT, index, maxFLOUT, lineColor, thickness, lineStyle);
Chart.DrawTrendLine(id+"down", startFLOUT, minFLOUT, index, minFLOUT, lineColor, thickness, lineStyle);
if (showFLOUTStd)
{
var diff = maxFLOUT - minFLOUT;
for (int i=1; i<= sdCount; i++)
{
Chart.DrawTrendLine(id+"up"+i, startFLOUT, maxFLOUT+0.5*i*diff, index, maxFLOUT+0.5*i*diff, lineColorSD, thicknessSD, lineSD);
Chart.DrawTrendLine(id+"down"+i, startFLOUT, minFLOUT-0.5*i*diff, index, minFLOUT-0.5*i*diff, lineColorSD, thicknessSD, lineSD);
}
}
}
if (showLondre && barLocalDateTime.Hour >= 2 && barLocalDateTime.Hour <= 5-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (maxLondre==-1 || maxLondre < Bars[index].High) maxLondre = Bars[index].High;
if (minLondre==-1 || minLondre > Bars[index].Low) minLondre = Bars[index].Low;
if (startLondre == -1)
{
startLondre = index;
cptLondre++;
if (maxLines != 0) cptLondre %= maxLines;
}
var lbl = Chart.DrawText("Txt_Londre_" + cptLondre, isIndice?"Londre":"Lon Op", startLondre, maxLondre, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine("maxLondre" + cptLondre, startLondre, maxLondre, index, maxLondre, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("minLondre" + cptLondre, startLondre, minLondre, index, minLondre, lineColor, thickness, lineStyle);
// lignes verticales
Chart.DrawTrendLine("leftLondre1" + cptLondre, startLondre, maxLondre, startLondre, Bars[startLondre].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("leftLondre2" + cptLondre, startLondre, Bars[startLondre].Low, startLondre, minLondre, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightLondre1" + cptLondre, index, maxLondre, index, Bars[index].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightLondre2" + cptLondre, index, Bars[index].Low, index, minLondre, lineColor, thickness, lineStyle);
}
else{
maxLondre = -1;
minLondre = -1;
startLondre = -1;
}
if (!isIndice && showLondreC && barLocalDateTime.Hour >= 10 && barLocalDateTime.Hour <= 12-1 && (TimeFrame <= TimeFrame.Hour2 || TimeFrame <= TimeFrame.HeikinHour2))
{
if (maxLondreC==-1 || maxLondreC < Bars[index].High) maxLondreC = Bars[index].High;
if (minLondreC==-1 || minLondreC > Bars[index].Low) minLondreC = Bars[index].Low;
if (startLondreC == -1)
{
startLondreC = index;
cptLondreC++;
if (maxLines != 0) cptLondreC %= maxLines;
}
var lbl = Chart.DrawText("Txt_LondreC_" + cptLondreC, "Lon Cl", startLondreC, maxLondreC, lineColor);
lbl.FontSize = fontSize;
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.HorizontalAlignment = HorizontalAlignment.Right;
Chart.DrawTrendLine("maxLondreC" + cptLondreC, startLondreC, maxLondreC, index, maxLondreC, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("minLondreC" + cptLondreC, startLondreC, minLondreC, index, minLondreC, lineColor, thickness, lineStyle);
// verticales
Chart.DrawTrendLine("leftLondreC1" + cptLondreC, startLondreC, maxLondreC, startLondreC, Bars[startLondreC].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("leftLondreC2" + cptLondreC, startLondreC, Bars[startLondreC].Low, startLondreC, minLondreC, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightLondreC1" + cptLondreC, index, maxLondreC, index, Bars[index].High, lineColor, thickness, lineStyle);
Chart.DrawTrendLine("rightLondreC2" + cptLondreC, index, Bars[index].Low, index, minLondreC, lineColor, thickness, lineStyle);
}
else{
maxLondreC = -1;
minLondreC = -1;
startLondreC = -1;
}
}
}
}
LB
lbellego
Joined on 20.06.2024
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: Luc KillSessions_withSourceCode.algo
- Rating: 5
- Installs: 720
- Modified: 22/07/2024 15:24
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
good thanks