Need for help Programming expert system! thanks a lot!

 

Say Hi to everybody!

I am studying hard at the mql4 now,it is really hard to learn a new thing.

As a biginner,I write expert system by useing other peoples code block,just by conbining them together.And sometime the mistake comes out.

I am designning a expert system that is based on Price and Volume Trend (PVT),see https://www.mql5.com/en/code/7061.

In my system I use two moving average of PVT as Buy/Sell singnal, if short-term moving average crosse and above the long-term moving average,open buy position and flat short position,and if short-term moving average crosse and below the long-term moving average,open sell position and flat long position.

I make the above base concept as a indicator and test it manully,the indicator works well.When come to the expert system,my cordes can be complied without any errors.But in expert testing,my code can’t give out any singnal.i don’t know what’s wrong with my codes.

Could someone good at mql4 give me a answer?Thanks a lot.

The following is my codes:

#property copyright "2008.04.22 Bili.private"

#property link "http://www.swifttrade.com"

//---- input parameters

extern double TakeProfit=130.0;

extern double Lots=0.1;

extern double TrailingStop=35.0;

extern int ShorttermSignalMa=5; //Pvtsum short-term ma Period

extern int LongtermSignalMa=8; //Pvtsum long-term ma Period

int init()

{

//----

//----

return(0);

}

int deinit()

{

//----

//----

return(0);

}

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

//| expert system PVT moving average function |

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

double PVT(int PvtMa)

{

double ExtMapBuffer1[]; // PVT

double ExtMapBuffer2[]; // PVT sum

double ExtMapBuffer3[]; //PVT ma

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0)

{

counted_bars--;

}

else return(0);

limit=Bars-counted_bars;

for(int i=0; i<limit; i++)

{

ExtMapBuffer1[i]= ((Close (i) - Close (i - 1)) / Close (i - 1)) * Volume(i);

}

for(i=0; i<limit; i++)

{

ExtMapBuffer2[i]+= ExtMapBuffer1[i];

}

for(i=0; i<limit; i++)

{

ExtMapBuffer3[i]=iMAOnArray(ExtMapBuffer2,Bars,PvtMa,0,MODE_SMA,i);

}

return(ExtMapBuffer3);

}

// -------------------------------------------------------------

int Crossed (double line1, double line2)

{

static int last_direction = 0;

static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up

if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed

{

last_direction = current_dirction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

int cnt, ticket, total;

double PvtShortMa, PvtLongMa;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<20)

{

Print("TakeProfit less than 20");

return(0); // check TakeProfit

}

PvtShortMa = PvtSumMa(PvtShortMa);

PvtLongMa = PvtSumMa(PvtLongMa);

int isCrossed = Crossed (PvtShortMa, PvtLongMa);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

if(isCrossed == 2)

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

if(OrderStopLoss()<Bid-Point*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

// should it be closed?

if(isCrossed == 1)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

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

Reason: