some issues with my indicator

 

Hi!

Since last week i've been trying to develop an indicator to tell me when to enter/close trades, to give an idea of how much profit /loss the trade was and the total profit of all trades.

So far I not doing bad but i have some issues. Here's the script:

//--------------------------------------------------------+

extern bool Show_Alert = true;

extern bool Display_Sell = true;

extern bool Display_Buy = true;

extern bool Display_Close = true;

extern string Start_Counting_from = "0 for all data 1000 will start from the last 1000bars etc";

extern int startcount = 288;



string objname2="Profitlabel";

double upArrow[];

double downArrow[];

string PatternText[5000];

//--------------------------------------------------------+

int init()

{



}



//--------------------------------------------------------+

int deinit()

{

ObjectsDeleteAll(0, OBJ_TEXT);

ObjectDelete(objname2);

return(0);

}

//--------------------------------------------------------+

int start()

{

double Range, AvgRange;

int counter, setalert;

static datetime prevtime = 0;

int shift;

int shift9;

int shiftt;

int shift1;

string period, prof;

int setPattern = 0;

int alert = 0;

int info;

int proftotb, proftots, proftot;

double ma5o, ma5c, stochm, stochs, macd, macdm, macdmp, macds, rsi9, spb, cpb, sps, cps, profits, profitb, profit, rsi3, rsi13;

//----

if(prevtime == Time[0])

{

return(0);

}

prevtime = Time[0];

//----



//----

for(int j = 0; j < Bars; j++)

{

PatternText[j] = "pattern-" + j;

}

//----

info = 1;

proftot = 0;

setalert = 0;

proftotb = 0;

proftots = 0;

int Corner2 = 1;

if( startcount == 0)

{

shift9 = Bars - 1;

}else

{

shift9 = startcount;

}

for (shift = shift9 ; shift >= 0; shift--)

{

counter = shift;

Range = 0;

AvgRange = 0;

for(counter = shift; counter <= shift + 9; counter++)

{

AvgRange = AvgRange + MathAbs(High[counter] - Low[counter]);

}

Range = AvgRange / 10;

shift1 = shift + 1;

ma5c=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,shift);

ma5o=iMA(NULL,0,5,0,MODE_EMA,PRICE_OPEN,shift);

macdm=iMACD(NULL,0,12,26,8,PRICE_CLOSE,MODE_MAIN,shift);

macdmp=iMACD(NULL,0,12,26,8,PRICE_CLOSE,MODE_MAIN,shift1);

macds=iMACD(NULL,0,12,26,8,PRICE_CLOSE,MODE_SIGNAL,shift);

rsi9=iRSI(NULL,0,9,PRICE_CLOSE,shift);



// Sell

// Check for Sell entries and closes

//

//Check for Sell entry

if((ma5o > ma5c) && (rsi9 < 50) && (macdm < macdmp) && (macdm < macds) && (info ==1))

{

if(Display_Sell == true)

{

info = 3;

sps = ((Open[shift]+Close[shift])/2) / Point;

ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift],

High[shift] + Range*0.5);

ObjectSetText(PatternText[shift], "-", 10,

"Courier", Red);

downArrow[shift1] = High[shift1] + Range*0.5;

if(setalert ==0 && Show_Alert == true)

{

Alert("Sell ", Symbol());

setalert =1;

}

}

}

//Check for Close

if(((ma5o < ma5c)|| (macds < macdm)) && (info ==3) )

{

if(Display_Close == true)

{

info = 1;

cps= ((Open[shift]+Close[shift])/2) / Point;

profits = (sps - cps)- ((Ask-Bid)/Point);

ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift],

High[shift] + Range*0.5);

ObjectSetText(PatternText[shift], DoubleToStr(profits,0), 10,

"Courier", SaddleBrown);

downArrow[shift1] = High[shift1] + Range*0.5;

proftots = proftots + profits;

if(setalert ==1 && Show_Alert == true)

{

Alert("Close ", Symbol());

setalert =0;

}

}



}

// Buy

// Check for Buy entries and closes

//

// search for Buy entry

if((ma5o < ma5c) && (rsi9 > 50) && (macdm > macdmp) && (macdm > macds) && (info ==1))

{

if(Display_Buy == true)

{

info = 2;

spb = ((Open[shift]+Close[shift])/2) / Point;

ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift],

Low[shift] - Range*0.5);

ObjectSetText(PatternText[shift], "+", 10,

"Courier", LimeGreen);

downArrow[shift1] = High[shift1] + Range*0.5;

if(setalert ==0 && Show_Alert == true)

{

Alert("Buy ", Symbol());

setalert =2;

}

}



}

//Check for Close

if(((ma5o > ma5c)|| (macds > macdm)) && (info ==2) )

