How to save value as condition after entering in a strategy?Tradingview Pine script save close price at time of strategy entryPine script - Enter position when multiple conditions are trueHow to get last value that equals the current value in TradingView Pine Script?How to transform Momentum strategy scripts to alert in pinescript?Can't solve two problems with PineScriptStrategy with multiple conditions true at different timesLong entry after a condition is met and after a certain number of bars printed in Pine Script (Tradingview)Strategy Tester in TradeView : problem with Qty and ReturnsStrategy Tester Exit Always Re-Enters

Why did Intel abandon unified CPU cache?

Does putting salt first make it easier for attacker to bruteforce the hash?

What differences exist between adamantine and adamantite in all editions of D&D?

Who is "He that flies" in Lord of the Rings?

A map of non-pathological topology?

Did Apple bundle a specific monitor with the Apple II+ for schools?

How to befriend someone who doesn't like to talk?

Printing Pascal’s triangle for n number of rows in Python

Write a function that checks if a string starts with or contains something

Why Does Mama Coco Look Old After Going to the Other World?

Why does smartdiagram replace the Greek letter xi by a number?

How to write a convincing religious myth?

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

How to avoid typing 'git' at the begining of every Git command

Live action TV show where High school Kids go into the virtual world and have to clear levels

Varying the size of dots in a plot according to information contained in list

Do you need to let the DM know when you are multiclassing?

Separate SPI data

How can I make 12 tone and atonal melodies sound interesting?

What is this Amiga 1200 mod?

What aircraft was used as Air Force One for the flight between Southampton and Shannon?

Amplitude of a crest and trough in a sound wave?

Getting UPS Power from One Room to Another

Fermat's statement about the ancients: How serious was he?



How to save value as condition after entering in a strategy?


Tradingview Pine script save close price at time of strategy entryPine script - Enter position when multiple conditions are trueHow to get last value that equals the current value in TradingView Pine Script?How to transform Momentum strategy scripts to alert in pinescript?Can't solve two problems with PineScriptStrategy with multiple conditions true at different timesLong entry after a condition is met and after a certain number of bars printed in Pine Script (Tradingview)Strategy Tester in TradeView : problem with Qty and ReturnsStrategy Tester Exit Always Re-Enters






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am having trouble with the following:



I enter a strategy which is closed after close < ema. For the sake of a better exit, I would like to close it when close > ema but the low (of any given bar after the entry) is lower than ema (low < ema).



I cannot figure out how to do the 'any given bar after the entry' moment. I guess the script should somehow store the value of the previous bar if true, but then comes the problem with scripting when the strategy has actually started. Any help would be appreciated!



PS. As you can see I am not a coder and this is probably hard to understand. I really apoligize for it and thank you for your time.



Mihail



I have tried with stating when the entry condition is on with strategy.position_avg_price > 0, adding to it the desired conditions:



 h = nz(strategy.position_avg_price) > 0 and not 
crossunder(close,ema(close,length)) and
crossunder(low,ema(close,length)) ? 1 : 0
rightborder = barstate.islast // treat the last bar (most recent bar)
as the right edge of the lookback window range
// if examining the last bar (newest bar, rightborder is true)
// set variable "val" to the previous value of series variable "h"
// else set to na so nothing is plotted
val = rightborder ? h[1] : na


But without success...



 scalp = b and c and d and e and f and g ? 1 : 0 // scalp is main 
variable, if 1 the strategy is entered//
if (scalp)
strategy.entry("Short", strategy.short, when = scalp) // entry of
strategy
if (crossunder(close,ema(close,length))) // usual close of strategy
strategy.close("Short")
if (not crossunder(close,ema(close,length)) and
crossunder(low,ema(close,length))) // attempt for a better exit!
strategy.close("Short")


After working on Mickey's suggestion:



///Entry 
if entry_on == 0 and scalp
strategy.entry("Short", strategy.short)
entry_on := 1

///Desired exit
if entry_on == 1 and crossunder(close,ema(close,length))
strategy.close("Short")
entry_on := 0

/// Risk mitigation - 1 - Additional risk mitigation (when close > ema but
low < ema of any given candle after entry -> exit at breakeven)

