Duplicative Code for Shorts and Longs

To add comments, please log in or register
Arbubu
213
Arbubu  

If you're looking for the same, but opposite things for short and long positions, you will get code which is quite duplicative. The differences will be simple things like use of < rather than > and use of OP_SELL rather than OP_BUY. Do people live with this, or do they try to streamline the code? If you streamline the code it might be a lot easier to make changes to it because you'd only have to make them in one place. I guess if you were to streamline the code you'd need to declare variables as arrays with two items, one for short and one for long, and you might want a function that returned the opposite of a Boolean value if the index in an array was 1 rather than 0. But I'm just wondering if people do this?

William Roeder
20251
William Roeder  
Most just duplicate the code. I write direction independent. You code everything for the buy case and let DIR reverse the sense. It takes some getting use to. Ask/Bid become now.open/now.close. MathMax becomes MathMaxDIR. You read (x-y)*DIR > 0 as x is above y (where above means higher for a buy or lower for a sell.) FWIW
//+------------------------------------------------------------------+
//| Adjust variables for trade direction.                            |
//+------------------------------------------------------------------+
                                        // Export to:
string  op.text;                        // OpenNew ModifyStops start
double  now.open,                       // OpenNew
        now.close,                      //         ModifyStops start
        open.to.Bid,                    //                          OpenNew
        stop.to.Bid,                    //         ModifyStops
        DIR;        // +/-1             // OpenNew ModifyStops start MathMaxDIR
int     op.code;    // OP_BUY/OP_SELL   // OpenNew                   LotSize
bool    need2refresh;                   // Import from RelTradeContext
void    SetDIR(int op){
    if (need2refresh){  RefreshRates(); need2refresh= false; }
    op.code = op;   if (op == OP_BUY){
        now.open    = Ask;              open.to.Bid = Bid-Ask;  // Open at Ask.
        now.close   = Bid;              stop.to.Bid = 0;        // Stop at Bid.
        DIR         = +1.;              op.text     = "Buy";
    } else {
        now.open    = Bid;              open.to.Bid = 0;        // Open at Bid.
        now.close   = Ask;              stop.to.Bid = Bid-Ask;  // Stop at Ask.
        DIR         = -1.;              op.text     = "Sell";
    }
}   // SetDIR
void    Refresh(){
//int   op.code; // OP_BUY/OP_SELL   // Import from SetDIR
    SetDIR(op.code);     return; }          // Refresh and update.
//+------------------------------------------------------------------+
//| Find a local extreme bar value                                   |
//+------------------------------------------------------------------+
int LocalExtreme(int WS, int LEbar=0, double d=INF){
    while(true){
        int LEbarPrev = LEbar;      LEbar = MaximalBar(WS, d, LEbarPrev);
        if (LEbar == LEbarPrev)     return(LEbar);
    }
    //NOTREACHED
}   // LocalExtreme
int MaximalBar(int length, double d=INF, int start=0){
    if (d>=INF) d=DIR;
    if (start+length >= Bars)   length = Bars - start;
    if (d>0)    return( Highest(NULL, 0, MODE_HIGH, length, start) );
    else        return(  Lowest(NULL, 0, MODE_LOW,  length, start) );
}
double MaximalPrice(int length, double d=INF, int start=0){
    if (d>=INF) d=DIR;  int LE = MaximalBar(length, d, start);
    if (d>0)    return(High[LE]);   else return(Low[LE]);
}
//+------------------------------------------------------------------+
//| Miscellaneous functions                                          |
//+------------------------------------------------------------------+
double  MathMaxDIR(double a, double b, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(MathMax(a,b));   return(MathMin(a,b));  }
double  IfD(double b, double s, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(b);              return(s);             }
string  IfS(string b, string s, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(b);              return(s);             }
int     IfI(int b, int s, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(b);              return(s);             }
double  Direction(int op_xxx){  return( 1. - 2. * (op_xxx%2) );                }
string  SDoubleToStr(double p, int d){
                                return(IfS("+","",p>=0)+DoubleToStr(p,d));     }
