|
Attention! Read the forum rules carefully before posting a topic.
Try to find an answer in Wiki before asking a question. Submit programming questions in this forum only. Off topics are strictly forbidden.
Any topics which do not satisfy these rules will be deleted.
help with stop loss |
Karolis
|
Post subject: help with stop loss |
Post rating: 0
|
Posted: Wed 16 Jan, 2013, 18:58
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
Can somebody help me ? My EA is without stop loss. Can somebody write command line example of stop loss ?
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 08:25
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 14:41
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
can fix stop loss or add this section in my code ? When i write in "stop loss" value 6, every order is closed with -0.6 - 0.8 pips.. private void secureOrder(Instrument instrument, IEngine.OrderCommand orderCommand, double price) { try { /* * Validation on free margin is covered by this isEmpty() check * See https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?p=30136#p30136 */ if (engine.getOrders().isEmpty()) { double takeProfit; double stopLoss = 0; if (IEngine.OrderCommand.BUY == orderCommand) { takeProfit = price + maxPipsProfit / 10000; } else takeProfit = price - maxPipsProfit / 10000; //console.getOut().println(getOrderSize() + ": price = " + price + " takeProfit = " + takeProfit); engine.submitOrder("AutoOrder" + orderCommand, instrument, orderCommand, getOrderSize(instrument), 0, 5, stopLoss, takeProfit); } } catch (JFException ex) { console.getErr().println(ex); } }
private double getOrderSize(Instrument instrument) { //Apply a scale to get the same risk no matter the instrument double scale = 1; //if (Instrument.EURGBP.equals(instrument)) scale = ; //else if (Instrument.USDCAD.equals(instrument)) scale = ; //I chose not to scale EURUSD since I feel it's quite a volatile pair
return scale * Math.max(minOrderSize, minOrderSize + (account.getEquity()-100000)/150000); } }
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 15:47
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 16:07
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
i don't' understand what is bad here..
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 16:22
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 17 Jan, 2013, 18:09
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
i'm green in coding. I can't to this by myself..
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Wed 30 Jan, 2013, 23:40
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
OK, i moved slightly forward. But still is one problem. My Stop loss is 9 pips. When strategy makes SELL order it closes it with 9 pips Stop Loss, but if it makes BUY order it closes with -0.6 pips loss. Can somebody say whats wrong in code ? private void secureOrder(Instrument instrument, IEngine.OrderCommand orderCommand, double price) { try { /* * Validation on free margin is covered by this isEmpty() check * See https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?p=30136#p30136 */ if (engine.getOrders().isEmpty()) { double takeProfit; double stopLoss; if (IEngine.OrderCommand.BUY == orderCommand) { takeProfit = price + maxPipsProfit / 10000; stopLoss = price - maxPipsLoss / 10000; } else takeProfit = price - maxPipsProfit / 10000; stopLoss = price + maxPipsLoss / 10000; //console.getOut().println(getOrderSize() + ": price = " + price + " takeProfit = " + takeProfit); engine.submitOrder("AutoOrder" + orderCommand, instrument, orderCommand, getOrderSize(instrument), 0, 5, stopLoss, takeProfit); } } catch (JFException ex) { console.getErr().println(ex); } } private double getOrderSize(Instrument instrument) {
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 31 Jan, 2013, 19:27
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Thu 31 Jan, 2013, 21:15
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
I changes what you write, but still same problem.. When sell it makes normal stop loss (9 pips), when buy it makes 0.6 (pips) stop loss... private void secureOrder(Instrument instrument, IEngine.OrderCommand orderCommand, double price) { try { /* * Validation on free margin is covered by this isEmpty() check * See https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?p=30136#p30136 */ if (engine.getOrders().isEmpty()) { double takeProfit; double stopLoss; if (IEngine.OrderCommand.BUY == orderCommand) { takeProfit = price + maxPipsProfit * instrument.getPipValue(); stopLoss = price - maxPipsLoss / 10000; } { takeProfit = price - maxPipsProfit * instrument.getPipValue(); stopLoss = price + maxPipsLoss / 10000; } //console.getOut().println(getOrderSize() + ": price = " + price + " takeProfit = " + takeProfit); engine.submitOrder("AutoOrder" + orderCommand, instrument, orderCommand, getOrderSize(instrument), 0, 5, stopLoss, takeProfit); } } catch (JFException ex) { console.getErr().println(ex); } }
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Fri 01 Feb, 2013, 08:28
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Please provide full example strategy which replicates the behavior.
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Mon 11 Feb, 2013, 18:27
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
Here is full Code.. package jforex;
import com.dukascopy.api.IAccount; import com.dukascopy.api.IBar; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IEngine; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IMessage; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.OfferSide; import com.dukascopy.api.Period; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set;
/** * * @author Karolis * @author $LastChangedBy: Karolis $ * $Date: $ * $Revision: 4 $ * * @since 1.2 * */ public class GreenPips implements IStrategy { private Set<Instrument> selectedInstruments = new HashSet<Instrument>();
//These variables are public so that @Configurable will work //@Configurable("Active Period") public Period selectedPeriod = Period.THIRTY_MINS; //@Configurable("Higher Period") public Period higherPeriod = Period.ONE_HOUR; //@Configurable("Max Pips Profit") public double maxPipsProfit = 14.0; public double maxPipsLoss = 7.0; //@Configurable("Order Size") public double minOrderSize = 6.0; //@Configurable("Maximum Allowed Spread (in pips)") public double maximumAllowedSpread = 3; //@Configurable("Cycle length") public int cycleLength = 13; //@Configurable("Higher cycle length") public int higherCycleLength = 30;
private enum TradeAction {BUYING, SELLING, NEUTRAL}; private Map<Instrument, TradeAction> higherPeriodActionMap = new HashMap<Instrument, TradeAction>();
private IEngine engine; private IConsole console; private IAccount account; private IIndicators indicators;
public void onStart(IContext context) throws JFException { this.engine = context.getEngine(); this.console = context.getConsole(); this.account = context.getAccount(); this.indicators = context.getIndicators();
selectInstrument(Instrument.EURUSD); //Subscribe to the instruments Set<Instrument> instruments = (Set<Instrument>)((HashSet<Instrument>)selectedInstruments).clone(); context.setSubscribedInstruments(instruments); }
private void selectInstrument(Instrument instrument) { selectedInstruments.add(instrument); higherPeriodActionMap.put(instrument, TradeAction.NEUTRAL); }
public void onTick(Instrument instrument, ITick tick) throws JFException { }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (selectedInstruments.contains(instrument)) { if (period.equals(selectedPeriod)) { double spread = (askBar.getClose() - bidBar.getClose()) * 10000; if (spread < maximumAllowedSpread) { //check to make sure it's a good entry on the smaller timeframe double rsi = indicators.rsi(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, cycleLength, 0);
//We only open an order if it satisfies both timeframes TradeAction higherPeriodAction = higherPeriodActionMap.get(instrument); if (TradeAction.BUYING.equals(higherPeriodAction) && rsi < 35) { secureOrder(instrument, IEngine.OrderCommand.BUY, bidBar.getClose()); } else if (TradeAction.SELLING.equals(higherPeriodAction) && rsi > 45) { secureOrder(instrument, IEngine.OrderCommand.SELL, askBar.getClose()); } } //else console.getOut().println("discarded bar due to the spread being too high (" + spread + ")"); } else if (period.equals(higherPeriod)) { double rsi = indicators.rsi(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, higherCycleLength, 0); if (rsi >= 70) higherPeriodActionMap.put(instrument, TradeAction.SELLING); else if (rsi <= 40) higherPeriodActionMap.put(instrument, TradeAction.BUYING); else higherPeriodActionMap.put(instrument, TradeAction.NEUTRAL); } } }
public void onMessage(IMessage message) throws JFException { }
public void onAccount(IAccount account) throws JFException { }
public void onStop() throws JFException { }
/** * Secures an order in a given direction. If an order is already on the * account then this call is ignored. * @param instrument the instrument to use * @param direction the direction of the order, long or short * @param price the close price of the latest bar (either bid or ask) */ private void secureOrder(Instrument instrument, IEngine.OrderCommand orderCommand, double price) { try { /* * Validation on free margin is covered by this isEmpty() check * See https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?p=30136#p30136 */ if (engine.getOrders().isEmpty()) { double takeProfit; double stopLoss; if (IEngine.OrderCommand.BUY == orderCommand) { takeProfit = price + maxPipsProfit * instrument.getPipValue(); stopLoss = price - maxPipsLoss / 10000; } { takeProfit = price - maxPipsProfit * instrument.getPipValue(); stopLoss = price + maxPipsLoss / 10000; } //console.getOut().println(getOrderSize() + ": price = " + price + " takeProfit = " + takeProfit); engine.submitOrder("AutoOrder" + orderCommand, instrument, orderCommand, getOrderSize(instrument), 0, 5, stopLoss, takeProfit); } } catch (JFException ex) { console.getErr().println(ex); } } private double getOrderSize(Instrument instrument) { //Apply a scale to get the same risk no matter the instrument double scale = 1; //if (Instrument.EURGBP.equals(instrument)) scale = ; //else if (Instrument.USDCAD.equals(instrument)) scale = ; //I chose not to scale EURUSD since I feel it's quite a volatile pair return scale * Math.max(minOrderSize, minOrderSize + (account.getEquity()-100000)/150000); } }
|
|
|
|
 |
API Support
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Wed 13 Feb, 2013, 16:05
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
The following code block always executes before every order submission: { takeProfit = price - maxPipsProfit * instrument.getPipValue(); stopLoss = price + maxPipsLoss / 10000; }
|
|
|
|
 |
Karolis
|
Post subject: Re: help with stop loss |
Post rating: 0
|
Posted: Wed 27 Feb, 2013, 20:55
|
|
User rating: 0
Joined: Sun 30 Sep, 2012, 21:43 Posts: 10 Location: Lithuania, Kaunas
|
Can you edit it ? I don't understand how..
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|