Announcement

Collapse
No announcement yet.

Time Restrictions

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

  • Time Restrictions

    Is it possible to put time restrictions on a formula? What I want to do is have the first trade be at market open and exit all trades at market close. I'm trying to apply this code to the MA formula that came with esignal. Any help would be greatly appreciated.

  • #2
    Here is what i have so far. The line where the LLLLLLLLLLLLLLs are is where i'm stuck.

    I need the MA study to only trade when the vTIMEofDAY = ok2trade and to exit all trades when vTIMEofDay does't equal ok2trade.

    var study = new MAStudy(15, 0, "Close", MAStudy.EXPONENTIAL);
    var StartTradingTime = 73000;
    var NoNewTradeTime = 135900;
    var EndTradingTime = 135900;

    function preMain() {
    setPriceStudy(true);
    }

    function main() {
    BarHr = getHour();
    BarMin = getMinute();
    BarSec = getSecond();

    cBarTimeInt =BarHr*10000+BarMin*100+BarSec*1;

    if(cBarTimeInt >= StartTradingTime
    && cBarTimeInt<NoNewTradeTime){
    vTIMEofDAY = "ok2trade";
    }

    if(cBarTimeInt >= NoNewTradeTime
    && cBarTimeInt <= EndTradingTime){
    vTIMEofDAY = "NoNewTrades"
    }

    if(cBarTimeInt < StartTradingTime
    || cBarTimeInt >= EndTradingTime){
    vTIMEofDAY = "closed"
    dots5.call("CancelAllFor",sym, fut, exp, ri, 0, xchg, cur);
    dots5.call("ClosePositionFor", sym, fut, exp, ri, 0, xchg, cur);
    }
    LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
    var v = study.getValue(MAStudy.MA);

    if(v == null)
    return;


    if(close() >= v) {
    if(!Strategy.isLong()) {
    Strategy.doLong("Crossing Up", Strategy.CLOSE, Strategy.THISBAR);
    }

    } else {
    if(!Strategy.isShort()) {
    Strategy.doShort("Crossing Down", Strategy.CLOSE, Strategy.THISBAR);
    }
    }

    if(Strategy.isLong()) {
    setBarBgColor(Color.green);
    } else if(Strategy.isShort()) {
    setBarBgColor(Color.red);
    }


    return v;
    }

    Comment


    • #3
      if(close() >= v && vTIMEofDAY=="ok2trade"){

      etc.

      Comment


      • #4
        OK. Thanks dloomis. That gets me into trades no sooner than the time set. Now what would be the code to exit at market any open trades when vTIMEofDAY="NoNewTrades" ? Is there a comand such as Strategy.Exit that checks for open positions and exits those at closing pice of the candle.

        Comment


        • #5
          czumwalt

          Following David's earlier example (assuming I understood it correctly) then you would do the following to close

          if (Strategy.isLong() == true && vTIMEofDAY=="closed"){
          Strategy.doSell("",Strategy.CLOSE,Strategy.THISBAR ,Strategy.DEFAULT, 0);
          etc

          Alex

          Comment

          Working...
          X