Reverse Polish Notation Java - stack

I want to make Reverse Polish Notation algorithm, but my code isn't working. Can anyone explain me why ?
In my code, I would like to take the characters, until "/+-*"tokens . If this tokens was next , make operation , that contains in case .
import java.util.Stack;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class ONP {
public static void main(String[] args) {
Stack<Double> stack = new Stack<Double>();
double number1;
double number2;
String x = JOptionPane.showInputDialog("Tokens");
StringTokenizer str = new StringTokenizer(x);
while (str.hasMoreElements()) {
str.nextElement();
stack.push((Double) str.nextElement());
for (int i = 0; i < x.length(); i++) {
switch (x.charAt(i)) {
case '+':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 + number2);
break;
case '-':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 - number2);
break;
case '/':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 / number2);
break;
case '*':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 * number2);
break;
}
}
System.out.println(stack.pop());
}
}
}
I would like to write it without StringBuilder or sth like that.
Input:
2 2 + 2 / 2 /
Output :
(2+2) / 2 / 2

try this
String s1;
while (str.hasMoreElements())
{
s1 = str.nextElement();
if(s1.equals("+") || s1.equals("-") || s1.equals("*") || s1.equals("/"))
{
switch (s1)
{
case '+':
number1 = stack.pop();
number2 = stack.pop();
System.out.print(number1 + " + " + number2);
break;
case '-':
number1 = stack.pop();
number2 = stack.pop();
System.out.print(number1 +" - "+ number2);
break;
case '/':
number1 = stack.pop();
number2 = stack.pop();
System.out.print(number1 +" / "+ number2);
break;
case '*':
number1 = stack.pop();
number2 = stack.pop();
System.out.print(number1 +" * "+ number2);
break;
}
}
else
{
stack.push(s1);
}
}
instead of
while (str.hasMoreElements()) {
str.nextElement();
stack.push((Double) str.nextElement());
for (int i = 0; i < x.length(); i++) {
switch (x.charAt(i)) {
case '+':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 + number2);
break;
case '-':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 - number2);
break;
case '/':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 / number2);
break;
case '*':
number1 = stack.pop();
number2 = stack.pop();
System.out.println(number1 * number2);
break;
}
}
System.out.println(stack.pop());
}

Related

Expert Advisor will not open buy trades and doesn't follow the conditions

