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!
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: