Keltner v2 by slipshod
46630 downloads / 4182 views / Created: 23.05.2013




Average Rating: 0
Indicator Description
Keltner Channels with extended features/limits over the bundled version:-
- Price can be any of the usual types, not just Close
- Multiplier is a floating point number, minimum set at 0.1
- Limits set at 400 for the MA Period (up from 200), and 200 for the ATR period (up from 50).
- Price can be any of the usual types, not just Close
- Multiplier is a floating point number, minimum set at 0.1
- Limits set at 400 for the MA Period (up from 200), and 200 for the ATR period (up from 50).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
//==============================================================
// Forex Strategy Builder
// Copyright � Miroslav Popov. All rights reserved.
//==============================================================
// THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
//==============================================================
using System;
using System.Drawing;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Store
{
public class Keltnerv2 : Indicator
{
public Keltnerv2()
{
IndicatorName = "Keltner v2";
PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close | SlotTypes.CloseFilter;
IndicatorAuthor = "Miroslav Popov, Andrew Sumner";
IndicatorVersion = "2.1";
IndicatorDescription = "Extended limits & features from bundled version.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
if (SlotType == SlotTypes.Open)
IndParam.ListParam[0].ItemList = new[]
{
"Enter long at Upper Band",
"Enter long at Lower Band"
};
else if (SlotType == SlotTypes.OpenFilter)
IndParam.ListParam[0].ItemList = new[]
{
"The bar opens below Upper Band",
"The bar opens above Upper Band",
"The bar opens below Lower Band",
"The bar opens above Lower Band",
"The position opens below Upper Band",
"The position opens above Upper Band",
"The position opens below Lower Band",
"The position opens above Lower Band",
"The bar opens below Upper Band after opening above it",
"The bar opens above Upper Band after opening below it",
"The bar opens below Lower Band after opening above it",
"The bar opens above Lower Band after opening below it"
};
else if (SlotType == SlotTypes.Close)
IndParam.ListParam[0].ItemList = new[]
{
"Exit long at Upper Band",
"Exit long at Lower Band"
};
else if (SlotType == SlotTypes.CloseFilter)
IndParam.ListParam[0].ItemList = new[]
{
"The bar closes below Upper Band",
"The bar closes above Upper Band",
"The bar closes below Lower Band",
"The bar closes above Lower Band"
};
else
IndParam.ListParam[0].ItemList = new[]
{
"Not Defined"
};
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Logic of application of the indicator.";
IndParam.ListParam[1].Caption = "Smoothing method";
IndParam.ListParam[1].ItemList = Enum.GetNames(typeof (MAMethod));
IndParam.ListParam[1].Index = (int) MAMethod.Exponential;
IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "The method of smoothing of central Moving Average.";
IndParam.ListParam[2].Caption = "Base price";
IndParam.ListParam[2].ItemList = Enum.GetNames(typeof (BasePrice));
IndParam.ListParam[2].Index = (int) BasePrice.Close;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "The price the central Moving Average is based on.";
// The NumericUpDown parameters
IndParam.NumParam[0].Caption = "MA period";
IndParam.NumParam[0].Value = 20;
IndParam.NumParam[0].Min = 1;
IndParam.NumParam[0].Max = 400;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "The central Moving Average period.";
IndParam.NumParam[1].Caption = "ATR period";
IndParam.NumParam[1].Value = 10;
IndParam.NumParam[1].Min = 1;
IndParam.NumParam[1].Max = 200;
IndParam.NumParam[1].Point = 0;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "Average True Range Period.";
IndParam.NumParam[3].Caption = "ATR multiplier";
IndParam.NumParam[3].Value = 2;
IndParam.NumParam[3].Min = 0.1;
IndParam.NumParam[3].Max = 10;
IndParam.NumParam[3].Point = 1;
IndParam.NumParam[3].Enabled = true;
IndParam.NumParam[3].ToolTip = "Average True Range Multiplier.";
// The CheckBox parameters
IndParam.CheckParam[0].Caption = "Use previous bar value";
IndParam.CheckParam[0].Enabled = true;
IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
var maMethod = (MAMethod) IndParam.ListParam[1].Index;
var price = (BasePrice) IndParam.ListParam[2].Index;
var nMA = (int) IndParam.NumParam[0].Value;
var atrPeriod = (int) IndParam.NumParam[1].Value;
var atrMultiplier = (double) IndParam.NumParam[3].Value;
int previous = IndParam.CheckParam[0].Checked ? 1 : 0;
// Calculation
double[] adMA = MovingAverage(nMA, 0, maMethod, Price(price));
var adAtr = new double[Bars];
var adUpBand = new double[Bars];
var adDnBand = new double[Bars];
int firstBar = Math.Max(nMA, atrPeriod) + previous + 2;
for (int iBar = 2; iBar < Bars; iBar++)
{
adAtr[iBar] = Math.Max(Math.Abs(High[iBar-1] - Close[iBar - 2]), Math.Abs(Close[iBar - 2] - Low[iBar-1]));
adAtr[iBar] = Math.Max(Math.Abs(High[iBar-1] - Low[iBar-1]), adAtr[iBar]);
}
adAtr = MovingAverage(atrPeriod, 0, maMethod, adAtr);
for (int iBar = nMA; iBar < Bars; iBar++)
{
adUpBand[iBar] = adMA[iBar] + adAtr[iBar]*atrMultiplier;
adDnBand[iBar] = adMA[iBar] - adAtr[iBar]*atrMultiplier;
}
// Saving the components
Component = new IndicatorComp[5];
Component[0] = new IndicatorComp
{
CompName = "Upper Band",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Blue,
FirstBar = firstBar,
Value = adUpBand
};
Component[1] = new IndicatorComp
{
CompName = "Moving Average",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Gold,
FirstBar = firstBar,
Value = adMA
};
Component[2] = new IndicatorComp
{
CompName = "Lower Band",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Blue,
FirstBar = firstBar,
Value = adDnBand
};
Component[3] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
Component[4] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
// Sets the Component's type
if (SlotType == SlotTypes.Open)
{
Component[3].DataType = IndComponentType.OpenLongPrice;
Component[3].CompName = "Long position entry price";
Component[4].DataType = IndComponentType.OpenShortPrice;
Component[4].CompName = "Short position entry price";
}
else if (SlotType == SlotTypes.OpenFilter)
{
Component[3].DataType = IndComponentType.AllowOpenLong;
Component[3].CompName = "Is long entry allowed";
Component[4].DataType = IndComponentType.AllowOpenShort;
Component[4].CompName = "Is short entry allowed";
}
else if (SlotType == SlotTypes.Close)
{
Component[3].DataType = IndComponentType.CloseLongPrice;
Component[3].CompName = "Long position closing price";
Component[4].DataType = IndComponentType.CloseShortPrice;
Component[4].CompName = "Short position closing price";
}
else if (SlotType == SlotTypes.CloseFilter)
{
Component[3].DataType = IndComponentType.ForceCloseLong;
Component[3].CompName = "Close out long position";
Component[4].DataType = IndComponentType.ForceCloseShort;
Component[4].CompName = "Close out short position";
}
if (SlotType == SlotTypes.Open || SlotType == SlotTypes.Close)
{
if (nMA > 1)
{
for (int iBar = firstBar; iBar < Bars; iBar++)
{
// Covers the cases when the price can pass through the band without a signal.
double dOpen = Open[iBar]; // Current open price
// Upper band
double dValueUp = adUpBand[iBar - previous]; // Current value
double dValueUp1 = adUpBand[iBar - previous - 1]; // Previous value
double dTempValUp = dValueUp;
if ((dValueUp1 > High[iBar - 1] && dValueUp < dOpen) ||
// The Open price jumps above the indicator
(dValueUp1 < Low[iBar - 1] && dValueUp > dOpen) ||
// The Open price jumps below the indicator
(Close[iBar - 1] < dValueUp && dValueUp < dOpen) || // The Open price is in a positive gap
(Close[iBar - 1] > dValueUp && dValueUp > dOpen)) // The Open price is in a negative gap
dTempValUp = dOpen; // The entry/exit level is moved to Open price
// Lower band
double dValueDown = adDnBand[iBar - previous]; // Current value
double dValueDown1 = adDnBand[iBar - previous - 1]; // Previous value
double dTempValDown = dValueDown;
if ((dValueDown1 > High[iBar - 1] && dValueDown < dOpen) ||
// The Open price jumps above the indicator
(dValueDown1 < Low[iBar - 1] && dValueDown > dOpen) ||
// The Open price jumps below the indicator
(Close[iBar - 1] < dValueDown && dValueDown < dOpen) ||
// The Open price is in a positive gap
(Close[iBar - 1] > dValueDown && dValueDown > dOpen)) // The Open price is in a negative gap
dTempValDown = dOpen; // The entry/exit level is moved to Open price
if (IndParam.ListParam[0].Text == "Enter long at Upper Band" ||
IndParam.ListParam[0].Text == "Exit long at Upper Band")
{
Component[3].Value[iBar] = dTempValUp;
Component[4].Value[iBar] = dTempValDown;
}
else
{
Component[3].Value[iBar] = dTempValDown;
Component[4].Value[iBar] = dTempValUp;
}
}
}
else
{
for (int iBar = 2; iBar < Bars; iBar++)
{
if (IndParam.ListParam[0].Text == "Enter long at Upper Band" ||
IndParam.ListParam[0].Text == "Exit long at Upper Band")
{
Component[3].Value[iBar] = adUpBand[iBar - previous];
Component[4].Value[iBar] = adDnBand[iBar - previous];
}
else
{
Component[3].Value[iBar] = adDnBand[iBar - previous];
Component[4].Value[iBar] = adUpBand[iBar - previous];
}
}
}
}
else
{
switch (IndParam.ListParam[0].Text)
{
case "The bar opens below Upper Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_below_the_Upper_Band);
break;
case "The bar opens above Upper Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_above_the_Upper_Band);
break;
case "The bar opens below Lower Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_below_the_Lower_Band);
break;
case "The bar opens above Lower Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_above_the_Lower_Band);
break;
case "The bar opens below Upper Band after opening above it":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_below_the_Upper_Band_after_opening_above_it);
break;
case "The bar opens above Upper Band after opening below it":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_above_the_Upper_Band_after_opening_below_it);
break;
case "The bar opens below Lower Band after opening above it":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_below_the_Lower_Band_after_opening_above_it);
break;
case "The bar opens above Lower Band after opening below it":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_opens_above_the_Lower_Band_after_opening_below_it);
break;
case "The position opens above Upper Band":
Component[0].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[2].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[0].UsePreviousBar = previous;
Component[2].UsePreviousBar = previous;
Component[3].DataType = IndComponentType.Other;
Component[4].DataType = IndComponentType.Other;
Component[3].ShowInDynInfo = false;
Component[4].ShowInDynInfo = false;
break;
case "The position opens below Upper Band":
Component[0].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
Component[2].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
Component[0].UsePreviousBar = previous;
Component[2].UsePreviousBar = previous;
Component[3].DataType = IndComponentType.Other;
Component[4].DataType = IndComponentType.Other;
Component[3].ShowInDynInfo = false;
Component[4].ShowInDynInfo = false;
break;
case "The position opens above Lower Band":
Component[0].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[0].UsePreviousBar = previous;
Component[2].UsePreviousBar = previous;
Component[3].DataType = IndComponentType.Other;
Component[4].DataType = IndComponentType.Other;
Component[3].ShowInDynInfo = false;
Component[4].ShowInDynInfo = false;
break;
case "The position opens below Lower Band":
Component[0].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
Component[0].UsePreviousBar = previous;
Component[2].UsePreviousBar = previous;
Component[3].DataType = IndComponentType.Other;
Component[4].DataType = IndComponentType.Other;
Component[3].ShowInDynInfo = false;
Component[4].ShowInDynInfo = false;
break;
case "The bar closes below Upper Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_closes_below_the_Upper_Band);
break;
case "The bar closes above Upper Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_closes_above_the_Upper_Band);
break;
case "The bar closes below Lower Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_closes_below_the_Lower_Band);
break;
case "The bar closes above Lower Band":
BandIndicatorLogic(firstBar, previous, adUpBand, adDnBand, ref Component[3], ref Component[4],
BandIndLogic.The_bar_closes_above_the_Lower_Band);
break;
}
}
}
public override void SetDescription()
{
switch (IndParam.ListParam[0].Text)
{
case "Enter long at Upper Band":
EntryPointLongDescription = "at Upper Band of " + ToString();
EntryPointShortDescription = "at Lower Band of " + ToString();
break;
case "Enter long at Lower Band":
EntryPointLongDescription = "at Lower Band of " + ToString();
EntryPointShortDescription = "at Upper Band of " + ToString();
break;
case "Exit long at Upper Band":
ExitPointLongDescription = "at Upper Band of " + ToString();
ExitPointShortDescription = "at Lower Band of " + ToString();
break;
case "Exit long at Lower Band":
ExitPointLongDescription = "at Lower Band of " + ToString();
ExitPointShortDescription = "at Upper Band of " + ToString();
break;
case "The bar opens below Upper Band":
EntryFilterLongDescription = "the bar opens below Upper Band of " + ToString();
EntryFilterShortDescription = "the bar opens above Lower Band of " + ToString();
break;
case "The bar opens above Upper Band":
EntryFilterLongDescription = "the bar opens above Upper Band of " + ToString();
EntryFilterShortDescription = "the bar opens below Lower Band of " + ToString();
break;
case "The bar opens below Lower Band":
EntryFilterLongDescription = "the bar opens below Lower Band of " + ToString();
EntryFilterShortDescription = "the bar opens above Upper Band of " + ToString();
break;
case "The bar opens above Lower Band":
EntryFilterLongDescription = "the bar opens above Lower Band of " + ToString();
EntryFilterShortDescription = "the bar opens below Upper Band of " + ToString();
break;
case "The position opens above Upper Band":
EntryFilterLongDescription = "the position opening price is higher than Upper Band of " + ToString();
EntryFilterShortDescription = "the position opening price is lower than Lower Band of " + ToString();
break;
case "The position opens below Upper Band":
EntryFilterLongDescription = "the position opening price is lower than Upper Band of " + ToString();
EntryFilterShortDescription = "the position opening price is higher than Lower Band of " +
ToString();
break;
case "The position opens above Lower Band":
EntryFilterLongDescription = "the position opening price is higher than Lower Band of " + ToString();
EntryFilterShortDescription = "the position opening price is lower than Upper Band of " + ToString();
break;
case "The position opens below Lower Band":
EntryFilterLongDescription = "the position opening price is lower than Lower Band of " + ToString();
EntryFilterShortDescription = "the position opening price is higher than Upper Band of " +
ToString();
break;
case "The bar opens below Upper Band after opening above it":
EntryFilterLongDescription = "the bar opens below Upper Band of " + ToString() +
" after the previous bar has opened above it";
EntryFilterShortDescription = "the bar opens above Lower Band of " + ToString() +
" after the previous bar has opened below it";
break;
case "The bar opens above Upper Band after opening below it":
EntryFilterLongDescription = "the bar opens above Upper Band of " + ToString() +
" after the previous bar has opened below it";
EntryFilterShortDescription = "the bar opens below Lower Band of " + ToString() +
" after the previous bar has opened above it";
break;
case "The bar opens below Lower Band after opening above it":
EntryFilterLongDescription = "the bar opens below Lower Band of " + ToString() +
" after the previous bar has opened above it";
EntryFilterShortDescription = "the bar opens above Upper Band of " + ToString() +
" after the previous bar has opened below it";
break;
case "The bar opens above Lower Band after opening below it":
EntryFilterLongDescription = "the bar opens above Lower Band of " + ToString() +
" after the previous bar has opened below it";
EntryFilterShortDescription = "the bar opens below Upper Band of " + ToString() +
" after the previous bar has opened above it";
break;
case "The bar closes below Upper Band":
ExitFilterLongDescription = "the bar closes below Upper Band of " + ToString();
ExitFilterShortDescription = "the bar closes above Lower Band of " + ToString();
break;
case "The bar closes above Upper Band":
ExitFilterLongDescription = "the bar closes above Upper Band of " + ToString();
ExitFilterShortDescription = "the bar closes below Lower Band of " + ToString();
break;
case "The bar closes below Lower Band":
ExitFilterLongDescription = "the bar closes below Lower Band of " + ToString();
ExitFilterShortDescription = "the bar closes above Upper Band of " + ToString();
break;
case "The bar closes above Lower Band":
ExitFilterLongDescription = "the bar closes above Lower Band of " + ToString();
ExitFilterShortDescription = "the bar closes below Upper Band of " + ToString();
break;
}
}
public override string ToString()
{
return IndicatorName +
(IndParam.CheckParam[0].Checked ? "* (" : " (") +
IndParam.ListParam[1].Text + ", " + // Method
IndParam.ListParam[2].Text + ", " + // Price
IndParam.NumParam[0].ValueToString + ", " + // MA period
IndParam.NumParam[1].ValueToString + ", " + // ATR Period
IndParam.NumParam[3].ValueToString + ")"; // ATR Multiplier
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274//+--------------------------------------------------------------------+ //| Copyright: (C) 2016 Forex Software Ltd. | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| other applications without a permission. | //| The contact information cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright (C) 2016 Forex Software Ltd." #property link "http://forexsb.com" #property version "2.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class Keltnerv2 : public Indicator { public: Keltnerv2(SlotTypes slotType) { SlotType=slotType; IndicatorName="Keltner v2"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Keltnerv2::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); // Reading the parameters MAMethod maMethod=(MAMethod) ListParam[1].Index; BasePrice basePrice=(BasePrice) ListParam[2].Index; int nMA=(int) NumParam[0].Value; int atrPeriod=(int) NumParam[1].Value; double atrMultiplier= NumParam[3].Value; int previous=CheckParam[0].Checked ? 1 : 0; // Calculation double price[]; Price(basePrice,price); double adMA[]; MovingAverage(nMA,0,maMethod,price,adMA); double adAtr[]; ArrayResize(adAtr,Data.Bars); ArrayInitialize(adAtr,0); double adUpBand[]; ArrayResize(adUpBand,Data.Bars); ArrayInitialize(adUpBand,0); double adDnBand[]; ArrayResize(adDnBand,Data.Bars); ArrayInitialize(adDnBand,0); int firstBar=MathMax(nMA,atrPeriod)+previous+2; for(int iBar=2; iBar<Data.Bars; iBar++) { adAtr[iBar] = MathMax(MathAbs(Data.High[iBar-1] - Data.Close[iBar - 2]), MathAbs(Data.Close[iBar - 2] - Data.Low[iBar-1])); adAtr[iBar] = MathMax(MathAbs(Data.High[iBar-1] - Data.Low[iBar-1]), adAtr[iBar]); } double maAtr[]; ArrayResize(maAtr,Data.Bars); ArrayInitialize(maAtr,0); MovingAverage(atrPeriod,0,maMethod,adAtr,maAtr); for(int iBar=nMA; iBar<Data.Bars; iBar++) { adUpBand[iBar] = adMA[iBar] + maAtr[iBar]*atrMultiplier; adDnBand[iBar] = adMA[iBar] - maAtr[iBar]*atrMultiplier; } // Saving the components ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Upper Band"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = firstBar; ArrayCopy(Component[0].Value,adUpBand); ArrayResize(Component[1].Value,Data.Bars); Component[1].CompName = "Moving Average"; Component[1].DataType = IndComponentType_IndicatorValue; Component[1].FirstBar = firstBar; ArrayCopy(Component[1].Value,adMA); ArrayResize(Component[2].Value,Data.Bars); Component[2].CompName = "Lower Band"; Component[2].DataType = IndComponentType_IndicatorValue; Component[2].FirstBar = firstBar; ArrayCopy(Component[2].Value,adDnBand); ArrayResize(Component[3].Value,Data.Bars); ArrayInitialize(Component[3].Value,0); Component[3].FirstBar=firstBar; ArrayResize(Component[4].Value,Data.Bars); ArrayInitialize(Component[4].Value,0); Component[4].FirstBar=firstBar; // Sets the Component's type if(SlotType==SlotTypes_Open) { Component[3].DataType = IndComponentType_OpenLongPrice; Component[3].CompName = "Long position entry price"; Component[4].DataType = IndComponentType_OpenShortPrice; Component[4].CompName = "Short position entry price"; } else if(SlotType==SlotTypes_OpenFilter) { Component[3].DataType = IndComponentType_AllowOpenLong; Component[3].CompName = "Is long entry allowed"; Component[4].DataType = IndComponentType_AllowOpenShort; Component[4].CompName = "Is short entry allowed"; } else if(SlotType==SlotTypes_Close) { Component[3].DataType = IndComponentType_CloseLongPrice; Component[3].CompName = "Long position closing price"; Component[4].DataType = IndComponentType_CloseShortPrice; Component[4].CompName = "Short position closing price"; } else if(SlotType==SlotTypes_CloseFilter) { Component[3].DataType = IndComponentType_ForceCloseLong; Component[3].CompName = "Close out long position"; Component[4].DataType = IndComponentType_ForceCloseShort; Component[4].CompName = "Close out short position"; } if(SlotType==SlotTypes_Open || SlotType==SlotTypes_Close) { if(nMA>1) { for(int iBar=firstBar; iBar<Data.Bars; iBar++) { // Covers the cases when the price can pass through the band without a signal. double dOpen=Data.Open[iBar]; // Current open price // Upper band double dValueUp=adUpBand[iBar-previous]; // Current value double dValueUp1=adUpBand[iBar-previous-1]; // Previous value double dTempValUp=dValueUp; if((dValueUp1>Data.High[iBar-1] && dValueUp<dOpen) || // The Data.Open price jumps above the indicator (dValueUp1<Data.Low[iBar-1] && dValueUp>dOpen) || // The Data.Open price jumps below the indicator (Data.Close[iBar-1]<dValueUp && dValueUp<dOpen) || // The Data.Open price is in a positive gap (Data.Close[iBar-1]>dValueUp && dValueUp>dOpen)) // The Data.Open price is in a negative gap dTempValUp=dOpen; // The entry/exit level is moved to Data.Open price // Lower band double dValueDown=adDnBand[iBar-previous]; // Current value double dValueDown1=adDnBand[iBar-previous-1]; // Previous value double dTempValDown=dValueDown; if((dValueDown1>Data.High[iBar-1] && dValueDown<dOpen) || // The Data.Open price jumps above the indicator (dValueDown1<Data.Low[iBar-1] && dValueDown>dOpen) || // The Data.Open price jumps below the indicator (Data.Close[iBar-1]<dValueDown && dValueDown<dOpen) || // The Data.Open price is in a positive gap (Data.Close[iBar-1]>dValueDown && dValueDown>dOpen)) // The Data.Open price is in a negative gap dTempValDown=dOpen; // The entry/exit level is moved to Data.Open price if(ListParam[0].Text=="Enter long at Upper Band" || ListParam[0].Text=="Exit long at Upper Band") { Component[3].Value[iBar] = dTempValUp; Component[4].Value[iBar] = dTempValDown; } else { Component[3].Value[iBar] = dTempValDown; Component[4].Value[iBar] = dTempValUp; } } } else { for(int iBar=2; iBar<Data.Bars; iBar++) { if(ListParam[0].Text=="Enter long at Upper Band" || ListParam[0].Text=="Exit long at Upper Band") { Component[3].Value[iBar] = adUpBand[iBar - previous]; Component[4].Value[iBar] = adDnBand[iBar - previous]; } else { Component[3].Value[iBar] = adDnBand[iBar - previous]; Component[4].Value[iBar] = adUpBand[iBar - previous]; } } } } else { if(ListParam[0].Text=="The bar opens below Upper Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_below_the_Upper_Band); else if(ListParam[0].Text=="The bar opens above Upper Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_above_the_Upper_Band); else if(ListParam[0].Text=="The bar opens below Lower Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_below_the_Lower_Band); else if(ListParam[0].Text=="The bar opens above Lower Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_above_the_Lower_Band); else if(ListParam[0].Text=="The bar opens below Upper Band after opening above it") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_below_Upper_Band_after_above); else if(ListParam[0].Text=="The bar opens above Upper Band after opening below it") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_above_Upper_Band_after_below); else if(ListParam[0].Text=="The bar opens below Lower Band after opening above it") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_below_Lower_Band_after_above); else if(ListParam[0].Text=="The bar opens above Lower Band after opening below it") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_opens_above_Lower_Band_after_below); else if(ListParam[0].Text=="The position opens above Upper Band") { Component[0].PosPriceDependence = PositionPriceDependence_PriceBuyHigher; Component[2].PosPriceDependence = PositionPriceDependence_PriceSellLower; Component[3].DataType = IndComponentType_Other; Component[4].DataType = IndComponentType_Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; } else if(ListParam[0].Text=="The position opens below Upper Band") { Component[0].PosPriceDependence = PositionPriceDependence_PriceBuyLower; Component[2].PosPriceDependence = PositionPriceDependence_PriceSellHigher; Component[3].DataType = IndComponentType_Other; Component[4].DataType = IndComponentType_Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; } else if(ListParam[0].Text=="The position opens above Lower Band") { Component[0].PosPriceDependence = PositionPriceDependence_PriceSellLower; Component[2].PosPriceDependence = PositionPriceDependence_PriceBuyHigher; Component[3].DataType = IndComponentType_Other; Component[4].DataType = IndComponentType_Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; } else if(ListParam[0].Text=="The position opens below Lower Band") { Component[0].PosPriceDependence = PositionPriceDependence_PriceSellHigher; Component[2].PosPriceDependence = PositionPriceDependence_PriceBuyLower; Component[3].DataType = IndComponentType_Other; Component[4].DataType = IndComponentType_Other; Component[3].ShowInDynInfo = false; Component[4].ShowInDynInfo = false; } else if(ListParam[0].Text=="The bar closes below Upper Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_closes_below_the_Upper_Band); else if(ListParam[0].Text=="The bar closes above Upper Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_closes_above_the_Upper_Band); else if(ListParam[0].Text=="The bar closes below Lower Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_closes_below_the_Lower_Band); else if(ListParam[0].Text=="The bar closes above Lower Band") BandIndicatorLogic(firstBar,previous,adUpBand,adDnBand,Component[3],Component[4],BandIndLogic_The_bar_closes_above_the_Lower_Band); } } //+------------------------------------------------------------------+
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2025, Forex Software Ltd.;
Copyright © 2006 - 2025, Forex Software Ltd.;