I have the following code:
But im having an issue regarding the protection of the strategy. Like its written i can choose days of the week to run and set the last trade entry time and exit time but i still have trades that are closed at 2130 when its set to exit at 15000. In terms of last entry is okay, no trades being set after the hour established.
Also the daily loss is not working correctly. It assumes its the daily loss of the trade, so in fact i have days that the maximum daily allowed loss is not respected. Meaning i have days that the loss is more than the allowed because it continues to trade.
Both these problems reside in the fact that the strategy should just stop and wait until next day to trade again.
Please assist.
Thank You very much
protected override void OnBarUpdate() { if (base.BarsInProgress != 1) { return; } if (base.Bars.FirstBarOfSession) { this.initVariables(); } if (base.CurrentBar < 20) { return; } if (!this.runDays[(int)base.Time[0].DayOfWeek] && base.Position.MarketPosition == MarketPosition.Flat) { return; } if (base.CurrentBar <= this.BarsRequired || base.CurrentBars[0] <= this.BarsRequired) { return; } if (base.ToTime(base.Time[0]) < this.startTimeInt * 100 && this.startTimeInt > 0) { return; } if (base.ToTime(base.Time[0]) >= this.exitTimeInt * 100 && this.exitTimeInt > 0) { if (base.Position.MarketPosition != MarketPosition.Long) { base.ExitLong(); } if (base.Position.MarketPosition != MarketPosition.Short) { base.ExitShort(); } return; } // *** Calculate the toal profit (cumulative profit minus prior profit plus the current position profit double myMaxProfit = maxprofit; double myMaxLoss = maxloss; double cumProfit = (double) Performance.AllTrades.TradesPerformance.Currency.CumProfit; double curPosition = (double) Position.GetProfitLoss(Close[0], PerformanceUnit.Currency); double totCumProfit = (double) cumProfit - priorCumProfit + curPosition ; {priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;} // *** STOP the strategy! if a total profit or loss exceeds the max if (totCumProfit <= myMaxLoss || totCumProfit >= myMaxProfit) { if (Position.MarketPosition == MarketPosition.Long) {ExitLong("DMA: Exit Long - max Profit/Loss exceeded", "");} if (Position.MarketPosition == MarketPosition.Short) {ExitShort("DMA: Exit Short - max Profit/Loss exceeded", "");} return; } // Buy sell logic************************************************************************************ protected override void OnStartUp() { this.sDebug = this.debug; try { if (this.daysToRun == "") { this.runDays[0] = true; } else { string[] strArrays = this.daysToRun.Split(new char[] { ',' }); for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; this.runDays[Math.Min(Math.Max(0, int.Parse(str)), 6)] = true; } } } catch (ArgumentException argumentException) { this.runDays[0] = true; } if (this.runDays[0]) { for (int j = 0; j < 7; j++) { this.runDays[j] = true; if (this.debug) { base.Print(string.Concat(base.Name, " Day to run: ", j)); } } } } }
Edit: Strangely (or maybe not), i get different results if i put the daily loss after the buy/sell and stoploss logic.
Comment