0

谁能帮助我,如何编写MQL4代码来知道结果是增加还是减少..

示例
如果一个结果是第一个0.0543,然后它增加,0.1342然后减少到0.10345,我想在下面的代码中实现它:

int start()
{       
    double val = iCustom( NULL, 0, "SS2009_B", 0, 0, 0 );

    ObjectSet(     "TimeLable11", OBJPROP_CORNER, obCorner );
    ObjectSetText( "TimeLable11", "Result : " + val,
                                  fsize,
                                  "Microsoft Sans Serif",
                                  Yellow
                                  );
    return( 0 );
}

我希望结果有一个增加或减少的通知

例子 :

Result : 0.1849  Increasing
Result : 0.01324 Decreasing
4

2 回答 2

1

举一个简单的例子:

//------------------------------------------------------------------
#property copyright "Copyright 2015"

//------------------------------------------------------------------
// Standard Variables (define a variable to store previous value)
//------------------------------------------------------------------
double  viIndicatorValuePrev            = 0.0000;

int start() {
    double viIndicatorValueNow = iCustom(NULL,0,"SS2009_B",0,0,0);

    //------------------------------------------------
    //Decide on direction (increasing or decreasing)
    //------------------------------------------------
    string vsDirection         = "Same";
    if( viIndicatorValueNow>viIndicatorValuePrev)
        vsDirection         = "Increasing";
    if( viIndicatorValueNow<viIndicatorValuePrev)
        vsDirection         = "Decreasing";
    //------------------------------------------------

    //------------------------------------------------
    // Do your thing here (ie, display stuff)
    //------------------------------------------------
    ObjectSet( "TimeLable11", OBJPROP_CORNER, obCorner);
    ObjectSetText(
        "TimeLable11"
        , "Result : " + viIndicatorValueNow + " (" + vsDirection + ")"
        , fsize, "Microsoft Sans Serif", Yellow
    );

    //------------------------------------------------
    // Store current value for future comparison
    //------------------------------------------------
    viIndicatorValuePrev = viIndicatorValueNow;
    //------------------------------------------------
    return(0);
}

希望这个例子有所帮助。

于 2015-03-04T23:53:28.043 回答
0

Solution to monitor & show delta per each aMarketEVENT call of start() ( upon aMarketQUOTE aka Tick arrival )

First you need a method to store and retain a value, that becomes "old", in the meantime, before the start() ( well actually an OnTick() in the new-MQL century ) is called next time.

A static double is a smart way to go:

static double previousVALUE = EMPTY;                   // initialised to EMPTY

Next, initialise it in accord with a forward-stepping logic of the Custom Indicator "SS2009_B"

if ( previousVALUE == EMPTY ) {
     previousVALUE  = iCustom( NULL,                  // a Symbol
                               0,                     // a TimeFRAME
                               "SS2009_B",
                               0,
                               0,                     // a Buffer# to get value from
                               1                      // step-back one Bar
                               );
    }

For syntax details, review MQL4 documentation

double  iCustom(
                   string symbol,    // symbol
                   int    timeframe, // timeframe
                   string name,      // path/name of the custom indicator compiled program
                   ...               // custom indicator input parameterA (if 
                   ...               // custom indicator input parameterB (if 
                   ...               // custom indicator input parameterC (if necessary)
                   int    mode,      // line index
                   int    shift      // shift
                   );

Finally calculate delta & show it on UI & shuffle to get ready for a next call

double currentVALUE =  iCustom( NULL, 0, "SS2009_B", 0, 0, 0);
double   deltaVALUE =  previousVALUE - currentVALUE;
      previousVALUE =   currentVALUE;    // shuffle [1]<-[0] before RET/next call

if ( deltaVALUE > 0 ){
     ObjectSetText( "TimeLabel11",
                    "RESULT: " + NormalizeDouble( deltaVALUE, Digits ) + "|^|",
                    fsize,
                    "Courier New",
                    Yellow              // can be deltaVALUE specific too :o)
                  );
   }
if ( deltaVALUE < 0 ){
     ObjectSetText( "TimeLabel11",
                    "RESULT: " + NormalizeDouble( deltaVALUE, Digits ) + "|v|",
                    fsize,
                    "Courier New",
                    Cyan               // can be deltaVALUE specific too :o)
                  );
   }

A few remarks on precision of float numbers

You may notice a use of NormalizeDouble()

This is rather important step, to avoid problems with comparison of floats.

If the precision of standard floats ( MQL4 double ) is not sufficient, you may opt-in to use extended precision numbers in MQL4. Check documentation for that.

However, always be carefull on float comparisons.

Best practice, to be on a safer side, is to use both NormalizeDouble() ( as a prevention ) and comparing to some thresholdDELTA like if ( MathAbs( floatA - floatB ) < anEqualityTresholdDELTA ){ ... } rather than if ( floatA == floatB ) {...}

于 2014-09-08T08:01:57.110 回答