need to store last profit taken

 

Hi guys, i need some help what im trying to do is store the (last profit taken price level) and

use as a condition for entry. A continuation of trend in the same direction.

Thanks for your help.

//+------------------------------------------------------------------+
//| RAYsMaRVI.mq4 |
//| Copyright © 2012, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+

#property copyright "Template Interbank FX, LLC"
#property link "http://www.ibfx.com"
#include <stderror.mqh>

//+------------------------------------------------------------------+
//| Global Variables / Includes |
//+------------------------------------------------------------------+
datetime CurrTime = 0;
datetime PrevTime = 0;
string Sym = "";
int TimeFrame = 0;
int Shift = 1;
int SymDigits = 5;
double SymPoints = 0.0001;

//+------------------------------------------------------------------+
//| Expert User Inputs |
//+------------------------------------------------------------------+
extern bool UseCompletedBars = true;
extern int RVIPeriods = 14;
extern double Lots = 0.01;
extern int MAGICx = 1235;
extern int ProfitTarget = 50;
extern int StopLoss = 100;
extern int Slippage = 3;
extern int MovingPeriod = 14;
extern int MovingShift = 5;
extern int CDFL = 2;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+

int init()
{
Sym = Symbol();
TimeFrame = Period();
SymPoints = MarketInfo( Sym, MODE_POINT );
SymDigits = MarketInfo( Sym, MODE_DIGITS );
//---
if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; }
else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; }

//----
return(0);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() { return(0); }

//+------------------------------------------------------------------+
//| Expert start function |
//+------------------------------------------------------------------+
int start()
{
int RealTime = 0;
if( UseCompletedBars )
{
CurrTime = iTime(Sym, TimeFrame, 1 );
if( CurrTime == PrevTime )
{
return(0);
}
//---- Update Vars
PrevTime = CurrTime;
RealTime = 1;
}

//---- Need to chek for a new Signal?
if( CountAll( Sym, MAGICx) == 0)
{

//---- Indicator 1 Values
double Indicator1CurrentValue = iRVI(NULL,0, RVIPeriods,MODE_MAIN,0+RealTime);
double Indicator1PreviousValue = iRVI(NULL,0, RVIPeriods,MODE_MAIN,1+RealTime);
//---- Indicator 2 Values
double Indicator2CurrentValue = iRVI(NULL,0, RVIPeriods,MODE_SIGNAL,0+RealTime);
double Indicator2PreviousValue = iRVI(NULL,0, RVIPeriods,MODE_SIGNAL,1+RealTime);

double ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

//---- Moving Average Cross System


// && Indicator1PreviousValue <= Indicator2PreviousValue
// && Indicator1PreviousValue >= Indicator2PreviousValue
// && Indicator1PreviousValue <0.0
// && Indicator1PreviousValue >0.0
// && Close[1]<ma

if( Indicator1CurrentValue > Indicator2CurrentValue ) { EnterLong(Sym, Lots, ""); }
else if( Indicator1CurrentValue < Indicator2CurrentValue ) { EnterShrt(Sym, Lots, ""); }
}
else CheckForClose();

//----

//----
return(0);
}


//+------------------------------------------------------------------+
//| Check For Close Functions |
//+------------------------------------------------------------------+

void CheckForClose()
{
//-------- go trading only for first tiks of new bar
// if(Volume[0]>1) return;

//------- get signal values

double ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
double Indicator1CurrentValue = iRVI(NULL,0, RVIPeriods,MODE_MAIN,0);
double Indicator2CurrentValue = iRVI(NULL,0, RVIPeriods,MODE_SIGNAL,0);
//-------

for ( int i = OrdersTotal() - 1; i >= 0; i-- )
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICx || OrderSymbol()!=Symbol()) continue;

//---- check order type

if( OrderType()==OP_BUY )
{

// || Close[1]<ma-CDFL*SymPoints

// if( Indicator1CurrentValue < Indicator2CurrentValue ||

if( Close[1]<ma-CDFL*SymPoints ) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
// iTotalTrades=0;
}

if( OrderType()==OP_SELL )

// || Close[1]>ma+CDFL*SymPoints
{
// if( Indicator1CurrentValue > Indicator2CurrentValue ||

if( Close[1]>ma+CDFL*SymPoints ) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
// iTotalTrades=0;
}
}
return;
}

//+------------------------------------------------------------------+
//| Expert Custom Functions |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| CountAll() |
//+------------------------------------------------------------------+

int CountAll( string Symbole, int Magic )
{
//----
int count = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if ( OrderMagicNumber() != MAGICx ) continue;
if ( OrderSymbol() != Symbole ) continue;

if ( OrderType() == OP_BUY ) { count++; }
else if ( OrderType() == OP_SELL ) { count++; }
}
//----
return(count);
}

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

