mql4 separate window indicator using sin wave function - mql4

Below is the original code that creates a sin wave in a separate window for MetaTrader 4. My question is how to code it for a 2nd, or 3rd sin wave. The original code is compiled and is functional. I will also attach the code I added to the original, it still creates the 1st sin wave but not the second wave. I need help coding the 2nd and 3rd waves. Thanks.
This is the original working code:
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2 // org 1
#property indicator_color1 Red //
//---- indicator buffers
double ExtBuffer1[];
double Dgr[];
extern datetime StartTime=D'1999.11.10 00:00';
//+---------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------+
int init()
{
IndicatorBuffers(1);
SetIndexStyle(0,DRAW_LINE); // 0
SetLevelValue(0,0); //
//----indicator buffers mapping //
SetIndexBuffer(0,ExtBuffer1);
SetIndexShift(0,25); // B. added
//---- initialization done
return(0);
}
//+---------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+---------------------------------------------------------------+
int start()
{
int Shift;
int i;
Shift=iBarShift(Symbol(),PERIOD_D1,StartTime); // Only run on PERIOD_D1
ArrayResize(Dgr,Shift+1); // ArrayResize(Dgr,Shift+1); ?????????????????
MyCalc(Shift,1); // ??? 1
for(i=Shift; i>=0; i--)
ExtBuffer1[i]=Dgr[i];
return(0);
}
//+---------------------------------------------------------------+
void MyCalc(int Shift,int i Yhigh)
{
int i;
for(i=Shift;i>=0;i--) // should be >
{
Dgr[i]=i*2.5;
// ..... B. added code below
double val=i*2.5; // 2.5
Dgr[i]=MathSin(3.14159*val/149)+Yhigh; // 149 ,,, Origina code ...
Dgr[i]=MathSin(3.14159*val/180)+Yhigh; changing the /180 to lower degree changes the
frequency
// ..... B. added above code
//Dgr[i]=MathSin(3.14159*Dgr[i]/180)+Yhigh; // Original Dgr[i]=MathSin(3.14159*-
Dgr[i]/180)+Yhigh;
}
}
This code below is the same code, but with my attempt at creating the 2nd sin wave code added to the original code.
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2 // org 1
#property indicator_color1 Red //
#property indicator_color2 Orange //
//---- indicator buffers
double ExtBuffer1[];
double ExtBuffer2[];
double Dgr[];
double Dgr2[];
extern datetime StartTime=D'1999.11.10 00:00';
//+---------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE); // 0
SetIndexStyle(1,DRAW_LINE); // 0
SetLevelValue(0,0); //
//----indicator buffers mapping //
SetIndexBuffer(0,ExtBuffer1);
SetIndexBuffer(1,ExtBuffer2);
SetIndexShift(0,25); // B. added
SetIndexShift(1,25); // B. added
//---- initialization done
return(0);
}
//+---------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+---------------------------------------------------------------+
int start()
{
int Shift;
int i;
Shift=iBarShift(Symbol(),PERIOD_D1,StartTime); // Only run on PERIOD_D1
ArrayResize(Dgr,Shift+1); // ArrayResize(Dgr,Shift+1); ?????????????????
ArrayResize(Dgr2,Shift+1);
MyCalc(Shift,1); // ??? 1
for(i=Shift; i>=0; i--)
ExtBuffer1[i]=Dgr[i];
ExtBuffer2[i]=Dgr2[i];
return(0);
}
//+---------------------------------------------------------------+
void MyCalc(int Shift, int Yhigh)
{
int i;
for(i=Shift;i>=0;i--) // should be >
{
Dgr[i]=i*2.5;
Dgr2[i]=i*2.5;
double val=i*2.5; // 2.5
double val2=i*2.5; // 2.5
Dgr[i]=MathSin(3.14159*val/149)+Yhigh; // 149
Dgr2[i]=MathSin(3.14159*val2/49)+Yhigh; // 49
}
}

