OrderProfit() Not Correct

To add comments, please log in or register
chua le
383
chua le  
#property copyright "Ch Le"

// External variables
extern double Lots = 0.01;
extern double StopLoss  = 300;
extern double TakeProfit = 600;

extern int Slip = 3;
extern int MagicNumber = 123;


// Global variables

double M15Ema5, M15Ema20,H1Ema5,H1Ema20,H1Ema100;

int CounterUp600520,CounterDown600520;
int CounterUp150520,CounterDown150520; 
int CounterBuy,CounterSell;
int MyTrades;
int BuyTicket;
int SellTicket;
int xMultiply;
int total;
double Profit;

datetime   P600520,T;

// Init function
int init()
{
if(Digits==2) xMultiply =10;
if(Digits==3) xMultiply=100;
if(Digits==5) xMultiply=10000;
}

// Start function
int start()
{

//MyTrades = CountTrades();

// Moving averages
 
 M15Ema5 = iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0);
 M15Ema20 = iMA(NULL,PERIOD_M15,20,0,MODE_EMA,PRICE_CLOSE,0);
 H1Ema5 = iMA(NULL,PERIOD_H1,5,0,MODE_EMA,PRICE_CLOSE,0);
 H1Ema20 = iMA(NULL,PERIOD_H1,20,0,MODE_EMA,PRICE_CLOSE,0);
 H1Ema100 = iMA(NULL,PERIOD_H1,100,0,MODE_EMA,PRICE_CLOSE,0);
 Profit= (iClose(Symbol(),PERIOD_H1,0)-OrderOpenPrice())*xMultiply;

// **************Closing Trades Started*********************

total=OrdersTotal();
//----
for(int i=0; i<total; i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()== OP_BUY && M15Ema5<=M15Ema20 && (CurTime()-OrderOpenTime()>3600) ) 
      {
OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red);
Print ("Close #"+BuyTicket+" "+Symbol()+" : M15Ema5 <=M15Ema20");  
CounterBuy=0;                                           
      }
if(OrderType()== OP_SELL && M15Ema5>=M15Ema20 && (CurTime()-OrderOpenTime()>3600) ) 
      {
OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
Print ("Close #"+SellTicket+" "+Symbol()+" : M15Ema5 >=M15Ema20");  
CounterSell=0;                                          
      }
}

// **************Closing Trades Ended*********************


// **************Opening Trades Started*********************

if (iTime(Symbol(),PERIOD_H1,0)!=P600520)
{   
if (H1Ema5>=H1Ema20 ) 
     {
 CounterDown600520=0;
 CounterUp600520 = CounterUp600520 + 1;            
 if (H1Ema20>=H1Ema100 && CounterUp600520==1 &&  M15Ema5>=M15Ema20 ) 
            {
 if (T!=iTime(Symbol(),PERIOD_H1,0)) BuyTicket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,Ask-StopLoss*Point,0,"Buy Order",MagicNumber,0,Green); 
 if (T!=iTime(Symbol(),PERIOD_H1,0)) Print ("#",BuyTicket," ",Symbol()," : (600520+) H1 E5X20 ");                                              
 T=iTime(Symbol(),PERIOD_H1,0);
 P600520=iTime(Symbol(),PERIOD_H1,0);
 return(0);
             }  
    }         
 if (H1Ema5 <=H1Ema20)
      {
CounterUp600520=0;
CounterDown600520= CounterDown600520 +1;             
if (H1Ema20<=H1Ema100 && CounterDown600520==1 &&  M15Ema5<=M15Ema20 ) 
            {
if (T!=iTime(Symbol(),PERIOD_H1,0)) SellTicket = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,Bid+StopLoss*Point,0,"Sell Order",MagicNumber,0,SkyBlue);
if (T!=iTime(Symbol(),PERIOD_H1,0)) Print ("#",SellTicket," ",Symbol()," : (600520-) H1 E5X20 ");
 T=iTime(Symbol(),PERIOD_H1,0); 
 P600520=iTime(Symbol(),PERIOD_M15,0);
 return(0);
             } 
      }        
}

// **************Opening Trades Ended*********************

// **************Print Profit Started*********************

for(int j=0; j<total; j++)
{

OrderSelect(j,SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY  )
    {
Print ("#",BuyTicket," ","Buy Profit : ", Profit); 
Print ("#",BuyTicket," ","Buy OrderProfit : ", OrderProfit());                                        
    }  
    
if(OrderType()== OP_SELL  )
    {
Print ("#",SellTicket," ","Sell Profit : ",-1*Profit);  
Print ("#",SellTicket," ","Sell OrderProfit : ",OrderProfit());  
    }   
}

// **************Print Profit Ended*********************


}


 

Hello,


I have the above coding. I tried for a long time to figure out why the OrderProfit()  is not correct. For verification purpose, I calculate Profit= (iClose(Symbol(),PERIOD_H1,0)-OrderOpenPrice())*xMultiply,  obviously OrderProfit() output is wrong. Kindly help.

nicholi shen
2392
nicholi shen  
order_profit = OrderProfit() + OrderSwap() + OrderCommission();
chua le
383
chua le  
Even add in OrderSwap and OrderCommision it is not correct too as the Printed OrderProfit is too much different from actual value calculated.
chua le
383
chua le  

Hi,


Could someone advise which part of the code not in order?

Thanks

To add comments, please log in or register