//+------------------------------------------------------------------+
//| Calculate Stop Long |
//+------------------------------------------------------------------+
double StopLong(double price,double stop,double point,double SymDgts )
{
if(stop==0) { return(0); }
else { return(NormalizeDouble( price-(stop*point),SymDgts)); }
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Calculate Stop Short |
//+------------------------------------------------------------------+
double StopShrt(double price,double stop,double point,double SymDgts )
{
if(stop==0) { return(0); }
else { return(NormalizeDouble( price+(stop*point),SymDgts)); }
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Calculate Profit Target Long |
//+------------------------------------------------------------------+
double TakeLong(double price,double take,double point,double SymDgts )
{
if(take==0) { return(0);}
else { return(NormalizeDouble( price+(take*point),SymDgts));}
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Calculate Profit Target Long |
//+------------------------------------------------------------------+
double TakeShrt(double price,double take,double point,double SymDgts )
{
if(take==0) { return(0);}
else { return(NormalizeDouble( price-(take*point),SymDgts));}
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Place Long Order |
//+------------------------------------------------------------------+

int EnterLong( string FinalSymbol, double FinalLots, string EA_Comment )
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;

while( !OrderLoop )
{
while( IsTradeContextBusy() ) { Sleep( 10 ); }

RefreshRates();
double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits );
double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits );

Ticket = OrderSend( FinalSymbol, OP_BUY, FinalLots, SymAsk, 0, 0.0, 0.0, EA_Comment, MAGICx, 0, CLR_NONE );

int Err=GetLastError();

switch (Err)
{
//---- Success
case ERR_NO_ERROR: OrderLoop = true;
if( OrderSelect( Ticket, SELECT_BY_TICKET ) )
{ OrderModify( Ticket, OrderOpenPrice(), StopLong(SymBid,StopLoss, SymPoints,SymDigits), TakeLong(SymAsk,ProfitTarget,SymPoints,SymDigits), 0, CLR_NONE ); }
break;

//---- Retry Error
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;

//---- Fatal known Error
case ERR_INVALID_STOPS: OrderLoop = true; Print( "Invalid Stops" ); break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print( "Invalid Lots" ); break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print( "Market Close" ); break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print( "Trades Disabled" ); break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print( "Not Enough Money" ); break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print( "Too Many Orders" ); break;

//---- Fatal Unknown Error
case ERR_NO_RESULT:
default: OrderLoop = true; Print( "Unknown Error - " + Err ); break;
//----
}
// end switch
if( TryCount > 10) { OrderLoop = true; }
}
//----
return(Ticket);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Place Shrt Order |
//+------------------------------------------------------------------+

int EnterShrt( string FinalSymbol, double FinalLots, string EA_Comment )
{
int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0;

while( !OrderLoop )
{
while( IsTradeContextBusy() ) { Sleep( 10 ); }

RefreshRates();
double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits );
double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits );

Ticket = OrderSend( FinalSymbol, OP_SELL, FinalLots, SymBid, 0, 0, 0, EA_Comment, MAGICx, 0, CLR_NONE );

int Err=GetLastError();

switch (Err)
{
//---- Success
case ERR_NO_ERROR: OrderLoop = true;
if( OrderSelect( Ticket, SELECT_BY_TICKET ) )
{ OrderModify( Ticket, OrderOpenPrice(), StopShrt(SymAsk,StopLoss, SymPoints,SymDigits), TakeShrt(SymBid,ProfitTarget, SymPoints,SymDigits), 0, CLR_NONE ); }
break;

//---- Retry Error
case ERR_SERVER_BUSY:
case ERR_NO_CONNECTION:
case ERR_INVALID_PRICE:
case ERR_OFF_QUOTES:
case ERR_BROKER_BUSY:
case ERR_TRADE_CONTEXT_BUSY: TryCount++; break;
case ERR_PRICE_CHANGED:
case ERR_REQUOTE: continue;

//---- Fatal known Error
case ERR_INVALID_STOPS: OrderLoop = true; Print( "Invalid Stops" ); break;
case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Print( "Invalid Lots" ); break;
case ERR_MARKET_CLOSED: OrderLoop = true; Print( "Market Close" ); break;
case ERR_TRADE_DISABLED: OrderLoop = true; Print( "Trades Disabled" ); break;
case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Print( "Not Enough Money" ); break;
case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Print( "Too Many Orders" ); break;

//---- Fatal Unknown Error
case ERR_NO_RESULT:
default: OrderLoop = true; Print( "Unknown Error - " + Err ); break;
//----
}
// end switch
if( TryCount > 10) { OrderLoop = true; }
}
//----
return(Ticket);
}

 

re-post using src button, please :)

so we can copy and test it on our machines to see where it wrong

 
ray2955:


Hi, im having a problem its saying text to long when i click " Add your comment".
 
delete your previous post please, its double posting, make copy from your metaeditor and use src button and paste it.
 
onewithzachy:
delete your previous post please, its double posting, make copy from your metaeditor and use src button and paste it.


Im following your suggestion and I even trimmed the fat from the program, but Im still getting text to long.

Ill try Attaching the file.

Files:
raysmarvi.mq4  13 kb
 

Thanks im going to try this.
Reason: