i want to round the double stoploss1 into an int.
i think it actually works, cause i get the right amount of contracts and SetStopLoss works also. my problem is that my exit conditions don´t work anymore.
int stoploss2 = System.Convert.ToInt16(stoploss1);
int stoploss2 = (int)stoploss1;
but i get the same result.
if i declare int stoploss = 20;
everything works just fine.
{
#region Variables
private int anzahlKontrakte1;
private int anzahlKontrakte2;
private string entryName1 = "GrailLongEURUSD1";
private string entryName2 = "GrailLongEURUSD2";
private string entryName3 = "GrailShortEURUSD1";
private string entryName4 = "GrailShortEURUSD2";
private double kurs = 1.1250;
private int kontostand = 2000;
private double einsatzprozent = 2;
private int xfacheatr = 20000;
private int stoploss;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
Add (EMA(20));
EMA(20).Plots[0].Pen.Color = Color.Red;
Add (EMA(34));
Add (ADX(14));
Add (Swing(8));
Add (ATR(20));
CalculateOnBarClose = false;
ExitOnClose = false;
EntryHandling = EntryHandling.UniqueEntries;
EntriesPerDirection = 1;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
double einsatzineuro = kontostand/100*einsatzprozent;
double einsatzinfremdwährung = einsatzineuro*kurs;
double stoploss1 = ATR(20)[0]*xfacheatr;
int stoploss2 = System.Convert.ToInt16(stoploss1);
anzahlKontrakte1 = (int) ((einsatzinfremdwährung/stoploss2*10000)*0.6);
anzahlKontrakte2 = (int) ((einsatzinfremdwährung/stoploss2*10000)*0.4);
// Make sure there are enough bars.
if (CurrentBar < 1)
return;
//Long Trade bei durchkreutzen des 20 EMA, wenn ADX größer als 30 und mindestens 10 Pips Unterschschied zwischen EMA 20 und EMA 34
if (Position.MarketPosition==MarketPosition.Flat&&Get CurrentAsk()<=EMA(20)[0]&&CrossBelow(Low, EMA(20),1)&&ADX(14)[0]>30.00&&Close[0]>Swing(8).SwingLow[0]&&BarsSinceExit()>8 || BarsSinceExit()==-1)
{
EnterLong(anzahlKontrakte1, entryName1);
EnterLong(anzahlKontrakte2, entryName2);
SetStopLoss(entryName1, CalculationMode.Ticks, stoploss2, false); // SL bei 1,5 des ATR
SetStopLoss(entryName2, CalculationMode.Ticks, stoploss2, false);
}
//Short Trade bei durchkreutzen des 20 EMA, wenn ADX größer als 30 und mindestens 10 Pips Unterschschied zwischen EMA 20 und EMA 34
if (Position.MarketPosition==MarketPosition.Flat&&Get CurrentBid()>=EMA(20)[0]&&CrossAbove(High, EMA(20),1)&&ADX(14)[0]>30.00&&Close[0]<Swing(8).SwingHigh[0]&&BarsSinceExit()>8 || BarsSinceExit()==-1)
{
EnterShort(anzahlKontrakte1, entryName3);
EnterShort(anzahlKontrakte2, entryName4);
SetStopLoss(entryName3, CalculationMode.Ticks, stoploss2, false); //SL bei 1,5 des ATR
SetStopLoss(entryName4, CalculationMode.Ticks, stoploss2, false);
}
//Positions Handling: Verkauft einen Teil wenn es letztes Hoch überschreitet,
//SL des Rests wird auf Einstand gesetzt und wird verkauft wenn EMA34 durchkreuzt wird
if(Position.Quantity==anzahlKontrakte1+anzahlKontr akte2&&Position.MarketPosition == MarketPosition.Long && Swing(8).SwingHigh[0]>Position.AvgPrice && CrossAbove(High, Swing(8).SwingHigh, 1))
{
ExitLong(entryName1);
SetStopLoss(entryName2, CalculationMode.Price, Position.AvgPrice, false); //SL wird auf Einstand gezogen
}
if(Position.Quantity==anzahlKontrakte2&&CrossBelow (Low,EMA(34),1))
{
ExitLong(entryName2);
}
//Positions Handling: Verkauft einen Teil wenn es letztes Tief unterschreitet,
//SL des Rests wird auf Einstand gesetzt und wird verkauft wenn EMA34 durchkreuzt wird
if (Position.Quantity==anzahlKontrakte1+anzahlKontrak te2&&Position.MarketPosition==MarketPosition.Short && Swing(8).SwingLow[0]<Position.AvgPrice && CrossBelow(Low, Swing(8).SwingLow,1))
{
ExitShort(entryName3);
SetStopLoss(entryName4, CalculationMode.Price, Position.AvgPrice, false); //SL wird auf Einstand gezogen
}
if (Position.Quantity==anzahlKontrakte2&&CrossAbove(H igh,EMA(34),1))
{
ExitShort(entryName4);
}
}
Comment