Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 kempotrader, Today, 08:56 AM
      0 responses
      7 views
      0 likes
      Last Post kempotrader  
      Started by kempotrader, Today, 08:54 AM
      0 responses
      4 views
      0 likes
      Last Post kempotrader  
      Started by mmenigma, Today, 08:54 AM
      0 responses
      2 views
      0 likes
      Last Post mmenigma  
      Started by halgo_boulder, Today, 08:44 AM
      0 responses
      2 views
      0 likes
      Last Post halgo_boulder  
      Started by drewski1980, Today, 08:24 AM
      0 responses
      4 views
      0 likes
      Last Post drewski1980  
      Working...
      X