# Get time of Buy

**URL:** <https://forums.collective2.com/t/get-time-of-buy/8269>\
**Category:** Seetu\
**Created:** [October 11, 2015, 7:23am UTC](https://forums.collective2.com/t/get-time-of-buy/8269 "2015-10-11T07:23:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoamMizrachi](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/noammizrachi/32/1487_2.png) [@NoamMizrachi](https://forums.collective2.com/u/NoamMizrachi)\
**Post date:** [October 11, 2015, 7:23am UTC](https://forums.collective2.com/t/get-time-of-buy/8269/1 "2015-10-11T07:23:23Z")

</div>

Hello all,

How can I know the exact time Buy was executed?  
I would like to do something as follow:  
“Sell after X days past from Buying”

Thanks in advanced

---

<div class="post-metadata">

**Author:** ![BobSvancara](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/bobsvancara/32/547_2.png) [@BobSvancara](https://forums.collective2.com/u/BobSvancara)\
**Post date:** [October 12, 2015, 1:06pm UTC](https://forums.collective2.com/t/get-time-of-buy/8269/2 "2015-10-12T13:06:32Z")

</div>

There is a similar question there.

See **More Question (position based management, turns, and triggers)**

Perhaps you will find an answer there.

---

<div class="post-metadata">

**Author:** ![NoamMizrachi](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/noammizrachi/32/1487_2.png) [@NoamMizrachi](https://forums.collective2.com/u/NoamMizrachi)\
**Post date:** [October 14, 2015, 11:55am UTC](https://forums.collective2.com/t/get-time-of-buy/8269/3 "2015-10-14T11:55:11Z")

</div>

Thanks.  
BarsSince(Buy) is perfect for my purpose.

But I think there is a bug in implementation (correct me if I’m wrong)  
I want to Sell a stock that bought 5 days ago, no matter what append in stock price.  
This is my simple program:

```
SetOption( "initialequity", 100000 );
SetPositionSize( 100, spsShares );
SetTradeDelays( 0, 0, 0, 0 );

BuyPrice = Close; 
SellPrice = Close;

Buy = Cross( 28, Rsi_Value); 
Sell = BarsSince(Buy) >= 5;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Filter = Buy OR Sell OR BarsSince(Buy);
AddColumn(Buy, "Buy");
AddColumn(Sell, "Sell");
AddColumn(BarsSince(Buy), "BarsSince(Buy)");
AddColumn(BarsSince(Buy) >= 5, "BarsSince(Buy) >= 5");
AddColumn(Rsi_Value, "RSI");

```

And when looking at the results ofExploration you can see that after 5 days, BarsSince(Buy) \>= 5 is TRUE but Sell is FALSE.  
What could be the problem? am I doing something wrong?  
 ![](https://yyz2.discourse-cdn.com/flex028/uploads/collective2/1779/99908e87cb4218c7.jpg)

Thanks

---

<div class="post-metadata">

**Author:** ![BobSvan2](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/bobsvan2/32/21391_2.png) [@BobSvan2](https://forums.collective2.com/u/BobSvan2)\
**Post date:** [October 14, 2015, 3:56pm UTC](https://forums.collective2.com/t/get-time-of-buy/8269/4 "2015-10-14T15:56:21Z")

</div>

I suppose that a missing “Rsi\_Value” is this: Rsi\_Value = CalcRSI();

I removed both ExRem commands

```
// Buy = ExRem(Buy,Sell);
// Sell = ExRem(Sell,Buy);

```

Here is my Exploration then:

![](https://yyz2.discourse-cdn.com/flex028/uploads/collective2/1780/742cf998b5228f01.png)

The problem is, that RSI crosses 28 again - sooner than after 5 bars.  
It generates a new Buy signal which “restarts” BarsSince(Buy) command and it starts from zero again.

If RSI does not fall under 28 within 5 bars then it works as expected.

I am not sure how to solve that right now.

---

<div class="post-metadata">

**Author:** ![NoamMizrachi](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/noammizrachi/32/1487_2.png) [@NoamMizrachi](https://forums.collective2.com/u/NoamMizrachi)\
**Post date:** [October 15, 2015, 10:41am UTC](https://forums.collective2.com/t/get-time-of-buy/8269/5 "2015-10-15T10:41:40Z")

</div>

Thanks Bob.  
I’m waiting for your solution

---

<div class="post-metadata">

**Author:** ![BobSvan2](https://yyz2.discourse-cdn.com/flex028/user_avatar/forums.collective2.com/bobsvan2/32/21391_2.png) [@BobSvan2](https://forums.collective2.com/u/BobSvan2)\
**Post date:** [October 15, 2015, 1:52pm UTC](https://forums.collective2.com/t/get-time-of-buy/8269/6 "2015-10-15T13:52:23Z")

</div>

Can you try this code?

```
SetOption("Symbols","DVN");

SetOption( "initialequity", 100000 );
SetPositionSize( 100, spsShares );
SetTradeDelays( 0, 0, 0, 0 );

Rsi_Value = RSI();

BuyPrice = Close; 
SellPrice = Close;

Buy = Cross( 28, Rsi_Value); 

// ======= New code ======>
// Remove consecutive BUYs within 5 bars after BUY (eh - how to say it better...)
counter = 5;
for(i=0;i < BarCount; i++){
  if (counter == 5 and Buy[i]) {
      counter = 0;      
  } else {
    if (counter < 5) {
        Buy[i] = 0;
    	counter = counter + 1;
    }
  }
}
// <======= New code ======

Sell = BarsSince(Buy) >= 5;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Filter = Buy OR Sell OR BarsSince(Buy);
AddColumn(Buy, "Buy");
AddColumn(Sell, "Sell");
AddColumn(BarsSince(Buy), "BarsSince(Buy)");
AddColumn(BarsSince(Buy) >= 5, "BarsSince(Buy) >= 5");
AddColumn(Rsi_Value, "RSI");

```

![](https://yyz2.discourse-cdn.com/flex028/uploads/collective2/1787/5039840ad8e88c27.png)
