Hi,
I am sorry to be asking such a basic question. I am having trouble from the manual and training courses on solving this.
Let’s say some variables and criteria have been met for me to initiate a long position at time T.
I would like to be able to add to that position and average down if the price falls any time after T.
The most I could gather was that you can only reference data from “n” units in the past.
Since the number of candles past since T is dynamic, I want to be able to define and “anchor” the values of the first buy signal to reference later.
Thank you so much for your help. I promise I will be better than this one day!
<style class=”dark-mode–filters”></style>
Hey @lucfral,
I want to be able to define and “anchor” the values of the first buy signal to reference later.
Any variable you declare a first time with ONCE, you can reference later. For example :
ONCE BuyPrice = 0
// Determine MyBuyConditions.
If MyBuyConditions then
Buy 10 Contracts at Market
BuyPrice = Close
endif
// This code is called at the end of each bar.
// I can now access BuyPrice when I want.
Graph BuyPrice
Preserving the price can be done in other (more formal and direct) ways too, but this is literally what you ask for and very useful in PRT coding in general.
If you would leave out the keyword ONCE then you would not be able to refer to BuyPrice in later calls of your code unless you would know how many bars ago you obtained a position the last time (and these are the more formal and direct means, all working with [n] one way or the other, n being a number of bars back).
Have fun with it all !
Peter
This version makes sure BuyPrice is not overwritten at each additional entry:
If notv OnMarket then
BuyPrice = 0
Endif
// Determine MyBuyConditions.
If MyBuyConditions then
Buy 10 Contracts at Market
If BuyPrice = 0 then
BuyPrice = Close
Endif
endif
// This code is called at the end of each bar.
// I can now access BuyPrice when I want.
Graph BuyPrice
Thank you both so much for your help It really means a lot.
Is there any way to create an IF statement based on the number of open contracts/position size?
Eg IF number of contracts/shares open is a certain amount or within some range, do or don’t do some other thing.
I looked through the glossary and it doesn’t appear that there is a code for this.
Thanks again!
CountOfPosition returns the number of contracts/lots traded (positive for Long trades, such as 3, negative for Short trades, such as -3).
Using ABS(CountOfPosition) makes both positive. You may also use CountOfLongShares and CountOfSnborgtShares, but I prefer CountOfPosition .
Example:
IF ABS(CountOfPosition) THEN //or IF CountOfLongShares or IF CountOfShortShares
.
. do something
.
ENDIF