issue with checking result of last two orders from the same Symbol - mql4

The following code works if it's attached to only a single pair, but if the EA is attached to multiple pairs, it also counts the result of cross pair and results in the wrong alert.
The idea of the following code is to decide if the attached pair has the last two trade ends positively. It should not count the other pair result from the history.
void Alert_if_the_last_two_ordes_for_same_pair_was_positive()
{
double profit = 0;
int orderId = -1;
datetime lastCloseTime = 0;
int cnt = OrdersHistoryTotal();
for(int i=0; i < cnt; i++)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime < OrderCloseTime() && orderId == 114)
{
lastCloseTime = OrderCloseTime();
profit = OrderProfit();
}
}
double profit1 = 0;
int orderId1 = -1;
datetime lastCloseTime1 = 0;
int cnt1 = OrdersHistoryTotal()-1;
for(int i1=0; i1 < cnt1; i1++)
{
if(!OrderSelect(i1, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId1 = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime1 < OrderCloseTime() && orderId1 == 114)
{
lastCloseTime1 = OrderCloseTime();
profit1 = OrderProfit();
}
}
double profit2 = 0;
int orderId2 = -1;
datetime lastCloseTime2 = 0;
int cnt2 = OrdersHistoryTotal()-2;
for(int i2=0; i2 < cnt2; i2++)
{
if(!OrderSelect(i2, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId2 = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime2 < OrderCloseTime() && orderId2 == 114)
{
lastCloseTime2 = OrderCloseTime();
profit2 = OrderProfit();
}
}
if(profit > 0 && profit1 > 0 )
{
Alert(" profit > 0 && profit1 = up for " + Symbol());
}

Related

How to get a profit of Nth open position MQL4 / MT4?

I want to create a function that can get random profit open position.
For example :
a profit of the Last 2 Open Position of overall Total Position
a profit of the First 3 Open Position of overall Total Position
Below function seems it only gets the First Open Position Profit :
double BuyProfit()
{
Price = 0;
datetime EarliestOrder = TimeCurrent();
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(EarliestOrder > OrderOpenTime())
{
EarliestOrder = OrderOpenTime();
Price = OrderProfit()+OrderSwap()+OrderCommission();
}
}
}
}
return Price;
}
Q : "How to get a profit of Nth open position MQL4/MT4?I want to create a function that can get random profit open position."
You may take this rapid-prototype to complete your actual logic code:
double nthNetPROFIT( const int Nth ) {
if ( Nth > OrdersTotal() ) return( EMPTY );
int softCount = 0;
double nthNetProfit = EMPTY;,
for( int ii = 0; ii < OrdersTotal(); ii++ ) {
if ( OrderSelect( ii, SELECT_BY_POS ) ) {
if ( OrderType() == OP_BUY // DEPENDS ON ACTUAL LOGIC
&& OrderSymbol() == Symbol() // DEPENDS ON ACTUAL LOGIC
&& OrderMagicNumber() == MagicNumber // DEPENDS ON ACTUAL LOGIC
) {
... // DEPENDS ON ACTUAL LOGIC
if ( ++softCount == Nth ){
nthNetProfit = OrderProfit()
+ OrderSwap()
+ OrderCommission();
break;
}
}
}
return( NormalizeDouble( nthNetProfit, 2 ) );
}

Run-Time Check Failure #2 - Stack around the variable 'maxlong' was corrupted

I wrote a code to print the length of the longest line from my input and print out as much as possible of the longest line(There is a maximum length of what I can output the longest line). But I got this error. I tried everything I could yet still no clue. Is there anyone who might help?
#include<stdio.h>
#define MAXLINE 10
int getline(char line[], int len) {
int i,c,max;
i = 0;
max = 0;
while ((c = getchar()) != EOF&&c!='\n') {
if (i < len) {
line[i] = c;
}
i += 1;
}
if (i < len) {
if (c == '\n') {
line[i] = c;
i += 1;
}
line[i] = '\0';
}
else if (i >= len) {
line[len] = '\0';
}
return i;
}
void copy(char from[], char to[]) {
int i = 0;
while (from[i] != '\0') {
to[i] = from[i];
i += 1;
}
to[i] = '\0';
}
main() {
char longline[MAXLINE];
char longestline[MAXLINE];
char maxlong[MAXLINE];
int length;
int max = 0;
int maxline = 0;
while ((length = getline(longline, MAXLINE)) > 0) {
if (length > max && length < MAXLINE) {
copy(longline, longestline);
max = length;
}
else if (length > MAXLINE) {
if (length > maxline) {
maxline = length;
copy(longline, maxlong);
}
}
}
if (maxline == 0) {
printf("%s", longestline);
}
else {
printf("%s\n%d\n", maxlong, maxline);
}
}

How to separate two Low[1] by if statement?

Example, previous low is 1.55. When next candle low is 1.66, then next next candle low is 1.44 which is lower than previous low (1.55) then do something. How to achieve it?
double previous_low = 0;
double new_low = 0;
double new_low2 = 0;
if (((previous_low==0)&&(Low[1] < Low[2])){
previous_low = Low[1];
}
if (previous_low != 0){
if (Low[1] < previous_low){
new_low2 = Low[1]; //it fail
}
if (Low[0] < previous_low){
new_low2 = Low[0]; //it works only if next 1 candle is lower than previous. It fail if next 2 candle is lower than previous_low.
}
}
It can be achive by bubblesort
public class BubbleSort
{
void bubbleSort(double arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
System.out.print("first value :"+arr[j]+" greater than" +"previos :"+arr[j+1] + " ");
System.out.println();
// swap arr[j+1] and arr[i]
double temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(double arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
double arr[] = {1.66, 1.55, 1.44};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}

mql4 check if order exists executed multiple times

I'm using this code to check if orders are existing for the price buy1 and sell1 in my code.
Somehow some orders get executed twice.
Shouldn't happen because i'm checking if there is any open order with the same takeprofit.
Anyone able to see whats wrong?
bool CheckBuyOrder1(double buy1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderTakeProfit() == buy1_tp && OrderComment() == "buy" )
return(true);
}
return(false);
}
bool CheckSellOrder1(double sell1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderTakeProfit() == sell1_tp && OrderComment() == "sell" )
return(true);
}
return(false);
}
int totalOrders = OrdersTotal();
void OnTick()
{
if(totalOrders != OrdersTotal()){
double vbid = MarketInfo("EURUSD",MODE_BID);
double bid = NormalizeDouble(vbid, 3);
double market_buy_tp = bid;
double buy1= bid + 0.002;
double buy1_tp= bid + 0.003;
if(CheckOpenOrders1(market_buy_tp)==false && CheckBuyOrder1(buy1_tp)==false){
int ticket9=OrderSend(Symbol(),OP_BUYSTOP,Lots,buy1,MaxSlippage,0,buy1_tp,"buy",16380,0,clrGreen);
}
double market_sell_tp = bid;
double sell1 = bid - 0.003;
double sell1_tp= bid - 0.004;
if(CheckOpenOrdersSell1(market_sell_tp)==false && CheckSellOrder1(sell1_tp)==false ){
int ticket19=OrderSend(Symbol(),OP_SELLSTOP,Lots,sell1,MaxSlippage,0,sell1_tp,"sell",16380,0,clrGreen);
}
totalOrders = OrdersTotal();
}}
Whenever you try to compare an OrderTakeProfit() value with some other double - keep in mind rounding and float representation.
E.g. if you need to compare 0.10 with another double that you believe is 0.10 - you might be surprised that 0.10 is 0.09(9)6 or 0.10(0)4
That is why sometimes you might not find such an order.
double o = Point / 2; // initialize in OnInit(), declare in global vars
...
bool CheckOrderBuy( double tp ){
for ( int i = OrdersTotal() - 1; i >= 0; i-- ){
if ( !OrderSelect( i, SELECT_BY_POS ) )continue;
if ( fabs( OrderTakeProfit() - tp ) < o
&& OrderType() == OP_BUY
) return true;
}
return false;
}

Boxing Detected Objects in OpenCV

I am currently implementing a connected components algorithm and the last step of the algorithm requires me to enclose the objects I found in a box. I have attempted to enclose an object in a box and this is the result:
As you can see some of them seem to be enclosed in a box. Some of the lines of the box are not seen unless I stretch the windows of the imshow function's output and also some of them have color when I expected a line with a shade of gray.
My question is: Is the object really getting enclosed since I remember when I ran a similar code of mine into a different OS the lines with color are not see at all but are seen in my computer. Additionally, why are some of the lines in a different color given that I was expecting a shade of gray.
Mat src, src_gray;
Mat dst, detected_edges;
const char* window_name = "THRESHOLDED IMAGE";
/**
* #function connectedComponent
*/
static void connectedComponent(int, void*)
{
Mat test; //dummy
Mat sub;
int newObject = 0;
int zeroTest = 0, nonZero = 0;
int arr[5] = {0,0,0,0,0};
/// Reduce noise with a kernel 3x3
blur( src_gray,detected_edges, Size(3,3) ); //filtering out of noise
namedWindow("INITIAL", WINDOW_NORMAL);
imshow("INITIAL",detected_edges);
resizeWindow("INITIAL", 300, 300);
threshold(detected_edges, detected_edges, 0,255, THRESH_BINARY | THRESH_OTSU);
int** newSub = new int*[detected_edges.rows];
for(int i = 0; i < detected_edges.rows; i++)
newSub[i] = new int[detected_edges.cols];
for(int i = 0; i < detected_edges.rows; i++){
for(int j = 0; j < detected_edges.cols; j++){
newSub[i][j] = 0;
}
}
/*INITIAL MARKING LOOP*/
for(int i = 0; i < detected_edges.rows; i++){
for(int j = 0; j < detected_edges.cols; j++){
if(detected_edges.at<uchar>(i,j) == 0){
if(i-1 < 0 && j-1 < 0){
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}else if(i-1 >= 0 && j-1 < 0){
if(newSub[i-1][j] != 0){
newSub[i][j] = newSub[i-1][j]; //only up has value
}else{
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}
}else if(i-1 < 0 && j-1 >= 0){
if(newSub[i-1][j] != 0){
newSub[i][j] = newSub[i-1][j]; //only left has value
}else{
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}
}else{
if(newSub[i-1][j] == 0 && newSub[i][j-1] == 0){
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}else if(newSub[i-1][j] == newSub[i][j-1]){ //same value
newSub[i][j] = newSub[i-1][j];
}else if((newSub[i-1][j] != 0 && newSub[i][j-1] == 0)){
newSub[i][j] = newSub[i-1][j]; //only up has value
}else if(newSub[i-1][j] == 0 && newSub[i][j-1] != 0 ){
newSub[i][j] = newSub[i][j-1]; //only left has value
}else if(newSub[i-1][j] != newSub[i][j-1]){
newSub[i][j] = newSub[i-1][j]; //different values follow upper's value
}
}
}
}
}
int a = 1;
int maxRows = detected_edges.rows;
int maxCols = detected_edges.cols;
/*CONNECTING PIXELS RIGHT-BOTTOM*/
while(a < newObject){
int update = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(newSub[i][j] == a){
if(i+1 < maxRows && j+1 < maxCols){
if(newSub[i][j+1] > a){ //both points allowed
int value = newSub[i][j+1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
if(newSub[i+1][j] > a){ //both points allowed
int value = newSub[i+1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a; //replace all instances of that value
}
}
}
update = 1;
}
}else if(i+1 > maxRows && j+1 < maxCols){
if(newSub[i][j+1] > a){ //bottom is not allowed
int value = newSub[i][j+1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a; //replace all instances of that value
}
}
}
update = 1;
}
}else if(i+1 < maxRows && j+1 > maxCols){
if(newSub[i+1][j] > a){ //right is not allowed
int value = newSub[i+1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
}
}
}
}
a++;
}
/*CONNECTING PIXELS LEFT-TOP*/
a = newObject;
while(a > 0){
int update = 0;
for(int i = maxRows-1; i > 0; i--){
for(int j = maxCols-1; j > 0 ; j--){
if(newSub[i][j] == a){
if(i-1 >= 0 && j-1 >= 0){
if(newSub[i][j-1] > a){ //both points allowed
int value = newSub[i][j-1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
if(newSub[i-1][j] > a){
int value = newSub[i-1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
}else if(i-1 >= 0 && j-1 < 0){
if(newSub[i][j-1] > a){ //left is not allowed
int value = newSub[i][j-1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){ //replace all instances of that value
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
}else if(i-1 < 0 && j-1 >= 0){
if(newSub[i-1][j] > a){ //top is not allowed
int value = newSub[i-1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){ //replace all instances of that value
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
}
}
}
}
a--;
}
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
int check = 0;
if(newSub[i][j] != 0){
for(int k = 0; k < 5; k++){
if(newSub[i][j] == arr[k]){ //check if there is an instance of the value in the given array of values
check = 1;
break;
}
}
if(check == 0){
for(int r = 0; r < 5; r++){
if(arr[r] == 0){
arr[r] = newSub[i][j]; //if new value is found add to array
break;
}
}
}
}
}
}
/*
I HAVE AN ARRAY CONTAINING ALL VALUES
**/
src.copyTo( sub, detected_edges);
sub = Scalar::all(0);
/*SET AN INTENSITY FOR CORRESPONDING VALUES*/
int intensity = 50;
a = 0;
while(a < 5){
int update = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(newSub[i][j] == arr[a]){
sub.at<uchar>(i,j) = intensity;
}
}
}
a++;
intensity = intensity + 50;
}
a = 250;
/*GETTING MIN-MAX COORDINATES*/
while(a >= 50){
int setter = 0;
int minRow = 0;
int minCol = 0;
int maxRow = 0;
int maxCol = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(sub.at<uchar>(i,j) == a){
if(setter == 0){
minRow = i;
minCol = j;
maxRow = i;
maxCol = j;
setter = 1;
}else{
if(i <= minRow){
minRow = i;
}
else{
if(i > maxRow){
maxRow = i;
}
}
if(j <= minCol){
minCol = j;
}
else{
if(j > maxCol){
maxCol = j;
}
}
}
}
}
}
/*THIS IS WHERE I MAKE MY BOUNDING BOX*/
for(int i = minRow; i < maxRow; i++){
sub.at<uchar>(i,minCol) = 255; //set up the horizontal lines
sub.at<uchar>(i,maxCol) = 255;
}
for(int i = minCol; i < maxCol; i++){
sub.at<uchar>(minRow,i) = 255; //set up the vertical lines
sub.at<uchar>(maxRow,i) = 255;
}
a = a - 50;
}
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow( window_name, dst );
namedWindow("FINAL", WINDOW_NORMAL);
imshow("FINAL",sub); //final output
resizeWindow("FINAL", 300, 300);
for(int i = 0; i < detected_edges.rows; i++)
delete[] newSub[i];
delete[] newSub;
}
/**
* #function main
*/
int main( int, char** argv )
{
/// Load an image
src = imread( argv[1] );
if( src.empty() )
{ return -1; }
/// Create a matrix of the same type and size as src (for dst)
dst.create( src.size(), src.type() );
/// Convert the image to grayscale
cvtColor( src, src_gray, COLOR_BGR2GRAY ); //grayscale for one channel for easy computation
/// Create a window
namedWindow( window_name, WINDOW_NORMAL );
resizeWindow(window_name, 300,300);
/// Show the image
connectedComponent(0, 0);
/// Wait until user exit program by pressing a key
waitKey(0);
return 0;
}

Resources