PDA

View Full Version : Profit Stop during day


jurgen
17th January 2011, 04:25 AM
I copied the profit stop for tradesim VitC code and tried to change for a profit stop during the day ie High > profit stop rather than close:

This is the mess I made:

void ProfitStopHigh(float _PercentThreshold)
{
bool InTrade=false; // boolean variable used for trade flag
float EntryPrice=0; // variable used to store the entry price on trade entry
float ProfitThreshold=0; // variable used to hold the profit threshold price

for(int i=0;i<BarCount;i++) // loop for all bars
{
Result[i]=0; // initialize result for each bar

if(!InTrade && User1[i]>0) // check if not in a trade and valid entry trigger
{
InTrade=true; // set the flag
EntryPrice=Open[i]; // set the entry price (WHY???)
// calculate the profit threshold price

ProfitThreshold=User2[i]*(1 + _PercentThreshold/100);
Result[i]=1; // mark a valid entry condition (WHY??? alredy triggered in meta)
}

if(InTrade) // if in the trade do the check
{
if(High[i] >= ProfitThreshold) // check for a profit stop condition
{
Result[i]+=2;
ExitPrice = ProfitThreshold; // threshold reached so mark a valid exit condition
InTrade=false; // reset the flag
}
}
}
}
An error comes up exitprice is already allocated (which is true is MS script) - but why is EntryPrice reset earlier? (when it is set in Meta)
How can the exitprice (based on threshold of profit stop during day) be returned from the script to metastock?
and
Why is EntryPrice set to open[i] array anyway - when it is already done in Meta code?
and
Why does the entrytrigger have to be returned to meta - when it is already used as an argument (user1) for the script. I would have thought that the Result[i] could simply be used for the exittrigger (for tradesim run).

Whilst Im new to VitC Ive done a fair bit of C# and old fortran programming but still dont get whats going on here (from this script)

cheers

David Samborsky
17th January 2011, 01:12 PM
This code is essentially the core of the code used in the fast Trailing Stop function provided in the TradeSim plugin.

It's operation is documented in the following document which should already be loaded on your system:-

http://www.compuvision.com.au/Downloads/Docs/AN2-Trailing_Stops.pdf (http://www.compuvision.com.au/vBulletin/../Downloads/Docs/AN2-Trailing_Stops.pdf)

With Vitamin-C you are at liberty to change how it works and we encourage people to customize it for their own use.

jurgen
17th January 2011, 04:51 PM
Thanks but my concern is to understand the Vit-C code - not use Tradesim that I already know.

Can anybody answer the questions I asked about the code?

I want to understand the logic so I can use Vit C in more complex situations - for example to provide multiple exit types (profit stop, time stops and indicator triggers.

jurgen
17th January 2011, 06:54 PM
I guess what I was getting at was that using an exit price at the profit stop (eg sell limit price during day) needs VitC to determine the exittrigger and the exit price - but it can only return one value.

So I assume that the exittrigger and the exit price need to call seperate VitC's - which would look similar (in that both would calculate when the trade exits) but result = profit threshold in the call from exit price.

Does that sound right?

David Samborsky
18th January 2011, 01:15 AM
Yes even though a typical Vitamin-C function returns only one array from a function you can be a bit clever in the way you return information.

For example on page 54 of the Vitamin-C User Guide there is an example of a simple profit stop function that encodes the entry/exit conditions using the one return array from the function. 0 means inactivity, 1 means entry trigger, 2 means exit trigger and 3 means entry and exit trigger on the same bar.

Alternatively it is possible to call the same function many times with different control parameters to get the function to return different information.

regards

david

jurgen
18th January 2011, 06:12 AM
Thanks David - Ive managed to code for the exit triggers for a profit stop (5%) and a timestop (5days) withing the same sim. However, I cant seem to get the exit price right since it equals either the threshold (entryprice x 1.05 - during day exit) or the close for the timestop. I have a seperate function VitC scipt which is similar to exit triggers but returns exit price instead - but does not work and tradesim database has spurious results. Sorry to go on but Ive been at this for ages and tried lots of variations.

Ive posted the code below:

//This is for exit triggers
void ProfitStop(float _PercentThreshold, int _NBars)
{
bool InTrade=false;

float ProfitThreshold;
int bars = 0;
for(int i=0;i<BarCount;i++,bars++)
{
Result[i]=0;

if(!InTrade && User1[i]>0)
{
InTrade=true;
ProfitThreshold=User2[i]*(1 + _PercentThreshold/100);
bars = 0;
}

if(InTrade)
{
if((Close[i] >= ProfitThreshold) || (bars >= _NBars))
{
Result[i]=1;
InTrade=false;
}
}
}
}


// this is for exit price
void ProfitStopPrice(float _PercentThreshold, int _NBars)
{
bool InTrade=false;
//float EntryPrice;
float ProfitThreshold;
int bars=0;

for(int i=0;i<BarCount;i++,bars++)
{

if(!InTrade && User1[i]>0)
{
InTrade=true;

ProfitThreshold=User2[i]*(1 + _PercentThreshold/100);
bars=0;
}

if(InTrade)
{
if(High[i] >= ProfitThreshold)
{
Result[i]= ProfitThreshold;
InTrade=false;
}
if((bars>= _NBars) && (InTrade == true))
{
Result[i] = Close[i]
InTrade=false;
}
}
}
}