The EA has no bugs but it doesn't really do what I expected it to do, it should, for example, sell from var_30[i], put a tp at var_2[i] and sl at var_30[i]+(var_30[i]-var_2[i])/2 or buy from var_20[i], put a tp at var_3[i] and sl at var_20[i]+(var_20[i]-var_3[i])/2
I tried to modify it several times but in the end, after using the strategy tester feature the graph always goes to 0, which shouldn't.
Here is the code:
#property description "Price Border strategy expert advisor by Razvan"
//--- Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
// init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82]+ str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
// deinit function
int deinit() {
return (0);
}
// start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) * var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier, ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
// function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
// function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " + func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
// function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7], Period()));
return (Period());
}
// function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
// function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`' && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8, var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_30[i] || Close[i] > var_30[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,var_30[i]+(var_30[i]- var_2[i])/2,var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen
return;
}
//--- buy conditions
else if(Low[i] < var_20[i] || Close[i] < var_20[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,var_20[i]+(var_20[i]-var_3[i])/2,var_3[i],"",0,Green);
return;
}}
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume[0]>0.1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]+(var_30[i]-var_2[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(var_20[i]-var_3[i])/2)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,30,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+

Expert Advisor is not showing any bug or warning, but doesn't work

I created an Expert Advisor that will open long and short positions based on some bands, there are the 1.6 band and 3.6 band, it sells at the 3.6 upper band, puts the tp at 1.6 lower band and sl at half the tp opposite to the entry. It has no bug or warning, but it simply doesn't work when I try to use it. Any help would be greatly appreciated.
#property description "Price Border strategy expert advisor by Razvan"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 Red
#property indicator_color3 Lime
//Inputs
extern double Lots =1;
extern double MaximumRisk =2;
extern double DecreaseFactor=3;
extern string TimeFrame = "All tf";
extern int HalfLength = 61;
extern int Price = 0;
extern double ATRMultiplier = 1.6;
extern double ATRMultiplier1 = 3.6;
extern int ATRPeriod = 480;
extern bool Interpolate = TRUE;
extern bool alertsOn = TRUE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsOnHighLow = TRUE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = TRUE;
extern bool alertsEmail = FALSE;
double var_1[];
double var_2[];
double var_3[];
double var_4[];
double var_20[];
double var_30[];
string string_1;
bool bool_1;
bool bool_2;
int var_5;
string string_2 = "nothing";
datetime Gt_176;
string string_3[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
int var_6[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int res;
//init function
int init() {
string str_4[256];
for (int i = 0; i < 256; i++) str_4[i] = CharToStr(i);
int var_7 = StrToInteger(str_4[67] + str_4[111] + str_4[112] + str_4[121] + str_4[32] + str_4[82] + str_4[105] + str_4[103] + str_4[104] + str_4[116] + str_4[32] +
str_4[169] + str_4[32] + str_4[75] + str_4[97] + str_4[122] + str_4[97] + str_4[111] + str_4[111] + str_4[32] + str_4[50] + str_4[48] + str_4[49] + str_4[49] + str_4[32]);
IndicatorBuffers(4);
HalfLength = MathMax(HalfLength, 1);
SetIndexBuffer(0, var_1);
SetIndexDrawBegin(0, HalfLength);
SetIndexBuffer(1, var_2);
SetIndexDrawBegin(1, HalfLength);
SetIndexBuffer(2, var_3);
SetIndexDrawBegin(2, HalfLength);
SetIndexBuffer(3, var_4);
string_1 = WindowExpertName();
bool_2 = TimeFrame == "returnBars";
if (bool_2) return (0);
bool_1 = TimeFrame == "calculateValue";
if (bool_1) return (0);
var_5 = func_3(TimeFrame);
IndicatorShortName(func_0(var_5) + " TMA bands )" + HalfLength + ")");
return (0);
}
//deinit function
int deinit() {
return (0);
}
//start function
int start() {
int var_7;
double double_1;
double double_2;
double double_3;
double double_31;
int var_8;
int var_9;
int var_10 = IndicatorCounted();
if (var_10 < 0) return (-1);
if (var_10 > 0) var_10--;
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
if (bool_2) {
var_1[0] = var_11 + 1;
return (0);
}
if (bool_1 || var_5 == Period()) {
for (int i = var_11; i >= 0; i--) {
double_1 = (HalfLength + 1) * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i);
double_2 = HalfLength + 1;
var_7 = 1;
for (int var_12 = HalfLength; var_7 <= HalfLength; var_12--) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i + var_7);
double_2 += var_12;
if (var_7 <= i) {
double_1 += var_12 * iMA(NULL, 0, 1, 0, MODE_SMA, Price, i - var_7);
double_2 += var_12;
}
var_7++;
}
double_3 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier;
double_31 = iATR(NULL, 0, ATRPeriod, i + 10) * ATRMultiplier1;
var_1[i] = double_1 / double_2;
var_2[i] = var_1[i] + double_3;
var_20[i] = var_1[i] + double_31;
var_30[i] = var_1[i] - double_31;
var_3[i] = var_1[i] - double_3;
var_4[i] = 0;
if (alertsOnHighLow) {
if (High[i] > var_2[i]) var_4[i] = 1;
if (Low[i] < var_3[i]) var_4[i] = -1;
} else {
if (Close[i] > var_2[i]) var_4[i] = 1;
if (Close[i] < var_3[i]) var_4[i] = -1;
}
}
if (!(!bool_1)) return (0);
func_1();
return (0);
}
var_11 = MathMax(var_11, MathMin(Bars - 1, iCustom(NULL, var_5, string_1, "returnBars", 0, 0) *
var_5 / Period()));
for (i = var_11; i >= 0; i--) {
var_8 = iBarShift(NULL, var_5, Time[i]);
var_1[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 0, var_8);
var_2[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 1, var_8);
var_3[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 2, var_8);
var_4[i] = iCustom(NULL, var_5, string_1, "calculateTma", HalfLength, Price, ATRMultiplier,
ATRPeriod, 3, var_8);
if (var_5 <= Period() || var_8 == iBarShift(NULL, var_5, Time[i - 1])) continue;
if (Interpolate) {
var_9 = iTime(NULL, var_5, var_8);
for (int var_14 = 1; i + var_14 < Bars && Time[i + var_14] >= var_9; var_14++) {
}
for (var_14 = 1; var_12 < var_14; var_12++) {
var_1[i + var_12] = var_1[i] + (var_1[i + var_14] - var_1[i]) * var_12 / var_14;
var_2[i + var_12] = var_2[i] + (var_2[i + var_14] - var_2[i]) * var_12 / var_14;
var_3[i + var_12] = var_3[i] + (var_3[i + var_14] - var_3[i]) * var_12 / var_14;
}
}
}
func_1();
return (0);
}
//function1
void func_1() {
int var_10;
if (alertsOn) {
if (alertsOnCurrent) var_10 = 0;
else var_10 = 1;
var_10 = iBarShift(NULL, 0, iTime(NULL, var_5, var_10));
if (var_4[var_10] != var_4[var_10 + 1]) {
if (var_4[var_10] == 1.0) func_4(var_10, "up");
if (var_4[var_10] == -1.0) func_4(var_10, "down");
}
}
}
//function4
void func_4(int var_12, string str_5) {
string str_6;
if (string_2 != str_5 || Gt_176 != Time[var_12]) {
string_2 = str_5;
Gt_176 = Time[var_12];
str_6 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " " +
func_0(var_5) + " TMA bands price penetrated ", str_5, " band");
if (alertsMessage) Alert(str_6);
if (alertsEmail) SendMail(StringConcatenate(Symbol(), "TMA bands "), str_6);
if (alertsSound) PlaySound("alert2.wav");
}
}
//function3
int func_3(string str_7) {
str_7 = func_2(str_7);
for (int var_7 = ArraySize(var_6) - 1; var_7 >= 0; var_7--)
if (str_7 == string_3[var_7] || str_7 == "" + var_6[var_7]) return (MathMax(var_6[var_7],
Period()));
return (Period());
}
//function0
string func_0(int var_12) {
for (int i = ArraySize(var_6) - 1; i >= 0; i--)
if (var_12 == var_6[i]) return (string_3[i]);
return ("");
}
//function2
string func_2(string str_7) {
int var_13;
string str_8 = str_7;
for (int var_11 = StringLen(str_7) - 1; var_11 >= 0; var_11--) {
var_13 = StringGetChar(str_8, var_11);
if ((var_13 > '`'` && var_13 < '{') || (var_13 > 'ß' && var_13 < 256)) str_8 = StringSetChar(str_8,
var_11, var_13 - 32);
else
if (var_13 > -33 && var_13 < 0) str_8 = StringSetChar(str_8, var_11, var_13 + 224);
}
return (str_8);
}
//Calculate open positions
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//Calculate optimal lot size
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//calculate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//return lot size
if(lot<1) lot=1;
return(lot);
}
//Check for open order conditions
void CheckForOpen()
{ int var_10 = IndicatorCounted();
int var_11 = MathMin(Bars - 1, Bars - var_10 + HalfLength);
for (int i = var_11; i >= 0; i--)
{if(High[i] > var_20[i] || Close[i] > var_20[i])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,var_20[i]+(fabs(var_20[i]-
var_3[i])/2),var_2[i],"",0,Red); //Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My
order",16384,0,clrGreen
return;
}
//buy conditions
if(Low[i] < var_30[i] || Close[i] < var_30[i])
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,var_30[i]-(fabs(var_30[i]-
var_2[i])/2),var_3[i],"",0,Green);
return;
}}
}
//Check for close order conditions
void CheckForClose()
{
if(Volume[0]>1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(High[i] >= var_3[i] || Low[i] <= var_30[i]-(fabs(var_30[i]-var_2[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Low[i] <= var_2[i] || High[i] >= var_20[i]+(fabs(var_20[i]-var_3[i])/2))
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
}
//OnTick function
void OnTick()
{
//check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
'

MQL4 increase the width of the COG

Here is the code:
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 RoyalBlue
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen
#property indicator_color4 Goldenrod
#property indicator_color5 Goldenrod
//-----------------------------------
extern int bars_back = 100;
extern int m = 2;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 1102;
//----------------------
double fx[], sqh[], sql[], stdh[], stdl[];
double ai[10,10], b[10], x[10], sx[20];
double sum;
int ip, p, n, f;
double qq, mm, tt;
int ii, jj, kk, ll, nn;
double sq, std;
//*******************************************
int init()
{
IndicatorShortName("Center of Gravity");
SetIndexStyle(0, DRAW_LINE, 2 );
SetIndexBuffer(0, fx);
SetIndexBuffer(1, sqh);
SetIndexBuffer(2, sql);
SetIndexBuffer(3, stdh);
SetIndexBuffer(4, stdl);
p = MathRound(bars_back);
nn = m + 1;
ObjectCreate("pr" + sName, 22, 0, Time[p], fx[p]);
ObjectSet("pr" + sName, 14, 159);
return(0);
}
//----------------------------------------------------------
int deinit()
{
ObjectDelete("pr" + sName);
}
//**********************************************************************************************
int start()
{
int mi;
//-------------------------------------------------------------------------------------------
ip = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
p = bars_back;
sx[1] = p + 1;
SetIndexDrawBegin(0, Bars - p - 1);
SetIndexDrawBegin(1, Bars - p - 1);
SetIndexDrawBegin(2, Bars - p - 1);
SetIndexDrawBegin(3, Bars - p - 1);
SetIndexDrawBegin(4, Bars - p - 1);
//----------------------sx-------------------------------------------------------------------
for(mi = 1; mi <= nn * 2 - 2; mi++)
{
sum = 0;
for(n = i; n <= i + p; n++)
{
sum += MathPow(n, mi);
}
sx[mi + 1] = sum;
}
//----------------------syx-----------
for(mi = 1; mi <= nn; mi++)
{
sum = 0.00000;
for(n = i; n <= i + p; n++)
{
if(mi == 1)
sum += Close[n];
else
sum += Close[n] * MathPow(n, mi - 1);
}
b[mi] = sum;
}
//===============Matrix=======================================================================================================
for(jj = 1; jj <= nn; jj++)
{
for(ii = 1; ii <= nn; ii++)
{
kk = ii + jj - 1;
ai[ii, jj] = sx[kk];
}
}
//===============Gauss========================================================================================================
for(kk = 1; kk <= nn - 1; kk++)
{
ll = 0; mm = 0;
for(ii = kk; ii <= nn; ii++)
{
if(MathAbs(ai[ii, kk]) > mm)
{
mm = MathAbs(ai[ii, kk]);
ll = ii;
}
}
if(ll == 0)
return(0);
if(ll != kk)
{
for(jj = 1; jj <= nn; jj++)
{
tt = ai[kk, jj];
ai[kk, jj] = ai[ll, jj];
ai[ll, jj] = tt;
}
tt = b[kk]; b[kk] = b[ll]; b[ll] = tt;
}
for(ii = kk + 1; ii <= nn; ii++)
{
qq = ai[ii, kk] / ai[kk, kk];
for(jj = 1; jj <= nn; jj++)
{
if(jj == kk)
ai[ii, jj] = 0;
else
ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
}
b[ii] = b[ii] - qq * b[kk];
}
}
x[nn] = b[nn] / ai[nn, nn];
for(ii = nn - 1; ii >= 1; ii--)
{
tt = 0;
for(jj = 1; jj <= nn - ii; jj++)
{
tt = tt + ai[ii, ii + jj] * x[ii + jj];
x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
}
}
//===========================================================================================================================
for(n = i; n <= i + p; n++)
{
sum = 0;
for(kk = 1; kk <= m; kk++)
{
sum += x[kk + 1] * MathPow(n, kk);
}
fx[n] = x[1] + sum;
}
//-----------------------------------Std-----------------------------------------------------------------------------------
sq = 0.0;
for(n = i; n <= i + p; n++)
{
sq += MathPow(Close[n] - fx[n], 2);
}
sq = MathSqrt(sq / (p + 1)) * kstd;
std = iStdDev(NULL, 0, p, MODE_SMA, 0, PRICE_CLOSE, i) * kstd;
for(n = i; n <= i + p; n++)
{
sqh[n] = fx[n] + sq;
sql[n] = fx[n] - sq;
stdh[n] = fx[n] + std;
stdl[n] = fx[n] - std;
}
//-------------------------------------------------------------------------------
ObjectMove("pr" + sName, 0, Time[p], fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
return(0);
}
//==========================================================================================================================
I would like the increase the lines width to 2.
I solved with
SetIndexStyle(0, DRAW_LINE, EMPTY,2, indicator_color1);
SetIndexStyle(1, DRAW_LINE, EMPTY,2, indicator_color2);
SetIndexStyle(2, DRAW_LINE, EMPTY,2, indicator_color3);
SetIndexStyle(3, DRAW_LINE, EMPTY,2, indicator_color4);
SetIndexStyle(4, DRAW_LINE, EMPTY,2, indicator_color5);

How to insert an alert into an MQL4 indicator-code?

I have a free MQL4 indicator that displays the most important resistance a support based on the timeframe.
It's a fantastic indicator, but I need an implementation with a message and a sound alert when a support or resistance is touched.
How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black
extern int LB = 3;
extern int maxBarsForPeriod = 1000;
extern bool showM01 = TRUE;
extern bool showM05 = TRUE;
extern bool showM15 = TRUE;
extern bool showM30 = TRUE;
extern bool showH01 = TRUE;
extern bool showH04 = TRUE;
extern bool showD01 = TRUE;
extern bool showW01 = TRUE;
extern bool showMN1 = TRUE;
int gi_120 = 0;
int gi_124 = 0;
int gi_128 = 0;
int gi_132 = 0;
int gi_136 = 0;
int gi_140 = 0;
int gi_144 = 0;
int gi_148 = 0;
int gi_152 = 0;
int gi_156 = 0;
int gi_160 = 0;
int gi_164 = 0;
int gi_168 = 0;
int gi_172 = 0;
int gi_176 = 0;
int gi_180 = 0;
int gi_184 = 0;
int gi_188 = 0;
double gda_192[];
double gda_196[];
double gda_200[];
double gda_204[];
double gda_208[];
double gda_212[];
double gda_216[];
double gda_220[];
double gda_224[];
double gda_228[];
double gda_232[];
double gda_236[];
double gda_240[];
double gda_244[];
double gda_248[];
double gda_252[];
double gda_256[];
double gda_260[];
int init() {
IndicatorShortName("3 Line Break On Chart +levels");
set_prevBarTime(1, 0);
set_prevBarTime(5, 0);
set_prevBarTime(15, 0);
set_prevBarTime(30, 0);
set_prevBarTime(60, 0);
set_prevBarTime(240, 0);
set_prevBarTime(1440, 0);
set_prevBarTime(10080, 0);
set_prevBarTime(43200, 0);
return (0);
}
int deinit() {
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res C"));
return (0);
}
double Diap(int ai_0, bool ai_4, int ai_8, int ai_12) {
double ld_ret_20;
if (ai_4) {
ld_ret_20 = get_max(ai_0, ai_12);
for (int li_16 = 1; li_16 < ai_8; li_16++)
if (get_max(ai_0, ai_12 - li_16) > ld_ret_20) ld_ret_20 = get_max(ai_0, ai_12 - li_16);
}
if (!ai_4) {
ld_ret_20 = get_min(ai_0, ai_12);
for (li_16 = 1; li_16 < ai_8; li_16++)
if (get_min(ai_0, ai_12 - li_16) < ld_ret_20) ld_ret_20 = get_min(ai_0, ai_12 - li_16);
}
return (ld_ret_20);
}
void EmulateDoubleBuffer(double ada_0[], int ai_4) {
if (ArraySize(ada_0) < ai_4) {
ArraySetAsSeries(ada_0, FALSE);
ArrayResize(ada_0, ai_4);
ArraySetAsSeries(ada_0, TRUE);
}
}
void DeleteHLineObject(string a_name_0) {
ObjectDelete(a_name_0);
ObjectDelete(a_name_0 + "_Label");
}
void ShowHLineObject(string a_name_0, int ai_8, int a_style_12, double ad_16, int ai_24) {
if (ObjectFind(a_name_0) != 0) CreateHLineObject(a_name_0, ai_8, a_style_12, ad_16, ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_PRICE1, ad_16);
ObjectSet(a_name_0 + "_Label", OBJPROP_TIME1, Time[0] + Period() * ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_STYLE, a_style_12);
ObjectSet(a_name_0, OBJPROP_PRICE1, ad_16);
}
void CreateHLineObject(string a_text_0, color a_color_8, int a_style_12, double a_price_16, int ai_24) {
ObjectCreate(a_text_0 + "_Label", OBJ_TEXT, 0, Time[0] + Period() * ai_24, a_price_16);
ObjectSetText(a_text_0 + "_Label", a_text_0, 7, "Verdana", a_color_8);
ObjectCreate(a_text_0, OBJ_HLINE, 0, Time[0], a_price_16);
ObjectSet(a_text_0, OBJPROP_STYLE, a_style_12);
ObjectSet(a_text_0, OBJPROP_COLOR, a_color_8);
ObjectSet(a_text_0, OBJPROP_WIDTH, 1);
}
string getPeriodAsString(int ai_0) {
string ls_ret_4 = 0;
switch (ai_0) {
case 1:
ls_ret_4 = "M1";
break;
case 5:
ls_ret_4 = "M5";
break;
case 15:
ls_ret_4 = "M15";
break;
case 30:
ls_ret_4 = "M30";
break;
case 60:
ls_ret_4 = "H1";
break;
case 240:
ls_ret_4 = "H4";
break;
case 1440:
ls_ret_4 = "D1";
break;
case 10080:
ls_ret_4 = "W1";
break;
case 43200:
ls_ret_4 = "MN1";
}
return (ls_ret_4);
}
void set_prevBarTime(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_120 = ai_4;
return;
case 5:
gi_124 = ai_4;
return;
case 15:
gi_128 = ai_4;
return;
case 30:
gi_132 = ai_4;
return;
case 60:
gi_136 = ai_4;
return;
case 240:
gi_140 = ai_4;
return;
case 1440:
gi_144 = ai_4;
return;
case 10080:
gi_148 = ai_4;
return;
case 43200:
gi_152 = ai_4;
return;
return;
}
}
int get_prevBarTime(int ai_0) {
switch (ai_0) {
case 1:
return (gi_120);
break;
case 5:
return (gi_124);
break;
case 15:
return (gi_128);
break;
case 30:
return (gi_132);
break;
case 60:
return (gi_136);
break;
case 240:
return (gi_140);
break;
case 1440:
return (gi_144);
break;
case 10080:
return (gi_148);
break;
case 43200:
return (gi_152);
}
return (0);
}
void set_prevBarCount(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_156 = ai_4;
return;
case 5:
gi_160 = ai_4;
return;
case 15:
gi_164 = ai_4;
return;
case 30:
gi_168 = ai_4;
return;
case 60:
gi_172 = ai_4;
return;
case 240:
gi_176 = ai_4;
return;
case 1440:
gi_180 = ai_4;
return;
case 10080:
gi_184 = ai_4;
return;
case 43200:
gi_188 = ai_4;
return;
return;
}
}
int get_prevBarCount(int ai_0) {
switch (ai_0) {
case 1:
return (gi_156);
break;
case 5:
return (gi_160);
break;
case 15:
return (gi_164);
break;
case 30:
return (gi_168);
break;
case 60:
return (gi_172);
break;
case 240:
return (gi_176);
break;
case 1440:
return (gi_180);
break;
case 10080:
return (gi_184);
break;
case 43200:
return (gi_188);
}
return (0);
}
void set_max(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_192[ai_4] = ad_8;
return;
case 5:
gda_196[ai_4] = ad_8;
return;
case 15:
gda_200[ai_4] = ad_8;
return;
case 30:
gda_204[ai_4] = ad_8;
return;
case 60:
gda_208[ai_4] = ad_8;
return;
case 240:
gda_212[ai_4] = ad_8;
return;
case 1440:
gda_216[ai_4] = ad_8;
return;
case 10080:
gda_220[ai_4] = ad_8;
return;
case 43200:
gda_224[ai_4] = ad_8;
return;
return;
}
}
double get_max(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_192[ai_4]);
break;
case 5:
return (gda_196[ai_4]);
break;
case 15:
return (gda_200[ai_4]);
break;
case 30:
return (gda_204[ai_4]);
break;
case 60:
return (gda_208[ai_4]);
break;
case 240:
return (gda_212[ai_4]);
break;
case 1440:
return (gda_216[ai_4]);
break;
case 10080:
return (gda_220[ai_4]);
break;
case 43200:
return (gda_224[ai_4]);
}
return (0.0);
}
void set_min(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_228[ai_4] = ad_8;
return;
case 5:
gda_232[ai_4] = ad_8;
return;
case 15:
gda_236[ai_4] = ad_8;
return;
case 30:
gda_240[ai_4] = ad_8;
return;
case 60:
gda_244[ai_4] = ad_8;
return;
case 240:
gda_248[ai_4] = ad_8;
return;
case 1440:
gda_252[ai_4] = ad_8;
return;
case 10080:
gda_256[ai_4] = ad_8;
return;
case 43200:
gda_260[ai_4] = ad_8;
return;
return;
}
}
double get_min(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_228[ai_4]);
break;
case 5:
return (gda_232[ai_4]);
break;
case 15:
return (gda_236[ai_4]);
break;
case 30:
return (gda_240[ai_4]);
break;
case 60:
return (gda_244[ai_4]);
break;
case 240:
return (gda_248[ai_4]);
break;
case 1440:
return (gda_252[ai_4]);
break;
case 10080:
return (gda_256[ai_4]);
break;
case 43200:
return (gda_260[ai_4]);
}
return (0.0);
}
void emulate_tlbmaxmin(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
EmulateDoubleBuffer(gda_192, ai_4);
EmulateDoubleBuffer(gda_228, ai_4);
return;
case 5:
EmulateDoubleBuffer(gda_196, ai_4);
EmulateDoubleBuffer(gda_232, ai_4);
return;
case 15:
EmulateDoubleBuffer(gda_200, ai_4);
EmulateDoubleBuffer(gda_236, ai_4);
return;
case 30:
EmulateDoubleBuffer(gda_204, ai_4);
EmulateDoubleBuffer(gda_240, ai_4);
return;
case 60:
EmulateDoubleBuffer(gda_208, ai_4);
EmulateDoubleBuffer(gda_244, ai_4);
return;
case 240:
EmulateDoubleBuffer(gda_212, ai_4);
EmulateDoubleBuffer(gda_248, ai_4);
return;
case 1440:
EmulateDoubleBuffer(gda_216, ai_4);
EmulateDoubleBuffer(gda_252, ai_4);
return;
case 10080:
EmulateDoubleBuffer(gda_220, ai_4);
EmulateDoubleBuffer(gda_256, ai_4);
return;
case 43200:
EmulateDoubleBuffer(gda_224, ai_4);
EmulateDoubleBuffer(gda_260, ai_4);
return;
return;
}
}
void displayPeriod(int a_timeframe_0) {
int li_4;
int l_count_8;
int li_12;
int li_20;
double ld_24;
double ld_32;
double ld_40;
double ld_48;
int l_count_56;
int l_count_60;
int li_unused_64;
if (get_prevBarTime(a_timeframe_0) == 0 || get_prevBarTime(a_timeframe_0) != iTime(Symbol(), a_timeframe_0, 0) || get_prevBarCount(a_timeframe_0) == 0 || get_prevBarCount(a_timeframe_0) != iBars(Symbol(), a_timeframe_0)) {
set_prevBarTime(a_timeframe_0, iTime(Symbol(), a_timeframe_0, 0));
set_prevBarCount(a_timeframe_0, iBars(Symbol(), a_timeframe_0));
li_4 = iBars(Symbol(), a_timeframe_0);
if (maxBarsForPeriod > 0 && li_4 > maxBarsForPeriod) li_4 = maxBarsForPeriod;
l_count_8 = 0;
li_12 = li_4;
emulate_tlbmaxmin(a_timeframe_0, li_4);
li_20 = 1;
while (iClose(Symbol(), a_timeframe_0, li_12 - 1) == iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) > iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) < iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
}
for (int li_16 = 1; li_16 < LB; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, li_16, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, li_16, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
for (li_16 = LB; li_16 < li_12; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, LB, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, LB, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
ld_24 = 0;
ld_32 = 0;
ld_40 = 0;
ld_48 = 0;
l_count_56 = 0;
l_count_60 = 0;
li_unused_64 = 0;
for (li_16 = 1; li_16 <= l_count_8; li_16++) {
if (get_max(a_timeframe_0, li_16) > get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_60 >= LB) ld_24 = get_max(a_timeframe_0, li_16 - LB);
else ld_24 = get_min(a_timeframe_0, li_16 - l_count_60 - 1);
ld_48 = get_max(a_timeframe_0, li_16);
ld_40 = 0;
ld_32 = 0;
l_count_60++;
l_count_56 = 0;
}
if (get_max(a_timeframe_0, li_16) < get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_56 >= LB) ld_32 = get_min(a_timeframe_0, li_16 - LB);
else ld_32 = get_max(a_timeframe_0, li_16 - l_count_56 - 1);
ld_40 = get_min(a_timeframe_0, li_16);
ld_24 = 0;
ld_48 = 0;
l_count_60 = 0;
l_count_56++;
}
}
if (ld_24 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"), 16711680, STYLE_SOLID, ld_24, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"));
if (ld_32 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"), 255, STYLE_SOLID, ld_32, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"));
if (ld_40 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"), 16711680, STYLE_DASHDOTDOT, ld_40, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"));
if (ld_48 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"), 255, STYLE_DASHDOTDOT, ld_48, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"));
}
}
int start() {
if (Period() <= PERIOD_M1 && showM01) displayPeriod(PERIOD_M1);
if (Period() <= PERIOD_M5 && showM05) displayPeriod(PERIOD_M5);
if (Period() <= PERIOD_M15 && showM15) displayPeriod(PERIOD_M15);
if (Period() <= PERIOD_M30 && showM30) displayPeriod(PERIOD_M30);
if (Period() <= PERIOD_H1 && showH01) displayPeriod(PERIOD_H1);
if (Period() <= PERIOD_H4 && showH04) displayPeriod(PERIOD_H4);
if (Period() <= PERIOD_D1 && showD01) displayPeriod(PERIOD_D1);
if (Period() <= PERIOD_W1 && showW01) displayPeriod(PERIOD_W1);
if (Period() <= PERIOD_MN1 && showMN1) displayPeriod(PERIOD_MN1);
return (0);
}
How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
First, straight forget to use a call to Alert() in CustomIndicator
MQL4/5 architecture strictly forbids one to call any blocking-mode method inside a CustomIndicator-type-of-MQL4/5-code.
This will not be possible, so let's focus on can-do alternative(s):
A ) one may send a signalling message to any block-able MQL4/5 script ( which is executed in another thread or an external process.
B ) one may use pipe-based signalling, if O/S permits to use these rudimentary tools.
C ) one may use an internal GlobalVariableSet() / GlobalVariableCheck() native MQL4/5 support for inter-thread data-based signalling / messaging, allowing a non-CustomIndicator ( Script ) MQL4/5 type of code to "remotely" handle any of the blocking parts of a service ( a call to native Alert() et al )
D ) one may use a file-based signalling, if all of above seems too complicated.
Having started with A ) years ago, I have never had a need to resort to B), the less the others "beyond" A), since then, yet these options still remains also possible.
The code will go as easy as:
if ( ... )
{ int RC = mql4zmq_msg_init_data( aSendMsgOBJ,
aSendMsgBuffUCHAR,
aSendMsgBUFF_SIZE * 4
);
// if RC
// ------------------------------------------------------.SEND()
zmq_send( aZmqPUBLISHER, aSendMsgOBJ );
// ------------------------------------------------------.SEND()
...
}
The remote receivers ( could be many ) then do whatever they want, be it in MQL4/5, python, Go, C++, java, ... chapel, FORTRAN, MATLAB, ...
Cool, isn't it?

Currency formatting for BlackBerry when locale set to French

I've writing an app that requires French language support. I'm having trouble getting currency to format correctly when the locale is set to FR. Is there any way to do this correctly?
I had to do it myself.
Created a "Formatter" class extending AFormatter with the following methods
Don't now how it is in France but in Argentina we use the following format 10.000.000,18 for example.
Hope it's helps or that someone provides a better solution
public static String addCommas(String s) {
int extraRound = 0;
boolean negative = false;
int c;
if(s.charAt(0) == '-') {
negative = true;
s = s.substring(1);
}
int len = s.indexOf('.')==-1?s.length():s.indexOf('.');
if(len > extraRound + 3) {
c = len - extraRound - 3;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 6) {
c = len - extraRound - 6;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 9) {
c = len - extraRound - 9;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 12) {
c = len - extraRound - 12;
s = s.substring(0, c) + "," + s.substring(c);
}
if(negative) {
s = '-' + s;
}
return s;
}
public static String addCommasNew(String s) {
int extraRound = 0;
boolean negative = false;
int c;
if(s.charAt(0) == '-') {
negative = true;
s = s.substring(1);
}
int len = s.indexOf(',')==-1?s.length():s.indexOf(',');
if(len > extraRound + 3) {
c = len - extraRound - 3;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 6) {
c = len - extraRound - 6;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 9) {
c = len - extraRound - 9;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 12) {
c = len - extraRound - 12;
s = s.substring(0, c) + "." + s.substring(c);
}
if(negative) {
s = '-' + s;
}
return s;
}
public static String removeScientific(String s) {
int eIndex = s.indexOf('E');
int initialPos = s.indexOf('.');
String result = s;
if (eIndex != -1){
int base = Integer.parseInt(s.substring(eIndex+1));
String pre = s.substring(0, initialPos);
String pos = s.substring(initialPos+1, eIndex);
String pos1 = "";
if (base < pos.length()){
String pos1a = pos.substring(0, base);
String pos1b = pos.substring(base, pos.length());
pos1 = pos1a + "." + pos1b;
} else {
pos1 = pos.substring(0, pos.length());
for (int i = 0; i < base-pos.length(); i++){
pos1 = pos1 + "0";
}
}
result = pre + pos1;
}
return result;
}
public static String changePointToComma(String s){
int initialPos = s.indexOf('.');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + "," + s.substring(initialPos + 1, s.length());
}
return result;
}
public static String changeCommaToPoint(String s){
int initialPos = s.indexOf(',');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + "." + s.substring(initialPos + 1, s.length());
}
return result;
}
public static String removeNumberFormat(String s){
int initialPos = s.indexOf('.');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + s.substring(initialPos + 1, s.length());
result = Formatter.removeNumberFormat(result);
} else {
return result = Formatter.changeCommaToPoint(s);
}
return result;
}
public static String roundDouble(String s, int presicion){
int initialPos = s.indexOf('.');
String result = s;
String pre;
String pos;
if (initialPos != -1){
pre = s.substring(0, initialPos);
pos = s.substring(initialPos + 1, s.length());
if (presicion < pos.length()){
pos = s.substring(initialPos + 1, initialPos + 1 + presicion );
int dec = Integer.parseInt(pos);
int next = Integer.parseInt(s.substring(initialPos + 1 + presicion, initialPos + 2 + presicion )); //to round the las digit
if (next > 4){
dec = dec + 1;
pos = dec + "";
if ((dec+"").length() > presicion){
pre = (Integer.parseInt(pre) + 1) + "";
pos = "0";
}
}
} else {
}
result = pre + "." + pos;
}
return result;
}

Resources