mardi 7 avril 2015

TP Limit Order

Hi,

I need help to change my Grid EA from Market Order to Limit Order.

Example:

1st trade = 0.01

2nd trade = 0.02

3rd trade = 0.03

TP = 10

My average price position = 1000

TP will be = 1010



I would like to see TP Limit Order = 1010 in chart for all open positions.



Thank you!




For now it Market Order, please see script:



Position Long;

Position Short;

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

//| Expert initialization function |

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

int OnInit()

{

if (Digits == 5 || Digits == 3) {

// Adjust for five (5) digit brokers.

pips2dbl = Point*10; pips2points = 10; Digits_pips = 1;

} else {

pips2dbl = Point; pips2points = 1; Digits_pips = 0;

}



//Count open trades and locate last trade

Long.Initialize(LONG);

Short.Initialize(SHORT);

//---

return(INIT_SUCCEEDED);

}

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

//| Expert deinitialization function |

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

void OnDeinit(const int reason)

{

//---



}

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

//| Expert tick function |

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

void OnTick()

{

//for each position

//check TP,SL level if hit close potion

//check grid levels if hit open new trade

if(newbar())

{

if(allowLong && Long.getTradeCount()==0)

Long.Initialize(LONG);

if(allowShort && Short.getTradeCount()==0)

Short.Initialize(SHORT);

}



if(allowLong && (heaging || Long.getTradeCount()>0 || Short.getTradeCount()==0))

Long.MonitorLevels();

if(allowShort&& (heaging || Short.getTradeCount()>0 || Long.getTradeCount()==0))

Short.MonitorLevels();

}

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

//| Tester function |

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

double OnTester()

{

//---

double ret=0.0;

//---

//---

return(ret);

}

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

//| ChartEvent function |

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

void OnChartEvent(const int id,

const long &lparam,

const double &dparam,

const string &sparam)

{

//---



}

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

bool newbar()

{

static int obars;

if(obars!= Bars)

{

obars = Bars;

return true;

}

return false;

}



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

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

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

string Position::OrderTypeToString(int orderType)

{

switch(orderType)

{

case OP_BUY : return("OP_BUY");

case OP_BUYLIMIT : return("OP_BUYLIMIT");

case OP_BUYSTOP : return("OP_BUYSTOP");

case OP_SELL : return("OP_SELL");

case OP_SELLLIMIT : return("OP_SELLLIMIT");

case OP_SELLSTOP : return("OP_SELLSTOP");

}

return "";

}

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

int Position::GetOrderDir(int orderType)

{

if(orderType == OP_BUY || orderType == OP_BUYLIMIT || orderType == OP_BUYSTOP)

return(LONG);

return(SHORT);

}

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

int Position::OpenOrder(int orderType, double lots, double price=EMPTY_VALUE, int StopLoss=0, int TakeProfit=0)

{

int ticket=0;

int colour;

double priceSL;

double priceTP;

if (price != EMPTY_VALUE)

price=NormalizeDouble(price,Digits) ;

else

{

if (orderType==OP_SELL) price=Bid;

if (orderType==OP_BUY ) price=Ask;

}

if(GetOrderDir(orderType) == LONG)

{

colour = Blue;

priceSL = price-StopLoss*pips2dbl;

priceTP = price+TakeProfit*pips2dbl;

}

else

{

colour = Red;

priceSL = price+StopLoss*pips2dbl;

priceTP = price-TakeProfit*pips2dbl;

}



if(StopLoss ==0) priceSL = 0;

if(TakeProfit==0) priceTP = 0;



ticket = OrderSend(Symbol(),orderType, lots, price, Slippage, priceSL, priceTP, EAName, magicNumber, 0, colour);

string ssl = priceSL == 0 ? "" : " SL:" + DoubleToStr(priceSL,Digits);

string stp = priceTP == 0 ? "" : " TP:" + DoubleToStr(priceTP,Digits);

Print("Open ",OrderTypeToString(orderType), " at ",price, " lots: ",lots," ticket:",ticket,ssl,stp);



return(ticket);

}

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

string Position::getName(){

if(dir==LONG) return "LONG";

if(dir==SHORT)return "SHORT";

return "?";

}

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

int Position::getTradeCount(){

return this.tradeCount;

}

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

void Position::ClosePosition(){

int orderType = dir==1 ? OP_BUY : OP_SELL;

int orderTypeLimit = dir==1 ? OP_BUYLIMIT : OP_SELLLIMIT;

int orderTypeStop = dir==1 ? OP_BUYSTOP : OP_SELLSTOP;



int total=OrdersTotal();

for (int i=total;i>=0;i--)

{

bool success = OrderSelect(i,SELECT_BY_POS,MODE_TR ADES);

if(!success) continue;

if(OrderMagicNumber()!=magicNumber) continue;

if(OrderSymbol()!=Symbol())continue ;

bool successC;

if( OrderType()==orderType && orderType == OP_BUY)

successC =OrderClose(OrderTicket(),OrderLots (),Bid,Slippage,Blue);

else if (OrderType()==orderType && orderType == OP_SELL)

successC = OrderClose(OrderTicket(),OrderLots( ),Ask,Slippage,Red);

else if (OrderType() == orderTypeLimit || OrderType() == orderTypeLimit)

successC = OrderDelete(OrderTicket()) ;

else

successC = true;

if(!successC)

Print("System faild to close order ticket:", OrderTicket(), " error:", GetLastError());

}

//ToDo:get closed orders from history and calculate closed position loss gain and print it



Initialize(dir);

}

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

void Position::OpenNewTrade()

{

//check if we have not reached max trade

if(this.tradeCount>=MaxTrades)

{

//if maxtrades reached then print message that trade cannot be opened

Print("Position has ",this.tradeCount," trades no more trades can be opened");

return;

}

//calculate new lotsize;

double lots = LOT;

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

lots *= Multiply;

lots = NormalizeDouble(lots,2);

//openTrade

int orderType = dir==LONG ? OP_BUY : OP_SELL;

if(debug) Print("Opening new trade orderType=",this.OrderTypeToString( orderType)," lots=", lots );

OpenOrder(orderType, lots);

//call initialize to update positon

Initialize(dir);

TP Limit Order

0 commentaires:

Enregistrer un commentaire