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

Random Entry Strategy (coin toss)

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

    Random Entry Strategy (coin toss)

    Hi all, I am trying to write a strategy for random entries. Essentially coin toss determining if the trade is long or short and simple stop and target parameters and scaling entries extis will be tested. Once the coin if flipped the trade is entered at market with a pre-defined stop and target.

    I have tried setting up the basic logic but it fails. My code only generates short trades, or only long trades hence the random function does not work the way I want it to. Please see below.

    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1
    Random randNum = new Random();
    tempVar=randNum.Next(
    0,1);


    if (tempVar > 0.5) //tempVar is float
    {
    EnterLong(DefaultQuantity,
    "");
    tempVar=randNum.Next(
    0,1);

    }
    else
    {
    EnterShort(DefaultQuantity,
    "");
    tempVar=randNum.Next(
    0,1);

    }


    If anyone could help me get this working cortrectly I would appreciate it greatly, thanks.

    #2
    Your saying tempVar is a float, but your assigning an interger to it.

    So declare tempVar as an int, then change conditional statement to..


    tempVar > 0

    Comment


      #3
      Originally posted by hunt4alpha View Post
      Hi all, I am trying to write a strategy for random entries. Essentially coin toss determining if the trade is long or short and simple stop and target parameters and scaling entries extis will be tested. Once the coin if flipped the trade is entered at market with a pre-defined stop and target.

      I have tried setting up the basic logic but it fails. My code only generates short trades, or only long trades hence the random function does not work the way I want it to. Please see below.

      protectedoverridevoid OnBarUpdate()
      {
      // Condition set 1
      Random randNum = new Random();
      tempVar=randNum.Next(
      0,1);


      if (tempVar > 0.5) //tempVar is float
      {
      EnterLong(DefaultQuantity,
      "");
      tempVar=randNum.Next(
      0,1);

      }
      else
      {
      EnterShort(DefaultQuantity,
      "");
      tempVar=randNum.Next(
      0,1);

      }


      If anyone could help me get this working cortrectly I would appreciate it greatly, thanks.
      You are using Next(int32, int32) method of the Random class. That method has an inclusive lower bound and an exclusive upper bound. Therefore, using 0 and 1 as your bounds is in effect saying that 0 is the only possible value. If you want to use 0 and 1 as your dichotomous value, then your bounds should be (0,2).

      Comment


        #4
        Originally posted by koganam View Post
        You are using Next(int32, int32) method of the Random class. That method has an inclusive lower bound and an exclusive upper bound. Therefore, using 0 and 1 as your bounds is in effect saying that 0 is the only possible value. If you want to use 0 and 1 as your dichotomous value, then your bounds should be (0,2).

        Thanks guys... got it going

        protectedoverridevoid Initialize()
        {
        SetProfitTarget(
        "", CalculationMode.Ticks, target);
        SetStopLoss(
        "", CalculationMode.Ticks, stop, false);
        CalculateOnBarClose =
        true;
        }
        ///<summary>
        /// Called on each bar update event (incoming tick)
        ///</summary>
        protectedoverridevoid OnBarUpdate()
        {
        // Condition set 1
        Random randNum = new Random();

        if (randNum.Next(0,2) == 1)
        {
        EnterLong(DefaultQuantity,
        "");
        }
        else
        {
        EnterShort(DefaultQuantity,
        "");


        }

        Comment


          #5
          Was wondering if anyone knows how I can get it to time out between trades for a couple of minutes ?

          I am using range bars set to 1, is it necessary to do this or will it look into the ticks inside the 1 minute bars to determine where executions were on the backtest. If it looks into the ticks within the 1 minute bars, I suppose an easy way to time out would be to tell it to wait a couple of bards before an entry. Though I am not sure how to do this.

          Please help me out with the timing issue. Thanks again, super helpful guys!
          Last edited by hunt4alpha; 06-08-2012, 08:28 PM.

          Comment


            #6
            Originally posted by hunt4alpha View Post
            Was wondering if anyone knows how I can get it to time out between trades for a couple of minutes ?

            I am using range bars set to 1, is it necessary to do this or will it look into the ticks inside the 1 minute bars to determine where executions were on the backtest. If it looks into the ticks within the 1 minute bars, I suppose an easy way to time out would be to tell it to wait a couple of bards before an entry. Though I am not sure how to do this.

            Please help me out with the timing issue. Thanks again, super helpful guys!
            In backtesting COBC is always going to be true, so there is no way to look inside bars. Of course, using a 1-range bar does mean in effect every tick move is being registered.

            You can create your timeout by recording the time when the strategy goes flat, then not allowing entry until your time lapse has passed.

            Comment


              #7
              Since your doing this with OnBarUpdate(), you wouldn't want to use property Thread.Sleep().

              So you would likely want to use use a conditional test based on saved variable and property Date.Time.Now

              Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

              Comment


                #8
                The Stopwatch.Elapsed property is another interesting approach.

                Comment


                  #9
                  Originally posted by borland View Post
                  The Stopwatch.Elapsed property is another interesting approach.

                  http://msdn.microsoft.com/en-us/libr...h.elapsed.aspx

                  Wow, this seems a little beyond my abilities. I tried paying with it but I can't get it to work. Is there, maybe, a simpler way to get it to skip the next ten 1 range bars after a trade is closed out?

                  All I wanted to do is when back testing allow a little bit of time between the exit of one and the entry of the next, this is obviously not for real live trading as much as it is for research purposes.

                  Comment


                    #10
                    Originally posted by borland View Post
                    Since your doing this with OnBarUpdate(), you wouldn't want to use property Thread.Sleep().

                    So you would likely want to use use a conditional test based on saved variable and property Date.Time.Now

                    http://msdn.microsoft.com/en-us/libr...etime.now.aspx

                    I am using this for a backtest, wouldnt this get the current time on my pc rather than during the entry on hte backtest?

                    Comment


                      #11
                      hunt4alpha,

                      That is correct, that gets the current PC time. You could use it in live trading.

                      if ( Historical )
                      {
                      }

                      Checks if you are in a backtest or on historical bars. In this case you could use another method to get the time.

                      Time[0] will always be the time stamp of the most current bar, even on historical bars as the strategy iterates through past bars.
                      Adam P.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_AdamP View Post
                        hunt4alpha,

                        That is correct, that gets the current PC time. You could use it in live trading.

                        if ( Historical )
                        {
                        }

                        Checks if you are in a backtest or on historical bars. In this case you could use another method to get the time.

                        Time[0] will always be the time stamp of the most current bar, even on historical bars as the strategy iterates through past bars.

                        Yes this is not for live trading purposes it is strictly for running on backtests only.


                        Thanks Adam! I will try to get this to work. Is there a way to grab the time stamp when I flatten a trade(on a backtest)?

                        if so then I can just compare the timestamp of current bar to the timestamp of my last flatten. Obviosuly an "if" statement to continue into the random entry execution code or to return nothing until say 3 minutes has passed would solve my issue. I never thought this would be so tricky.

                        Looking forward to hearing back from you on this if you don't mind I am a newbie and really appreciate the help. Thanks again.

                        Comment


                          #13
                          Got it to work by doing a logical test on Time[0], before entering my trade logic (rand entry). I take the time stamp evey time a trade is flattened using :

                          if (position.MarketPosition == MarketPosition.Flat)
                          {
                          x=Time[
                          0];
                          x = x + TimeSpan.Parse(
                          "00:08:00");

                          y=randNum.Next(
                          0,2);

                          }



                          Looks like I am all set, thanks for the help guys!

                          Comment


                            #14
                            Here's some alternate random entry code if anybody's interested. link

                            Comment


                              #15
                              Alternate entry between long and short

                              Hi.... I know this thread was about a random time based entry but this was the closest I could find to what I am trying to do.
                              Does anyone know a piece of code that that will allow my all my entries to continually alternate back and forth from LONG TO SHORT LONG TO SHORT LONG TO SHORT and so on and so on.
                              Basically even if there was present logic to make a LONG entry and our last entry was already a LONG entry , the algo would wait until there was the logic only to take the SHORT entry next.

                              If anyone knows a bit of code that would allow me to do this I would appreciate it.

                              Thanks

                              Quantismo

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Mindset, 05-06-2023, 09:03 PM
                              10 responses
                              262 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by michi08, 10-05-2018, 09:31 AM
                              5 responses
                              741 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by The_Sec, Today, 02:29 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              4 responses
                              62 views
                              0 likes
                              Last Post aligator  
                              Started by sightcareclickhere, Today, 01:55 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post sightcareclickhere  
                              Working...
                              X