Announcement

Collapse
No announcement yet.

paper trading

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

  • #16
    marvslater
    Declare a global variable (ie outside of function main) called for example isInTrade and set it initially to 0

    var isInTrade = 0;

    The variable will have three states ie 0 for flat, 1 for long and -1 for short.
    In the conditions to go long you will check that isInTrade is not equal to 1 and you will then set it to 1 as a long trade is entered

    if(isInTrade != 1 && close() > MAStudy){
    trade.Buy(getSymbol(),100,PaperTradeBroker.MARKET) ;
    isInTrade = 1;
    }


    In the conditions to go short you will instead check that isInTrade is not equal to -1 and then set it to -1 once a short trade is entered.
    When you execute a trade to either sell a long position or cover a short (without initiating a trade in the opposite direction) set isInTrade to 0
    Hope this helps
    Alex

    Comment


    • #17
      Perfect! Works great. It does seem a little counter intuitive. I was, using your example, putting if ( isInTrade == 1 && ... in my conditons but then after the trade.Buy(getSymbol.... I thought you would have to change the flag so that it wouldn't repeat so I entered isInTrade = -1 thinking that would make the next order a sell. Any way, I don't really understand it but it works.
      Thanks.

      Comment


      • #18
        marvslater
        Instead of 0, 1 and -1 for the three states of the isInTrade variable think of them as "flat", "long" and "short".
        So when you declare the variable you set it to
        var isInTrade = "flat";
        Then in your conditional statement you use
        if(isInTrade != "long" && your_condition){
        trade.Buy(....)
        isInTrade = "long"
        }

        The first time that your_condition is true and isInTrade is not equal to "long" the trade will be executed.
        At that point you set isInTrade to "long" which means that at the next iteration of the formula the condition isInTrade != "long"... will no longer be true and no further trades will be executed.
        Hope this explains the process better
        Alex

        Comment

        Working...
        X