if entry_on == 1 and close > ema(close, length) and low < ema(close, length)
entry_on := 2

if entry_on == 2 and crossover(close,strategy.position_avg_price)
strategy.close("Short")
entry_on := 0

/// Risk mitigation - 2 - exit 15 bars after entry if not desired exit or
risk mitigation - 1

if entry_on == 1 and scalp[15]
strategy.close("Short")
entry_on := 0









share|improve this question






























    1















    I am having trouble with the following:



    I enter a strategy which is closed after close < ema. For the sake of a better exit, I would like to close it when close > ema but the low (of any given bar after the entry) is lower than ema (low < ema).



    I cannot figure out how to do the 'any given bar after the entry' moment. I guess the script should somehow store the value of the previous bar if true, but then comes the problem with scripting when the strategy has actually started. Any help would be appreciated!



    PS. As you can see I am not a coder and this is probably hard to understand. I really apoligize for it and thank you for your time.



    Mihail



    I have tried with stating when the entry condition is on with strategy.position_avg_price > 0, adding to it the desired conditions:



     h = nz(strategy.position_avg_price) > 0 and not 
    crossunder(close,ema(close,length)) and
    crossunder(low,ema(close,length)) ? 1 : 0
    rightborder = barstate.islast // treat the last bar (most recent bar)
    as the right edge of the lookback window range
    // if examining the last bar (newest bar, rightborder is true)
    // set variable "val" to the previous value of series variable "h"
    // else set to na so nothing is plotted
    val = rightborder ? h[1] : na


    But without success...



     scalp = b and c and d and e and f and g ? 1 : 0 // scalp is main 
    variable, if 1 the strategy is entered//
    if (scalp)
    strategy.entry("Short", strategy.short, when = scalp) // entry of
    strategy
    if (crossunder(close,ema(close,length))) // usual close of strategy
    strategy.close("Short")
    if (not crossunder(close,ema(close,length)) and
    crossunder(low,ema(close,length))) // attempt for a better exit!
    strategy.close("Short")


    After working on Mickey's suggestion:



    ///Entry 
    if entry_on == 0 and scalp
    strategy.entry("Short", strategy.short)
    entry_on := 1

    ///Desired exit
    if entry_on == 1 and crossunder(close,ema(close,length))
    strategy.close("Short")
    entry_on := 0

    /// Risk mitigation - 1 - Additional risk mitigation (when close > ema but
    low < ema of any given candle after entry -> exit at breakeven)

    if entry_on == 1 and close > ema(close, length) and low < ema(close, length)
    entry_on := 2

    if entry_on == 2 and crossover(close,strategy.position_avg_price)
    strategy.close("Short")
    entry_on := 0

    /// Risk mitigation - 2 - exit 15 bars after entry if not desired exit or
    risk mitigation - 1

    if entry_on == 1 and scalp[15]
    strategy.close("Short")
    entry_on := 0









    share|improve this question


























      1












      1








      1








      I am having trouble with the following:



      I enter a strategy which is closed after close < ema. For the sake of a better exit, I would like to close it when close > ema but the low (of any given bar after the entry) is lower than ema (low < ema).



      I cannot figure out how to do the 'any given bar after the entry' moment. I guess the script should somehow store the value of the previous bar if true, but then comes the problem with scripting when the strategy has actually started. Any help would be appreciated!



      PS. As you can see I am not a coder and this is probably hard to understand. I really apoligize for it and thank you for your time.



      Mihail



      I have tried with stating when the entry condition is on with strategy.position_avg_price > 0, adding to it the desired conditions:



       h = nz(strategy.position_avg_price) > 0 and not 
      crossunder(close,ema(close,length)) and
      crossunder(low,ema(close,length)) ? 1 : 0
      rightborder = barstate.islast // treat the last bar (most recent bar)
      as the right edge of the lookback window range
      // if examining the last bar (newest bar, rightborder is true)
      // set variable "val" to the previous value of series variable "h"
      // else set to na so nothing is plotted
      val = rightborder ? h[1] : na


      But without success...



       scalp = b and c and d and e and f and g ? 1 : 0 // scalp is main 
      variable, if 1 the strategy is entered//
      if (scalp)
      strategy.entry("Short", strategy.short, when = scalp) // entry of
      strategy
      if (crossunder(close,ema(close,length))) // usual close of strategy
      strategy.close("Short")
      if (not crossunder(close,ema(close,length)) and
      crossunder(low,ema(close,length))) // attempt for a better exit!
      strategy.close("Short")


      After working on Mickey's suggestion:



      ///Entry 
      if entry_on == 0 and scalp
      strategy.entry("Short", strategy.short)
      entry_on := 1

      ///Desired exit
      if entry_on == 1 and crossunder(close,ema(close,length))
      strategy.close("Short")
      entry_on := 0

      /// Risk mitigation - 1 - Additional risk mitigation (when close > ema but
      low < ema of any given candle after entry -> exit at breakeven)

      if entry_on == 1 and close > ema(close, length) and low < ema(close, length)
      entry_on := 2

      if entry_on == 2 and crossover(close,strategy.position_avg_price)
      strategy.close("Short")
      entry_on := 0

      /// Risk mitigation - 2 - exit 15 bars after entry if not desired exit or
      risk mitigation - 1

      if entry_on == 1 and scalp[15]
      strategy.close("Short")
      entry_on := 0









      share|improve this question
















      I am having trouble with the following:



      I enter a strategy which is closed after close < ema. For the sake of a better exit, I would like to close it when close > ema but the low (of any given bar after the entry) is lower than ema (low < ema).



      I cannot figure out how to do the 'any given bar after the entry' moment. I guess the script should somehow store the value of the previous bar if true, but then comes the problem with scripting when the strategy has actually started. Any help would be appreciated!



      PS. As you can see I am not a coder and this is probably hard to understand. I really apoligize for it and thank you for your time.



      Mihail



      I have tried with stating when the entry condition is on with strategy.position_avg_price > 0, adding to it the desired conditions:



       h = nz(strategy.position_avg_price) > 0 and not 
      crossunder(close,ema(close,length)) and
      crossunder(low,ema(close,length)) ? 1 : 0
      rightborder = barstate.islast // treat the last bar (most recent bar)
      as the right edge of the lookback window range
      // if examining the last bar (newest bar, rightborder is true)
      // set variable "val" to the previous value of series variable "h"
      // else set to na so nothing is plotted
      val = rightborder ? h[1] : na


      But without success...



       scalp = b and c and d and e and f and g ? 1 : 0 // scalp is main 
      variable, if 1 the strategy is entered//
      if (scalp)
      strategy.entry("Short", strategy.short, when = scalp) // entry of
      strategy
      if (crossunder(close,ema(close,length))) // usual close of strategy
      strategy.close("Short")
      if (not crossunder(close,ema(close,length)) and
      crossunder(low,ema(close,length))) // attempt for a better exit!
      strategy.close("Short")


      After working on Mickey's suggestion:



      ///Entry 
      if entry_on == 0 and scalp
      strategy.entry("Short", strategy.short)
      entry_on := 1

      ///Desired exit
      if entry_on == 1 and crossunder(close,ema(close,length))
      strategy.close("Short")
      entry_on := 0

      /// Risk mitigation - 1 - Additional risk mitigation (when close > ema but
      low < ema of any given candle after entry -> exit at breakeven)

      if entry_on == 1 and close > ema(close, length) and low < ema(close, length)
      entry_on := 2

      if entry_on == 2 and crossover(close,strategy.position_avg_price)
      strategy.close("Short")
      entry_on := 0

      /// Risk mitigation - 2 - exit 15 bars after entry if not desired exit or
      risk mitigation - 1

      if entry_on == 1 and scalp[15]
      strategy.close("Short")
      entry_on := 0






      pine-script tradingview-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 2:37







      Mihail Bukov

















      asked Mar 24 at 15:57









      Mihail BukovMihail Bukov

      63




      63






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Try something like this:



          entry_on = 0.0
          entry_on := entry_on[1] //this will carry entry_on result from last candle
          if entry_on == 0 and close > ema(close, length)
          xx enter your open position code
          entry_on := 1

          if entry_on == 1
          if close < ema(close, length) or low < ema(close, length)
          xx enter your close position code
          entry_on := 0





          share|improve this answer























          • Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

            – Mihail Bukov
            Mar 25 at 2:10












          • once again thx a lot. The adjusted code is at the bottom of the question.

            – Mihail Bukov
            Mar 25 at 2:43











          • Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

            – Mihail Bukov
            Mar 26 at 2:09












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325661%2fhow-to-save-value-as-condition-after-entering-in-a-strategy%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Try something like this:



          entry_on = 0.0
          entry_on := entry_on[1] //this will carry entry_on result from last candle
          if entry_on == 0 and close > ema(close, length)
          xx enter your open position code
          entry_on := 1

          if entry_on == 1
          if close < ema(close, length) or low < ema(close, length)
          xx enter your close position code
          entry_on := 0





          share|improve this answer























          • Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

            – Mihail Bukov
            Mar 25 at 2:10












          • once again thx a lot. The adjusted code is at the bottom of the question.

            – Mihail Bukov
            Mar 25 at 2:43











          • Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

            – Mihail Bukov
            Mar 26 at 2:09
















          1














          Try something like this:



          entry_on = 0.0
          entry_on := entry_on[1] //this will carry entry_on result from last candle
          if entry_on == 0 and close > ema(close, length)
          xx enter your open position code
          entry_on := 1

          if entry_on == 1
          if close < ema(close, length) or low < ema(close, length)
          xx enter your close position code
          entry_on := 0





          share|improve this answer























          • Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

            – Mihail Bukov
            Mar 25 at 2:10












          • once again thx a lot. The adjusted code is at the bottom of the question.

            – Mihail Bukov
            Mar 25 at 2:43











          • Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

            – Mihail Bukov
            Mar 26 at 2:09














          1












          1








          1







          Try something like this:



          entry_on = 0.0
          entry_on := entry_on[1] //this will carry entry_on result from last candle
          if entry_on == 0 and close > ema(close, length)
          xx enter your open position code
          entry_on := 1

          if entry_on == 1
          if close < ema(close, length) or low < ema(close, length)
          xx enter your close position code
          entry_on := 0





          share|improve this answer













          Try something like this:



          entry_on = 0.0
          entry_on := entry_on[1] //this will carry entry_on result from last candle
          if entry_on == 0 and close > ema(close, length)
          xx enter your open position code
          entry_on := 1

          if entry_on == 1
          if close < ema(close, length) or low < ema(close, length)
          xx enter your close position code
          entry_on := 0






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 20:24









          MikeyyMikeyy

          754




          754












          • Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

            – Mihail Bukov
            Mar 25 at 2:10












          • once again thx a lot. The adjusted code is at the bottom of the question.

            – Mihail Bukov
            Mar 25 at 2:43











          • Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

            – Mihail Bukov
            Mar 26 at 2:09


















          • Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

            – Mihail Bukov
            Mar 25 at 2:10












          • once again thx a lot. The adjusted code is at the bottom of the question.

            – Mihail Bukov
            Mar 25 at 2:43











          • Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

            – Mihail Bukov
            Mar 26 at 2:09

















          Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

          – Mihail Bukov
          Mar 25 at 2:10






          Thanks a lot. Your suggestion definitely helped. However, for some reason, despite the fact that the code starts with strategy and not with script, when I add it to the chart the strategy is not triggered and there is a wierd last line in the pine tab 'script added to the chart' (as there is no plot function in the code nothing is shown). Any idea why? Could you briefly look at the code that I ended up doing after reading your post and tell me whether it makes sense? I had to make several adjustments and I might have missed somewhere the something

          – Mihail Bukov
          Mar 25 at 2:10














          once again thx a lot. The adjusted code is at the bottom of the question.

          – Mihail Bukov
          Mar 25 at 2:43





          once again thx a lot. The adjusted code is at the bottom of the question.

          – Mihail Bukov
          Mar 25 at 2:43













          Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

          – Mihail Bukov
          Mar 26 at 2:09






          Found that 'sript added to the chart' appears even for strategy scripts, so disregard my comment. But somewhere the code is wrong as trades are not triggered

          – Mihail Bukov
          Mar 26 at 2:09




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325661%2fhow-to-save-value-as-condition-after-entering-in-a-strategy%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript