package jforex.converted;
import java.awt.Color;
import com.dukascopy.api.*;
public class ManageTPv2_4 extends ConnectorStrategy {


protected void initProperties() {
if (properties == null) {
properties = new Properties();
}
properties.setProperty("link","stockwet@yahoo.com");
properties.setProperty("copyright","Copyright ï¿½ 2006, Taylor Stockwell");

}
@Configurable("") public int First_Target=toInt(1);
@Configurable("") public int Target_Increment=toInt(3);
@Configurable("") public int Close_Lots=toInt(1);
@Configurable("") public boolean Move_Stops=Bool(true);
@Configurable("") public int First_Stop_Target=toInt(10);
@Configurable("") public int First_Stop=toInt(0);
@Configurable("") public int Second_Stop_Target=toInt(30);
@Configurable("") public int Second_Stop=toInt(10);

@Configurable("") public int Magic_Number=toInt(0);
int nextTP = 0;
boolean sl = true;
int range=toInt(5);
int multiplier = 0;

public int init() throws JFException {

sl = Bool(0);

nextTP = toInt(First_Target);


return 0;
}

public int deinit() throws JFException {

sl = Bool(0);

nextTP = toInt(First_Target);

if(true)return toInt(0);return 0;
}

public int start() throws JFException {

getOpenOrders();

getSpread();

if(true)return toInt(0);return 0;
}

public double getPipValue(Number ord, Number dir) throws JFException {return getPipValue(ord.doubleValue(), dir.intValue());}
public double getPipValue(Number ord, boolean dir) throws JFException {
return getPipValue(ord.doubleValue(), toInt(dir));}
public double getPipValue(boolean ord, Number dir) throws JFException {
return getPipValue(toDouble(ord), dir.intValue());}
public double getPipValue(boolean ord, boolean dir) throws JFException {
return getPipValue(toDouble(ord), toInt(dir));}
public double getPipValue(double ord, int dir) throws JFException {
double val = 0.0;

RefreshRates();

if (dir == 1){

val = toDouble((NormalizeDouble(ord,Digits)-NormalizeDouble(Ask,Digits)));

} else {
val = toDouble((NormalizeDouble(Bid,Digits)-NormalizeDouble(ord,Digits)));

}
val = toDouble(val/Point);

if(true)return toDouble(val);return 0.0;
}

public int getSpread() throws JFException {
int spread = 0;

spread = toInt(MarketInfo(Instrument(),MODE_SPREAD));

if(true)return toInt(spread);return 0;
}


public void getOpenOrders() throws JFException {
int nsl = 0;
int nsd = 0;
String mngMagic = "";
int totalorders = 0;
int j=toInt(0);
double val = 0.0;

totalorders = toInt(OrdersTotal());

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

OrderSelect(j,SELECT_BY_POS,MODE_TRADES);

if ((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderSymbol().equals(Instrument()) && (Magic_Number == 0 || Magic_Number == OrderMagicNumber())){

val = toDouble(getPipValue(OrderOpenPrice(),OrderType()));


if (Move_Stops){

checkStops(val,OrderTicket());
}
takeProfit(val,OrderTicket());
}
if (Magic_Number == 0){

mngMagic = toString("All "+Symbol()+" trades.");

} else {
mngMagic = toString("Trades with magic number = "+Magic_Number);

}
if (compareEquals(0,sl)){

nsl = toInt(First_Stop_Target);

nsd = toInt(First_Stop);

} else {
nsl = toInt(Second_Stop_Target);

nsd = toInt(Second_Stop);

}
Comment("Order Open: ",OrderOpenPrice(),"\nPip Count: ",val,"\nNext Stop Target: ",nsl,"\nNext Stop Differential: ",nsd,"\nNext TP: ",nextTP,"\nSL: ",sl,"\nMax Loss: ","\nManaging: ",mngMagic);

}
return ;
}

public void takeProfit(Number pips, Number ticket) throws JFException {takeProfit(pips.intValue(), ticket.intValue());}
public void takeProfit(boolean pips, Number ticket) throws JFException {
takeProfit(toInt(pips), ticket.intValue());}
public void takeProfit(Number pips, boolean ticket) throws JFException {
takeProfit(pips.intValue(), toInt(ticket));}
public void takeProfit(boolean pips, boolean ticket) throws JFException {
takeProfit(toInt(pips), toInt(ticket));}
public void takeProfit(int pips, int ticket) throws JFException {

if (OrderSelect(ticket,SELECT_BY_TICKET) == true){

if (pips >= nextTP && pips < (nextTP + Target_Increment)){

if (OrderType() == 1){

if (OrderClose(ticket,Close_Lots,Ask,3,YellowGreen)){

nextTP += toInt(Target_Increment);

} else {
Print("Error closing order : ",GetLastError());

}
} else 
if (OrderClose(ticket,Close_Lots,Bid,3,YellowGreen)){

nextTP += toInt(Target_Increment);

} else {
Print("Error closing order : ",GetLastError());

}}}return ;
}

public void checkStops(Number pips, Number ticket) throws JFException {checkStops(pips.intValue(), ticket.intValue());}
public void checkStops(boolean pips, Number ticket) throws JFException {
checkStops(toInt(pips), ticket.intValue());}
public void checkStops(Number pips, boolean ticket) throws JFException {
checkStops(pips.intValue(), toInt(ticket));}
public void checkStops(boolean pips, boolean ticket) throws JFException {
checkStops(toInt(pips), toInt(ticket));}
public void checkStops(int pips, int ticket) throws JFException {

if (compareEquals(0,sl) && pips < Second_Stop_Target){

if (pips >= First_Stop_Target && pips < (Second_Stop_Target)){

moveStops(ticket,First_Stop);
}
} else 
if (compareEquals(1,sl) || pips >= Second_Stop_Target){

if (pips >= Second_Stop_Target){

moveStops(ticket,Second_Stop);
}}return ;
}

public void moveStops(Number ticket, Number stopDiff) throws JFException {moveStops(ticket.intValue(), stopDiff.intValue());}
public void moveStops(boolean ticket, Number stopDiff) throws JFException {
moveStops(toInt(ticket), stopDiff.intValue());}
public void moveStops(Number ticket, boolean stopDiff) throws JFException {
moveStops(ticket.intValue(), toInt(stopDiff));}
public void moveStops(boolean ticket, boolean stopDiff) throws JFException {
moveStops(toInt(ticket), toInt(stopDiff));}
public void moveStops(int ticket, int stopDiff) throws JFException {

if (OrderSelect(ticket,SELECT_BY_TICKET) == true){

Print("moveStops called ",ticket," ",stopDiff);

if (OrderType() == 1){

OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-stopDiff*Point,OrderTakeProfit(),0,Plum);

sl = Bool(1);

} else {
OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()+stopDiff*Point,OrderTakeProfit(),0,Plum);

sl = Bool(1);

}}return ;
}

public void killTrade(Number pips, Number ticket) throws JFException {killTrade(pips.intValue(), ticket.intValue());}
public void killTrade(boolean pips, Number ticket) throws JFException {
killTrade(toInt(pips), ticket.intValue());}
public void killTrade(Number pips, boolean ticket) throws JFException {
killTrade(pips.intValue(), toInt(ticket));}
public void killTrade(boolean pips, boolean ticket) throws JFException {
killTrade(toInt(pips), toInt(ticket));}
public void killTrade(int pips, int ticket) throws JFException {

if (OrderSelect(ticket,SELECT_BY_TICKET) == true){


if (OrderType() == 1){

OrderClose(ticket,OrderLots(),Ask,3,Red);

} else {
OrderClose(ticket,OrderLots(),Bid,3,Red);

}}return ;
}


public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException{}
/**/};