Announcement

Collapse
No announcement yet.

button & functioncall can execute code?

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

  • button & functioncall can execute code?

    Even after a search for examples, I wonder if code can be executed when a button is pressed.

    With this code, only the last message appear even when in trading hours.
    Can I put some code or should it be a function?

    Cheers

    Andrei





    function callClose_position(nButtonPressed, position, now, day_open, day_close, s_name, s_type, s_exp, s_right, s_strike, s_exch, s_curr, num_contracts)
    {
    if ( getButtonPressed( nButtonPressed ) == 1 )
    {
    if ((now >= day_open) && (now < day_close))
    {
    if (position == LONG)
    {
    dots5.call("ClosePosition", s_name, s_type, s_exp, s_right, s_strike, s_exch, s_curr, num_contracts);
    drawShapeRelative(0, high()+0.25, Shape.UPARROW, null, Color.yellow, Shape.ONTOP, BLBarOffset+"EOD Long");
    }
    else if (position == SHORT)
    {
    dots5.call("ClosePosition", s_name, s_type, s_exp, s_right, s_strike, s_exch, s_curr, num_contracts);
    drawShapeRelative(0, low()-0.25, Shape.DOWNARROW, null, Color.yellow, Shape.ONTOP, BLBarOffset+"EOD Short");
    }
    else if (position == NONE)
    log_msg("No position to close...");
    }
    else
    log_msg("Can't close a position outside the trading hours..."); <==== only this appear
    }
    return null;
    }

  • #2
    Yes it can, but not in the manner in which you have it laid out (you are very close though). I took a look at your code, I believe your problem lies with how you call your function, as I personally have not seen it done with multiple variables such as you are doing. When you call a function from a button event, I thought all that was supplied was the button which was pressed. All the additional variables which are in your function statement will become null values within your function.

    Therefore,if ((now >= day_open) && (now < day_close)){ is a test of nulls, so the else statement associated with this if statement is always true, thus your quandry. Please see my fileshare area "Functions" for my button examples. While you are there, take a look at the debug area and download the trace program. Use of this tool would have shown you the problem (or it may show that the problem is completely different).

    If I were to bet though, I would think that every variable in your function declaration, with the exception of the first is null

    Comment


    • #3
      Thank you very much for your explanations.

      Indeed, I have based it on your "button example" efs but there wasn't any code to execute... that's why I am somewhat confused.

      If all the variables are null within the function, does that mean I have to include all the ancillary code to give them the values I need for proper execution?
      For example define "now", "day_close"etc.

      Cheers

      Andrei

      Comment


      • #4
        First I would try the a debugPrintln of some of the variables to see if I am right.

        What I would do though is if the variables are already globals, just use them, don't reassign them in the function.

        e.g.
        function callClose_position(nButtonPressed);

        then if all these variables are globals, you don't need to pass them.

        e.g.
        position, now, day_open, day_close, s_name, s_type, s_exp, s_right, s_strike, s_exch, s_curr, num_contracts

        Comment


        • #5
          ahh, a do over...

          I had writen a nice lengthy reply to this to help you out, but when I posted it, it went away (into the BB nowhere land). Of course, I did not COPY it to the clipboard, so "do over".

          Basically (the short version), you want to create control variables (global variables) that you can use to control the button functions.
          You can't pass anything to the button function directly, you have to pass it as control variables (which are global variables).

          So, you need to declare these variables and set their initial values..

          var variable1 = 0; // button control variable
          var variable2 = true;
          var variable3 = 2.0;
          var tButton1Text = " start ";

          Now, within your main() function, you want to control the variables and draw the button.

          function main() {

          if (variable1 == 0) { tButton1Text = " Start "; }
          if (variable1 == 1) { tButton1Text = " PTs "; }
          if (variable1 == 2) { tButton1Text = " Stop "; }
          if (variable1 == 3) { tButton1Text = " Exit "; }

          drawTextPixel(15,15, tButton1Text + "@URL=EFS:tButtonOne" , Color.black, Color.RGB(0xE0, 0xFF, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, null, "Tresults");

          }

          Next, you have to create your button functions..

          function tButtonOne(nButtonPressed)
          {
          variable1 += 1;
          if (variable1 > 3) { variable1 = 0; }
          if (variable1 == 0) { tButton1Text = " Start "; }
          if (variable1 == 1) { tButton1Text = " PTs "; }
          if (variable1 == 2) { tButton1Text = " Stop "; }
          if (variable1 == 3) { tButton1Text = " Exit "; }

          //.. do your code here



          // redraw the button
          drawTextPixel(15,15, tButton1Text + "@URL=EFS:tButtonOne" , Color.black, Color.RGB(0xE0, 0xFF, 0xE0), Text.FRAME | Text.ONTOP | Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, null, "Tresults");

          }

          That is the basics of my last attempted post. Sorry about the delay.

          B
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #6
            Yes, some variables were in pre-main for a Menu-type and were thus not global.

            The button and the code work now perfectly.
            I really must make teh effort to understand your trace.efs as I am sure it would help
            a lot...

            Thanks again.

            Cheers

            Andrei

            Comment


            • #7
              you are very welcome I am uploading a new version tonight that has less overhead required to use.

              Regards

              Comment

              Working...
              X