Announcement

Collapse
No announcement yet.

File IO with ninja

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

  • File IO with ninja

    Hi

    I have created this script to manage trades once triggered by global vars with ninjatrader. Ive got this far, what id like to do is setglobal vars if and when the order is filled, Not sure how to do this part,

    Also im not sure if the stplimit entries are correct for ninja. I use no strategy within ninja trader and would like it all managed by this efs, so a stop at entry is essential.

    If any of you gurus out there can help and edit the attached file, it will be much appreciated.
    Attached Files
    Last edited by biswar; 05-31-2004, 03:25 AM.

  • #2
    Ninja Code..

    I have worked with this tool before. I'll post all of the code I have for use with them..

    This code was pulled from another system, so you'll probably have to go thru it and "glean" what you need.

    B

    PHP Code:

    function WriteNTFile(OrderTypeContractsPTLevel) {
      var 
    = new File("order.txt");
         if (
    OrderType == "BUY") {
          if (
    CheckStateNTFile() != 1) {
           if (
    /*(CheckStateNTFile() == 0) || */(CheckStateNTFile() == 2) || (CheckStateNTFile() == -1)) {
             
    // close/cancel current order
              
    WriteNTCloseFile();
              
    WriteNTCancelFile();
           }
           
    debugPrintln("Executing Order "+OrderType+":"+Contracts);
           
    drawShapeRelative(0low()-0.75Shape.UPARROWnullColor.limeShape.ONTOPBarCount+"LongArrow");
           
    f.open("wt");
               
    f.writeln("ES,"+Contracts+",GTC,"+OrderType+",MKT,0,0,,,,,"+Simulation+",,");
           
    f.close();
           
    StrategyisInTrade true;
           
    StrategyisShort false;
           
    StrategyisLong true;
           
    AdjStopNeeded true;
           
    AdjStopStage 0;
           
    //TradePending = true;
          
    }
         }
         if (
    OrderType == "SELL") {
          if (
    CheckStateNTFile() != -1) {
           if (
    /*(CheckStateNTFile() == 0) || */(CheckStateNTFile() == 2) || (CheckStateNTFile() == 1)) {
             
    // close/cancel current order
              
    WriteNTCloseFile();
              
    WriteNTCancelFile();
           }
           
    debugPrintln("Executing Order "+OrderType+":"+Contracts);
           
    drawShapeRelative(0high()+0.75Shape.DOWNARROWnullColor.redShape.ONTOPBarCount+"ShortArrow");
           
    f.open("wt");
               
    f.writeln("ES,"+Contracts+",GTC,"+OrderType+",MKT,0,0,,,,,"+Simulation+",,");
    //           f.writeln('"ES","'+OrderType+'","'+Contracts+'","0","BRACKET","MKT","0","'+nStopPrice+'","'+PTLevel+'","0","5","0","FALSE","0","0",');
           
    f.close();
           
    StrategyisInTrade true;
           
    StrategyisShort true;
           
    StrategyisLong false;
           
    AdjStopNeeded true;   
           
    AdjStopStage 0;
          
    // TradePending = true;
          
    }
         }
    //     WriteNTStopAdjustFile();
    }

    function 
    WriteNTCloseFile() {
      var 
    = new File("close.txt");
                    
    f.open("wt");
                       
    f.writeln("ES");
                    
    f.close();
      
    debugPrintln(vHour+":"+vMin+" : Close Issued");
           
    StrategyisInTrade false;
           
    StrategyisShort false;
           
    StrategyisLong false;
    }

    function 
    WriteNTStopAdjustFile(NTTypeType) {
     if (
    AdjStopNeeded) {
      if ((
    CheckStateNTFile() == 1) || (CheckStateNTFile() == -1)) {
       var 
    = new File("order.txt");
        if(!
    f.exists()) {  
          if (
    NTType == 0) {
                    
    f.open("wt");
                       
    f.writeln("ES,"+DefEntryContracts+",GTC,"+Type+",STP,0,"+nStopPrice+",,,,,"+Simulation+",,");
                    
    //   f.writeln("ES,NEW1,"+nPTPrice+","+nStopPrice+",");
                    
    f.close();
           
    debugPrintln(vHour+":"+vMin+" : Stop Adj. Issued"+nStopPrice );
          }
          if (
    NTType == 1) {
                    
    f.open("wt");
                       
    f.writeln("ES,"+DefEntryContracts+",GTC,"+Type+",STPLMT,"+nPTPrice+",0,,,,,"+Simulation+",,");
                    
    //   f.writeln("ES,NEW1,"+nPTPrice+","+nStopPrice+",");
                    
    f.close();
           
    debugPrintln(vHour+":"+vMin+" : PT Adj. Issued"+nPTPrice );
          }
         
    AdjStopStage ++;
        }
       if (
    AdjStopStage 1) {  AdjStopNeeded false;  }
      }
     }
    }


    function 
    WriteNTCancelFile() {
      var 
    = new File("cancel.txt");
                    
    f.open("wt");
                       
    f.writeln("ES");
                    
    f.close();
      
    debugPrintln(vHour+":"+vMin+" : Cancel Issued");
    }


    function 
    CheckStateNTFile() {
    var 
    returnvalue 0;
      var 
    = new File("ESupdate.txt");
            if(
    f.exists()) {
                    
    f.open("rt");
                     var 
    stateline;
                       
    stateline f.readln();
                    
    f.close();
              var 
    PosCount stateline.split(",");

              if (
    PosCount[0] == "FLAT") {
               
    returnvalue 0;
               
    StrategyisInTrade false;
               
    StrategyisShort false;
               
    StrategyisLong false;
              } else if (
    PosCount[0] == "PENDING") {
               
    returnvalue 2;
              } else if (
    PosCount[0] == "LONG") {
               
    returnvalue 1;
               
    StrategyisInTrade true;
               
    StrategyisShort false;
               
    StrategyisLong true;
              } else if (
    PosCount[0] == "SHORT") {
               
    returnvalue = -1;
               
    StrategyisInTrade true;
               
    StrategyisShort true;
               
    StrategyisLong false;
              }
            } else {
               
    returnvalue 0;
            }
            
    debugPrintln("NT Status :"+returnvalue);
        return (
    returnvalue);

    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      File IO

      Thanks Brad

      i have found the problem i been having, I have used part of your script
      namely this part below

      The Data i get from Ninjatrader is this
      "LONG",60,565.4,#2004-06-03 19:16:13#,0,#2004-06-03 19:28:25#,

      The function you have below doesnt correctly read position 0, is there a better way of finding this value, Everytime it checks, it returns 0.




      function CheckStateNTFile() {
      var returnvalue = 0;
      var f = new File("ESupdate.txt");
      if(f.exists()) {
      f.open("rt");
      var stateline;
      stateline = f.readln();
      f.close();
      var PosCount = stateline.split(",");

      if (PosCount[0] == "FLAT") {
      returnvalue = 0;
      StrategyisInTrade = false;
      StrategyisShort = false;
      StrategyisLong = false;
      } else if (PosCount[0] == "PENDING") {
      returnvalue = 2;
      } else if (PosCount[0] == "LONG") {
      returnvalue = 1;
      StrategyisInTrade = true;
      StrategyisShort = false;
      StrategyisLong = true;
      } else if (PosCount[0] == "SHORT") {
      returnvalue = -1;
      StrategyisInTrade = true;
      StrategyisShort = true;
      StrategyisLong = false;
      }
      } else {
      returnvalue = 0;
      }
      debugPrintln("NT Status :"+returnvalue);
      return (returnvalue);
      }

      Comment


      • #4
        Did you ever figure this out? I am trying to do the same thing and seem at a loss for getting these file checks performed properly?

        Regards,
        Keth

        Comment


        • #5
          File IO with ninja

          yep got there in the end have a script up and running working fine
          this modified code works below


          function CheckStateNTFile() {
          var returnvalue = 0;
          var f = new File("ER2update.txt");


          if(f.exists()) {
          f.open("rt");
          var stateline;
          stateline = f.readln();
          f.close();
          if (stateline != null){
          var PosCount = stateline.split(",");

          var s = PosCount[0];

          var position = s.substr(1,4);
          //debugPrintln(position);
          if (position == "FLAT") {
          returnvalue = "50";
          StrategyisInTrade = false;
          StrategyisShort = false;
          StrategyisLong = false;

          } else if (position == "0") {
          returnvalue = "2";
          } else if (position == "LONG") {
          returnvalue = "1";
          islong = "True";
          StrategyisInTrade = true;
          StrategyisShort = false;
          StrategyisLong = true;
          contractson = new Number(PosCount[1]);
          fillprice = new Number(PosCount[2]);
          } else if (position == "SHOR") {
          returnvalue = -"1";
          isshort = "True"
          StrategyisInTrade = true;
          StrategyisShort = true;
          StrategyisLong = false;
          fillprice = new Number(PosCount[2]);
          }
          } else {
          returnvalue = 0;
          }
          debugPrintln("NT Status :"+returnvalue);
          return (returnvalue);

          }
          }
          }

          Comment


          • #6
            Thanks!

            Comment

            Working...
            X