void    MaximizeDIR(double &a, double b, double d=INF){
                                        a=MathMaxDIR(a,b,d);        return;    }
void    StrApnd(string &v, string a){   v=StringConcatenate(v,a);   return;    }
int     MathMinI(int a, int b){
                        if(a<b) return(a);              return(b);             }
int     Operation(double d){
                        if(d>0) return(OP_BUY);         return(OP_SELL);       }
double  MathCeilDIR(double a, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(MathCeil(a));    return(MathFloor(a));  }
int     MathMaxI(int a, int b){
                        if(a>b) return(a);              return(b);             }
double  MathMinDIR(double a, double b, double d=INF){   //Import DIR from SetDIR
    if(d>=INF) d=DIR;   if(d>0) return(MathMin(a,b));   return(MathMax(a,b));  }
double  MathFloorDIR(double a, double b, double d=INF){
    if(d>=INF) d=DIR;   if(d>0) return(MathFloor(a,b)); return(MathCeil(a,b)); }
void    MinimizeDIR(double &a, double b, double d=INF){
                                        a=MathMinDIR(a,b,d);        return;    }
int     Isign(double d){    if(d>0) return(+1);         return(-1);            }
double  Fractal(int& LEbar, double& atLeast, double d=INF){
    if (d>=INF) d=DIR;
    LEbar += 2;
        int hist = MathMinI(500, Bars);
    while(LEbar < hist){
        int LEbarPrev = LEbar;  LEbar = MaximalBar(5, d, LEbarPrev-2);
        if (d > 0){ double  LEprice = High[LEbar];
            if (LEprice < atLeast){     LEbar = LEbarPrev+3;    continue;   }
        }
        else{               LEprice = Low[LEbar];
            if (LEprice > atLeast){     LEbar = LEbarPrev+3;    continue;   }
        }
        atLeast = LEprice;
        if (LEbar == LEbarPrev) break;
        if (LEbar < LEbarPrev)  LEbar = LEbarPrev+3;
    }
    return(atLeast);
}
...
    double  minGapStop  = MarketInfo(Symbol(), MODE_STOPLEVEL)*Point);
    string  symbolChart = Symbol();
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS) 
    &&  OrderSymbol()      == symbolChart     // My chart
    &&  OrderMagicNumber() == magic.number){  // Only my orders w/
        SetDIR( OrderType() );              // op.code, DIR, now.close
        ...
        SetStops(oo.SL, oo.TP, size, oo.price);
        double  SLmax   = now.close -DIR* minGapStop;
        if ((oo.SL-SLmax)*DIR > 0)              // Above max?
           oo.SL = SLmax;                       // Can't.
        if ((SLorig != NO_SL) &&  (oo.SL-SLorig)*DIR < Point)
            oo.SL = SLorig;                     // SL up only.
Arbubu
213
Arbubu  
Thanks. I've managed it in the way I outlined. The code is quite a bit shorter, but harder to read. I prefer it, but it's probably not for everyone.
Willie Freshour
124
Willie Freshour  

Curious; for speed in relation of finishing work.

(which will finish the work the fastest?  me typing manually or looking for a code)

I found your code and I wonder I can plug it in.

Willie Freshour
124
Willie Freshour  
Willie Freshour:

Curious; for speed in relation of finishing work.

(which will finish the work the fastest?  me typing manually or looking for a code)

I found your code and I wonder I can plug it in.

It appears so difficult I don't know where to begin.  2nd time I'll pass. or not.

Willie Freshour
124
Willie Freshour  
Willie Freshour:

It appears so difficult I don't know where to begin.  2nd time I'll pass. or not.

Something with less typing for buy....because the coding is

if(Cross(0, Open[0] > 110.310)) //Candlestick Open crosses above fixed value


For getting a loop yes? maybe I can write  

ordersend () + -.025 *point

with that the  outcomes ought buy at every .025 change with the cross above the price calculated in its -.025 calculation

  (decending buy order from (i.e. 108.908 next order descending is 108.883)    Merry Christmas.

To add comments, please log in or register