[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: AB system into TradeSim Database



PureBytes Links

Trading Reference Links

Hello Alistair,
You are correct as it is designed to work as an include file. The
following code shows how to use it and I have also provided the
include file as well. The filename is "TradeSimFn.afl" and it is saved
to the AB include directory set in preferences. 

The afl can be run as scan / exploration. Only trades between the AA
range dates are written to the Test.trt file.

Please be specific with your description and difficulties. It can be
frustrating but information is needed to solve the problem.


This is the line that writes to the file

        fputs(sTicker + sEntryDate + sExitDate +
                 sEntryPrice + sExitPrice +
                  sBuyHiLo + sSellHiLo +
                    sVolume +"\n", fh );

And these are the translations from AB to Tradesim to construct the
above code. Some are concatonate, ie. sSellHiLo==(at exit High+low)

   TradeSim       AmiBroker   Format  Comment/description
______________________________________________________________________
   ticker          Name()               string
   Type	    "L"				string	(L)ong trades only
   Entry Date	   YYYYMMDD		string	Buy Date
   Exit Date       YYYYMMDD		string	Sell Date
   InitialStop     InitialStop	7.4	MUST be calculated
   BuyPrice	   BuyPrice	7.4
   SellPrice       SellPrice	7.4
   EntryLowPrice   Low		7.4	optional - Low at Buy Date
   EntryHighPrice  High		7.4	optional - High at Buy Date
   ExitLowPrice    Low		7.4	optional - Low at Sell Date
   ExitHighPrice   High		7.4	optional - High at Sell Date
   TradedSize      Volume	3.0	optional - Volume at Buy Date


So, referring to TradeSim format in the include file, I have coded a
sample AFL system to run it, as an example. Not tried but straight
forward. Be Careful of the line wrap as I could not remove all of them.
regards
franc

//////////////////////////////////////////////////////////////////////
//Sample system
//////////////////////////////////////////////////////////////////////
global TradedVol, InitialStop;

Buy = Cross( EMA(C,11), EMA(C,51) );
Sell = Cross( CCI(51), CCI(14) );

Capital = 100000;
SetOption("InitialEquity",Capital);
SetTradeDelays(1,1,0,0);
SetPositionSize(-10, spsPercentOfEquity);
BuyPrice = Open;
SellPrice = Open;

InitalStop = Ref( C-2*ATR(10), -1);

capital = 100000;
SetOption("InitialEquity", Capital);
SetPositionSize(-10, Capital);

TradedVol = int( (Capital/10)/BuyPrice ); //number of shares bought

//identify the file in the AB include directory 
#include<TradeSimFn.afl>

//call the TradeSim function, sort the trades and
//output to c:\Temp\ABtoMS.trt
//the shift (2nd parameter is to align with buy/sell tradedelays)
nshift = 1;
ABtoMS("", nshift);

/*that's all (as far as I remember)
Note: the include file was written for an earlier TradeSim version.
I believe that TradeSim now also allows shorts.
This may mean modifying the ABtoMS function.
Refer to the TradeSim documentation for the text file input format.
*/



//////////////////////////////////////////////////////////////////////
//include file saved to AB include directory - "TradeSimFn.afl"
/////////////////////////////////////////////////////////////////////////////////////////////////////

// Procedure ABtoMS( sFileName, Shift ): TextFile

/////////////////////////////////////////////////////////////////////////////////////////////////////
//  Function ABtoMS()
//  writes a Tradesim compatible Trade Database textfile
//  from a Scan/Exploration
//  The completed buy/sell trades are within the AA Range setting
//  Tradesim cannot accept incomplete trades;
//  trades are closed at AA ToRange setting or lastBar.

// NOTES:
//  1) output writen to msFileName = "C:\\Temp\\ABtoMS.trt"
//  2) The output is appended to an existing file
//     OR creates a new file.
//  3) InitialStop MUST BE CALCULATED (global variable)
//  4) AA Range read, only quotations between the range are written
//     to the file
//  5) Open Trades are completed using the ToRange Date values
//  6) Amibroker outputs buy/sell signals.
//     Need delay for actual trade

/*
--------------------------------------------------------------------------------------------------
   TRADESIM FIELD AMIBROKER    FORMAT	COMMENT
  
--------------------------------------------------------------------------------------------------
   ticker          Name()               string
   Type	    "L"				string	(L)ong trades only
   Entry Date	   YYYYMMDD		string	Buy Date
   Exit Date       YYYYMMDD		string	Sell Date
   InitialStop     InitialStop	7.4	MUST be calculated
   BuyPrice	   BuyPrice	7.4
   SellPrice       SellPrice	7.4
   EntryLowPrice   Low		7.4	optional - Low at Buy Date
   EntryHighPrice  High		7.4	optional - High at Buy Date
   ExitLowPrice    Low		7.4	optional - Low at Sell Date
   ExitHighPrice   High		7.4	optional - High at Sell Date
   TradedSize      Volume	3.0	optional - Volume at Buy Date
//---------------------------------------------------------------------------------------------------
   TRADE RECORD CONSTRUCTION:
   a single space between each field required

   sTicker			symbol + " L"
   sEntryDate			EntryDate
   sExitDate			ExitDate
   sEntryPrice		        InitialStop, Buyprice
   sExitPrice			Sellprice,
   sBuyHiLO			Low, High
   sSellHiLo			Low, High
   sVolume			Volume
**/
/////////////////////////////////////////////////////////////////////////////////////////////////////

procedure ABtoMS( msFileName, Shift )
{
  local bfirst, bRange;
  local sTicker, sTradedSize;
  local sPageDesc, sHeader;
  local sEntryDate, sEntryPrice, sBuyHiLo;
  local sExitDate, sExitPrice, sSellHiLo;
  local y, m, d, i, fh;


  if(msFileName=="") { msFileName="c:\\Temp\\ABtoMS.trt"; }

  //Read InBarRange; true when between From/To dates or n quotations
  bRange = Status("barinrange");

  // generate the year, month, day arrays for all the quotations
  y = Year();
  m = Month();
  d = Day();

//---------------------------------------------------------------------------------------------------
/** this is an extract of the format Tradesim requires...
**# Text Trade Database Example
**# Comments always begin with the # character
**
**# The compulsory fields are always required
**# in the order shown on the next line,
**
**# [Symbol][Trade Position][Entry Date][Exit Date]
**# [Initial Stop][Entry Price][Exit Price]
**
**# The optional fields which are NOT used in this example
**# should be ordered as follows,
**# [Low Entry Price][High Entry Price][Low Exit Price]
**# [High Exit Price][Traded Volume]
**
**BHP L 19960419 19960607 0.0000 19.2200 18.5800
**BHP S 19960607 19961018 0.0000 18.5800 17.0400
*/
// create a new or open an existing file to append trade details
// make sure the directory exists

  // file does not exist, so create a new file
  fh =fopen( msFileName, "a");
  if ( fh )
  {
    // a buy must occur before a sell,
    // only the first buy and sell accepted
    // subsequent multiple Buy/Sell signals ignored,
    // until trade details complete
    // ignore signals until Buy found,
    // then follow with Sell to construct trade record
    bfirst = 0;

    for( i = 0; i < BarCount; i++ )
    {
      //find the first buy, then construct Buy details
      j = i - Shift;
      if( j < 0 ) J = 0;

      if( Buy[j] AND bRange[i] == 1 AND bfirst == 0 )
      {
        // initialise the output strings to default
        bfirst      = 1;
        sTicker     = "      ";				// (6)
        sEntryDate  = "         ";			// (9)
        sExitDate   = "         ";			// (9)
        sEntryPrice = "                        ";	//(16)
        sExitPrice  = "        ";			// (8)
        sBuyHiLo    = "                ";		//(16)
        sSellHiLo   = "                ";		//(16)
        sVolume     = "         ";			//(8+1)

        // Construct the Trade BUY details
        sTicker    = Name() + " L ";
        sEntryDate = StrFormat("%04.0f%02.0f%02.0f ",y[i],m[i],d[i] );
        sEntryPrice = StrFormat("%07.4f %07.4f
",InitialStop[j],BuyPrice[i] );
        sBuyHiLo    = StrFormat( "%07.4f %07.4f ",L[i], H[i] );
        sVolume     = StrFormat( "%3.0f ",TradedVol[j] );
      }  // end if buy

      //find the first sell after buy,
      //the (symbol lastbar -> [i] < barcount)
      //OR (AA ToRange) could occur before Sell Signal
      //construct the Trade Sell details to complete the trade record.
      //      if( ( Sell[i-Shift] OR ( bRange[i] == 0 )
      //           OR ( i >= BarCount -1 ) ) AND bfirst == 1 ) 
   if((Sell[j] OR ( bRange[i]==0 ) OR (i>=BarCount-1)) AND bfirst ==1)
      {
        bfirst = 0;
        // construct SELL details
        sExitDate = StrFormat( "%04.0f%02.0f%02.0f ",y[i],m[i],d[i] );
        sExitPrice  = StrFormat( "%07.4f ",SellPrice[i] );
        sSellHiLo   = StrFormat( "%07.4f %07.4f ",L[i],H[i] );

        //buy & sell details complete,
        //now able to write the trade record
        fputs(sTicker + sEntryDate + sExitDate +
                 sEntryPrice + sExitPrice +
                  sBuyHiLo + sSellHiLo +
                    sVolume +"\n", fh );
      }  // end if sell

    }  //end for

    fclose( fh );
  }  //end fh

}  //end function ABtoMS
/////////////////////////////////////////////////////////////////////////////////////////////////////---







In amibroker@xxxxxxxxxxxxxxx, "agkan24" <agkan24@xxx> wrote:
>
> --- In amibroker@xxxxxxxxxxxxxxx, "justinwonono" <justinwonono@> wrote:
> >
> > Ok thanks franc, I'll check what I've done wrong.
> > 
> > Regards
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "oceanchimes" <oceanchimes@> wrote:
> > >
> > > Justinwonono
> > > I used a call to the include file in my AFL. Then ran a
> > > backtest/exploration for the period using AA filter for all stocks.
> > > Worked as expected, no problems with single symbol out only.
> > > franc
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "justinwonono" <justinwonono@>
> wrote:
> > > >
> > > > 
> > > > Hi franc,
> > > > 
> > > > First, thanks for your code, much appreciated, I gave this a go
> but I
> > > > only seemed to get output for the current symbol backtest, I can't
> > > > output a filtered backtest. Any ideas what I might be doing
wrong or
> > > > is that what it is meant to do? ie only the current symbol.
> > > > 
> > > > Regards
> > > > 
> > > > Justinwonono
> > > > 
> > > > --- In amibroker@xxxxxxxxxxxxxxx, "oceanchimes" <oceanchimes@>
> wrote:
> > > > >
> > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Greg" <crootster@> wrote:
> > > > > >
> > > > > > I have a system or two that I have been working on and being
> > that I 
> > > > > > have TradeSim I would like to put it across to its database to
> > > > test it 
> > > > >....
> > > > > > Cheers Greg
> > > > > >
> > > > > Greg,
> > > > > This is a function called from AFL exploration ( backtest
> > > > > whatever)that I wrote some time ago.  It worked for long only
> > trades.
> > > > > I understand Tradesim allows text file input that include short
> > > > > trades. Perhaps you could modify? Hope this is what you were
> looking
> > > > for.
> > > > > franc
> > > > >
> > > >
> > >
> >
> 
> I'm having trouble getting this to work. How do you actually use it?
> Am I supposed to modify the function (ie adjust buy details etc) or do
> you just call it as an #include in another AFL file which has my buy
> and sell commands? 
> 
> Any help would be appreciated,
> 
> Alistair
>




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/