You've just mixed up some lines of your code. Try the following.
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Orange
//---- indicator buffers
double ExtBuffer1[], ExtBuffer2[];
double Dgr1[], Dgr2[];
extern datetime StartTime=D'1999.11.10 00:00';
//+---------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtBuffer1); SetIndexShift(0,25);
SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,ExtBuffer2); SetIndexShift(1,25);
SetLevelValue(0,0);
return(0);
}
//+---------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+---------------------------------------------------------------+
int start()
{
int Shift;
int i;
Shift=iBarShift(Symbol(),PERIOD_D1,StartTime);
ArrayResize(Dgr1,Shift+1); ArrayResize(Dgr2,Shift+1);
MyCalc(Shift,1);
for(i=Shift; i>=0; i--)
{
ExtBuffer1[i]=Dgr1[i];
ExtBuffer2[i]=Dgr2[i];
}
return(0);
}
//+---------------------------------------------------------------+
void MyCalc(int Shift,int Yhigh)
{
int i;
for(i=Shift;i>=0;i--)
{
Dgr1[i]=i*2.5; Dgr2[i]=i*2.5;
double val1=i*2.5;
double val2=i*2.5;
Dgr1[i]=MathSin(3.14159*val1/149)+Yhigh;
Dgr2[i]=MathSin(3.14159*val2/49)+Yhigh;
}
}

Related

OnClick inside CAppDialog not handle CBmpButton Clicks

I am trying to create a Dialog with two CBmpButtons. The UX works fine BUT when the programs needs to handle the button clicks just detects the first button and no the other one. Am I doing something wrong in here? I have made another Panel using CAppDialog and the code handles all the button clicks perfectly but not in this one. Thanks in advance.
This is my code:
Class
#include <Controls\Dialog.mqh>
#include <Controls\BmpButton.mqh>
class Tester : public CAppDialog
{
private:
CBmpButton btn_trendline;
CBmpButton btn_crosshair;
public:
Tester(void) {};
~Tester(void) {}
virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2);
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
protected:
bool CreateTLBtn();
bool CreateCHBtn();
void OnClickedTLBtn();
void OnClickedCHBtn();
};
EVENT_MAP_BEGIN(Tester)
ON_EVENT(ON_CLICK,btn_crosshair,OnClickedCHBtn)
ON_EVENT(ON_CLICK,btn_trendline,OnClickedTLBtn)
EVENT_MAP_END(CAppDialog)
bool Tester::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
if ( !CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2) )
return false;
if ( !CreateCHBtn() )
return false;
if ( !CreateTLBtn() )
return false;
return true;
}
bool Tester::CreateCHBtn(void)
{
if ( !btn_crosshair.Create(0, "crosshair_btn", 0, 5, 3, 15, 15) )
return false;
btn_crosshair.BmpNames("\\Images\\ic_test_off.bmp","\\Images\\ic_test_on.bmp");
btn_trendline.Locking(true);
if ( !Add(btn_crosshair) )
return false;
return true;
}
bool Tester::CreateTLBtn(void)
{
if ( !btn_trendline.Create(0, "trendline_btn", 0, 40, 3, 15, 15) )
return false;
btn_trendline.BmpNames("\\Images\\ic_test_off.bmp","\\Images\\ic_test_on.bmp");
btn_trendline.Locking(true);
if ( !Add(btn_trendline) )
return false;
return true;
}
void Tester::OnClickedCHBtn(void)
{
Print("CH");
}
void Tester::OnClickedTLBtn(void)
{
Print("TL");
}
Indicator:
#include <#indicesGodd\Test.mqh>
Tester tester;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
if (!tester.Create(0, "Probando", 0, 5, 5, 300, 150))
return(INIT_FAILED);
if ( !tester.Run() )
return(INIT_FAILED);
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
tester.Destroy(reason);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
{
tester.ChartEvent(id, lparam, dparam, sparam);
}
//+------------------------------------------------------------------+

Arrow object appears only once in visual mode in MQL4

I'm an MQL4 newbie and would like to draw an arrow each time a simple trigger is made.
I don't understand why in the attached code below the arrow appears only once while the comment is displayed every time the trigger is made.
How can I solve this issue?
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double UpBarPlace1 = Close[1]>Open[1];
double UpBarPlace2 = Close[2]>Open[2];
double UpBarPlace3 = Close[3]>Open[3];
double DwBarPlace1 = Close[1]<Open[1];
double DwBarPlace2 = Close[2]<Open[2];
double DwBarPlace3 = Close[3]<Open[3];
if(UpBarPlace1 == 1)
{
Comment ("Sell for 2 range size target ");
ObjectCreate(_Symbol,"Arrow", OBJ_ARROW,0,TimeCurrent(),Close[1]);
} else
{
Comment("Ambiguous");
}
}
The object_name (second parameter in ObjectCreate function) param must by unique for each object.
In your code, the object_name is always equal "Arrow", and this is the reason why the arrow appears only once.
For example, you can create a global counter and iterate it on every arrow create trigger.
ObjectCreate(_Symbol,"Arrow_" + IntegerToString(arrowIndexingCounter), OBJ_ARROW,0,TimeCurrent(),Close[1]);
arrowIndexingCounter++;
object_name -> Name of the object. The name must be unique within a chart, including its subwindows.
ObjectCreate function docs

Cannot synthesize property 'testjj' with incomplete type 'void'

