Exits using stop  [SOLVED]

Questions about MultiCharts and user contributed studies.
Spikehog
Posts: 39
Joined: 09 Dec 2013
Has thanked: 26 times
Been thanked: 1 time

Exits using stop

Postby Spikehog » 26 Feb 2016

Code: Select all

If marketposition <> 0 and Time >= 1400 then
Begin
If OpenEntryMaxProfitPerContract(0)*0.02 > 400 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.800) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 350 and OpenEntryMaxProfitPerContract(0)*0.02 <= 400 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.750) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 300 and OpenEntryMaxProfitPerContract(0)*0.02 <= 350 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.700) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 250 and OpenEntryMaxProfitPerContract(0)*0.02 <= 300 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.650) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 200 and OpenEntryMaxProfitPerContract(0)*0.02 <= 250 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.600) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 150 and OpenEntryMaxProfitPerContract(0)*0.02 <= 200 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.550) else
If OpenEntryMaxProfitPerContract(0)*0.02 > 100 and OpenEntryMaxProfitPerContract(0)*0.02 <= 150 then LXNight = PosTradeEntryPrice(0,0) + (OpenEntryMaxProfitPerContract(0)*0.02*0.500) else
If RiskOFF then LXNight = PosTradeEntryPrice(0,0) else
If RiskON then LXNight = PosTradeEntryPrice(0,0) - (Risk30mins + 1 ) else
If KeepLE then LXNight = PosTradeEntryPrice(0,0) ;
End;

If Currentbar >= 1 and MarketPosition <> 0 and Time > 1700 then
Begin
If Date >= 1130408 then Sell ( "LXNight" ) All Contracts next bar at LXNight stop;
End;
At the time of typing this post, this real trade had unrealised profit of 278 (OpenEntryMaxProfitPerContract(0)*0.02) per contract. However LXNight was still at breakeven price. It's like magnet which stays firmly at breakeven price. I know this has to do with the statements/conditions I am using in the strategy. I am not sure if I have misunderstood the "Else" statement, please correct me if I am wrong. MC reads the statements from top to bottom. Once the condition is met, it stops right there and won't go down till the bar ends. And it starts reading from top again when a new bar starts. So I put the conditions that are hardly met first and followed by the easy ones and then the last statement is the easiest to be met. Am I right?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Exits using stop  [SOLVED]

Postby TJ » 26 Feb 2016

A few tips on coding:

1. Reduce the repetitive calculations. As is, your code is unnecessarily recalculating the same operation 20 times!
Also, assigning the operation to a variable makes the code easier to read.

try this

Code: Select all

var: Threshold(0);

Threshold = OpenEntryMaxProfitPerContract(0)*0.02; // Calculate once for the loop

If Threshold > 400 then LXNight = ...
else
If Threshold > 350 then LXNight = ...
2. The 2nd part of the filter condition is redundant, because if Threshold>400, it will never go past the first filter.
(again, a waste of CPU cycles)

ie. the red highlighted part is useless
if Threshold > 400 then LXNight = ......
else
if Threshold > 350 and Threshold <= 400 then LXNight = ...

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Exits using stop

Postby TJ » 26 Feb 2016

::
I am not sure if I have misunderstood the "Else" statement, please correct me if I am wrong. MC reads the statements from top to bottom. Once the condition is met, it stops right there and won't go down till the bar ends. And it starts reading from top again when a new bar starts.
::
In an IF-THEN-ELSE filter,

When an "IF" condition is met,
MultiCharts will execute the "THEN" part of the statement,
MultiCharts will skip the rest of the "ELSE" in the loop
and jump to the "END" (or ; ).


Return to “MultiCharts”