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

trouble with spot losses

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

    #16
    Hello daglas,
    To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

    Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

    I look forward to assisting you further.

    *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
    JoydeepNinjaTrader Customer Service

    Comment


      #17
      Originally posted by NinjaTrader_Joydeep View Post
      Hello daglas,
      To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

      Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

      I look forward to assisting you further.

      *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
      I can attach toy code here.
      Know I am attaching code of closing position by market orders.
      Code:
              protected override void OnMarketData(MarketDataEventArgs e)
      {
          
          //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          foreach (Position p in Account.Positions)
      {
           //Print(p.Instrument.FullName);
              if (p.Instrument.FullName == Instrument.FullName)
              {
             //        Print(p.AvgPrice);
              }
      }
      
                  System.Collections.IEnumerator ListPositions = Account.Positions.GetEnumerator();
      
      for (int i = 0; i < Account.Positions.Count; i++)
      {
      ListPositions.MoveNext();
      Print("Open Position: " + ListPositions.Current);
          td=ListPositions.Current+"";
      
          if (td.Length<2) {mytd="";}
          if (td.IndexOf("Instrument='"+Instrument.MasterInstrument.Name) > -1)
          {
          //    td.Split(" ");
              array1=td.Split(' ');
              Print(array1[1]);
              Print(array1[2]);
              Print(array1[3]);
              Print(array1[4]);
              //==============
              Print(array1[4].Substring(6,array1[4].Length-6));
              prc=Convert.ToDouble(array1[4].Substring(6,array1[4].Length-6));
              //===================
              Print(array1[5]);
              Print(array1[5].Substring(9,array1[5].Length-9));
              vol=Convert.ToInt32(array1[5].Substring(9,array1[5].Length-9));
              Print(array1[6]);
              Print(array1[7]);
              Print(array1[7].Substring(9,array1[7].Length-9));
              napr=array1[7].Substring(9,array1[7].Length-9);
              
              if (napr=="Short")
              {
              prof=(prc-Close[0])/0.0001*vol*10;
                  Print("profit="+prof);
              }
              //)))))))))))))))))))))))))))))))))))))))))))))))))
                      if (napr=="Long")
              {
              prof=(Close[0]-prc)/0.0001*vol*10;
                  Print("profit="+prof);
              }
              Print(proflimit);
              Print(stopl);
              //============================================
          //    Print(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency));
              if (prof>proflimit)
                      {
                          Print("try to close profit");
                      if (napr=="Short") { 
                      EnterLong(vol); 
                      Print("try to close Short");
                      
                      }
                      if (napr=="Long") { 
                          
                      EnterShort(vol);
                          
                      Print("try to close Long");
                      }
                      }
                      //====================================
                                      if (prof<stopl)
                      {
                          Print("try to close loss");
                      if (napr=="Short") { 
                          EnterLong(vol); 
                      }
                      if (napr=="Long") { 
                          EnterShort(vol);
                          }
                      }
          }
      }    
      }

      Comment


        #18
        Toy code like this
        Code:
                /// </summary>
                protected override void Initialize()
                {
                    CalculateOnBarClose = true;
                    SetStopLoss(150,true);
                    SetProfitTarget(400);
        
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    if (a>b&&Position.MarketPosition!=MarketPosition.Long)
                    
                        {
                            EnterLong(1);
                            sp=-100;
                            
                        }
                        //--------------
                                if (a<b&&Position.MarketPosition!=MarketPosition.Short)
                        {
                            EnterShort(1);
                            bp=100000;
                            
                        }
                        //--------------------
                
                        //====================================
                                        
                        
                    
                    
                }

        Comment


          #19
          also I have a question.
          How can my previously attached code increase size of position? I want to use fixed lot size. I use trigger (position !=long) before opening long position and (position!= short) before opening short positions. But I observe 3-4 qty in properties of position.
          How can I fix this?

          Comment


            #20
            Hello daglas,
            You have used codes like
            Code:
            EnterLong(vol);
            What is the value of "vol"? Is it calculated dynamically or is a fixed value?

            Unfortunately your code is not clear enough for to give a definite answer.
            JoydeepNinjaTrader Customer Service

            Comment


              #21
              I use fixed volume for opening position, it always =1.

              Comment


                #22
                Hello daglas,
                Unfortunately it is not clear from the code you provided but I would suggest to check the market position as flat before entering a trade.

                Like

                Code:
                if (condiations && Position.MarketPosition == MarketPosition.Flat)
                {
                   //enter a position
                }
                JoydeepNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by f.saeidi, Today, 12:14 PM
                4 responses
                10 views
                0 likes
                Last Post f.saeidi  
                Started by ZenCortexHurry, Today, 01:07 PM
                0 responses
                3 views
                0 likes
                Last Post ZenCortexHurry  
                Started by ZenCortexHurry, Today, 01:04 PM
                0 responses
                2 views
                0 likes
                Last Post ZenCortexHurry  
                Started by Russ Moreland, Today, 12:54 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by philmg, Today, 12:55 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_ChristopherJ  
                Working...
                X