221 lines
4.8 KiB
Plaintext
221 lines
4.8 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center TP Indicator
|
|
// ---------------------------------------------
|
|
// saherelm implementation of above oscillator ...
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
//
|
|
// Global Properties ...
|
|
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
#property link "https://saherelm.ir"
|
|
#property version "1.00"
|
|
#property description "SaherElm TP Indicator"
|
|
#property strict
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
//
|
|
// Set the R2R Length ...
|
|
input int swingLoopback = 7; // Swing Loopback
|
|
input double slThrshold = 10; // SL Threshold
|
|
input double riskToRewardRatio = 1.5; // Risk to Reward Ratio
|
|
//
|
|
// END Inputs ...
|
|
//
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
// define indicator buffers ...
|
|
#property indicator_buffers 2
|
|
#property indicator_plots 2
|
|
|
|
//
|
|
// here we declare plot to system ...
|
|
#property indicator_label1 "Long TP"
|
|
#property indicator_type1 DRAW_LINE
|
|
#property indicator_color1 clrLime
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
// here we declare plot to system ...
|
|
#property indicator_label2 "Short TP"
|
|
#property indicator_type2 DRAW_LINE
|
|
#property indicator_color2 clrRed
|
|
#property indicator_style2 STYLE_SOLID
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
// Declare Buffers ...
|
|
#define longTPBufferIndex 0
|
|
#define shortTPBufferIndex 1
|
|
|
|
//
|
|
double longTPBuffer[];
|
|
double shortTPBuffer[];
|
|
|
|
//
|
|
string slIndicatorName = "x-saherelm.sl";
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// Start Event Handlers ...
|
|
//
|
|
//
|
|
// Initialization ...
|
|
int OnInit() {
|
|
//
|
|
// Initialize what we want ...
|
|
SetIndexBuffer(longTPBufferIndex, longTPBuffer);
|
|
SetIndexDrawBegin(longTPBufferIndex, swingLoopback);
|
|
|
|
//
|
|
SetIndexBuffer(shortTPBufferIndex, shortTPBuffer);
|
|
SetIndexDrawBegin(shortTPBufferIndex, swingLoopback);
|
|
|
|
//
|
|
// initialization done ...
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
|
|
//
|
|
// Do Calculation ...
|
|
int OnCalculate(
|
|
const int rates_total,
|
|
const int prev_calculated,
|
|
const datetime &time[],
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &low[],
|
|
const double &close[],
|
|
const long &tick_volume[],
|
|
const long &volume[],
|
|
const int &spread[]
|
|
) {
|
|
//
|
|
// this counts Available Bars ...
|
|
int limit;
|
|
|
|
//
|
|
// because in some cases we may have more than one input for
|
|
// calculation and we must prevent any calculation
|
|
// untill we pass the biggest input length, here we get max Input length
|
|
// and then wait until pass it ...
|
|
int maxLength = MathMax(0, swingLoopback);
|
|
|
|
//
|
|
// input variable, we return 0 means nothing passed ...
|
|
if (rates_total < maxLength) {
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
// found which candles calculated before ...
|
|
limit = (prev_calculated == 0) ? rates_total - maxLength - 1 : rates_total - prev_calculated + 1;
|
|
|
|
//
|
|
// this is the main loop of calculations, for each bar index ...
|
|
for (int i = limit - 1; i >= 0; i--) {
|
|
//
|
|
// Start Calculation here ...
|
|
// i is bar_index ...
|
|
|
|
//
|
|
// Calculate Long TP ...
|
|
CalculateLongTP(
|
|
open,
|
|
i
|
|
);
|
|
|
|
//
|
|
// Calculate Short TP ...
|
|
CalculateShortTP(
|
|
open,
|
|
i
|
|
);
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// START Functions ...
|
|
//
|
|
//
|
|
// Calculate Long TPs based on SL and Open Price ...
|
|
void CalculateLongTP(
|
|
const double &open[],
|
|
const int &bar_index
|
|
) {
|
|
//
|
|
// read long sl ...
|
|
double longSL = iCustom(
|
|
_Symbol,
|
|
_Period,
|
|
slIndicatorName,
|
|
swingLoopback,
|
|
slThrshold,
|
|
0, // Long SL Buffer Index ...
|
|
bar_index
|
|
);
|
|
|
|
//
|
|
double longRisk = open[bar_index] - longSL;
|
|
double longReward = longRisk * riskToRewardRatio;
|
|
|
|
//
|
|
double longTP = open[bar_index] + longReward;
|
|
longTP = NormalizeDouble(longTP, _Digits);
|
|
|
|
//
|
|
longTPBuffer[bar_index] = longTP;
|
|
}
|
|
|
|
//
|
|
// Calculate Short TPs based on SL and Open Price ...
|
|
void CalculateShortTP(
|
|
const double &open[],
|
|
const int &bar_index
|
|
) {
|
|
//
|
|
// read short sl ...
|
|
double shortSL = iCustom(
|
|
_Symbol,
|
|
_Period,
|
|
slIndicatorName,
|
|
swingLoopback,
|
|
slThrshold,
|
|
1, // Short SL Buffer Index ...
|
|
bar_index
|
|
);
|
|
|
|
//
|
|
double shortRisk = shortSL - open[bar_index];
|
|
double shortReward = shortRisk * riskToRewardRatio;
|
|
|
|
//
|
|
double shortTP = open[bar_index] - shortReward;
|
|
shortTP = NormalizeDouble(shortTP, _Digits);
|
|
|
|
//
|
|
shortTPBuffer[bar_index] = shortTP;
|
|
}
|
|
//
|
|
// END Functions ...
|
|
// |