StopLoss not working Zephyr Scalper - Code Help.
StopLoss not working Zephyr Scalper - Code Help.
09 May 2014, 23:29
I downloaded the Zephyr Scalper and i noticed that the initial stop loss is not set when the order is placed. The Trailing stop works fine, but the Cbot will not set the initial stop loss. When doing backtesting, the only time it takes a loss is if initially the trade was in profit and the Trailing Stop Trigger was hit. Then it sets a stop loss according to Trailing stop settings. Other than that, Trade sits there forever (in loss) until price finally comes back and it hits the take profit or Trailing stop Trigger.
Can anyone who knows code see if there is an error in the code below? Maybe it has a simple error that someone can detect.
Thanks a lot.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class ZephynScalper : Robot { [Parameter("Periods SMA", DefaultValue = 35)] public int Periods_SMA { get; set; } [Parameter("Source SMA")] public DataSeries Source_SMA { get; set; } [Parameter("TakeProfit", DefaultValue = 30, MinValue = 1)] public int TakeProfit { get; set; } [Parameter("Stop Loss", DefaultValue = 50, MinValue = 1)] public int StopLoss { get; set; } [Parameter("Trail start", DefaultValue = 12, MinValue = 1)] public int Trail_start { get; set; } [Parameter("Trail", DefaultValue = 8, MinValue = 1)] public int Trail { get; set; } [Parameter("Volume", DefaultValue = 10000, MinValue = 10000)] public int Volume { get; set; } [Parameter(DefaultValue = MovingAverageType.Simple)] public MovingAverageType MaType { get; set; } [Parameter("K Periods", DefaultValue = 5)] public int KPeriods { get; set; } [Parameter("D Periods", DefaultValue = 3)] public int DPeriods { get; set; } [Parameter("K Slowing", DefaultValue = 3)] public int K_Slowing { get; set; } private bool Sellingongoing = false; private bool Buyingongoing = false; private int OpenTrades = 0; double tradePrice; private SimpleMovingAverage _SMA; private StochasticOscillator _SOC; protected override void OnStart() { _SMA = Indicators.SimpleMovingAverage(Source_SMA, Periods_SMA); _SOC = Indicators.StochasticOscillator(KPeriods, K_Slowing, DPeriods, MaType); Positions.Closed += PositionsOnClosed; } protected override void OnTick() { //Print("Ask: " + Symbol.Ask); //købs pris //Print("Bid: " + Symbol.Bid); //salgs pris //Print(_SMA.Result.LastValue); //Print("D" + _SOC.PercentD.LastValue); //Print("K" + _SOC.PercentK.LastValue); foreach (var _p in Positions.FindAll("ZephynScalper", Symbol, TradeType.Buy)) { if (_p.Pips > Trail_start) { if (tradePrice < Symbol.Bid) { double NewStopLoss = Symbol.Ask - Trail * Symbol.PipSize; ModifyPosition(_p, NewStopLoss, _p.TakeProfit.Value); tradePrice = Symbol.Ask; //Print("Stop loss: " + NewStopLoss); //Print("Current price: " + Symbol.Ask); } } } foreach (var _p2 in Positions.FindAll("ZephynScalper", Symbol, TradeType.Sell)) { if (_p2.Pips > Trail_start) { if (tradePrice > Symbol.Ask) { double NewStopLoss = Symbol.Bid + Trail * Symbol.PipSize; ModifyPosition(_p2, NewStopLoss, _p2.TakeProfit.Value); tradePrice = Symbol.Bid; } } } if (_SMA.Result.LastValue < Symbol.Bid) { if (_SOC.PercentD.LastValue < 20) { if (_SOC.PercentK.LastValue < 20) { if (_SOC.PercentK.LastValue > _SOC.PercentD.LastValue) { if (Buyingongoing == false) { Open(TradeType.Buy); tradePrice = Symbol.Bid; Buyingongoing = true; } } } } } else if (_SMA.Result.LastValue > Symbol.Ask) { if (_SOC.PercentD.LastValue > 80) { if (_SOC.PercentK.LastValue > 80) { if (_SOC.PercentK.LastValue < _SOC.PercentD.LastValue) { if (Sellingongoing == false) { Open(TradeType.Sell); tradePrice = Symbol.Ask; Sellingongoing = true; } } } } } } private void Open(TradeType tradeType) { // var position = Positions.Find("ZephynScalper", Symbol, tradeType); // if (position == null) // ExecuteMarketOrder(tradeType, Symbol, Volume, "ZephynScalper", StopLoss, TakeProfit); if (OpenTrades < 2) { ExecuteMarketOrder(tradeType, Symbol, Volume, "ZephynScalper", null, TakeProfit); OpenTrades++; } } private void PositionsOnClosed(PositionClosedEventArgs args) { var position = args.Position; //Print("Position closed with {0} profit", position.GrossProfit); if (position.Label.ToString() == "ZephynScalper") { OpenTrades--; } if (position.TradeType == TradeType.Sell) { Sellingongoing = false; } if (position.TradeType == TradeType.Buy) { Buyingongoing = false; } if (OpenTrades < 0) OpenTrades = 0; } protected override void OnStop() { // Put your deinitialization logic here } } }
emeeder
10 May 2014, 02:19
Nevermind. I figured it out. Simply change "null" in the source code to "StopLoss" and and click rebuild.