{

if(Display_Close == true)

{

info = 1;

cpb= ((Open[shift]+Close[shift])/2) / Point;

profitb = (cpb - spb)- ((Ask-Bid)/Point);

ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift],

Low[shift] - Range*0.5);

ObjectSetText(PatternText[shift], DoubleToStr(profitb,0), 10,

"Courier", SaddleBrown);

downArrow[shift1] = High[shift1] + Range*0.5;

proftotb = proftotb + profitb;

if(setalert ==2 && Show_Alert == true)

{

Alert("Close ", Symbol());

setalert =0;

}

}



}

//Calculate overall profit

//underdevelopment

//results may not be acurate

if( shift== 0)

{

proftot = proftotb + proftots;

ObjectCreate(objname2, OBJ_LABEL, 0, 0, 0);

ObjectSet(objname2, OBJPROP_CORNER, Corner2);

ObjectSet(objname2, OBJPROP_XDISTANCE, 10);

ObjectSet(objname2, OBJPROP_YDISTANCE, 20);

ObjectSetText(objname2, "Total Profit (not accurate)= " +DoubleToStr(proftot,0), 10,"Courier", White);

}

}

My main problems are:

-> Everytime a new bar opens all the alerts are shown again. Is it possible to just show when the entry or close position occur?

-> When i start the platform the labels are all mixed up and i have to go to the indicator and switch something so every number goes to the right position. why is this hapening?

-> The profit part is wrong and one of the main reasons is because i cant get the Bid/Ask price of that bar. Is it possible to know that?

Thanks

 

  1. string PatternText[5000];
    for(int j = 0; j < Bars; j++){
       PatternText[j] = "pattern-" + j;
    }
    
    PatternText[] are constants can be done in init. When Bars exceeds 5000, exceeding array bounds.
  2. extern bool Display_Buy = true;
    if(Display_Sell == true)
    
    if (true==true) is redundant. if(Display_Sell)..
    ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift],
    High[shift] + Range*0.5);
    
    Once created, the object exists and create fails - the object is NOT moved. After a new bar is created you create object Pattern-1 identical to Pattern-0. You never delete the original. Don't use shift in object names, Time[shift] is unique.
    void TLine( string name, datetime T0, double P0, datetime T1, double P1
              , color clr, bool ray=false ){                #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        /**/ if(ObjectMove( name, 0, T0, P0 ))      ObjectMove(name, 1, T1, P1);
        else if(!ObjectCreate( name, OBJ_TREND, WINDOW_MAIN, T0, P0, T1, P1 ))
            Alert("ObjectCreate(",name,",TREND) failed: ", GetLastError() );
        else if (!ObjectSet( name, OBJPROP_RAY, ray ))
            Alert("ObjectSet(", name, ",Ray) failed: ", GetLastError());
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [2] failed: ", GetLastError());
        string  P0t = PriceToStr(P0);           if (MathAbs(P0 - P1) >= Point)
                P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));
        if (!ObjectSetText(name, P0t, 10))
            Alert("ObjectSetText(",name,") [2] failed: ", GetLastError());
    }
    void HLine(string name, double P0, color clr){  //      #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        /**/ if (ObjectMove( name, 0, Time[0], P0 )){}
        else if(!ObjectCreate( name, OBJ_HLINE, WINDOW_MAIN, Time[0], P0 ))
            Alert("ObjectCreate(",name,",HLINE) failed: ", GetLastError() );
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
        if (!ObjectSetText(name, PriceToStr(P0), 10))
            Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
    }
    

 

Well first of all thank's for your help.

I still i don't get the void thing.

So you said don't use shift in object names ( Time[shift] ) then you wrote

void TLine( string name, datetime T0, double P0, datetime T1, double P1, color clr, bool ray=false )

Since the object creation is inside the cycle for(), how can i not use something like shift to determine the datetime T0 and datetime T1?

And i dont get the part where you write

string  P0t = PriceToStr(P0); 
if (MathAbs(P0 - P1) >= Point)
P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));

What does stringconcatenate does ??

sory for my lack of knowledge.

and once again thank's for helping

 
  1. Since the object creation is inside the cycle for(), how can i not use something like shift to determine the datetime T0 and datetime T1?
    You were using names line Pattern-1, Pattern-2. The 1 and 2 was the shift. I didn't say you couldn't use "Pattern-"+Time[shift].
  2. What does stringconcatenate does ??

    RTFM
 
WHRoeder:
  1. Since the object creation is inside the cycle for(), how can i not use something like shift to determine the datetime T0 and datetime T1?
    You were using names line Pattern-1, Pattern-2. The 1 and 2 was the shift. I didn't say you couldn't use "Pattern-"+Time[shift].
  2. What does stringconcatenate does ??

    RTFM
Once again thank you