Code challenge

PirateTrader

New member
I have tradePL variable which is working, now I need to use its value earlier in the script for stopSignal, problem is that it is not yet defined and there is code dependency which not allows for re organise its position within the code. Here is the code to show the dependencies:

Code:
input MaxTradeLoss = 300;

def StopTrade = if (TradePL >= MaxTradeLoss and TradePL < 0) then 1 else 0;
#######################################
## STOP Signals
#######################################
def BuyStop = if !useStops then 0 else if state == state.long and (haOpen > haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if state == state.short and (haOpen < haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0>0


#######################################
## Maintain the position of trades
#######################################

def CurrentPosition; # holds whether flat = 0 long = 1 short = -1

if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
CurrentPosition = 0;
} else {
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal) {
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal) {
CurrentPosition = -1;
} else if (BuyStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}


def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;

#######################################
## Orders
#######################################

def isOrder = if CurrentPosition ...

# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ....

# Current Open Trade Profit or Loss
def TradePL = if isLong then Round(((close - orderPrice).....

End CODE

As you can see the goal is to use StopTrade (which based on TradePL in the bottom of the script) to be part of the BuyStop/SellStop conditions (at the top) but I cant move it up as each bold parts of the code depends on the part above them.
TradePL depends on IsLong and OrderPrice which depends on IsOrder which depends on CurrentPostion calculation which has the BuyStop/SellStop as one of the conditions - which I want to modify to include condition that relays on TradePL at the bottom.
Is there coding solution to it? Tried creating new variable but this doesnt help as the code structure and dependancy will alsways apply as each code depends on the one above him.
Any help would be highly apprciated.

Error is of course for TradePL at the top not yet defind:
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49


Thanks!
 
Last edited:
I have tradePL variable which is working, now I need to use its value earlier in the script for stopSignal, problem is that it is not yet defined and there is code dependency which not allows for re organise its position within the code. Here is the code to show the dependencies:

Code:
input MaxTradeLoss = 300;

def StopTrade = if (TradePL >= MaxTradeLoss and TradePL < 0) then 1 else 0;
#######################################
## STOP Signals
#######################################
def BuyStop = if !useStops then 0 else if state == state.long and (haOpen > haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if state == state.short and (haOpen < haClose) and Flat or (currentTime2 <= 0) or StopTrade==1 then 1 else 0 ; # insert condition to stop in place of the 0>0


#######################################
## Maintain the position of trades
#######################################

def CurrentPosition; # holds whether flat = 0 long = 1 short = -1

if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
CurrentPosition = 0;
} else {
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal) {
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal) {
CurrentPosition = -1;
} else if (BuyStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellStop and useStops) {
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}


def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;

#######################################
## Orders
#######################################

def isOrder = if CurrentPosition ...

# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and ....

# Current Open Trade Profit or Loss
def TradePL = if isLong then Round(((close - orderPrice).....

End CODE

As you can see the goal is to use StopTrade (which based on TradePL in the bottom of the script) to be part of the BuyStop/SellStop conditions (at the top) but I cant move it up as each bold parts of the code depends on the part above them.
TradePL depends on IsLong and OrderPrice which depends on IsOrder which depends on CurrentPostion calculation which has the BuyStop/SellStop as one of the conditions - which I want to modify to include condition that relays on TradePL at the bottom.
Is there coding solution to it? Tried creating new variable but this doesnt help as the code structure and dependancy will alsways apply as each code depends on the one above him.
Any help would be highly apprciated.

Error is of course for TradePL at the top not yet defind:
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49
No such variable: TradePL at 296:21
No such variable: TradePL at 296:49


Thanks!

Without all of the code being provided, the bold changes might help.

input MaxTradeLoss = 300;
def tradepl;
def StopTrade = if (tradepl >= MaxTradeLoss and tradepl < 0) then 1 else 0;

# Current Open Trade Profit or Loss
tradepl = if isLong then Round(close - orderPrice) ....;
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
367 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top