Why I cant use property on void function under header #interface ?
#property (nonatomic) void testjj;
-in m file:
-(void)testjj{
NSLog(#"testjtestj");
}
Cannot synthesize property 'testjj' with incomplete type 'void'
The comments deal with declaring a void variable as an ivar. The comments also elaborate a bit on the difference between void and void pointer. This is important.
It seems you want to have an ivar that is, in fact, a void function. That is of course possible, if you make use of void pointer and not void, as shown below.
// A few functions for later
void f1( int a, int b )
{
NSLog(#"f1");
}
void f2( int a, int b )
{
NSLog(#"f2");
}
void F1( void )
{
NSLog(#"F1");
}
void F2( void )
{
NSLog(#"F2");
}
// Typedef makes life easy
typedef void ( func )( int a, int b );
typedef void ( Func )( void );
// Some class
#interface MyClass : NSObject
// Defining ivars ...
// Typedef is easier
#property (nonatomic) func * ma;
#property (nonatomic) Func * mb;
// Direct is also possible
#property (nonatomic) void ( * na )( int, int );
#property (nonatomic) void ( * nb )( void );
#end
#implementation MyClass
// Assigning the ivars
- ( void ) setIt
{
self.ma = f1;
self.mb = F1;
self.na = f2;
self.nb = F2;
}
// 'Using' the ivars
- ( void ) doIt
{
self.ma ( 1, 2 );
self.mb ();
self.na ( 1, 2 );
self.nb ();
}
// Have some fun
- ( void ) swapIt
{
func * t1 = self.ma;
Func * t2 = self.mb;
self.ma = self.na;
self.mb = self.nb;
self.na = t1;
self.nb = t2;
}
#end
in .h
- (void)testjj;
in .m
self.testjj;
That is OK.

JNA to Go DLL - How do I get String returned from Go Func?

I have a Java program that is using JNA to call a Go Func. Here's the Interface to the Go func in Java:
public interface GPG extends Library {
// GoString class maps to: C type struct { const char *p; GoInt n; }
public class GoString extends Structure {
public static class ByValue extends GoString implements Structure.ByValue {}
public String p;
public long n;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"p","n"});
}
}
// Foreign functions
public GoString.ByValue decrypt(GoString.ByValue encString, GoString.ByValue secretKeyring, GoString.ByValue passphrase);
}
The func signature in Go is:
func decrypt(encString string, secretKeyring string, passphrase string) string
The Go generated C header has:
/* Created by "go tool cgo" - DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern GoString decrypt(GoString p0, GoString p1, GoString p2);
#ifdef __cplusplus
}
#endif
I call the Go Func from Java using this code:
GPG gpg = (GPG) Native.loadLibrary("C:/lib/gpg.dll", GPG.class);
GPG.GoString.ByValue encString = new GPG.GoString.ByValue();
encString.p = value;
encString.n = encString.p.length();
GPG.GoString.ByValue secretKeyring = new GPG.GoString.ByValue();
secretKeyring.p = "c:/gnupg/secring.gpg";
secretKeyring.n = secretKeyring.p.length();
GPG.GoString.ByValue passphrase = new GPG.GoString.ByValue();
passphrase.p = "SecretPassPhrase";
passphrase.n = passphrase.p.length();
GPG.GoString.ByValue decValue = gpg.decrypt(encString, secretKeyring, passphrase);
Clearly the func is being called and processes up to the return of the result string. But it then produces: "panic: runtime error: cgo result has Go pointer"
How do I get a String result back from Go?
Using go version go1.10 windows/amd64, JNA 4.5.1, Java 1.8.0_152
Your GO function should looks like this:
//export decrypt
func decrypt(encString string, secretKeyring string, passphrase string) *C.char {
//... your code here
var str string = "returning string"
return C.CString(str)
}
Java Interface:
public String decrypt(GoString.ByValue encString, GoString.ByValue secretKeyring, GoString.ByValue passphrase);
Your const char * in _GoString_ should use a Pointer instead, then use Pointer.getString() with the provided offset to obtain the actual string.
If Go itself is rejecting a string return value, you'll likely have to instead populate a buffer provided by the caller.

Xcode 5 empty project has a unknown path in header search paths setting

The path is
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
where a FlexLexer.h is there. Don't know why this path or this header file is necessary. Here is source code of FlexLexer.h:
// -*-C++-*-
// FlexLexer.h -- define interfaces for lexical analyzer classes generated
// by flex
// Copyright (c) 1993 The Regents of the University of California.
// All rights reserved.
//
// This code is derived from software contributed to Berkeley by
// Kent Williams and Tom Epperly.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the University nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE.
// This file defines FlexLexer, an abstract class which specifies the
// external interface provided to flex C++ lexer objects, and yyFlexLexer,
// which defines a particular lexer class.
//
// If you want to create multiple lexer classes, you use the -P flag
// to rename each yyFlexLexer to some other xxFlexLexer. You then
// include <FlexLexer.h> in your other sources once per lexer class:
//
// #undef yyFlexLexer
// #define yyFlexLexer xxFlexLexer
// #include <FlexLexer.h>
//
// #undef yyFlexLexer
// #define yyFlexLexer zzFlexLexer
// #include <FlexLexer.h>
// ...
#ifndef __FLEX_LEXER_H
// Never included before - need to define base class.
#define __FLEX_LEXER_H
#include <iostream>
# ifndef FLEX_STD
# define FLEX_STD std::
# endif
extern "C++" {
struct yy_buffer_state;
typedef int yy_state_type;
class FlexLexer {
public:
virtual ~FlexLexer() { }
const char* YYText() const { return yytext; }
size_t YYLeng() const { return yyleng; }
virtual void
yy_switch_to_buffer( struct yy_buffer_state* new_buffer ) = 0;
virtual struct yy_buffer_state*
yy_create_buffer( FLEX_STD istream* s, int size ) = 0;
virtual void yy_delete_buffer( struct yy_buffer_state* b ) = 0;
virtual void yyrestart( FLEX_STD istream* s ) = 0;
virtual int yylex() = 0;
// Call yylex with new input/output sources.
int yylex( FLEX_STD istream* new_in, FLEX_STD ostream* new_out = 0 )
{
switch_streams( new_in, new_out );
return yylex();
}
// Switch to new input/output streams. A nil stream pointer
// indicates "keep the current one".
virtual void switch_streams( FLEX_STD istream* new_in = 0,
FLEX_STD ostream* new_out = 0 ) = 0;
int lineno() const { return yylineno; }
int debug() const { return yy_flex_debug; }
void set_debug( int flag ) { yy_flex_debug = flag; }
protected:
char* yytext;
size_t yyleng;
int yylineno; // only maintained if you use %option yylineno
int yy_flex_debug; // only has effect with -d or "%option debug"
};
}
#endif // FLEXLEXER_H
#if defined(yyFlexLexer) || ! defined(yyFlexLexerOnce)
// Either this is the first time through (yyFlexLexerOnce not defined),
// or this is a repeated include to define a different flavor of
// yyFlexLexer, as discussed in the flex manual.
#define yyFlexLexerOnce
extern "C++" {
class yyFlexLexer : public FlexLexer {
public:
// arg_yyin and arg_yyout default to the cin and cout, but we
// only make that assignment when initializing in yylex().
yyFlexLexer( FLEX_STD istream* arg_yyin = 0, FLEX_STD ostream* arg_yyout = 0 );
virtual ~yyFlexLexer();
void yy_switch_to_buffer( struct yy_buffer_state* new_buffer );
struct yy_buffer_state* yy_create_buffer( FLEX_STD istream* s, int size );
void yy_delete_buffer( struct yy_buffer_state* b );
void yyrestart( FLEX_STD istream* s );
void yypush_buffer_state( struct yy_buffer_state* new_buffer );
void yypop_buffer_state();
virtual int yylex();
virtual void switch_streams( FLEX_STD istream* new_in, FLEX_STD ostream* new_out = 0 );
virtual int yywrap();
protected:
virtual size_t LexerInput( char* buf, size_t max_size );
virtual void LexerOutput( const char* buf, size_t size );
virtual void LexerError( const char* msg );
void yyunput( int c, char* buf_ptr );
int yyinput();
void yy_load_buffer_state();
void yy_init_buffer( struct yy_buffer_state* b, FLEX_STD istream* s );
void yy_flush_buffer( struct yy_buffer_state* b );
int yy_start_stack_ptr;
int yy_start_stack_depth;
int* yy_start_stack;
void yy_push_state( int new_state );
void yy_pop_state();
int yy_top_state();
yy_state_type yy_get_previous_state();
yy_state_type yy_try_NUL_trans( yy_state_type current_state );
int yy_get_next_buffer();
FLEX_STD istream* yyin; // input source for default LexerInput
FLEX_STD ostream* yyout; // output sink for default LexerOutput
// yy_hold_char holds the character lost when yytext is formed.
char yy_hold_char;
// Number of characters read into yy_ch_buf.
size_t yy_n_chars;
// Points to current character in buffer.
char* yy_c_buf_p;
int yy_init; // whether we need to initialize
int yy_start; // start state number
// Flag which is used to allow yywrap()'s to do buffer switches
// instead of setting up a fresh yyin. A bit of a hack ...
int yy_did_buffer_switch_on_eof;
size_t yy_buffer_stack_top; /**< index of top of stack. */
size_t yy_buffer_stack_max; /**< capacity of stack. */
struct yy_buffer_state ** yy_buffer_stack; /**< Stack as an array. */
void yyensure_buffer_stack(void);
// The following are not always needed, but may be depending
// on use of certain flex features (like REJECT or yymore()).
yy_state_type yy_last_accepting_state;
char* yy_last_accepting_cpos;
yy_state_type* yy_state_buf;
yy_state_type* yy_state_ptr;
char* yy_full_match;
int* yy_full_state;
int yy_full_lp;
int yy_lp;
int yy_looking_for_trail_begin;
int yy_more_flag;
int yy_more_len;
int yy_more_offset;
int yy_prev_more_offset;
};
}
#endif // yyFlexLexer || ! yyFlexLexerOnce

Resources