protected override void Execute() { //Parameters int TimeStoppLong = input_TimeStoppLong.ValueInt; int TimeStoppShort = input_TimeStoppShort.ValueInt; double SLATRFactorLong = input_SLATRFactorLong.Value; double SLATRFactorShort = input_SLATRFactorShort.Value; double TPATRFactorLong = input_TPATRFactorLong.Value; double TPATRFactorShort = input_TPATRFactorShort.Value; int SMAPeriodeLong = input_SMAPeriodeLong.ValueInt; int SMAPeriodeShort = input_SMAPeriodeShort.ValueInt; double OffsetLong = 0.02; double OffsetShort = 0.02; double trailingStoppPriceLong = 0; double trailingStoppPriceShort = 0; double initStoppPriceLong = 0; double initStoppPriceShort = 0; double currentTargetPriceLong = 0; double currentTargetPriceShort = 0; double AlertEntry = 0; double AlertStoppLoss = 0; double AlertTakeProfit = 0; bool drawDetails = true; bool Screener = false; int barStart = 50; DataSeries StoppPrice = new DataSeries(Bars, "Stopp Price"); DataSeries TargetPrice = new DataSeries(Bars, "Target Price"); //HideVolume HideVolume(); //ATR ChartPane ATRPane = CreatePane(40, false, true); DrawLabel(ATRPane, "ATR(10)", Color.Black); PlotSeries(ATRPane, ATR.Series(Bars, 10), Color.Black, WealthLab.LineStyle.Solid, 2); // SMA Filter Long PlotSeries(PricePane, SMA.Series(Close, SMAPeriodeLong), Color.Blue, WealthLab.LineStyle.Solid, 2); PlotStops(); if (Screener) barStart = Bars.Count - 1; for(int bar = barStart; bar < Bars.Count; bar++) { //Position Position p = LastPosition; // Berechne das Offset für Longs OffsetLong = Close[bar] / 1000; if (OffsetLong < 0.02) OffsetLong = 0.02; // Berechne das Offset für Shorts OffsetShort = Close[bar] / 1000; if (OffsetShort < 0.02) OffsetShort = 0.02; //Berechne potentielle Trailing Stopps trailingStoppPriceShort = High[bar] + SLATRFactorShort * ATR.Series(Bars, 10)[bar]; if (IsLastPositionActive)// Position eröffnet { // Long Position if( LastPosition.PositionType == PositionType.Long ) { //AnnotateChart( PricePane, "long", bar, Low[bar], Color.Black); //Trailingstopp berechnen und prüfen trailingStoppPriceLong = Low[bar] - SLATRFactorLong * ATR.Series(Bars,10)[bar]; trailingStoppPriceLong = Math.Max(trailingStoppPriceLong, StoppPrice[bar - 1]); //TrailingStopp wird verwendet bool useTrailingStoppLong = false; if (useTrailingStoppLong) //nachgezogener Stopp { StoppPrice[bar] = trailingStoppPriceLong; } else //initialStopp { StoppPrice[bar] = StoppPrice[bar - 1]; } //Trailingstopp bzw. InitialStopp GTC setzen SellAtStop(bar + 1, p, StoppPrice[bar], "StoppLoss Long"); DrawLine( PricePane, bar+1,StoppPrice[bar] , bar+1, StoppPrice[bar], Color.Red, WealthLab.LineStyle.Dotted, 2 ); //Takeprofit GTC setzen SellAtLimit(bar + 1, p, currentTargetPriceLong, "TakeProfit Long"); DrawLine( PricePane, bar+1,currentTargetPriceLong , bar+1, currentTargetPriceLong, Color.Red, WealthLab.LineStyle.Dotted, 2 ); // ZeitStopp bool useTimeStoppLong = true; if(useTimeStoppLong && bar + 1 - p.EntryBar >= TimeStoppLong ) { ExitAtMarket(bar + 1, p, "Time-based Exit Long"); } } // Short Position if( LastPosition.PositionType == PositionType.Short ) { //AnnotateChart( PricePane, "short", bar, Low[bar], Color.Black); //Trailingstopp berechnen und prüfen trailingStoppPriceShort = High[bar] + SLATRFactorShort * ATR.Series(Bars, 10)[bar]; trailingStoppPriceShort = Math.Min(trailingStoppPriceShort, StoppPrice[bar - 1]); //TrailingStopp wird verwendet bool useTrailingStoppShort = false; if (useTrailingStoppShort) //nachgezogener Stopp { StoppPrice[bar] = trailingStoppPriceShort; } else //initialStopp { StoppPrice[bar] = StoppPrice[bar - 1]; } //Trailingstopp bzw. InitialStopp GTC setzen CoverAtStop(bar + 1, p, StoppPrice[bar], "StoppLoss Short"); //DrawLine( PricePane, bar,StoppPrice[bar] , bar, StoppPrice[bar], Color.Red, WealthLab.LineStyle.Dotted, 2 ); //Takeprofit GTC setzen CoverAtLimit(bar + 1, p, currentTargetPriceShort, "TakeProfit Short"); //DrawLine( PricePane, bar,currentTargetPriceShort , bar, currentTargetPriceShort, Color.Red, WealthLab.LineStyle.Dotted, 2 ); // ZeitStopp bool useTimeStoppShort = true; if(useTimeStoppShort && bar + 1 - p.EntryBar >= TimeStoppShort ) { ExitAtMarket(bar + 1, p, "Time-based Exit Short"); } } } else //Einstiege { //Signalgeber Long //kleine freundliche Kerze bool cL1 = High[bar] - Low[bar] < 0.8 * ATR.Series(Bars,10)[bar]; // kleine Kerze bool cL2 = Close[bar] > Open[bar]; //freundliche Kerze bool cL3 = High[bar] < High[bar - 1]; bool cL4 = Low[bar] < Low[bar - 1] || Low[bar - 1] < Low[bar - 2]; bool cL5 = Close[bar] >= High[bar] - ((High[bar] - Low[bar]) / 2); //Close im oberen Drittel bool cL6 = LowestBar.Series(Low, 2)[bar] > LowestBar.Series(Low, 15)[bar]; //kein neues tief // Long Einstieg if (cL1 && cL2 && cL3 && cL4 && cL5 && cL6) { initStoppPriceLong = Low[bar] - SLATRFactorLong * ATR.Series(Bars,10)[bar]; RiskStopLevel = initStoppPriceLong; AlertEntry = High[bar] + OffsetLong; if (BuyAtStop(bar+1, High[bar] + OffsetLong, "Enter Long") != null) { SetPaneBackgroundColor(PricePane,bar, Color.LightGreen); AnnotateChart( PricePane, "StoppBuy: "+(String.Format("{0:0.00}", High[bar] + OffsetLong)), bar, High[bar] + OffsetLong, Color.Black); DrawLine( PricePane, bar-1,High[bar] + OffsetLong , bar+1, High[bar] + OffsetLong, Color.Blue, WealthLab.LineStyle.Dotted, 2 ); AnnotateChart(ATRPane, ATR.Value(bar, Bars, 10).ToString(), bar, ATR.Value(bar, Bars, 10) + Bars.SymbolInfo.Tick * 5 , Color.Black); //InitialStopp setzen StoppPrice[bar] = initStoppPriceLong; AlertStoppLoss = initStoppPriceLong; SellAtStop(bar + 1, LastActivePosition, initStoppPriceLong, "InitialStoppLoss Long"); AnnotateChart( PricePane, "SL: "+(String.Format("{0:0.00}", initStoppPriceLong)), bar, initStoppPriceLong, Color.Black); DrawLine( PricePane, bar-1,initStoppPriceLong , bar+1, initStoppPriceLong, Color.Red, WealthLab.LineStyle.Dotted, 2 ); //Berechne Target und setzen currentTargetPriceLong = High[bar] + TPATRFactorLong * ATR.Series(Bars,10)[bar]; AlertTakeProfit = currentTargetPriceLong; SellAtLimit(bar + 1, LastActivePosition, currentTargetPriceLong, "TakeProfit Long"); AnnotateChart( PricePane, "TP: "+(String.Format("{0:0.00}", currentTargetPriceLong)), bar, currentTargetPriceLong, Color.Black); DrawLine( PricePane, bar-1,currentTargetPriceLong , bar+1, currentTargetPriceLong, Color.Red, WealthLab.LineStyle.Dotted, 2 ); } } //Signalgeber Short //kleine freundliche Kerze bool cS1 = High[bar] - Low[bar] < 0.8 * ATR.Series(Bars,10)[bar]; // kleine Kerze bool cS2 = Close[bar] < Open[bar]; //freundliche Kerze bool cS3 = Low[bar] < Low[bar - 1]; bool cS4 = High[bar] > High[bar - 1] || High[bar - 1] < High[bar - 2]; bool cS5 = Close[bar] <= Low[bar] + ((High[bar]-Low[bar])/2); //Close im uteren Drittel bool cS6 = HighestBar.Series(High, 2)[bar] > HighestBar.Series(High, 15)[bar]; //kein neues hoch // Short Einstieg if (cS1 && cS2 && cS3 && cS4 && cS5 && cS6) { initStoppPriceShort = High[bar] + SLATRFactorShort * ATR.Series(Bars, 10)[bar]; RiskStopLevel = initStoppPriceShort; AlertEntry = Low[bar] - OffsetShort; if (ShortAtStop(bar+1, Low[bar] - OffsetShort, "Enter Short") != null) { SetPaneBackgroundColor(PricePane,bar, Color.Salmon); AnnotateChart( PricePane, "StoppSell: "+(String.Format("{0:0.00}", Low[bar] - OffsetShort)), bar, Low[bar] - OffsetShort, Color.Black); DrawLine( PricePane, bar-1,Low[bar] - OffsetShort , bar+1, Low[bar] - OffsetShort, Color.Blue, WealthLab.LineStyle.Dotted, 2 ); AnnotateChart(ATRPane, ATR.Value(bar, Bars, 10).ToString(), bar, ATR.Value(bar, Bars, 10) + Bars.SymbolInfo.Tick * 5 , Color.Black); //InitialStopp setzen StoppPrice[bar] = initStoppPriceShort; AlertStoppLoss = initStoppPriceShort; CoverAtStop(bar + 1, LastActivePosition, initStoppPriceShort, "InitialStoppLoss Short"); AnnotateChart( PricePane, "SL: "+(String.Format("{0:0.00}", initStoppPriceShort)), bar, initStoppPriceShort, Color.Black); DrawLine( PricePane, bar-1,initStoppPriceShort , bar+1, initStoppPriceShort, Color.Red, WealthLab.LineStyle.Dotted, 2 ); //Berechne Target und setzen currentTargetPriceShort = Low[bar] - TPATRFactorShort * ATR.Series(Bars,10)[bar]; AlertTakeProfit = currentTargetPriceShort; CoverAtLimit(bar + 1, LastActivePosition, currentTargetPriceShort, "TakeProfit Short"); AnnotateChart( PricePane, "TP: "+(String.Format("{0:0.00}", currentTargetPriceShort)), bar, currentTargetPriceShort, Color.Black); DrawLine( PricePane, bar-1,currentTargetPriceShort , bar+1, currentTargetPriceShort, Color.Red, WealthLab.LineStyle.Dotted, 2 ); } } } } // bar for next if (Screener) { string path = @"C:\Temp\" + String.Format( "{0:yyyyMMdd}", Bars.Date[Bars.Count - 1]) + "AlertsSTFB.csv"; SaveAlerts(path,AlertEntry, AlertStoppLoss, AlertTakeProfit); } } //function execute public void SaveAlerts(string path, double Entry, double StoppLoss, double TakeProfit) { string dateFormat = "dd.MM.yyyy"; if( Alerts.Count > 0 ) { System.IO.TextWriter tw = new System.IO.StreamWriter(path, true); for( int i = 0; i < Alerts.Count; i++ ) { Alert a = Alerts[i]; string s = a.AlertDate.ToString(dateFormat); s = s + ";" + a.Symbol; s = s + ";" + Bars.SecurityName; s = s + ";" + a.PositionType.ToString(); s = s + ";" + Entry.ToString("N2"); s = s + ";" + StoppLoss.ToString("N2"); s = s + ";" + TakeProfit.ToString("N2"); tw.WriteLine(s); } tw.Close(); } }// function SaveAlerts