• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with exit condition after convert double to int

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem with exit condition after convert double to int

    Hey,
    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.

    double stoploss1 = ATR(20)[0]*xfacheatr;
    int stoploss2 = System.Convert.ToInt16(stoploss1);
    i also tried:
    int stoploss2 = (int)stoploss1;
    but i get the same result.

    if i declare int stoploss = 20;
    everything works just fine.



    public class GrailSystemEURUSD : Strategy
    {
    #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);
    }


    }
    Last edited by mko1983; 08-18-2016, 01:18 PM.

    #2
    Hello mko1983,

    Thank you for writing in.

    What debugging steps have you already done with your code?

    Are you ensuring that your conditions to exit are becoming true? You can do this by using prints in your code. More information about this can be found here: http://ninjatrader.com/support/forum...58&postcount=1

    Have you enabled TraceOrders to check the behavior of your orders? What is the TraceOrders output telling you (the output can be seen in the Output Window, accessed under the Tools menu of the Control Center)? More information about TraceOrders can be found here: http://ninjatrader.com/support/helpG...raceorders.htm
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thanks zacharyG. Now i know what is wrong. Since everything is called OnBarUpdate() my amount of Contracts (anzahlKontrakte) changes with each bar, so Position.PositionQuantity on my ExitCondition is never equal to my amount of Contracts from EntryCondition.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Captain_Qwark, Today, 10:55 AM
      3 responses
      19 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by kmpc1, Today, 11:03 AM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_PatrickG  
      Started by jransom, Today, 09:08 AM
      1 response
      19 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by mtamaku, Today, 09:19 AM
      0 responses
      23 views
      0 likes
      Last Post mtamaku
      by mtamaku
       
      Started by traderMNQ, Today, 09:00 AM
      4 responses
      21 views
      0 likes
      Last Post traderMNQ  
      Working...
      X